Upload app.py
Browse files- Web/app.py +193 -188
Web/app.py
CHANGED
@@ -1,188 +1,193 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
if '
|
10 |
-
if '
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
st.
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
}
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
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 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
st.session_state.
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#import openai
|
2 |
+
from openai import OpenAI
|
3 |
+
import streamlit as st
|
4 |
+
import toml
|
5 |
+
import base64
|
6 |
+
import requests
|
7 |
+
|
8 |
+
#initial values
|
9 |
+
if 'chating_now' not in st.session_state: st.session_state['chating_now'] = False
|
10 |
+
if 'patient_six' not in st.session_state: st.session_state['patient_six'] = ""
|
11 |
+
if 'patient_age' not in st.session_state: st.session_state['patient_age'] = ""
|
12 |
+
|
13 |
+
client = OpenAI(base_url="http://localhost:8888/v1/", api_key="not-needed")
|
14 |
+
|
15 |
+
#Steramlit initial -------------------------------------------------------------------
|
16 |
+
secrets = toml.load("streamlit/secrets.toml")
|
17 |
+
|
18 |
+
st.set_page_config(layout="wide")
|
19 |
+
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)
|
20 |
+
#將頂端的空白減少
|
21 |
+
st.markdown(
|
22 |
+
"""
|
23 |
+
<style>
|
24 |
+
.appview-container .main .block-container {{
|
25 |
+
padding-top: {padding_top}rem;
|
26 |
+
padding-bottom: {padding_bottom}rem;
|
27 |
+
}}
|
28 |
+
|
29 |
+
</style>""".format(
|
30 |
+
padding_top=1, padding_bottom=1
|
31 |
+
),
|
32 |
+
unsafe_allow_html=True,
|
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 |
+
#st.write(st.session_state.patient_six)
|
112 |
+
#st.write(st.session_state.patient_age)
|
113 |
+
#st.write(st.session_state.chating_now)
|
114 |
+
if st.session_state.chating_now is True:
|
115 |
+
q_input = st.chat_input("線上醫療咨詢服務。請輸入您的問題...")
|
116 |
+
|
117 |
+
#--------------------------------------------------------------------------------------------------------------
|
118 |
+
if "openai_model" not in st.session_state:
|
119 |
+
st.session_state["openai_model"] = "gpt-3.5-turbo"
|
120 |
+
|
121 |
+
if "messages" not in st.session_state:
|
122 |
+
st.session_state.messages = []
|
123 |
+
|
124 |
+
for message in st.session_state.messages:
|
125 |
+
with st.chat_message(message["role"]):
|
126 |
+
st.markdown(message["content"])
|
127 |
+
|
128 |
+
def set_moredesc():
|
129 |
+
st.session_state.exec_moredesc = True
|
130 |
+
|
131 |
+
#last_prompt = prompt
|
132 |
+
if prompt := q_input:
|
133 |
+
st.session_state.last_prompt = "患者資料: 性別 {}, 年齡介於{}\n\n問題:".format(st.session_state.patient_six, st.session_state.patient_age) + prompt
|
134 |
+
print("Prompt:",prompt)
|
135 |
+
person_info = "患者資料: 性別 {}, 年齡介於{}\n\n".format(st.session_state.patient_six, st.session_state.patient_age)
|
136 |
+
st.session_state.patient_info = person_info
|
137 |
+
|
138 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
139 |
+
with st.chat_message("user"):
|
140 |
+
st.markdown(prompt)
|
141 |
+
|
142 |
+
with st.chat_message("assistant"):
|
143 |
+
message_placeholder = st.empty()
|
144 |
+
full_response = ""
|
145 |
+
|
146 |
+
for response in client.chat.completions.create(
|
147 |
+
model=st.session_state["openai_model"],
|
148 |
+
max_tokens=256,
|
149 |
+
temperature=0.1,
|
150 |
+
top_p=1,
|
151 |
+
frequency_penalty=0,
|
152 |
+
messages=[
|
153 |
+
{"role": m["role"], "content": "---------------------\n注意,簡單扼要針對患者的問題來回答,字數不多於128字,回答內容不可包含任何醫院名稱以及醫師姓名和其它與問題無關的內容。\n---------------------\n\n" + \
|
154 |
+
person_info + m["content"] }
|
155 |
+
for m in st.session_state.messages
|
156 |
+
],
|
157 |
+
stream=True,
|
158 |
+
):
|
159 |
+
|
160 |
+
full_response += (response.choices[0].delta.content or "")
|
161 |
+
message_placeholder.markdown(full_response + "▌")
|
162 |
+
print(st.session_state.patient_info, end=" ")
|
163 |
+
|
164 |
+
message_placeholder.markdown(full_response)
|
165 |
+
st.session_state.last_response = full_response
|
166 |
+
submit_moredesc = st.button(label='更詳細說明', on_click=set_moredesc)
|
167 |
+
|
168 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
169 |
+
#--------------------------------------------------------------------------------------------------------------
|
170 |
+
|
171 |
+
|
172 |
+
if st.session_state.exec_moredesc:
|
173 |
+
st.session_state.exec_moredesc = False
|
174 |
+
more_ask = {"role": "user", "content": \
|
175 |
+
"{}\n\n------------\n{}\n\n 你剛剛回答:{}\n------------\n病患希望你再針對剛簡短的回答內容,予以詳細的解釋說明,請不要重複之前的回答內容。相關醫療專有名詞請一起解釋。".format(st.session_state.patient_info, st.session_state.last_prompt, st.session_state.last_response) }
|
176 |
+
#st.write(more_ask)
|
177 |
+
with st.chat_message("assistant"):
|
178 |
+
message_placeholder = st.empty()
|
179 |
+
full_response = ""
|
180 |
+
for response in client.chat.completions.create(
|
181 |
+
model=st.session_state["openai_model"],
|
182 |
+
max_tokens=2048,
|
183 |
+
temperature=0.1,
|
184 |
+
top_p=1,
|
185 |
+
frequency_penalty=0,
|
186 |
+
messages=[more_ask],
|
187 |
+
stream=True,
|
188 |
+
):
|
189 |
+
full_response += (response.choices[0].delta.content or "")
|
190 |
+
message_placeholder.markdown(full_response + "▌")
|
191 |
+
print(full_response, end=" ")
|
192 |
+
|
193 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|