Spaces:
Running
on
Zero
Running
on
Zero
Thanush
commited on
Commit
·
6daaaf3
1
Parent(s):
736646e
Enhance logging in handlers and update consultation prompt for clarity and empathy
Browse files- medbot/handlers.py +17 -4
- medbot/interface.py +1 -1
- medbot/prompts.py +21 -12
medbot/handlers.py
CHANGED
@@ -1,8 +1,12 @@
|
|
1 |
import spaces
|
|
|
2 |
from .model import ModelManager
|
3 |
from .memory import MedicalMemoryManager
|
4 |
from .prompts import CONSULTATION_PROMPT, MEDICINE_PROMPT
|
5 |
|
|
|
|
|
|
|
6 |
model_manager = ModelManager()
|
7 |
memory_manager = MedicalMemoryManager()
|
8 |
conversation_turns = 0
|
@@ -22,13 +26,17 @@ def build_me_llama_prompt(system_prompt, history, user_input):
|
|
22 |
def respond(message, chat_history):
|
23 |
global conversation_turns
|
24 |
conversation_turns += 1
|
25 |
-
|
|
|
|
|
26 |
prompt = build_me_llama_prompt(CONSULTATION_PROMPT, chat_history, message)
|
27 |
-
response = model_manager.generate(prompt)
|
|
|
28 |
memory_manager.add_interaction(message, response)
|
29 |
chat_history.append((message, response))
|
30 |
return "", chat_history
|
31 |
else:
|
|
|
32 |
patient_summary = memory_manager.get_patient_summary()
|
33 |
memory_context = memory_manager.get_memory_context()
|
34 |
summary_prompt = build_me_llama_prompt(
|
@@ -36,10 +44,14 @@ def respond(message, chat_history):
|
|
36 |
chat_history,
|
37 |
message
|
38 |
)
|
39 |
-
summary
|
|
|
|
|
40 |
full_patient_info = f"Patient Summary: {patient_summary}\n\nDetailed Summary: {summary}"
|
41 |
med_prompt = f"<s>[INST] {MEDICINE_PROMPT.format(patient_info=full_patient_info, memory_context=memory_context)} [/INST] "
|
42 |
-
|
|
|
|
|
43 |
final_response = (
|
44 |
f"**COMPREHENSIVE MEDICAL SUMMARY:**\n{summary}\n\n"
|
45 |
f"**MEDICATION AND HOME CARE SUGGESTIONS:**\n{medicine_suggestions}\n\n"
|
@@ -55,4 +67,5 @@ def reset_chat():
|
|
55 |
conversation_turns = 0
|
56 |
memory_manager.reset_session()
|
57 |
reset_msg = "New consultation started. Please tell me about your symptoms or health concerns."
|
|
|
58 |
return [(None, reset_msg)], ""
|
|
|
1 |
import spaces
|
2 |
+
import logging
|
3 |
from .model import ModelManager
|
4 |
from .memory import MedicalMemoryManager
|
5 |
from .prompts import CONSULTATION_PROMPT, MEDICINE_PROMPT
|
6 |
|
7 |
+
# Configure logging
|
8 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
|
9 |
+
|
10 |
model_manager = ModelManager()
|
11 |
memory_manager = MedicalMemoryManager()
|
12 |
conversation_turns = 0
|
|
|
26 |
def respond(message, chat_history):
|
27 |
global conversation_turns
|
28 |
conversation_turns += 1
|
29 |
+
logging.info(f"User input: {message}")
|
30 |
+
if conversation_turns < 4:
|
31 |
+
logging.info("Using CONSULTATION_PROMPT for information gathering.")
|
32 |
prompt = build_me_llama_prompt(CONSULTATION_PROMPT, chat_history, message)
|
33 |
+
response = model_manager.generate(prompt, max_new_tokens=128)
|
34 |
+
logging.info(f"Model response: {response}")
|
35 |
memory_manager.add_interaction(message, response)
|
36 |
chat_history.append((message, response))
|
37 |
return "", chat_history
|
38 |
else:
|
39 |
+
logging.info("Using CONSULTATION_PROMPT for summary and MEDICINE_PROMPT for suggestions.")
|
40 |
patient_summary = memory_manager.get_patient_summary()
|
41 |
memory_context = memory_manager.get_memory_context()
|
42 |
summary_prompt = build_me_llama_prompt(
|
|
|
44 |
chat_history,
|
45 |
message
|
46 |
)
|
47 |
+
logging.info("Generating summary with CONSULTATION_PROMPT.")
|
48 |
+
summary = model_manager.generate(summary_prompt, max_new_tokens=400)
|
49 |
+
logging.info(f"Summary response: {summary}")
|
50 |
full_patient_info = f"Patient Summary: {patient_summary}\n\nDetailed Summary: {summary}"
|
51 |
med_prompt = f"<s>[INST] {MEDICINE_PROMPT.format(patient_info=full_patient_info, memory_context=memory_context)} [/INST] "
|
52 |
+
logging.info("Generating medicine suggestions with MEDICINE_PROMPT.")
|
53 |
+
medicine_suggestions = model_manager.generate(med_prompt, max_new_tokens=200)
|
54 |
+
logging.info(f"Medicine suggestions: {medicine_suggestions}")
|
55 |
final_response = (
|
56 |
f"**COMPREHENSIVE MEDICAL SUMMARY:**\n{summary}\n\n"
|
57 |
f"**MEDICATION AND HOME CARE SUGGESTIONS:**\n{medicine_suggestions}\n\n"
|
|
|
67 |
conversation_turns = 0
|
68 |
memory_manager.reset_session()
|
69 |
reset_msg = "New consultation started. Please tell me about your symptoms or health concerns."
|
70 |
+
logging.info("Session reset. New consultation started.")
|
71 |
return [(None, reset_msg)], ""
|
medbot/interface.py
CHANGED
@@ -4,7 +4,7 @@ from .handlers import respond, reset_chat
|
|
4 |
def build_interface():
|
5 |
with gr.Blocks(theme="soft") as demo:
|
6 |
gr.Markdown("# 🏥 Dr Bavana's Medical Assistant ")
|
7 |
-
gr.Markdown("By
|
8 |
with gr.Row():
|
9 |
with gr.Column(scale=4):
|
10 |
chatbot = gr.Chatbot(height=500)
|
|
|
4 |
def build_interface():
|
5 |
with gr.Blocks(theme="soft") as demo:
|
6 |
gr.Markdown("# 🏥 Dr Bavana's Medical Assistant ")
|
7 |
+
gr.Markdown("By Blue Planet Infosolutions Pvt Ltd")
|
8 |
with gr.Row():
|
9 |
with gr.Column(scale=4):
|
10 |
chatbot = gr.Chatbot(height=500)
|
medbot/prompts.py
CHANGED
@@ -131,18 +131,27 @@
|
|
131 |
|
132 |
|
133 |
# Simple
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
-
|
139 |
-
-
|
140 |
-
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
GUIDANCE:
|
148 |
1. OTC options: General categories only (e.g., "pain relievers"). Follow package directions.
|
|
|
131 |
|
132 |
|
133 |
# Simple
|
134 |
+
CONSULTATION_PROMPT = '''You are a professional virtual doctor. Your goal is to collect detailed information about the user's health condition, symptoms, medical history, medications, lifestyle, and other relevant data.
|
135 |
+
**IMPORTANT** Ask for name and age first with a greeting .
|
136 |
+
**IMPORTANT** Be empathetic thorough, and context-aware.
|
137 |
+
|
138 |
+
Ask 1-2 follow-up questions at a time to gather more details about:
|
139 |
+
- Name and age
|
140 |
+
- Detailed description of symptoms
|
141 |
+
- Duration (when did it start?)
|
142 |
+
- Severity (scale of 1-10)
|
143 |
+
- Aggravating or alleviating factors
|
144 |
+
- Related symptoms
|
145 |
+
- Medical history
|
146 |
+
- Current medications and allergies
|
147 |
+
- Always refer to the previous conversation and avoid repeating questions.
|
148 |
+
- Ask only for information that has not yet been provided.
|
149 |
+
- If the user has already answered a question, do not ask it again.
|
150 |
+
After collecting sufficient information (5-6 exchanges), summarize findings and suggest when they should seek professional care. Do NOT make specific diagnoses or recommend specific treatments.
|
151 |
+
Respond empathetically and clearly. Always be professional and thorough.'''
|
152 |
+
|
153 |
+
MEDICINE_PROMPT = '''You are an experienced medical doctor with 15+ years of clinical practice. Analyze the patient's condition thoroughly and provide a comprehensive medical assessment as you would in a real consultation. Follow strict safety protocols
|
154 |
+
Based on: {patient_info}
|
155 |
|
156 |
GUIDANCE:
|
157 |
1. OTC options: General categories only (e.g., "pain relievers"). Follow package directions.
|