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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import os
|
2 |
+
# import streamlit as st
|
3 |
+
# from groq import Groq
|
4 |
+
# import re
|
5 |
+
# from datetime import datetime
|
6 |
+
|
7 |
+
# # Initialize Groq client
|
8 |
+
# client = Groq(api_key="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")
|
9 |
+
|
10 |
+
# # Set page configuration
|
11 |
+
# st.set_page_config(
|
12 |
+
# page_title="Virtual Medical Assistant",
|
13 |
+
# page_icon="π¨ββοΈ",
|
14 |
+
# layout="wide",
|
15 |
+
# initial_sidebar_state="expanded",
|
16 |
+
# )
|
17 |
+
|
18 |
+
# # Custom CSS with improved chat styling
|
19 |
+
# st.markdown(
|
20 |
+
# """
|
21 |
+
# <style>
|
22 |
+
# .main {
|
23 |
+
# background-color: #f5f7f9;
|
24 |
+
# }
|
25 |
+
# .stButton>button {
|
26 |
+
# width: 100%;
|
27 |
+
# border-radius: 5px;
|
28 |
+
# height: 3em;
|
29 |
+
# background-color: #0083B8;
|
30 |
+
# color: white;
|
31 |
+
# border: none;
|
32 |
+
# }
|
33 |
+
# .stButton>button:hover {
|
34 |
+
# background-color: #00669e;
|
35 |
+
# }
|
36 |
+
# .chat-container {
|
37 |
+
# background-color: #f0f2f6;
|
38 |
+
# padding: 20px;
|
39 |
+
# border-radius: 10px;
|
40 |
+
# box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
41 |
+
# margin-bottom: 20px;
|
42 |
+
# }
|
43 |
+
# .user-message {
|
44 |
+
# background-color: #0083B8;
|
45 |
+
# padding: 10px 15px;
|
46 |
+
# border-radius: 15px 15px 0 15px;
|
47 |
+
# margin: 10px 0;
|
48 |
+
# margin-left: 20%;
|
49 |
+
# color: white;
|
50 |
+
# text-align: right;
|
51 |
+
# box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
52 |
+
# }
|
53 |
+
# .assistant-message {
|
54 |
+
# background-color: #e9ecef;
|
55 |
+
# padding: 10px 15px;
|
56 |
+
# border-radius: 15px 15px 15px 0;
|
57 |
+
# color: #333;
|
58 |
+
# margin: 10px 0;
|
59 |
+
# margin-right: 20%;
|
60 |
+
# text-align: left;
|
61 |
+
# box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
62 |
+
# }
|
63 |
+
# .sidebar-content {
|
64 |
+
# padding: 20px;
|
65 |
+
# }
|
66 |
+
# .main-title {
|
67 |
+
# text-align: center;
|
68 |
+
# color: #0083B8;
|
69 |
+
# padding: 20px 0;
|
70 |
+
# }
|
71 |
+
# .delete-button {
|
72 |
+
# background-color: #dc3545 !important;
|
73 |
+
# }
|
74 |
+
# .delete-button:hover {
|
75 |
+
# background-color: #c82333 !important;
|
76 |
+
# }
|
77 |
+
# </style>
|
78 |
+
# """,
|
79 |
+
# unsafe_allow_html=True,
|
80 |
+
# )
|
81 |
+
|
82 |
+
# # System prompt remains the same
|
83 |
+
# SYSTEM_PROMPT = """You are a virtual medical assistant with great expertise and empathy. Your role is:
|
84 |
+
|
85 |
+
# 1. Scope of Assistance:
|
86 |
+
# - Diagnose common symptoms
|
87 |
+
# - Suggest possible conditions
|
88 |
+
# - Provide recommendations for common over-the-counter medications
|
89 |
+
# - Offer advice on managing health issues
|
90 |
+
# - Answer medical questions only
|
91 |
+
# - Inform the patient about symptoms of illnesses
|
92 |
+
|
93 |
+
# 2. Important Limitations:
|
94 |
+
# - Do not answer non-medical questions
|
95 |
+
# - When asked a non-medical question, simply say, "Sorry, this question is outside my medical expertise."
|
96 |
+
# - Provide definitive diagnoses only for very common conditions
|
97 |
+
# - Do not prescribe prescription medications
|
98 |
+
# - Do not provide advice on serious medical conditions
|
99 |
+
# - Do not greet the user in every response. Do so only if the user starts with a greeting.
|
100 |
+
|
101 |
+
# 3. When dealing with symptoms:
|
102 |
+
|
103 |
+
# A. Gather information - Ask specific questions about:
|
104 |
+
# - Duration of symptoms
|
105 |
+
# - Severity of symptoms
|
106 |
+
# - Age
|
107 |
+
# - Medical history
|
108 |
+
# - Current medications
|
109 |
+
# - Associated symptoms
|
110 |
+
|
111 |
+
# B. Assessment:
|
112 |
+
# - Analyze the reported symptoms
|
113 |
+
# - Link symptoms to common possible conditions
|
114 |
+
# - Determine the severity of the condition
|
115 |
+
|
116 |
+
# C. Recommendations:
|
117 |
+
# - Suggest safe home remedies
|
118 |
+
# - Recommend over-the-counter medications
|
119 |
+
# - Provide prevention and self-care tips
|
120 |
+
|
121 |
+
# 4. When to refer the patient to a doctor:
|
122 |
+
# - In case of severe symptoms
|
123 |
+
# - If symptoms persist for a long time
|
124 |
+
# - If the condition worsens
|
125 |
+
# - When specific medical tests are needed
|
126 |
+
# - In emergencies
|
127 |
+
|
128 |
+
# 5. Mandatory Reminder:
|
129 |
+
# - Always emphasize that the advice provided is not a substitute for consulting a doctor
|
130 |
+
# - Encourage visiting a healthcare provider in serious cases
|
131 |
+
# - Explain that this advice is general and not an official medical diagnosis
|
132 |
+
|
133 |
+
# 6. Language and Communication:
|
134 |
+
# - Use simple and clear language
|
135 |
+
# - Maintain a professional and empathetic tone
|
136 |
+
# - Avoid complex medical terminology
|
137 |
+
|
138 |
+
# 7. In emergencies:
|
139 |
+
# - Direct the patient immediately to the nearest emergency department
|
140 |
+
# - Provide simple first aid instructions if necessary
|
141 |
+
# - Emphasize the importance of seeking immediate medical help
|
142 |
+
|
143 |
+
# Always remember: Patient safety is the top priority, and in case of doubt, always recommend visiting a doctor."""
|
144 |
+
|
145 |
+
# # Initialize session state
|
146 |
+
# if "chat_history" not in st.session_state:
|
147 |
+
# st.session_state.chat_history = {}
|
148 |
+
# if "current_chat_id" not in st.session_state:
|
149 |
+
# st.session_state.current_chat_id = None
|
150 |
+
# if "temp_chat" not in st.session_state:
|
151 |
+
# st.session_state.temp_chat = None
|
152 |
+
|
153 |
+
|
154 |
+
# def get_groq_response(messages):
|
155 |
+
# try:
|
156 |
+
# chat_completion = client.chat.completions.create(
|
157 |
+
# messages=messages,
|
158 |
+
# model="llama3-8b-8192",
|
159 |
+
# temperature=0.7,
|
160 |
+
# max_tokens=1000,
|
161 |
+
# )
|
162 |
+
# return chat_completion.choices[0].message.content
|
163 |
+
# except Exception as e:
|
164 |
+
# return f"Sorry, there was an error in the connection: {str(e)}"
|
165 |
+
|
166 |
+
|
167 |
+
# def create_new_chat():
|
168 |
+
# chat_id = datetime.now().strftime("%Y%m%d_%H%M%S")
|
169 |
+
# st.session_state.temp_chat = [{"role": "system", "content": SYSTEM_PROMPT}]
|
170 |
+
# st.session_state.current_chat_id = chat_id
|
171 |
+
# return chat_id
|
172 |
+
|
173 |
+
|
174 |
+
# def save_chat():
|
175 |
+
# if st.session_state.temp_chat and len(st.session_state.temp_chat) > 1:
|
176 |
+
# st.session_state.chat_history[st.session_state.current_chat_id] = (
|
177 |
+
# st.session_state.temp_chat
|
178 |
+
# )
|
179 |
+
# st.session_state.temp_chat = None
|
180 |
+
|
181 |
+
|
182 |
+
# def delete_chat(chat_id):
|
183 |
+
# if chat_id in st.session_state.chat_history:
|
184 |
+
# del st.session_state.chat_history[chat_id]
|
185 |
+
# if st.session_state.current_chat_id == chat_id:
|
186 |
+
# st.session_state.current_chat_id = None
|
187 |
+
|
188 |
+
|
189 |
+
# def get_chat_preview(chat):
|
190 |
+
# for message in chat:
|
191 |
+
# if message["role"] == "user":
|
192 |
+
# preview = message["content"][:30]
|
193 |
+
# return f"{preview}..." if len(message["content"]) > 30 else preview
|
194 |
+
# return "New Chat"
|
195 |
+
|
196 |
+
|
197 |
+
# # Main layout
|
198 |
+
# st.markdown(
|
199 |
+
# '<h1 class="main-title">π¨ββοΈ Virtual Medical Assistant</h1>', unsafe_allow_html=True
|
200 |
+
# )
|
201 |
+
|
202 |
+
# # Sidebar
|
203 |
+
# with st.sidebar:
|
204 |
+
# st.markdown('<div class="sidebar-content">', unsafe_allow_html=True)
|
205 |
+
|
206 |
+
# if st.button("New Chat β", key="new_chat"):
|
207 |
+
# create_new_chat()
|
208 |
+
|
209 |
+
# st.markdown("### Previous Chats")
|
210 |
+
|
211 |
+
# for chat_id in st.session_state.chat_history:
|
212 |
+
# col1, col2 = st.columns([4, 1])
|
213 |
+
# with col1:
|
214 |
+
# if st.button(
|
215 |
+
# get_chat_preview(st.session_state.chat_history[chat_id]),
|
216 |
+
# key=f"select_{chat_id}",
|
217 |
+
# ):
|
218 |
+
# st.session_state.current_chat_id = chat_id
|
219 |
+
# st.session_state.temp_chat = None
|
220 |
+
# with col2:
|
221 |
+
# if st.button("ποΈ", key=f"delete_{chat_id}", help="Delete Chat"):
|
222 |
+
# delete_chat(chat_id)
|
223 |
+
# st.rerun()
|
224 |
+
|
225 |
+
# st.markdown("</div>", unsafe_allow_html=True)
|
226 |
+
|
227 |
+
# # Main chat interface
|
228 |
+
# current_chat = st.session_state.temp_chat or (
|
229 |
+
# st.session_state.chat_history.get(st.session_state.current_chat_id, None)
|
230 |
+
# )
|
231 |
+
|
232 |
+
# if current_chat:
|
233 |
+
# st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
234 |
+
|
235 |
+
# # Display chat history
|
236 |
+
# for message in current_chat[1:]: # Skip system prompt
|
237 |
+
# if message["role"] == "user":
|
238 |
+
# st.markdown(
|
239 |
+
# f'<div class="user-message">{message["content"]}</div>',
|
240 |
+
# unsafe_allow_html=True,
|
241 |
+
# )
|
242 |
+
# else:
|
243 |
+
# st.markdown(
|
244 |
+
# f'<div class="assistant-message">{message["content"]}</div>',
|
245 |
+
# unsafe_allow_html=True,
|
246 |
+
# )
|
247 |
+
|
248 |
+
# st.markdown("</div>", unsafe_allow_html=True)
|
249 |
+
|
250 |
+
# # User input
|
251 |
+
# user_input = st.chat_input("Type your question here...")
|
252 |
+
# if user_input:
|
253 |
+
# # Check if current_chat_id exists, if not create a new one
|
254 |
+
# if not st.session_state.current_chat_id:
|
255 |
+
# create_new_chat()
|
256 |
+
# current_chat = st.session_state.temp_chat
|
257 |
+
|
258 |
+
# # Add user message
|
259 |
+
# current_chat.append({"role": "user", "content": user_input})
|
260 |
+
|
261 |
+
# # Get assistant response
|
262 |
+
# with st.spinner("Thinking..."):
|
263 |
+
# assistant_response = get_groq_response(current_chat)
|
264 |
+
|
265 |
+
# # Add assistant response to chat
|
266 |
+
# current_chat.append({"role": "assistant", "content": assistant_response})
|
267 |
+
|
268 |
+
# # Save chat if temporary
|
269 |
+
# if st.session_state.temp_chat:
|
270 |
+
# save_chat()
|
271 |
+
# st.rerun()
|
272 |
+
# else:
|
273 |
+
# st.markdown(
|
274 |
+
# """
|
275 |
+
# <div style='text-align: center; padding: 20px;'>
|
276 |
+
# <p style='color: black; font-size: 16px;'>
|
277 |
+
# Start a new chat by clicking the 'New Chat' β button
|
278 |
+
# </p>
|
279 |
+
# </div>
|
280 |
+
# """,
|
281 |
+
# unsafe_allow_html=True,
|
282 |
+
# )
|
283 |
+
|
284 |
+
# # Footer
|
285 |
+
# st.markdown(
|
286 |
+
# """
|
287 |
+
# <div style='text-align: center; color: #666; padding: 20px;'>
|
288 |
+
# <p>Reminder: This medical assistant is for general consultations only. Please consult a doctor for serious medical conditions.</p>
|
289 |
+
# </div>
|
290 |
+
# """,
|
291 |
+
# unsafe_allow_html=True,
|
292 |
+
# )
|
293 |
+
|
294 |
+
|
295 |
+
|
296 |
+
|
297 |
+
import os
|
298 |
+
import streamlit as st
|
299 |
+
from groq import Groq
|
300 |
+
import re
|
301 |
+
from datetime import datetime
|
302 |
+
from dotenv import load_dotenv
|
303 |
+
|
304 |
+
load_dotenv()
|
305 |
+
# Initialize Groq client with environment variable
|
306 |
+
Groq_API_KEY = os.getenv("GROQ_API_KEY")
|
307 |
+
# Initialize Groq client
|
308 |
+
client = Groq(api_key= Groq_API_KEY)
|
309 |
+
|
310 |
+
# Set page configuration
|
311 |
+
st.set_page_config(
|
312 |
+
page_title="Virtual Medical Assistant",
|
313 |
+
page_icon="π¨ββοΈ",
|
314 |
+
layout="wide",
|
315 |
+
initial_sidebar_state="expanded",
|
316 |
+
)
|
317 |
+
|
318 |
+
# Custom CSS with improved chat styling
|
319 |
+
st.markdown(
|
320 |
+
"""
|
321 |
+
<style>
|
322 |
+
.main {
|
323 |
+
background-color: #f5f7f9;
|
324 |
+
}
|
325 |
+
.stButton>button {
|
326 |
+
width: 100%;
|
327 |
+
border-radius: 5px;
|
328 |
+
height: 3em;
|
329 |
+
background-color: #0083B8;
|
330 |
+
color: white;
|
331 |
+
border: none;
|
332 |
+
}
|
333 |
+
.stButton>button:hover {
|
334 |
+
background-color: #00669e;
|
335 |
+
}
|
336 |
+
.chat-container {
|
337 |
+
background-color: #f0f2f6;
|
338 |
+
padding: 20px;
|
339 |
+
border-radius: 10px;
|
340 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
341 |
+
margin-bottom: 20px;
|
342 |
+
}
|
343 |
+
.user-message {
|
344 |
+
background-color: #0083B8;
|
345 |
+
padding: 10px 15px;
|
346 |
+
border-radius: 15px 15px 0 15px;
|
347 |
+
margin: 10px 0;
|
348 |
+
margin-left: 20%;
|
349 |
+
color: white;
|
350 |
+
text-align: right;
|
351 |
+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
352 |
+
}
|
353 |
+
.assistant-message {
|
354 |
+
background-color: #e9ecef;
|
355 |
+
padding: 10px 15px;
|
356 |
+
border-radius: 15px 15px 15px 0;
|
357 |
+
color: #333;
|
358 |
+
margin: 10px 0;
|
359 |
+
margin-right: 20%;
|
360 |
+
text-align: left;
|
361 |
+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
362 |
+
}
|
363 |
+
.sidebar-content {
|
364 |
+
padding: 20px;
|
365 |
+
}
|
366 |
+
.main-title {
|
367 |
+
text-align: center;
|
368 |
+
color: #0083B8;
|
369 |
+
padding: 20px 0;
|
370 |
+
}
|
371 |
+
.delete-button {
|
372 |
+
background-color: #dc3545 !important;
|
373 |
+
}
|
374 |
+
.delete-button:hover {
|
375 |
+
background-color: #c82333 !important;
|
376 |
+
}
|
377 |
+
</style>
|
378 |
+
""",
|
379 |
+
unsafe_allow_html=True,
|
380 |
+
)
|
381 |
+
|
382 |
+
# Updated System prompt for multilingual support
|
383 |
+
SYSTEM_PROMPT = """You are a multilingual virtual medical assistant with great expertise and empathy. Your role is:
|
384 |
+
|
385 |
+
1. Scope of Assistance:
|
386 |
+
- Diagnose common symptoms
|
387 |
+
- Suggest possible conditions
|
388 |
+
- Provide recommendations for common over-the-counter medications
|
389 |
+
- Offer advice on managing health issues
|
390 |
+
- Answer medical questions only
|
391 |
+
- Inform the patient about symptoms of illnesses
|
392 |
+
|
393 |
+
2. Important Limitations:
|
394 |
+
- Do not answer non-medical questions
|
395 |
+
- When asked a non-medical question, simply say in the user's language: "Sorry, this question is outside my medical expertise."
|
396 |
+
- Provide definitive diagnoses only for very common conditions
|
397 |
+
- Do not prescribe prescription medications
|
398 |
+
- Do not provide advice on serious medical conditions
|
399 |
+
- Do not greet the user in every response. Do so only if the user starts with a greeting.
|
400 |
+
|
401 |
+
3. When dealing with symptoms:
|
402 |
+
|
403 |
+
A. Gather information - Ask specific questions about:
|
404 |
+
- Duration of symptoms
|
405 |
+
- Severity of symptoms
|
406 |
+
- Age
|
407 |
+
- Medical history
|
408 |
+
- Current medications
|
409 |
+
- Associated symptoms
|
410 |
+
|
411 |
+
B. Assessment:
|
412 |
+
- Analyze the reported symptoms
|
413 |
+
- Link symptoms to common possible conditions
|
414 |
+
- Determine the severity of the condition
|
415 |
+
|
416 |
+
C. Recommendations:
|
417 |
+
- Suggest safe home remedies
|
418 |
+
- Recommend over-the-counter medications
|
419 |
+
- Provide prevention and self-care tips
|
420 |
+
|
421 |
+
4. When to refer the patient to a doctor:
|
422 |
+
- In case of severe symptoms
|
423 |
+
- If symptoms persist for a long time
|
424 |
+
- If the condition worsens
|
425 |
+
- When specific medical tests are needed
|
426 |
+
- In emergencies
|
427 |
+
|
428 |
+
5. Mandatory Reminder:
|
429 |
+
- Always emphasize that the advice provided is not a substitute for consulting a doctor
|
430 |
+
- Encourage visiting a healthcare provider in serious cases
|
431 |
+
- Explain that this advice is general and not an official medical diagnosis
|
432 |
+
|
433 |
+
6. Language and Communication:
|
434 |
+
- ALWAYS respond in the same language the user is using
|
435 |
+
- If the user writes in Spanish, respond in Spanish
|
436 |
+
- If the user writes in Arabic, respond in Arabic
|
437 |
+
- If the user writes in French, respond in French
|
438 |
+
- For any language the user chooses, respond in that same language
|
439 |
+
- Use simple and clear language
|
440 |
+
- Maintain a professional and empathetic tone
|
441 |
+
- Avoid complex medical terminology
|
442 |
+
|
443 |
+
7. In emergencies:
|
444 |
+
- Direct the patient immediately to the nearest emergency department
|
445 |
+
- Provide simple first aid instructions if necessary
|
446 |
+
- Emphasize the importance of seeking immediate medical help
|
447 |
+
|
448 |
+
Always remember: Patient safety is the top priority, and in case of doubt, always recommend visiting a doctor."""
|
449 |
+
|
450 |
+
# Initialize session state
|
451 |
+
if "chat_history" not in st.session_state:
|
452 |
+
st.session_state.chat_history = {}
|
453 |
+
if "current_chat_id" not in st.session_state:
|
454 |
+
st.session_state.current_chat_id = None
|
455 |
+
if "temp_chat" not in st.session_state:
|
456 |
+
st.session_state.temp_chat = None
|
457 |
+
|
458 |
+
|
459 |
+
def get_groq_response(messages):
|
460 |
+
try:
|
461 |
+
chat_completion = client.chat.completions.create(
|
462 |
+
messages=messages,
|
463 |
+
model="llama3-8b-8192",
|
464 |
+
temperature=0.7,
|
465 |
+
max_tokens=1000,
|
466 |
+
)
|
467 |
+
return chat_completion.choices[0].message.content
|
468 |
+
except Exception as e:
|
469 |
+
return f"Sorry, there was an error in the connection: {str(e)}"
|
470 |
+
|
471 |
+
|
472 |
+
def create_new_chat():
|
473 |
+
chat_id = datetime.now().strftime("%Y%m%d_%H%M%S")
|
474 |
+
st.session_state.temp_chat = [{"role": "system", "content": SYSTEM_PROMPT}]
|
475 |
+
st.session_state.current_chat_id = chat_id
|
476 |
+
return chat_id
|
477 |
+
|
478 |
+
|
479 |
+
def save_chat():
|
480 |
+
if st.session_state.temp_chat and len(st.session_state.temp_chat) > 1:
|
481 |
+
st.session_state.chat_history[st.session_state.current_chat_id] = (
|
482 |
+
st.session_state.temp_chat
|
483 |
+
)
|
484 |
+
st.session_state.temp_chat = None
|
485 |
+
|
486 |
+
|
487 |
+
def delete_chat(chat_id):
|
488 |
+
if chat_id in st.session_state.chat_history:
|
489 |
+
del st.session_state.chat_history[chat_id]
|
490 |
+
if st.session_state.current_chat_id == chat_id:
|
491 |
+
st.session_state.current_chat_id = None
|
492 |
+
|
493 |
+
|
494 |
+
def get_chat_preview(chat):
|
495 |
+
for message in chat:
|
496 |
+
if message["role"] == "user":
|
497 |
+
preview = message["content"][:30]
|
498 |
+
return f"{preview}..." if len(message["content"]) > 30 else preview
|
499 |
+
return "New Chat"
|
500 |
+
|
501 |
+
|
502 |
+
# Main layout
|
503 |
+
st.markdown(
|
504 |
+
'<h1 class="main-title">π¨ββοΈ Call on Doc Virtual Medical Assistant</h1>', unsafe_allow_html=True
|
505 |
+
)
|
506 |
+
|
507 |
+
# Add language indicator in the sidebar
|
508 |
+
with st.sidebar:
|
509 |
+
st.markdown('<div class="sidebar-content">', unsafe_allow_html=True)
|
510 |
+
|
511 |
+
st.markdown("### Multilingual Support")
|
512 |
+
st.markdown("This assistant automatically responds in the language you use to ask your question.")
|
513 |
+
|
514 |
+
if st.button("New Chat β", key="new_chat"):
|
515 |
+
create_new_chat()
|
516 |
+
|
517 |
+
st.markdown("### Previous Chats")
|
518 |
+
|
519 |
+
for chat_id in st.session_state.chat_history:
|
520 |
+
col1, col2 = st.columns([4, 1])
|
521 |
+
with col1:
|
522 |
+
if st.button(
|
523 |
+
get_chat_preview(st.session_state.chat_history[chat_id]),
|
524 |
+
key=f"select_{chat_id}",
|
525 |
+
):
|
526 |
+
st.session_state.current_chat_id = chat_id
|
527 |
+
st.session_state.temp_chat = None
|
528 |
+
with col2:
|
529 |
+
if st.button("ποΈ", key=f"delete_{chat_id}", help="Delete Chat"):
|
530 |
+
delete_chat(chat_id)
|
531 |
+
st.rerun()
|
532 |
+
|
533 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
534 |
+
|
535 |
+
# Main chat interface
|
536 |
+
current_chat = st.session_state.temp_chat or (
|
537 |
+
st.session_state.chat_history.get(st.session_state.current_chat_id, None)
|
538 |
+
)
|
539 |
+
|
540 |
+
if current_chat:
|
541 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
542 |
+
|
543 |
+
# Display chat history
|
544 |
+
for message in current_chat[1:]: # Skip system prompt
|
545 |
+
if message["role"] == "user":
|
546 |
+
st.markdown(
|
547 |
+
f'<div class="user-message">{message["content"]}</div>',
|
548 |
+
unsafe_allow_html=True,
|
549 |
+
)
|
550 |
+
else:
|
551 |
+
st.markdown(
|
552 |
+
f'<div class="assistant-message">{message["content"]}</div>',
|
553 |
+
unsafe_allow_html=True,
|
554 |
+
)
|
555 |
+
|
556 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
557 |
+
|
558 |
+
# User input
|
559 |
+
user_input = st.chat_input("Type your question in any language...")
|
560 |
+
if user_input:
|
561 |
+
# Check if current_chat_id exists, if not create a new one
|
562 |
+
if not st.session_state.current_chat_id:
|
563 |
+
create_new_chat()
|
564 |
+
current_chat = st.session_state.temp_chat
|
565 |
+
|
566 |
+
# Add user message
|
567 |
+
current_chat.append({"role": "user", "content": user_input})
|
568 |
+
|
569 |
+
# Get assistant response
|
570 |
+
with st.spinner("Thinking..."):
|
571 |
+
assistant_response = get_groq_response(current_chat)
|
572 |
+
|
573 |
+
# Add assistant response to chat
|
574 |
+
current_chat.append({"role": "assistant", "content": assistant_response})
|
575 |
+
|
576 |
+
# Save chat if temporary
|
577 |
+
if st.session_state.temp_chat:
|
578 |
+
save_chat()
|
579 |
+
st.rerun()
|
580 |
+
else:
|
581 |
+
st.markdown(
|
582 |
+
"""
|
583 |
+
<div style='text-align: center; padding: 20px;'>
|
584 |
+
<p style='color: black; font-size: 16px;'>
|
585 |
+
Start a new chat by clicking the 'New Chat β' button
|
586 |
+
</p>
|
587 |
+
<p style='color: white; font-size: 14px;'>
|
588 |
+
You can ask questions in any language, and the assistant will respond in the same language.
|
589 |
+
</p>
|
590 |
+
</div>
|
591 |
+
""",
|
592 |
+
unsafe_allow_html=True,
|
593 |
+
)
|
594 |
+
|
595 |
+
# Footer
|
596 |
+
st.markdown(
|
597 |
+
"""
|
598 |
+
<div style='text-align: center; color: #666; padding: 20px;'>
|
599 |
+
<p>Reminder: This medical assistant is for general consultations only. Please consult a doctor for serious medical conditions.</p>
|
600 |
+
</div>
|
601 |
+
""",
|
602 |
+
unsafe_allow_html=True,
|
603 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit==1.39.0
|
2 |
+
groq==0.11.0
|