Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -36,7 +36,8 @@ class AdaptiveMedicalBot:
|
|
36 |
self.setup_models()
|
37 |
self.load_datasets()
|
38 |
self.setup_adaptive_learning()
|
39 |
-
self.conversation_history = [] #
|
|
|
40 |
|
41 |
class AdaptiveBotConfig:
|
42 |
MODEL_NAME = "google/gemma-7b"
|
@@ -199,7 +200,7 @@ class AdaptiveMedicalBot:
|
|
199 |
logger.error(f"Error processing feedback: {e}")
|
200 |
|
201 |
def create_demo():
|
202 |
-
"""Set up Gradio interface for the chatbot"""
|
203 |
try:
|
204 |
bot = AdaptiveMedicalBot()
|
205 |
|
@@ -216,47 +217,173 @@ def create_demo():
|
|
216 |
logger.error(f"Chat error: {e}")
|
217 |
return history + [
|
218 |
{"role": "user", "content": message},
|
219 |
-
{"role": "assistant", "content": "I'm experiencing technical difficulties. For emergencies, call 999."}
|
220 |
]
|
221 |
|
222 |
def process_feedback(feedback: str, history: List[Dict[str, str]], comment: str = ""):
|
|
|
223 |
try:
|
224 |
if history and len(history) >= 2:
|
225 |
last_user_msg = history[-2]["content"]
|
226 |
last_bot_msg = history[-1]["content"]
|
227 |
-
bot.handle_feedback(
|
|
|
|
|
|
|
|
|
228 |
except Exception as e:
|
229 |
logger.error(f"Error processing feedback: {e}")
|
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 |
# Event Handlers
|
258 |
submit.click(
|
259 |
-
|
260 |
inputs=[msg, chatbot],
|
261 |
outputs=[chatbot]
|
262 |
).then(
|
@@ -266,7 +393,7 @@ def create_demo():
|
|
266 |
)
|
267 |
|
268 |
msg.submit(
|
269 |
-
|
270 |
inputs=[msg, chatbot],
|
271 |
outputs=[chatbot]
|
272 |
).then(
|
@@ -275,46 +402,47 @@ def create_demo():
|
|
275 |
msg
|
276 |
)
|
277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
feedback.change(
|
279 |
-
|
280 |
inputs=[feedback, chatbot, feedback_text],
|
281 |
outputs=[]
|
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 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
**NHS 111**: Available 24/7 for urgent but non-life-threatening situations, medical advice, and guidance.
|
315 |
-
|
316 |
-
**GP Services**: Routine check-ups, non-urgent medical issues, and prescription renewals.
|
317 |
-
""")
|
318 |
|
319 |
return demo
|
320 |
|
@@ -323,8 +451,16 @@ def create_demo():
|
|
323 |
raise
|
324 |
|
325 |
if __name__ == "__main__":
|
326 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
327 |
demo = create_demo()
|
328 |
-
demo.launch()
|
|
|
329 |
|
330 |
-
|
|
|
36 |
self.setup_models()
|
37 |
self.load_datasets()
|
38 |
self.setup_adaptive_learning()
|
39 |
+
self.conversation_history = [] # Store conversation history
|
40 |
+
self.symptom_tracker = {} # Track symptoms and severity across conversation
|
41 |
|
42 |
class AdaptiveBotConfig:
|
43 |
MODEL_NAME = "google/gemma-7b"
|
|
|
200 |
logger.error(f"Error processing feedback: {e}")
|
201 |
|
202 |
def create_demo():
|
203 |
+
"""Set up Gradio interface for the chatbot with enhanced styling and functionality."""
|
204 |
try:
|
205 |
bot = AdaptiveMedicalBot()
|
206 |
|
|
|
217 |
logger.error(f"Chat error: {e}")
|
218 |
return history + [
|
219 |
{"role": "user", "content": message},
|
220 |
+
{"role": "assistant", "content": "I'm experiencing technical difficulties. For emergencies, please call 999."}
|
221 |
]
|
222 |
|
223 |
def process_feedback(feedback: str, history: List[Dict[str, str]], comment: str = ""):
|
224 |
+
"""Process feedback with optional comment"""
|
225 |
try:
|
226 |
if history and len(history) >= 2:
|
227 |
last_user_msg = history[-2]["content"]
|
228 |
last_bot_msg = history[-1]["content"]
|
229 |
+
bot.handle_feedback(
|
230 |
+
last_user_msg,
|
231 |
+
last_bot_msg,
|
232 |
+
1 if feedback == "π" else -1
|
233 |
+
)
|
234 |
except Exception as e:
|
235 |
logger.error(f"Error processing feedback: {e}")
|
236 |
|
237 |
+
# Create enhanced Gradio interface
|
238 |
+
with gr.Blocks(theme=gr.themes.Soft(
|
239 |
+
primary_hue="blue",
|
240 |
+
secondary_hue="indigo",
|
241 |
+
neutral_hue="slate",
|
242 |
+
font=gr.themes.GoogleFont("Inter")
|
243 |
+
)) as demo:
|
244 |
+
# Custom CSS for enhanced styling
|
245 |
+
gr.HTML("""
|
246 |
+
<style>
|
247 |
+
.container { max-width: 900px; margin: auto; }
|
248 |
+
.header { text-align: center; padding: 20px; }
|
249 |
+
.emergency-banner {
|
250 |
+
background-color: #ff4444;
|
251 |
+
color: white;
|
252 |
+
padding: 10px;
|
253 |
+
text-align: center;
|
254 |
+
font-weight: bold;
|
255 |
+
margin-bottom: 20px;
|
256 |
+
}
|
257 |
+
.features-grid {
|
258 |
+
display: grid;
|
259 |
+
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
260 |
+
gap: 20px;
|
261 |
+
padding: 20px;
|
262 |
+
}
|
263 |
+
.feature-card {
|
264 |
+
background: #f8f9fa;
|
265 |
+
padding: 15px;
|
266 |
+
border-radius: 10px;
|
267 |
+
text-align: center;
|
268 |
+
}
|
269 |
+
</style>
|
270 |
+
""")
|
271 |
+
|
272 |
+
# Emergency Banner
|
273 |
+
gr.HTML("""
|
274 |
+
<div class="emergency-banner">
|
275 |
+
π¨ For medical emergencies, always call 999 immediately π¨
|
276 |
+
</div>
|
277 |
+
""")
|
278 |
+
|
279 |
+
# Header Section
|
280 |
+
with gr.Row(elem_classes="header"):
|
281 |
+
gr.Markdown("""
|
282 |
+
# GP Medical Triage Assistant - Pearly
|
283 |
+
|
284 |
+
Welcome to your personal medical triage assistant. I'm here to help assess your symptoms and guide you to appropriate care.
|
285 |
+
""")
|
286 |
+
|
287 |
+
# Main Features Grid
|
288 |
+
gr.HTML("""
|
289 |
+
<div class="features-grid">
|
290 |
+
<div class="feature-card">
|
291 |
+
π₯ GP Appointments
|
292 |
+
</div>
|
293 |
+
<div class="feature-card">
|
294 |
+
π Symptom Assessment
|
295 |
+
</div>
|
296 |
+
<div class="feature-card">
|
297 |
+
β‘ Urgent Care Guide
|
298 |
+
</div>
|
299 |
+
<div class="feature-card">
|
300 |
+
π Medical Advice
|
301 |
+
</div>
|
302 |
+
</div>
|
303 |
+
""")
|
304 |
+
|
305 |
+
# Chat Interface
|
306 |
+
with gr.Row():
|
307 |
+
with gr.Column(scale=4):
|
308 |
+
chatbot = gr.Chatbot(
|
309 |
+
value=[{
|
310 |
+
"role": "assistant",
|
311 |
+
"content": "Hello! I'm Pearly, your GP medical assistant. How can I help you today?"
|
312 |
+
}],
|
313 |
+
height=500,
|
314 |
+
elem_id="chatbot",
|
315 |
+
type="messages",
|
316 |
+
show_label=False
|
317 |
+
)
|
318 |
+
|
319 |
+
with gr.Row():
|
320 |
+
msg = gr.Textbox(
|
321 |
+
label="Your message",
|
322 |
+
placeholder="Type your message here...",
|
323 |
+
lines=2,
|
324 |
+
scale=4
|
325 |
+
)
|
326 |
+
submit = gr.Button("Send", variant="primary", scale=1)
|
327 |
+
|
328 |
+
with gr.Column(scale=1):
|
329 |
+
# Quick Actions Panel
|
330 |
+
gr.Markdown("### Quick Actions")
|
331 |
+
emergency_btn = gr.Button("π¨ Emergency Info", variant="secondary")
|
332 |
+
nhs_111_btn = gr.Button("π NHS 111 Info", variant="secondary")
|
333 |
+
booking_btn = gr.Button("π
GP Booking", variant="secondary")
|
334 |
+
|
335 |
+
# Conversation Controls
|
336 |
+
gr.Markdown("### Controls")
|
337 |
+
clear = gr.Button("ποΈ Clear Chat")
|
338 |
+
|
339 |
+
# Feedback Section
|
340 |
+
gr.Markdown("### Feedback")
|
341 |
+
feedback = gr.Radio(
|
342 |
+
choices=["π", "π"],
|
343 |
+
label="Was this response helpful?",
|
344 |
+
visible=True
|
345 |
+
)
|
346 |
+
feedback_text = gr.Textbox(
|
347 |
+
label="Additional comments (optional)",
|
348 |
+
placeholder="Tell us more about your experience...",
|
349 |
+
lines=2
|
350 |
+
)
|
351 |
+
|
352 |
+
# Examples Section
|
353 |
+
with gr.Accordion("Example Messages", open=False):
|
354 |
+
gr.Examples(
|
355 |
+
examples=[
|
356 |
+
["I've been having severe headaches for the past week"],
|
357 |
+
["I need to book a routine checkup"],
|
358 |
+
["I'm feeling very anxious lately and need help"],
|
359 |
+
["My child has had a fever for 2 days"],
|
360 |
+
["I need information about COVID-19 testing"]
|
361 |
+
],
|
362 |
+
inputs=msg
|
363 |
+
)
|
364 |
+
|
365 |
+
# Information Accordions
|
366 |
+
with gr.Accordion("NHS Services Guide", open=False):
|
367 |
+
gr.Markdown("""
|
368 |
+
### Emergency Services (999)
|
369 |
+
- Life-threatening emergencies
|
370 |
+
- Severe injuries
|
371 |
+
- Suspected heart attack or stroke
|
372 |
+
|
373 |
+
### NHS 111
|
374 |
+
- Urgent but non-emergency situations
|
375 |
+
- Medical advice needed
|
376 |
+
- Unsure where to go
|
377 |
+
|
378 |
+
### GP Services
|
379 |
+
- Routine check-ups
|
380 |
+
- Non-urgent medical issues
|
381 |
+
- Prescription renewals
|
382 |
+
""")
|
383 |
+
|
384 |
# Event Handlers
|
385 |
submit.click(
|
386 |
+
chat,
|
387 |
inputs=[msg, chatbot],
|
388 |
outputs=[chatbot]
|
389 |
).then(
|
|
|
393 |
)
|
394 |
|
395 |
msg.submit(
|
396 |
+
chat,
|
397 |
inputs=[msg, chatbot],
|
398 |
outputs=[chatbot]
|
399 |
).then(
|
|
|
402 |
msg
|
403 |
)
|
404 |
|
405 |
+
clear.click(
|
406 |
+
lambda: [[], ""],
|
407 |
+
None,
|
408 |
+
[chatbot, msg]
|
409 |
+
)
|
410 |
+
|
411 |
feedback.change(
|
412 |
+
process_feedback,
|
413 |
inputs=[feedback, chatbot, feedback_text],
|
414 |
outputs=[]
|
415 |
)
|
416 |
+
|
417 |
+
# Quick Action Button Handlers
|
418 |
+
def show_emergency_info():
|
419 |
+
return """π¨ Emergency Services (999)
|
420 |
+
- For life-threatening emergencies
|
421 |
+
- Severe chest pain
|
422 |
+
- Difficulty breathing
|
423 |
+
- Severe bleeding
|
424 |
+
- Loss of consciousness
|
425 |
+
"""
|
426 |
+
|
427 |
+
def show_nhs_111_info():
|
428 |
+
return """π NHS 111 Service
|
429 |
+
- Available 24/7
|
430 |
+
- Medical advice
|
431 |
+
- Local service information
|
432 |
+
- Urgent care guidance
|
433 |
+
"""
|
434 |
+
|
435 |
+
def show_booking_info():
|
436 |
+
return """π
GP Booking Options
|
437 |
+
- Online booking
|
438 |
+
- Phone booking
|
439 |
+
- Routine appointments
|
440 |
+
- Urgent appointments
|
441 |
+
"""
|
442 |
+
|
443 |
+
emergency_btn.click(lambda: show_emergency_info(), outputs=[msg])
|
444 |
+
nhs_111_btn.click(lambda: show_nhs_111_info(), outputs=[msg])
|
445 |
+
booking_btn.click(lambda: show_booking_info(), outputs=[msg])
|
|
|
|
|
|
|
|
|
|
|
446 |
|
447 |
return demo
|
448 |
|
|
|
451 |
raise
|
452 |
|
453 |
if __name__ == "__main__":
|
454 |
+
# Initialize environment
|
455 |
+
load_dotenv()
|
456 |
+
|
457 |
+
# Set up Hugging Face login if token exists
|
458 |
+
hf_token = os.getenv("HF_TOKEN")
|
459 |
+
if hf_token:
|
460 |
+
login(token=hf_token)
|
461 |
+
|
462 |
+
# Launch demo
|
463 |
demo = create_demo()
|
464 |
+
demo.launch(share=True)
|
465 |
+
|
466 |
|
|