File size: 9,790 Bytes
862ae5e 94f6a81 862ae5e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 133 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
import os
import streamlit as st
from groq import Groq
import re
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()
# Initialize Groq client with environment variable
Groq_API_KEY = os.getenv("GROQ_API_KEY")
# Initialize Groq client
client = Groq(api_key= Groq_API_KEY)
# Set page configuration
st.set_page_config(
page_title="Virtual Medical Assistant",
page_icon="π¨ββοΈ",
layout="wide",
initial_sidebar_state="expanded",
)
# Custom CSS with improved chat styling
st.markdown(
"""
<style>
.main {
background-color: #f5f7f9;
}
.stButton>button {
width: 100%;
border-radius: 5px;
height: 3em;
background-color: #0083B8;
color: white;
border: none;
}
.stButton>button:hover {
background-color: #00669e;
}
.chat-container {
background-color: #f0f2f6;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
.user-message {
background-color: #0083B8;
padding: 10px 15px;
border-radius: 15px 15px 0 15px;
margin: 10px 0;
margin-left: 20%;
color: white;
text-align: right;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}
.assistant-message {
background-color: #e9ecef;
padding: 10px 15px;
border-radius: 15px 15px 15px 0;
color: #333;
margin: 10px 0;
margin-right: 20%;
text-align: left;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}
.sidebar-content {
padding: 20px;
}
.main-title {
text-align: center;
color: #0083B8;
padding: 20px 0;
}
.delete-button {
background-color: #dc3545 !important;
}
.delete-button:hover {
background-color: #c82333 !important;
}
</style>
""",
unsafe_allow_html=True,
)
# Updated System prompt for multilingual support
SYSTEM_PROMPT = """You are a multilingual virtual medical assistant with great expertise and empathy. Your role is:
1. Scope of Assistance:
- Diagnose common symptoms
- Suggest possible conditions
- Provide recommendations for common over-the-counter medications
- Offer advice on managing health issues
- Answer medical questions only
- Inform the patient about symptoms of illnesses
2. Important Limitations:
- Do not answer non-medical questions
- When asked a non-medical question, simply say in the user's language: "Sorry, this question is outside my medical expertise."
- Provide definitive diagnoses only for very common conditions
- Do not prescribe prescription medications
- Do not provide advice on serious medical conditions
- Do not greet the user in every response. Do so only if the user starts with a greeting.
3. When dealing with symptoms:
A. Gather information - Ask specific questions about:
- Duration of symptoms
- Severity of symptoms
- Age
- Medical history
- Current medications
- Associated symptoms
B. Assessment:
- Analyze the reported symptoms
- Link symptoms to common possible conditions
- Determine the severity of the condition
C. Recommendations:
- Suggest safe home remedies
- Recommend over-the-counter medications
- Provide prevention and self-care tips
4. When to refer the patient to a doctor:
- In case of severe symptoms
- If symptoms persist for a long time
- If the condition worsens
- When specific medical tests are needed
- In emergencies
5. Mandatory Reminder:
- Always emphasize that the advice provided is not a substitute for consulting a doctor
- Encourage visiting a healthcare provider in serious cases
- Explain that this advice is general and not an official medical diagnosis
6. Language and Communication:
- ALWAYS respond in the same language the user is using
- If the user writes in Spanish, respond in Spanish
- If the user writes in Arabic, respond in Arabic
- If the user writes in French, respond in French
- For any language the user chooses, respond in that same language
- Use simple and clear language
- Maintain a professional and empathetic tone
- Avoid complex medical terminology
7. In emergencies:
- Direct the patient immediately to the nearest emergency department
- Provide simple first aid instructions if necessary
- Emphasize the importance of seeking immediate medical help
Always remember: Patient safety is the top priority, and in case of doubt, always recommend visiting a doctor."""
# Initialize session state
if "chat_history" not in st.session_state:
st.session_state.chat_history = {}
if "current_chat_id" not in st.session_state:
st.session_state.current_chat_id = None
if "temp_chat" not in st.session_state:
st.session_state.temp_chat = None
def get_groq_response(messages):
try:
chat_completion = client.chat.completions.create(
messages=messages,
model="llama3-8b-8192",
temperature=0.7,
max_tokens=1000,
)
return chat_completion.choices[0].message.content
except Exception as e:
return f"Sorry, there was an error in the connection: {str(e)}"
def create_new_chat():
chat_id = datetime.now().strftime("%Y%m%d_%H%M%S")
st.session_state.temp_chat = [{"role": "system", "content": SYSTEM_PROMPT}]
st.session_state.current_chat_id = chat_id
return chat_id
def save_chat():
if st.session_state.temp_chat and len(st.session_state.temp_chat) > 1:
st.session_state.chat_history[st.session_state.current_chat_id] = (
st.session_state.temp_chat
)
st.session_state.temp_chat = None
def delete_chat(chat_id):
if chat_id in st.session_state.chat_history:
del st.session_state.chat_history[chat_id]
if st.session_state.current_chat_id == chat_id:
st.session_state.current_chat_id = None
def get_chat_preview(chat):
for message in chat:
if message["role"] == "user":
preview = message["content"][:30]
return f"{preview}..." if len(message["content"]) > 30 else preview
return "New Chat"
# Main layout
st.markdown(
'<h1 class="main-title">π¨ββοΈ Call on Doc Virtual Medical Assistant</h1>', unsafe_allow_html=True
)
# Add language indicator in the sidebar
with st.sidebar:
st.markdown('<div class="sidebar-content">', unsafe_allow_html=True)
st.markdown("### Multilingual Support")
st.markdown("This assistant automatically responds in the language you use to ask your question.")
if st.button("New Chat β", key="new_chat"):
create_new_chat()
st.markdown("### Previous Chats")
for chat_id in st.session_state.chat_history:
col1, col2 = st.columns([4, 1])
with col1:
if st.button(
get_chat_preview(st.session_state.chat_history[chat_id]),
key=f"select_{chat_id}",
):
st.session_state.current_chat_id = chat_id
st.session_state.temp_chat = None
with col2:
if st.button("ποΈ", key=f"delete_{chat_id}", help="Delete Chat"):
delete_chat(chat_id)
st.rerun()
st.markdown("</div>", unsafe_allow_html=True)
# Main chat interface
current_chat = st.session_state.temp_chat or (
st.session_state.chat_history.get(st.session_state.current_chat_id, None)
)
if current_chat:
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
# Display chat history
for message in current_chat[1:]: # Skip system prompt
if message["role"] == "user":
st.markdown(
f'<div class="user-message">{message["content"]}</div>',
unsafe_allow_html=True,
)
else:
st.markdown(
f'<div class="assistant-message">{message["content"]}</div>',
unsafe_allow_html=True,
)
st.markdown("</div>", unsafe_allow_html=True)
# User input
user_input = st.chat_input("Type your question in any language...")
if user_input:
# Check if current_chat_id exists, if not create a new one
if not st.session_state.current_chat_id:
create_new_chat()
current_chat = st.session_state.temp_chat
# Add user message
current_chat.append({"role": "user", "content": user_input})
# Get assistant response
with st.spinner("Thinking..."):
assistant_response = get_groq_response(current_chat)
# Add assistant response to chat
current_chat.append({"role": "assistant", "content": assistant_response})
# Save chat if temporary
if st.session_state.temp_chat:
save_chat()
st.rerun()
else:
st.markdown(
"""
<div style='text-align: center; padding: 20px;'>
<p style='color: white; font-size: 16px;'>
Start a new chat by clicking the 'New Chat β' button
</p>
<p style='color: white; font-size: 14px;'>
You can ask questions in any language, and the assistant will respond in the same language.
</p>
</div>
""",
unsafe_allow_html=True,
)
# Footer
st.markdown(
"""
<div style='text-align: center; color: #666; padding: 20px;'>
<p>Reminder: This medical assistant is for general consultations only. Please consult a doctor for serious medical conditions.</p>
</div>
""",
unsafe_allow_html=True,
) |