Update app.py
Browse files
app.py
CHANGED
@@ -1,299 +1,3 @@
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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