Muhammadidrees commited on
Commit
1fe24dd
Β·
verified Β·
1 Parent(s): 20c94fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -124
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 - Interactive Medical Consultation
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": "How can I help you today?"})
38
  yield history
39
  return
40
 
41
- # Build conversation context from history
42
- conversation_context = ""
43
- if len(history) > 1:
44
- # Include previous exchanges for context
45
- for msg in history[:-1]:
46
- if msg["role"] == "user":
47
- conversation_context += f"PATIENT: {msg['content']}\n"
48
- else:
49
- conversation_context += f"DOCTOR: {msg['content']}\n"
50
-
51
- # Medical conversation prompt - like real doctor-patient interaction
52
- prompt = f"""You are an experienced medical doctor conducting a patient consultation. Have a natural, interactive conversation where you:
53
 
54
- - Ask relevant follow-up questions to understand symptoms better
55
- - Gather medical history (medications, lifestyle, family history)
56
- - Provide medical assessment and recommendations
57
- - Suggest medications with dosages when appropriate
58
- - Give diet and lifestyle advice
59
- - Explain what tests or next steps are needed
60
 
61
- Respond naturally as a caring doctor would. Keep responses concise (2-4 sentences). Ask ONE specific follow-up question when you need more information.
62
 
63
- Previous conversation:
64
- {conversation_context}
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.75,
73
- top_p=0.92,
74
- top_k=45,
75
  do_sample=True,
76
- max_new_tokens=250,
77
  pad_token_id=tokenizer.pad_token_id,
78
  eos_token_id=tokenizer.eos_token_id,
79
- repetition_penalty=1.2,
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 = clean_doctor_response(response)
93
 
94
- # Stream response token by token
95
  history.append({"role": "assistant", "content": ""})
96
- for i in range(0, len(response), 4):
97
- chunk = response[:i + 4]
98
- history[-1]["content"] = chunk + "β–Œ"
99
  yield history.copy()
100
- time.sleep(0.012)
101
 
102
  history[-1]["content"] = response
103
  yield history
104
 
105
 
106
- def clean_doctor_response(response: str) -> str:
107
- """Clean the doctor's response to be natural and conversational."""
108
-
109
- # Remove role labels if present
110
- prefixes_to_remove = ["doctor:", "assistant:", "response:", "patient:"]
111
- response_lower = response.lower()
112
- for prefix in prefixes_to_remove:
113
- if response_lower.startswith(prefix):
114
- response = response[len(prefix):].strip()
115
- break
116
-
117
- # Stop at repetitive patterns or gibberish
118
- stop_phrases = ["accordingly", "respectively", "speaking correctly", "faithfully yours"]
119
- for phrase in stop_phrases:
120
- if phrase in response.lower():
121
- # Find first occurrence and cut there
122
- idx = response.lower().find(phrase)
123
- response = response[:idx].strip()
124
- break
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 Interface
151
  # =======================================================
152
  with gr.Blocks(theme=gr.themes.Soft(), css="""
153
  .medical-header {
154
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
155
  padding: 20px;
156
- border-radius: 10px;
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 Doctor Consultation</h1>
166
- <p>Interactive Medical Conversation β€’ Just Like Visiting Your Doctor</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="πŸ’¬ Doctor-Patient Consultation",
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=500,
186
  show_copy_button=True
187
  )
188
 
189
  with gr.Row():
190
  user_input = gr.Textbox(
191
- placeholder="Describe your symptoms or answer the doctor's questions...",
192
- label="🧍 Patient (You)",
193
- lines=2,
194
  scale=4
195
  )
196
 
197
  with gr.Row():
198
- send_btn = gr.Button("πŸ’¬ Send", variant="primary", scale=1)
199
- clear_btn = gr.Button("πŸ”„ New Consultation", scale=1)
200
 
201
- gr.Markdown("### πŸ’‘ Example Conversations")
202
  gr.Examples(
203
  examples=[
204
- "Hi Doctor, I've been having fever and body aches for 2 days",
205
- "I have numbness in my toes and difficulty walking",
206
- "I've been feeling very tired all the time lately",
207
- "I have chest pain and shortness of breath",
208
- "I get headaches almost every day",
209
- "I have stomach pain after eating"
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 with Context Memory
223
  # =======================================================
224
  def respond(message, history):
225
  user_message = message.strip()
226
  if not user_message:
227
  return "", history
228
 
229
- # Add user message to history
230
  history.append({"role": "user", "content": user_message})
231
 
232
- # Generate response with full conversation context
233
- for updated_history in generate_doctor_response(history):
234
- # Update the last assistant message
235
- if history[-1]["role"] == "assistant":
 
 
 
236
  history[-1]["content"] = updated_history[-1]["content"]
237
  yield "", history
238
 
239
  # =======================================================
240
- # Button & Input Bindings
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 Consultation System Starting...")
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
+ )