Update app.py
Browse files
app.py
CHANGED
|
@@ -23,195 +23,152 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 23 |
model.eval()
|
| 24 |
|
| 25 |
print("β
Model loaded successfully!")
|
| 26 |
-
print(f"Device map: {model.hf_device_map}")
|
| 27 |
-
print(f"Model device: {next(model.parameters()).device}")
|
| 28 |
|
| 29 |
|
| 30 |
# =======================================================
|
| 31 |
-
#
|
| 32 |
# =======================================================
|
| 33 |
-
|
| 34 |
-
user_message = history[-1]["content"]
|
| 35 |
-
|
| 36 |
-
if not user_message.strip():
|
| 37 |
-
history.append({"role": "assistant", "content": "β οΈ Please describe your symptoms or ask a question."})
|
| 38 |
-
yield history
|
| 39 |
-
return
|
| 40 |
-
|
| 41 |
-
# π©Ί Refined, Doctor-Like Prompt
|
| 42 |
-
prompt = f"""
|
| 43 |
-
You are Dr. Aiden, a compassionate, calm, and experienced medical doctor.
|
| 44 |
-
You speak naturally, like in a real consultation, providing medical reasoning and empathy.
|
| 45 |
-
You should:
|
| 46 |
-
- Greet the patient kindly and acknowledge their concern.
|
| 47 |
-
- Offer a likely cause in simple medical terms.
|
| 48 |
-
- Suggest possible medicines (with safe dosage and common over-the-counter names).
|
| 49 |
-
- Recommend home remedies, foods, and hydration advice.
|
| 50 |
-
- Share short lifestyle or rest tips to aid recovery.
|
| 51 |
-
- End with reassurance and a disclaimer.
|
| 52 |
-
|
| 53 |
-
Keep your tone friendly yet professional β like an experienced doctor talking directly to the patient.
|
| 54 |
-
Avoid using headings, bullet points, or medical jargon unless necessary.
|
| 55 |
-
Keep your response under 180 words.
|
| 56 |
-
|
| 57 |
-
Patient says: "{user_message}"
|
| 58 |
-
|
| 59 |
-
Dr. Aiden:
|
| 60 |
-
"""
|
| 61 |
-
|
| 62 |
-
# Tokenize input
|
| 63 |
-
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
|
| 64 |
-
|
| 65 |
-
gen_config = GenerationConfig(
|
| 66 |
-
temperature=0.7,
|
| 67 |
-
top_p=0.9,
|
| 68 |
-
do_sample=True,
|
| 69 |
-
max_new_tokens=600,
|
| 70 |
-
pad_token_id=tokenizer.pad_token_id,
|
| 71 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 72 |
-
repetition_penalty=1.15,
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
input_len = inputs["input_ids"].shape[1]
|
| 76 |
-
with torch.no_grad():
|
| 77 |
-
output_ids = model.generate(**inputs, generation_config=gen_config)
|
| 78 |
-
|
| 79 |
-
generated_ids = output_ids[0][input_len:]
|
| 80 |
-
response = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
|
| 81 |
-
|
| 82 |
-
# Clean up the response
|
| 83 |
-
response = clean_medical_response(response)
|
| 84 |
-
|
| 85 |
-
# Stream response (simulated)
|
| 86 |
-
history.append({"role": "assistant", "content": ""})
|
| 87 |
-
for i in range(0, len(response), 5):
|
| 88 |
-
history[-1]["content"] = response[:i + 5] + "β"
|
| 89 |
-
yield history.copy()
|
| 90 |
-
time.sleep(0.01)
|
| 91 |
-
|
| 92 |
-
history[-1]["content"] = response
|
| 93 |
-
yield history
|
| 94 |
|
| 95 |
|
| 96 |
# =======================================================
|
| 97 |
-
#
|
| 98 |
# =======================================================
|
| 99 |
-
def
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
response += "."
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
|
| 117 |
|
| 118 |
# =======================================================
|
| 119 |
-
# Gradio
|
| 120 |
# =======================================================
|
| 121 |
-
with gr.Blocks(theme=gr.themes.Soft()
|
| 122 |
-
.medical-header {
|
| 123 |
-
background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
|
| 124 |
-
padding: 20px;
|
| 125 |
-
border-radius: 12px;
|
| 126 |
-
color: white;
|
| 127 |
-
text-align: center;
|
| 128 |
-
margin-bottom: 20px;
|
| 129 |
-
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
|
| 130 |
-
}
|
| 131 |
-
""") as demo:
|
| 132 |
-
|
| 133 |
gr.HTML("""
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
""")
|
| 139 |
|
| 140 |
chatbot = gr.Chatbot(
|
| 141 |
-
label="
|
|
|
|
| 142 |
type='messages',
|
| 143 |
avatar_images=(
|
| 144 |
-
"https://cdn-icons-png.flaticon.com/512/706/706830.png",
|
| 145 |
-
"https://cdn-icons-png.flaticon.com/512/3774/3774299.png"
|
| 146 |
-
),
|
| 147 |
-
height=550,
|
| 148 |
-
show_copy_button=True
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
with gr.Row():
|
| 152 |
-
user_input = gr.Textbox(
|
| 153 |
-
placeholder="Describe your symptoms or ask a question (e.g., 'I have a fever and sore throat for two days')...",
|
| 154 |
-
label="π§ Describe Your Symptoms",
|
| 155 |
-
lines=3,
|
| 156 |
-
scale=4
|
| 157 |
)
|
| 158 |
-
|
| 159 |
-
with gr.Row():
|
| 160 |
-
send_btn = gr.Button("π¬ Ask Dr. Aiden", variant="primary", size="lg")
|
| 161 |
-
clear_btn = gr.Button("π§Ή New Consultation", size="lg")
|
| 162 |
-
|
| 163 |
-
gr.Markdown("### π‘ Example Questions")
|
| 164 |
-
gr.Examples(
|
| 165 |
-
examples=[
|
| 166 |
-
"I have a fever and headache for two days. What should I take?",
|
| 167 |
-
"I feel tired all day and have trouble sleeping. What could be wrong?",
|
| 168 |
-
"I have mild chest tightness when I exercise. Should I worry?",
|
| 169 |
-
"I'm feeling anxious and stressed. Any natural remedies?",
|
| 170 |
-
"I have stomach pain after eating. What can I do?",
|
| 171 |
-
"I caught a cold and sore throat. What treatment do you recommend?",
|
| 172 |
-
],
|
| 173 |
-
inputs=user_input,
|
| 174 |
)
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
| 179 |
def respond(message, history):
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
history.append({"role": "assistant", "content": updated_history[-1]["content"]})
|
| 193 |
-
else:
|
| 194 |
-
history[-1]["content"] = updated_history[-1]["content"]
|
| 195 |
-
yield "", history
|
| 196 |
-
|
| 197 |
-
# =======================================================
|
| 198 |
-
# Button Bindings
|
| 199 |
-
# =======================================================
|
| 200 |
send_btn.click(respond, [user_input, chatbot], [user_input, chatbot])
|
| 201 |
user_input.submit(respond, [user_input, chatbot], [user_input, chatbot])
|
| 202 |
-
clear_btn.click(
|
| 203 |
|
| 204 |
|
| 205 |
# =======================================================
|
| 206 |
-
# Launch
|
| 207 |
# =======================================================
|
| 208 |
if __name__ == "__main__":
|
| 209 |
-
print("
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
demo.queue(max_size=20)
|
| 213 |
-
demo.launch(
|
| 214 |
-
share=True,
|
| 215 |
-
show_error=True,
|
| 216 |
-
server_name="0.0.0.0"
|
| 217 |
-
)
|
|
|
|
| 23 |
model.eval()
|
| 24 |
|
| 25 |
print("β
Model loaded successfully!")
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
# =======================================================
|
| 29 |
+
# Global Memory for Doctor Flow
|
| 30 |
# =======================================================
|
| 31 |
+
session = {"name": None, "age": None, "gender": None, "stage": "intro"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
# =======================================================
|
| 35 |
+
# Generate Doctor Response
|
| 36 |
# =======================================================
|
| 37 |
+
def doctor_response(user_message):
|
| 38 |
+
global session
|
| 39 |
+
user_message = user_message.strip()
|
| 40 |
+
|
| 41 |
+
# Step 1: Greeting
|
| 42 |
+
if session["stage"] == "intro":
|
| 43 |
+
session["stage"] = "ask_name"
|
| 44 |
+
return "π¨ββοΈ Hello! Iβm Dr. Aiden. May I know your name, please?"
|
| 45 |
+
|
| 46 |
+
# Step 2: Get Name
|
| 47 |
+
elif session["stage"] == "ask_name":
|
| 48 |
+
session["name"] = user_message.split()[0].capitalize()
|
| 49 |
+
session["stage"] = "ask_age"
|
| 50 |
+
return f"Nice to meet you, {session['name']}! How old are you?"
|
| 51 |
+
|
| 52 |
+
# Step 3: Get Age
|
| 53 |
+
elif session["stage"] == "ask_age":
|
| 54 |
+
words = user_message.split()
|
| 55 |
+
for w in words:
|
| 56 |
+
if w.isdigit():
|
| 57 |
+
session["age"] = int(w)
|
| 58 |
+
session["stage"] = "ask_gender"
|
| 59 |
+
return f"Got it, {session['name']}. Are you male or female?"
|
| 60 |
+
return "Please tell me your age in numbers, like 25 or 30."
|
| 61 |
+
|
| 62 |
+
# Step 4: Get Gender
|
| 63 |
+
elif session["stage"] == "ask_gender":
|
| 64 |
+
if "male" in user_message.lower():
|
| 65 |
+
session["gender"] = "male"
|
| 66 |
+
elif "female" in user_message.lower():
|
| 67 |
+
session["gender"] = "female"
|
| 68 |
+
else:
|
| 69 |
+
return "Could you please specify whether you are male or female?"
|
| 70 |
+
|
| 71 |
+
session["stage"] = "consult"
|
| 72 |
+
return f"Thanks, {session['name']}! So you're a {session['age']}-year-old {session['gender']}. What brings you in today?"
|
| 73 |
+
|
| 74 |
+
# Step 5: Medical Consultation Mode
|
| 75 |
+
elif session["stage"] == "consult":
|
| 76 |
+
name = session["name"]
|
| 77 |
+
age = session["age"]
|
| 78 |
+
gender = session["gender"]
|
| 79 |
+
|
| 80 |
+
prompt = f"""
|
| 81 |
+
You are Dr. Aiden β a warm, professional, and conversational doctor talking naturally with a patient.
|
| 82 |
+
|
| 83 |
+
Patient Info:
|
| 84 |
+
- Name: {name}
|
| 85 |
+
- Age: {age}
|
| 86 |
+
- Gender: {gender}
|
| 87 |
+
|
| 88 |
+
Speak in a caring and natural tone (like a friendly doctor in a private clinic).
|
| 89 |
+
|
| 90 |
+
Include in your response:
|
| 91 |
+
1. Acknowledgement of their symptoms
|
| 92 |
+
2. Possible causes (simple explanation)
|
| 93 |
+
3. Simple medicines with dosage (if applicable)
|
| 94 |
+
4. Food, rest, and hydration advice
|
| 95 |
+
5. When to see a real doctor
|
| 96 |
+
6. Short closing reassurance
|
| 97 |
+
|
| 98 |
+
Patient: {user_message}
|
| 99 |
+
Doctor:"""
|
| 100 |
+
|
| 101 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
|
| 102 |
+
gen_cfg = GenerationConfig(
|
| 103 |
+
temperature=0.7,
|
| 104 |
+
top_p=0.9,
|
| 105 |
+
max_new_tokens=450,
|
| 106 |
+
repetition_penalty=1.15,
|
| 107 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 108 |
+
eos_token_id=tokenizer.eos_token_id
|
| 109 |
+
)
|
| 110 |
|
| 111 |
+
with torch.no_grad():
|
| 112 |
+
output = model.generate(**inputs, generation_config=gen_cfg)
|
| 113 |
|
| 114 |
+
output_text = tokenizer.decode(output[0], skip_special_tokens=True).strip()
|
| 115 |
+
output_text = output_text.replace("Doctor:", "").replace("Patient:", "").strip()
|
|
|
|
| 116 |
|
| 117 |
+
# Final cleanup
|
| 118 |
+
if not output_text.endswith((".", "!", "?")):
|
| 119 |
+
output_text += "."
|
| 120 |
+
output_text += "\n\nβοΈ *Note: This advice is AI-generated and not a substitute for professional medical care.*"
|
| 121 |
|
| 122 |
+
return output_text
|
| 123 |
|
| 124 |
|
| 125 |
# =======================================================
|
| 126 |
+
# Gradio Interface
|
| 127 |
# =======================================================
|
| 128 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
gr.HTML("""
|
| 130 |
+
<div style="text-align:center; background-color:#4C7DFF; color:white; padding:20px; border-radius:10px;">
|
| 131 |
+
<h1>π Your Consultation with Dr. Aiden</h1>
|
| 132 |
+
<p>Empathetic β’ Knowledgeable β’ Natural β Your AI Medical Advisor</p>
|
| 133 |
+
</div>
|
| 134 |
""")
|
| 135 |
|
| 136 |
chatbot = gr.Chatbot(
|
| 137 |
+
label="π¨ββοΈ Chat with Dr. Aiden",
|
| 138 |
+
height=550,
|
| 139 |
type='messages',
|
| 140 |
avatar_images=(
|
| 141 |
+
"https://cdn-icons-png.flaticon.com/512/706/706830.png",
|
| 142 |
+
"https://cdn-icons-png.flaticon.com/512/3774/3774299.png"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
+
user_input = gr.Textbox(placeholder="Say 'Hi Doctor' to start your consultation...", label="Your Message", lines=2)
|
| 147 |
+
send_btn = gr.Button("π¬ Send", variant="primary")
|
| 148 |
+
clear_btn = gr.Button("π§Ή New Consultation")
|
| 149 |
+
|
| 150 |
def respond(message, history):
|
| 151 |
+
if history is None:
|
| 152 |
+
history = []
|
| 153 |
+
response = doctor_response(message)
|
| 154 |
+
history.append({"role": "user", "content": message})
|
| 155 |
+
history.append({"role": "assistant", "content": response})
|
| 156 |
+
return "", history
|
| 157 |
+
|
| 158 |
+
def reset():
|
| 159 |
+
global session
|
| 160 |
+
session = {"name": None, "age": None, "gender": None, "stage": "intro"}
|
| 161 |
+
return []
|
| 162 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
send_btn.click(respond, [user_input, chatbot], [user_input, chatbot])
|
| 164 |
user_input.submit(respond, [user_input, chatbot], [user_input, chatbot])
|
| 165 |
+
clear_btn.click(reset, None, chatbot, queue=False)
|
| 166 |
|
| 167 |
|
| 168 |
# =======================================================
|
| 169 |
+
# Launch
|
| 170 |
# =======================================================
|
| 171 |
if __name__ == "__main__":
|
| 172 |
+
print("π₯ Launching Dr. Aiden...")
|
| 173 |
+
demo.queue()
|
| 174 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|