Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -93,47 +93,61 @@ for message in st.session_state.messages:
|
|
93 |
st.markdown(message["content"])
|
94 |
|
95 |
|
96 |
-
#
|
97 |
-
if
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
#
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
stream=
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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})
|