Update app.py
Browse files
app.py
CHANGED
@@ -13,10 +13,13 @@ logging.basicConfig(
|
|
13 |
)
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
|
|
|
|
|
|
16 |
class HealthAssistant:
|
17 |
def __init__(self, use_smaller_model=True):
|
18 |
if use_smaller_model:
|
19 |
-
self.model_name = "
|
20 |
else:
|
21 |
self.model_name = "Qwen/Qwen2-VL-7B-Instruct"
|
22 |
|
@@ -77,76 +80,74 @@ class HealthAssistant:
|
|
77 |
|
78 |
return "general"
|
79 |
|
80 |
-
def
|
81 |
-
"""
|
82 |
-
base_context =
|
83 |
-
{self._get_health_context()}
|
84 |
-
|
85 |
-
"""
|
86 |
|
87 |
prompts = {
|
88 |
-
"symptom_check": f"""
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
97 |
5. When to seek medical care
|
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 |
-
URGENT Health Situation:
|
126 |
-
Condition: {message}
|
127 |
-
|
128 |
-
Critical guidance:
|
129 |
1. Severity assessment
|
130 |
2. Immediate actions needed
|
131 |
-
3. Emergency signs
|
132 |
-
4.
|
133 |
5. Precautions while waiting
|
134 |
-
⚠️ SEEK IMMEDIATE MEDICAL ATTENTION FOR EMERGENCIES
|
135 |
-
""",
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
145 |
4. Important considerations
|
146 |
-
5. Additional recommendations
|
147 |
-
"""
|
148 |
}
|
149 |
-
|
150 |
return prompts.get(query_type, prompts["general"])
|
151 |
|
152 |
def generate_response(self, message: str, history: List = None) -> str:
|
@@ -154,14 +155,16 @@ Provide structured response:
|
|
154 |
if not hasattr(self, 'model') or self.model is None:
|
155 |
return "System is initializing. Please try again in a moment."
|
156 |
|
157 |
-
# Detect query type
|
158 |
query_type = self._detect_query_type(message)
|
159 |
-
prompt = self._get_specialized_prompt(query_type, message)
|
160 |
|
|
|
|
|
|
|
161 |
# Add conversation history if available
|
162 |
if history:
|
163 |
-
prompt += "\n\
|
164 |
-
for prev_msg, prev_response in history[-2:]:
|
165 |
prompt += f"\nQ: {prev_msg}\nA: {prev_response}\n"
|
166 |
|
167 |
# Tokenize
|
@@ -178,10 +181,9 @@ Provide structured response:
|
|
178 |
outputs = self.model.generate(
|
179 |
inputs["input_ids"],
|
180 |
max_new_tokens=150,
|
181 |
-
num_beams=
|
182 |
-
temperature=0.
|
183 |
top_p=0.9,
|
184 |
-
no_repeat_ngram_size=3,
|
185 |
pad_token_id=self.tokenizer.pad_token_id,
|
186 |
eos_token_id=self.tokenizer.eos_token_id
|
187 |
)
|
@@ -192,7 +194,7 @@ Provide structured response:
|
|
192 |
skip_special_tokens=True
|
193 |
)
|
194 |
|
195 |
-
#
|
196 |
response = self._format_response(response, query_type)
|
197 |
|
198 |
# Cleanup
|
@@ -207,36 +209,31 @@ Provide structured response:
|
|
207 |
|
208 |
def _format_response(self, response: str, query_type: str) -> str:
|
209 |
"""Format and clean the response"""
|
210 |
-
# Remove repeated headers
|
211 |
lines = [line.strip() for line in response.split('\n') if line.strip()]
|
212 |
clean_lines = []
|
213 |
seen = set()
|
214 |
|
215 |
for line in lines:
|
216 |
-
# Skip common headers and duplicates
|
217 |
-
if any(header in line for header in ["Location:", "Date:", "M.D.", "Medical"]):
|
218 |
-
continue
|
219 |
if line not in seen:
|
220 |
seen.add(line)
|
221 |
clean_lines.append(line)
|
222 |
|
223 |
-
# Add appropriate
|
224 |
-
|
225 |
-
"emergency_guidance": "🚨",
|
226 |
-
"symptom_check": "🔍",
|
227 |
-
"medication_info": "💊",
|
228 |
-
"lifestyle_advice": "💡",
|
229 |
-
"general": "ℹ️"
|
230 |
}
|
231 |
|
232 |
-
|
233 |
-
|
234 |
-
# Combine and format
|
235 |
-
formatted_response = f"{emoji} " + "\n".join(clean_lines)
|
236 |
|
237 |
-
# Add disclaimer
|
238 |
if query_type in ["emergency_guidance", "medication_info"]:
|
239 |
-
formatted_response += "\n\n⚠️ This is general information only. Always consult healthcare professionals
|
240 |
|
241 |
return formatted_response
|
242 |
|
@@ -261,7 +258,8 @@ Provide structured response:
|
|
261 |
med_info += f" | Note: {med['Notes']}"
|
262 |
context_parts.append(med_info)
|
263 |
|
264 |
-
return "\n".join(context_parts) if context_parts else "No health data
|
|
|
265 |
def add_metrics(self, weight: float, steps: int, sleep: float) -> bool:
|
266 |
try:
|
267 |
self.metrics.append({
|
@@ -301,13 +299,8 @@ class GradioInterface:
|
|
301 |
if not message.strip():
|
302 |
return "", history
|
303 |
|
304 |
-
# Generate response
|
305 |
response = self.assistant.generate_response(message, history)
|
306 |
-
|
307 |
-
# Update history
|
308 |
history.append([message, response])
|
309 |
-
|
310 |
-
# Clear input and return updated history
|
311 |
return "", history
|
312 |
|
313 |
def add_health_metrics(self, weight: float, steps: int, sleep: float) -> str:
|
@@ -337,7 +330,7 @@ class GradioInterface:
|
|
337 |
return "❌ Error adding medication."
|
338 |
|
339 |
def create_interface(self):
|
340 |
-
with gr.Blocks(title="Medical Health Assistant"
|
341 |
gr.Markdown("""
|
342 |
# 🏥 Medical Health Assistant
|
343 |
|
@@ -351,7 +344,7 @@ class GradioInterface:
|
|
351 |
chatbot = gr.Chatbot(
|
352 |
value=[],
|
353 |
height=450,
|
354 |
-
|
355 |
)
|
356 |
with gr.Row():
|
357 |
msg = gr.Textbox(
|
@@ -360,8 +353,8 @@ class GradioInterface:
|
|
360 |
show_label=False,
|
361 |
scale=9
|
362 |
)
|
363 |
-
send_btn = gr.Button("
|
364 |
-
clear_btn = gr.Button("
|
365 |
|
366 |
# Health Metrics
|
367 |
with gr.Tab("📊 Health Metrics"):
|
@@ -383,7 +376,7 @@ class GradioInterface:
|
|
383 |
minimum=0,
|
384 |
maximum=24
|
385 |
)
|
386 |
-
metrics_btn = gr.Button("
|
387 |
metrics_status = gr.Markdown()
|
388 |
|
389 |
# Medication Manager
|
@@ -407,7 +400,7 @@ class GradioInterface:
|
|
407 |
label="Notes (optional)",
|
408 |
placeholder="Additional instructions or notes"
|
409 |
)
|
410 |
-
med_btn = gr.Button("
|
411 |
med_status = gr.Markdown()
|
412 |
|
413 |
# Event handlers
|
@@ -427,7 +420,6 @@ class GradioInterface:
|
|
427 |
outputs=[med_status]
|
428 |
)
|
429 |
|
430 |
-
# Add helpful information
|
431 |
gr.Markdown("""
|
432 |
### ⚠️ Important Medical Disclaimer
|
433 |
This AI assistant provides general health information only.
|
@@ -436,7 +428,6 @@ class GradioInterface:
|
|
436 |
- Seek immediate medical attention for emergencies
|
437 |
""")
|
438 |
|
439 |
-
# Enable queuing for better performance
|
440 |
demo.queue()
|
441 |
|
442 |
return demo
|
|
|
13 |
)
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
16 |
+
# Set torch threads
|
17 |
+
torch.set_num_threads(4)
|
18 |
+
|
19 |
class HealthAssistant:
|
20 |
def __init__(self, use_smaller_model=True):
|
21 |
if use_smaller_model:
|
22 |
+
self.model_name = "facebook/opt-125m"
|
23 |
else:
|
24 |
self.model_name = "Qwen/Qwen2-VL-7B-Instruct"
|
25 |
|
|
|
80 |
|
81 |
return "general"
|
82 |
|
83 |
+
def _prepare_medical_prompt(self, message: str, query_type: str) -> str:
|
84 |
+
"""Prepare medical prompt based on query type"""
|
85 |
+
base_context = self._get_health_context()
|
|
|
|
|
|
|
86 |
|
87 |
prompts = {
|
88 |
+
"symptom_check": f"""You are a medical AI assistant. Based on the following health context and symptoms, provide a careful analysis.
|
89 |
+
|
90 |
+
Current Health Context:
|
91 |
+
{base_context}
|
92 |
+
|
93 |
+
Patient's Symptoms: {message}
|
94 |
+
|
95 |
+
Provide a structured response covering:
|
96 |
+
1. Key symptoms identified
|
97 |
+
2. Possible common causes
|
98 |
+
3. General recommendations
|
99 |
+
4. Warning signs to watch for
|
100 |
5. When to seek medical care
|
101 |
+
|
102 |
+
Remember to maintain a professional and careful tone.""",
|
103 |
+
|
104 |
+
"medication_info": f"""You are a medical AI assistant. Provide information about the medication inquiry while noting you cannot give prescription advice.
|
105 |
+
|
106 |
+
Current Health Context:
|
107 |
+
{base_context}
|
108 |
+
|
109 |
+
Medication Query: {message}
|
110 |
+
|
111 |
+
Provide general information about:
|
112 |
+
1. Basic medication category/purpose
|
113 |
+
2. General usage patterns
|
114 |
+
3. Common considerations
|
115 |
+
4. Important precautions
|
116 |
+
5. When to consult a healthcare provider
|
117 |
+
|
118 |
+
Remember to emphasize this is general information only.""",
|
119 |
+
|
120 |
+
"emergency_guidance": f"""You are a medical AI assistant. This appears to be an urgent situation.
|
121 |
+
|
122 |
+
Current Health Context:
|
123 |
+
{base_context}
|
124 |
+
|
125 |
+
Urgent Situation: {message}
|
126 |
+
|
127 |
+
Provide immediate guidance:
|
|
|
|
|
|
|
|
|
128 |
1. Severity assessment
|
129 |
2. Immediate actions needed
|
130 |
+
3. Emergency warning signs
|
131 |
+
4. Whether to call emergency services
|
132 |
5. Precautions while waiting
|
|
|
|
|
133 |
|
134 |
+
Always emphasize seeking immediate medical care for emergencies.""",
|
135 |
+
|
136 |
+
"general": f"""You are a medical AI assistant. Provide helpful health information based on the query.
|
137 |
|
138 |
+
Current Health Context:
|
139 |
+
{base_context}
|
140 |
+
|
141 |
+
Health Query: {message}
|
142 |
+
|
143 |
+
Provide a structured response covering:
|
144 |
+
1. Understanding of the question
|
145 |
+
2. Relevant health information
|
146 |
+
3. General guidance
|
147 |
4. Important considerations
|
148 |
+
5. Additional recommendations"""
|
|
|
149 |
}
|
150 |
+
|
151 |
return prompts.get(query_type, prompts["general"])
|
152 |
|
153 |
def generate_response(self, message: str, history: List = None) -> str:
|
|
|
155 |
if not hasattr(self, 'model') or self.model is None:
|
156 |
return "System is initializing. Please try again in a moment."
|
157 |
|
158 |
+
# Detect query type
|
159 |
query_type = self._detect_query_type(message)
|
|
|
160 |
|
161 |
+
# Prepare prompt
|
162 |
+
prompt = self._prepare_medical_prompt(message, query_type)
|
163 |
+
|
164 |
# Add conversation history if available
|
165 |
if history:
|
166 |
+
prompt += "\n\nRecent conversation context:"
|
167 |
+
for prev_msg, prev_response in history[-2:]:
|
168 |
prompt += f"\nQ: {prev_msg}\nA: {prev_response}\n"
|
169 |
|
170 |
# Tokenize
|
|
|
181 |
outputs = self.model.generate(
|
182 |
inputs["input_ids"],
|
183 |
max_new_tokens=150,
|
184 |
+
num_beams=1,
|
185 |
+
temperature=0.7,
|
186 |
top_p=0.9,
|
|
|
187 |
pad_token_id=self.tokenizer.pad_token_id,
|
188 |
eos_token_id=self.tokenizer.eos_token_id
|
189 |
)
|
|
|
194 |
skip_special_tokens=True
|
195 |
)
|
196 |
|
197 |
+
# Format response
|
198 |
response = self._format_response(response, query_type)
|
199 |
|
200 |
# Cleanup
|
|
|
209 |
|
210 |
def _format_response(self, response: str, query_type: str) -> str:
|
211 |
"""Format and clean the response"""
|
212 |
+
# Remove repeated headers
|
213 |
lines = [line.strip() for line in response.split('\n') if line.strip()]
|
214 |
clean_lines = []
|
215 |
seen = set()
|
216 |
|
217 |
for line in lines:
|
|
|
|
|
|
|
218 |
if line not in seen:
|
219 |
seen.add(line)
|
220 |
clean_lines.append(line)
|
221 |
|
222 |
+
# Add appropriate prefix based on query type
|
223 |
+
prefixes = {
|
224 |
+
"emergency_guidance": "🚨 URGENT: ",
|
225 |
+
"symptom_check": "🔍 Analysis: ",
|
226 |
+
"medication_info": "💊 Medication Info: ",
|
227 |
+
"lifestyle_advice": "💡 Health Advice: ",
|
228 |
+
"general": "ℹ️ "
|
229 |
}
|
230 |
|
231 |
+
prefix = prefixes.get(query_type, "ℹ️ ")
|
232 |
+
formatted_response = prefix + "\n".join(clean_lines)
|
|
|
|
|
233 |
|
234 |
+
# Add disclaimer for certain types
|
235 |
if query_type in ["emergency_guidance", "medication_info"]:
|
236 |
+
formatted_response += "\n\n⚠️ Note: This is general information only. Always consult healthcare professionals."
|
237 |
|
238 |
return formatted_response
|
239 |
|
|
|
258 |
med_info += f" | Note: {med['Notes']}"
|
259 |
context_parts.append(med_info)
|
260 |
|
261 |
+
return "\n".join(context_parts) if context_parts else "No health data recorded"
|
262 |
+
|
263 |
def add_metrics(self, weight: float, steps: int, sleep: float) -> bool:
|
264 |
try:
|
265 |
self.metrics.append({
|
|
|
299 |
if not message.strip():
|
300 |
return "", history
|
301 |
|
|
|
302 |
response = self.assistant.generate_response(message, history)
|
|
|
|
|
303 |
history.append([message, response])
|
|
|
|
|
304 |
return "", history
|
305 |
|
306 |
def add_health_metrics(self, weight: float, steps: int, sleep: float) -> str:
|
|
|
330 |
return "❌ Error adding medication."
|
331 |
|
332 |
def create_interface(self):
|
333 |
+
with gr.Blocks(title="Medical Health Assistant") as demo:
|
334 |
gr.Markdown("""
|
335 |
# 🏥 Medical Health Assistant
|
336 |
|
|
|
344 |
chatbot = gr.Chatbot(
|
345 |
value=[],
|
346 |
height=450,
|
347 |
+
show_label=False
|
348 |
)
|
349 |
with gr.Row():
|
350 |
msg = gr.Textbox(
|
|
|
353 |
show_label=False,
|
354 |
scale=9
|
355 |
)
|
356 |
+
send_btn = gr.Button("Send", scale=1)
|
357 |
+
clear_btn = gr.Button("Clear Chat")
|
358 |
|
359 |
# Health Metrics
|
360 |
with gr.Tab("📊 Health Metrics"):
|
|
|
376 |
minimum=0,
|
377 |
maximum=24
|
378 |
)
|
379 |
+
metrics_btn = gr.Button("Save Metrics")
|
380 |
metrics_status = gr.Markdown()
|
381 |
|
382 |
# Medication Manager
|
|
|
400 |
label="Notes (optional)",
|
401 |
placeholder="Additional instructions or notes"
|
402 |
)
|
403 |
+
med_btn = gr.Button("Add Medication")
|
404 |
med_status = gr.Markdown()
|
405 |
|
406 |
# Event handlers
|
|
|
420 |
outputs=[med_status]
|
421 |
)
|
422 |
|
|
|
423 |
gr.Markdown("""
|
424 |
### ⚠️ Important Medical Disclaimer
|
425 |
This AI assistant provides general health information only.
|
|
|
428 |
- Seek immediate medical attention for emergencies
|
429 |
""")
|
430 |
|
|
|
431 |
demo.queue()
|
432 |
|
433 |
return demo
|