Update app.py
Browse files
app.py
CHANGED
|
@@ -22,222 +22,180 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 22 |
)
|
| 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 |
-
# Generate Doctor Response
|
| 32 |
# =======================================================
|
| 33 |
def generate_doctor_response(history):
|
| 34 |
user_message = history[-1]["content"]
|
| 35 |
|
| 36 |
if not user_message.strip():
|
| 37 |
-
history.append({"role": "assistant", "content": "
|
| 38 |
yield history
|
| 39 |
return
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
prompt = f"""You are an experienced medical doctor conducting a patient consultation. Have a natural, interactive conversation where you:
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
- Suggest medications with dosages when appropriate
|
| 58 |
-
- Give diet and lifestyle advice
|
| 59 |
-
- Explain what tests or next steps are needed
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
PATIENT: {user_message}
|
| 66 |
-
DOCTOR:"""
|
| 67 |
|
| 68 |
# Tokenize input
|
| 69 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
|
| 70 |
|
| 71 |
gen_config = GenerationConfig(
|
| 72 |
-
temperature=0.
|
| 73 |
-
top_p=0.
|
| 74 |
-
top_k=45,
|
| 75 |
do_sample=True,
|
| 76 |
-
max_new_tokens=
|
| 77 |
pad_token_id=tokenizer.pad_token_id,
|
| 78 |
eos_token_id=tokenizer.eos_token_id,
|
| 79 |
-
repetition_penalty=1.
|
| 80 |
-
no_repeat_ngram_size=3
|
| 81 |
)
|
| 82 |
|
| 83 |
input_len = inputs["input_ids"].shape[1]
|
| 84 |
-
|
| 85 |
with torch.no_grad():
|
| 86 |
output_ids = model.generate(**inputs, generation_config=gen_config)
|
| 87 |
|
| 88 |
generated_ids = output_ids[0][input_len:]
|
| 89 |
response = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
|
| 90 |
|
| 91 |
-
# Clean response
|
| 92 |
-
response =
|
| 93 |
|
| 94 |
-
# Stream response
|
| 95 |
history.append({"role": "assistant", "content": ""})
|
| 96 |
-
for i in range(0, len(response),
|
| 97 |
-
|
| 98 |
-
history[-1]["content"] = chunk + "β"
|
| 99 |
yield history.copy()
|
| 100 |
-
time.sleep(0.
|
| 101 |
|
| 102 |
history[-1]["content"] = response
|
| 103 |
yield history
|
| 104 |
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
# Limit to reasonable number of sentences (4-6 max)
|
| 127 |
-
sentences = [s.strip() + '.' for s in response.split('.') if s.strip()]
|
| 128 |
-
if len(sentences) > 6:
|
| 129 |
-
response = ' '.join(sentences[:6])
|
| 130 |
-
else:
|
| 131 |
-
response = ' '.join(sentences)
|
| 132 |
-
|
| 133 |
-
# Remove incomplete sentences at the end
|
| 134 |
-
if response and response[-1] not in '.!?':
|
| 135 |
-
last_period = response.rfind('.')
|
| 136 |
-
if last_period > 0:
|
| 137 |
-
response = response[:last_period + 1]
|
| 138 |
-
|
| 139 |
-
# Clean up extra spaces
|
| 140 |
-
response = ' '.join(response.split())
|
| 141 |
-
|
| 142 |
-
# Fallback for very short or empty responses
|
| 143 |
-
if len(response.strip()) < 20:
|
| 144 |
-
response = "Could you tell me more about your symptoms? When did they start?"
|
| 145 |
-
|
| 146 |
return response.strip()
|
| 147 |
|
| 148 |
|
| 149 |
# =======================================================
|
| 150 |
-
# Gradio
|
| 151 |
# =======================================================
|
| 152 |
with gr.Blocks(theme=gr.themes.Soft(), css="""
|
| 153 |
.medical-header {
|
| 154 |
-
background: linear-gradient(135deg, #
|
| 155 |
padding: 20px;
|
| 156 |
-
border-radius:
|
| 157 |
color: white;
|
| 158 |
text-align: center;
|
| 159 |
margin-bottom: 20px;
|
|
|
|
| 160 |
}
|
| 161 |
""") as demo:
|
| 162 |
|
| 163 |
gr.HTML("""
|
| 164 |
<div class="medical-header">
|
| 165 |
-
<h1>π₯ AI
|
| 166 |
-
<p>
|
| 167 |
</div>
|
| 168 |
""")
|
| 169 |
|
| 170 |
-
gr.Markdown("""
|
| 171 |
-
### π¬ How This Works:
|
| 172 |
-
- Describe your symptoms or health concerns
|
| 173 |
-
- The AI doctor will ask questions to understand your condition
|
| 174 |
-
- You'll get medical advice, medication suggestions, and lifestyle recommendations
|
| 175 |
-
- Have a natural back-and-forth conversation just like a real doctor visit
|
| 176 |
-
""")
|
| 177 |
-
|
| 178 |
chatbot = gr.Chatbot(
|
| 179 |
-
label="π¬
|
| 180 |
type='messages',
|
| 181 |
avatar_images=(
|
| 182 |
"https://cdn-icons-png.flaticon.com/512/706/706830.png", # Patient
|
| 183 |
"https://cdn-icons-png.flaticon.com/512/3774/3774299.png" # Doctor
|
| 184 |
),
|
| 185 |
-
height=
|
| 186 |
show_copy_button=True
|
| 187 |
)
|
| 188 |
|
| 189 |
with gr.Row():
|
| 190 |
user_input = gr.Textbox(
|
| 191 |
-
placeholder="Describe your symptoms or
|
| 192 |
-
label="π§
|
| 193 |
-
lines=
|
| 194 |
scale=4
|
| 195 |
)
|
| 196 |
|
| 197 |
with gr.Row():
|
| 198 |
-
send_btn = gr.Button("π¬
|
| 199 |
-
clear_btn = gr.Button("
|
| 200 |
|
| 201 |
-
gr.Markdown("### π‘ Example
|
| 202 |
gr.Examples(
|
| 203 |
examples=[
|
| 204 |
-
"
|
| 205 |
-
"I have
|
| 206 |
-
"I
|
| 207 |
-
"I
|
| 208 |
-
"I
|
| 209 |
-
"I
|
| 210 |
],
|
| 211 |
inputs=user_input,
|
| 212 |
)
|
| 213 |
|
| 214 |
-
gr.Markdown("""
|
| 215 |
-
---
|
| 216 |
-
β οΈ **Medical Disclaimer:** This AI provides general medical information for educational purposes.
|
| 217 |
-
It is NOT a substitute for professional medical advice. Always consult a qualified healthcare
|
| 218 |
-
provider for diagnosis and treatment. In case of emergency, call emergency services immediately.
|
| 219 |
-
""")
|
| 220 |
-
|
| 221 |
# =======================================================
|
| 222 |
-
# Respond Function
|
| 223 |
# =======================================================
|
| 224 |
def respond(message, history):
|
| 225 |
user_message = message.strip()
|
| 226 |
if not user_message:
|
| 227 |
return "", history
|
| 228 |
|
| 229 |
-
#
|
| 230 |
history.append({"role": "user", "content": user_message})
|
| 231 |
|
| 232 |
-
#
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
| 236 |
history[-1]["content"] = updated_history[-1]["content"]
|
| 237 |
yield "", history
|
| 238 |
|
| 239 |
# =======================================================
|
| 240 |
-
# Button
|
| 241 |
# =======================================================
|
| 242 |
send_btn.click(respond, [user_input, chatbot], [user_input, chatbot])
|
| 243 |
user_input.submit(respond, [user_input, chatbot], [user_input, chatbot])
|
|
@@ -249,12 +207,11 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
|
|
| 249 |
# =======================================================
|
| 250 |
if __name__ == "__main__":
|
| 251 |
print("="*60)
|
| 252 |
-
print("π₯ AI Doctor
|
| 253 |
-
print(" Interactive medical conversation with context memory")
|
| 254 |
print("="*60)
|
| 255 |
demo.queue(max_size=20)
|
| 256 |
demo.launch(
|
| 257 |
share=True,
|
| 258 |
show_error=True,
|
| 259 |
server_name="0.0.0.0"
|
| 260 |
-
)
|
|
|
|
| 22 |
)
|
| 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 |
+
# Generate Doctor Response (Refined for natural tone)
|
| 32 |
# =======================================================
|
| 33 |
def generate_doctor_response(history):
|
| 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 |
+
# Clean the response
|
| 98 |
+
# =======================================================
|
| 99 |
+
def clean_medical_response(response: str) -> str:
|
| 100 |
+
remove_prefixes = ["assistant:", "doctor:", "dr. aiden:", "response:", "patient:"]
|
| 101 |
+
for p in remove_prefixes:
|
| 102 |
+
if response.lower().startswith(p):
|
| 103 |
+
response = response[len(p):].strip()
|
| 104 |
+
|
| 105 |
+
response = response.replace("Dr. Aiden:", "").strip()
|
| 106 |
+
|
| 107 |
+
# Ensure punctuation
|
| 108 |
+
if response and response[-1] not in ".!?":
|
| 109 |
+
response += "."
|
| 110 |
+
|
| 111 |
+
# Add disclaimer if missing
|
| 112 |
+
if "βοΈ" not in response and "consult" not in response.lower():
|
| 113 |
+
response += "\n\nβοΈ *Please note: This is AI-generated medical guidance, not a substitute for a licensed healthcare provider. Always consult a doctor for personal medical care.*"
|
| 114 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
return response.strip()
|
| 116 |
|
| 117 |
|
| 118 |
# =======================================================
|
| 119 |
+
# Gradio UI
|
| 120 |
# =======================================================
|
| 121 |
with gr.Blocks(theme=gr.themes.Soft(), css="""
|
| 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 |
<div class="medical-header">
|
| 135 |
+
<h1>π₯ Dr. Aiden β AI Medical Consultation</h1>
|
| 136 |
+
<p>Friendly β’ Professional β’ Science-Backed Guidance</p>
|
| 137 |
</div>
|
| 138 |
""")
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
chatbot = gr.Chatbot(
|
| 141 |
+
label="π¬ Your Consultation with Dr. Aiden",
|
| 142 |
type='messages',
|
| 143 |
avatar_images=(
|
| 144 |
"https://cdn-icons-png.flaticon.com/512/706/706830.png", # Patient
|
| 145 |
"https://cdn-icons-png.flaticon.com/512/3774/3774299.png" # Doctor
|
| 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 |
+
# Respond Function (stateless model, persistent chat)
|
| 178 |
# =======================================================
|
| 179 |
def respond(message, history):
|
| 180 |
user_message = message.strip()
|
| 181 |
if not user_message:
|
| 182 |
return "", history
|
| 183 |
|
| 184 |
+
# Show user input
|
| 185 |
history.append({"role": "user", "content": user_message})
|
| 186 |
|
| 187 |
+
# Model sees only current input (no memory)
|
| 188 |
+
temp_history = [{"role": "user", "content": user_message}]
|
| 189 |
+
|
| 190 |
+
for updated_history in generate_doctor_response(temp_history):
|
| 191 |
+
if len(history) == 0 or history[-1]["role"] != "assistant":
|
| 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])
|
|
|
|
| 207 |
# =======================================================
|
| 208 |
if __name__ == "__main__":
|
| 209 |
print("="*60)
|
| 210 |
+
print("π₯ Dr. Aiden β AI Medical Doctor is starting...")
|
|
|
|
| 211 |
print("="*60)
|
| 212 |
demo.queue(max_size=20)
|
| 213 |
demo.launch(
|
| 214 |
share=True,
|
| 215 |
show_error=True,
|
| 216 |
server_name="0.0.0.0"
|
| 217 |
+
)
|