chtseng commited on
Commit
b07eb7f
1 Parent(s): b743eac

Upload 3 files

Browse files
Files changed (3) hide show
  1. Web/app.py +188 -0
  2. Web/doctor.png +0 -0
  3. Web/streamlit/secrets.toml +1 -0
Web/app.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import streamlit as st
3
+ import toml
4
+ import base64
5
+ import requests
6
+
7
+ #initial values
8
+ if 'chating_now' not in st.session_state: st.session_state['chating_now'] = False
9
+ if 'patient_six' not in st.session_state: st.session_state['patient_six'] = ""
10
+ if 'patient_age' not in st.session_state: st.session_state['patient_age'] = ""
11
+
12
+ client = OpenAI(base_url="http://chatapi.sunplusit.com.tw:1234/v1/", api_key="not-needed")
13
+
14
+ #-------------------------------------------------------------------
15
+ secrets = toml.load("streamlit/secrets.toml")
16
+ st.set_page_config(layout="wide")
17
+ st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)
18
+
19
+ st.markdown(
20
+ """
21
+ <style>
22
+ .appview-container .main .block-container {{
23
+ padding-top: {padding_top}rem;
24
+ padding-bottom: {padding_bottom}rem;
25
+ }}
26
+
27
+ </style>""".format(
28
+ padding_top=1, padding_bottom=1
29
+ ),
30
+ unsafe_allow_html=True,
31
+ )
32
+ #-------------------------------------------------------------------
33
+
34
+ colA, colB = st.columns([0.3,2])
35
+ with colA:
36
+ st.image('doctor.png', width=100)
37
+ with colB:
38
+ st.title("TAIDE 7B - 醫療AI問答系統")
39
+
40
+ # Store the initial value of widgets in session state
41
+ if "visibility" not in st.session_state:
42
+ st.session_state.visibility = "visible"
43
+ st.session_state.disabled = False
44
+ if "visibility" not in st.session_state:
45
+ st.session_state.moredesc = False
46
+
47
+ if "exec_moredesc" not in st.session_state:
48
+ st.session_state.exec_moredesc = False
49
+ #-----------------------------------------------------------------------------------------
50
+
51
+ last_prompt = ""
52
+ last_response = ""
53
+ q_input = ""
54
+ form_login = st.empty()
55
+ form_logout = st.empty()
56
+
57
+ with form_login.container():
58
+
59
+ with st.form(key='patient_form'):
60
+ col1, col2, col3 = st.columns([0.5,0.5,0.5])
61
+ with col1:
62
+ patient_six = st.radio(
63
+ "性別",
64
+ ["男性", "女性"],
65
+ index=None,
66
+ label_visibility = "collapsed"
67
+ )
68
+ with col2:
69
+ patient_age = st.selectbox(
70
+ '年齡區間',
71
+ ('0-9歲', '10-19歲', '20-29歲', '30-39歲', '40-49歲', '50-59歲', '60-69歲', '70-79歲', '80-89歲', '90-99歲', '100歲以上'),
72
+ index=None, label_visibility = "collapsed")
73
+ with col3:
74
+ submit = st.form_submit_button(label='開始咨詢',use_container_width = True, type='primary')
75
+
76
+ if submit:
77
+ st.session_state.patient_six = patient_six
78
+ st.session_state.patient_age = patient_age
79
+ st.session_state.chating_now = True
80
+
81
+ with form_logout.container():
82
+ col1, col2, col3 = st.columns([0.5,0.5,0.5])
83
+ with col1:
84
+ if "男" in st.session_state.patient_six:
85
+ sixtxt = ":blue[男性] :boy:"
86
+ else:
87
+ sixtxt = ":blue[女性] :girl:"
88
+
89
+ st.markdown("您的性別: {}".format(sixtxt))
90
+
91
+ with col2:
92
+ st.write("您的年齡區間: {}".format(st.session_state.patient_age))
93
+
94
+ with col3:
95
+ if st.button("結束咨詢", key="end",use_container_width = True, type='primary'):
96
+ st.session_state.patient_six = ""
97
+ st.session_state.patient_age = ""
98
+ st.session_state.chating_now = False
99
+ st.session_state.messages = []
100
+
101
+
102
+ if st.session_state.patient_six=="" or st.session_state.patient_age=="" or st.session_state.chating_now is False:
103
+ st.session_state.patient_six = ""
104
+ st.session_state.patient_age = ""
105
+ st.session_state.chating_now = False
106
+ form_logout.empty()
107
+
108
+ else:
109
+ form_login.empty()
110
+
111
+
112
+ if st.session_state.chating_now is True:
113
+ q_input = st.chat_input("線上醫療咨詢服務。請輸入您的問題...")
114
+
115
+ #--------------------------------------------------------------------------------------------------------------
116
+ if "openai_model" not in st.session_state:
117
+ st.session_state["openai_model"] = "gpt-3.5-turbo"
118
+
119
+ if "messages" not in st.session_state:
120
+ st.session_state.messages = []
121
+
122
+ for message in st.session_state.messages:
123
+ with st.chat_message(message["role"]):
124
+ st.markdown(message["content"])
125
+
126
+ def set_moredesc():
127
+ st.session_state.exec_moredesc = True
128
+
129
+
130
+ if prompt := q_input:
131
+ st.session_state.last_prompt = "患者資料: 性別 {}, 年齡介於{}\n\n問題:".format(st.session_state.patient_six, st.session_state.patient_age) + prompt
132
+ print("Prompt:",prompt)
133
+ st.session_state.messages.append({"role": "user", "content": prompt})
134
+ with st.chat_message("user"):
135
+ st.markdown(prompt)
136
+
137
+ with st.chat_message("assistant"):
138
+ message_placeholder = st.empty()
139
+ full_response = ""
140
+
141
+ for response in client.chat.completions.create(
142
+ model=st.session_state["openai_model"],
143
+ max_tokens=256,
144
+ temperature=0.1,
145
+ top_p=1,
146
+ frequency_penalty=0,
147
+ messages=[
148
+ {"role": m["role"], "content": "---------------------\n注意,簡單扼要針對患者的問題來回答,字數不多於128字,回答內容不可包含任何醫院名稱以及醫師姓名和其它與問題無關的內容。\n---------------------\n\n" + \
149
+ "患者資料: 性別 {}, 年齡介於{}\n\n".format(st.session_state.patient_six, st.session_state.patient_age) + m["content"] }
150
+ for m in st.session_state.messages
151
+ ],
152
+ stream=True,
153
+ ):
154
+
155
+ full_response += (response.choices[0].delta.content or "")
156
+ message_placeholder.markdown(full_response + "▌")
157
+ print(full_response, end=" ")
158
+
159
+ message_placeholder.markdown(full_response)
160
+ st.session_state.last_response = full_response
161
+ submit_moredesc = st.button(label='更詳細說明', on_click=set_moredesc)
162
+
163
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
164
+ #--------------------------------------------------------------------------------------------------------------
165
+
166
+
167
+ if st.session_state.exec_moredesc:
168
+ st.session_state.exec_moredesc = False
169
+ more_ask = {"role": "user", "content": \
170
+ "\n------------\n{}\n\n 你剛剛回答:{}\n------------\n病患希望你再針對剛簡短的回答內容,予以詳細的解釋說明,如果有醫療專有名詞,請解釋。".format(st.session_state.last_prompt, st.session_state.last_response) }
171
+
172
+ with st.chat_message("assistant"):
173
+ message_placeholder = st.empty()
174
+ full_response = ""
175
+ for response in client.chat.completions.create(
176
+ model=st.session_state["openai_model"],
177
+ max_tokens=2048,
178
+ temperature=0.1,
179
+ top_p=1,
180
+ frequency_penalty=0,
181
+ messages=[more_ask],
182
+ stream=True,
183
+ ):
184
+ full_response += (response.choices[0].delta.content or "")
185
+ message_placeholder.markdown(full_response + "▌")
186
+ print(full_response, end=" ")
187
+
188
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
Web/doctor.png ADDED
Web/streamlit/secrets.toml ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY = "YOUR_OPENAI_API_KEY_HERE"