drmasad commited on
Commit
8bfe0fe
·
verified ·
1 Parent(s): c9807e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -44
app.py CHANGED
@@ -93,47 +93,61 @@ for message in st.session_state.messages:
93
  st.markdown(message["content"])
94
 
95
 
96
- # Accept user input
97
- if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
98
-
99
- # Display user message in chat message container
100
- with st.chat_message("user"):
101
- st.markdown(prompt)
102
- # Add user message to chat history
103
- st.session_state.messages.append({"role": "user", "content": prompt})
104
-
105
- # Define instructions for the model to focus on conciseness and relevance
106
- instructions = """
107
- Act as a highly knowledgeable endocrinology doctor with expertise in explaining complex medical information in an understandable way to patients who do not have a medical background. Your responses should not only convey empathy and care but also demonstrate a high level of medical accuracy and reliability.
108
- When crafting your explanations, please adhere to the following guidelines:
109
- - Prioritize medical accuracy: Ensure all information provided is up-to-date and reflects current medical consensus. Use evidence-based medical knowledge to inform your responses.
110
- - Clarify complex concepts: Break down medical terms and concepts into understandable language. Use analogies related to everyday experiences to help explain complex ideas when possible.
111
- - Provide actionable advice: Where appropriate, offer practical and specific advice that the patient can follow to address their concerns or manage their condition, including when to consult a healthcare professional.
112
- - Address concerns directly: Understand and directly respond to the patient's underlying concerns or questions, offering clear explanations and reassurance about their condition or treatment options.
113
- - Promote informed decision-making: Empower the patient with the knowledge they need to make informed health decisions. Highlight key considerations and options available to them in managing their health.
114
- Your response should be a blend of professional medical advice and compassionate communication, creating a dialogue that educates, reassures, and empowers the patient.
115
- Strive to make your response as informative and authoritative as a consultation with a human doctor, ensuring the patient feels supported and knowledgeable about their health concerns.
116
- You will answer as if you are talking to a patient directly
117
- """
118
-
119
-
120
- # Create the full prompt with instructions
121
- full_prompt = f"<s>[INST] {prompt} [/INST] {instructions}</s>"
122
-
123
- # Display assistant response in chat message container
124
- with st.chat_message("assistant"):
125
- stream = client.chat.completions.create(
126
- model=model_links[selected_model],
127
- messages=[
128
- {"role": m["role"], "content": full_prompt}
129
- for m in st.session_state.messages
130
- ],
131
- temperature=temp_values,
132
- stream=True,
133
- max_tokens=1024,
134
- )
135
- response = st.write_stream(stream)
136
- # Remove the '</s>' from the response if it exists
137
- response = response.replace('</s>', '').strip()
138
- st.session_state.messages.append(
139
- {"role": "assistant", "content": response})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  st.markdown(message["content"])
94
 
95
 
96
+ # Initialize the streaming status flag
97
+ if "is_streaming" not in st.session_state:
98
+ st.session_state.is_streaming = False
99
+
100
+ # Chat input handling
101
+ if st.session_state.is_streaming:
102
+ st.chat_input("The assistant is currently responding. Please wait...") # Inform the user to wait
103
+ else:
104
+ # If not streaming, allow user input
105
+ if prompt := st.chat_input("Ask me anything about diabetes"):
106
+ st.session_state.is_streaming = True # Set the flag to indicate streaming has started
107
+
108
+ with st.chat_message("user"):
109
+ st.markdown(prompt)
110
+
111
+ # Add the user message to chat history
112
+ st.session_state.messages.append({"role": "user", "content": prompt})
113
+
114
+ instructions = """
115
+ Act as a highly knowledgeable endocrinology doctor with expertise in explaining complex medical information in an understandable way to patients who do not have a medical background. Your responses should not only convey empathy and care but also demonstrate a high level of medical accuracy and reliability.
116
+ When crafting your explanations, please adhere to the following guidelines:
117
+ - Prioritize medical accuracy: Ensure all information provided is up-to-date and reflects current medical consensus. Use evidence-based medical knowledge to inform your responses.
118
+ - Clarify complex concepts: Break down medical terms and concepts into understandable language. Use analogies related to everyday experiences to help explain complex ideas when possible.
119
+ - Provide actionable advice: Where appropriate, offer practical and specific advice that the patient can follow to address their concerns or manage their condition, including when to consult a healthcare professional.
120
+ - Address concerns directly: Understand and directly respond to the patient's underlying concerns or questions, offering clear explanations and reassurance about their condition or treatment options.
121
+ - Promote informed decision-making: Empower the patient with the knowledge they need to make informed health decisions. Highlight key considerations and options available to them in managing their health.
122
+ Your response should be a blend of professional medical advice and compassionate communication, creating a dialogue that educates, reassures, and empowers the patient.
123
+ Strive to make your response as informative and authoritative as a consultation with a human doctor, ensuring the patient feels supported and knowledgeable about their health concerns.
124
+ You will answer as if you are talking to a patient directly
125
+ """
126
+
127
+ full_prompt = f"<s>[INST] {prompt} [/INST] {instructions}</s>"
128
+
129
+ # Display assistant response in chat message container
130
+ with st.chat_message("assistant"):
131
+ # Stream the response
132
+ stream = client.chat.completions.create(
133
+ model=model_links[selected_model],
134
+ messages=[
135
+ {"role": m["role"], "content": full_prompt}
136
+ for m in st.session_state.messages
137
+ ],
138
+ temperature=temp_values,
139
+ stream=True,
140
+ max_tokens=1024,
141
+ )
142
+ response = st.write_stream(stream)
143
+
144
+ # Process and clean the response
145
+ response = response.replace('</s>', '').strip() # Clean unnecessary characters
146
+
147
+ st.markdown(response)
148
+
149
+ # Indicate that streaming is complete
150
+ st.session_state.is_streaming = False
151
+
152
+ # Store the final response
153
+ st.session_state.messages.append({"role": "assistant", "content": response})