chtseng commited on
Commit
5c8e368
1 Parent(s): a2bd0a8

Upload app.py

Browse files
Files changed (1) hide show
  1. Web/app.py +193 -188
Web/app.py CHANGED
@@ -1,188 +1,193 @@
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.xxxx.xxx.xxxx: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})
 
 
 
 
 
 
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})