Muhammadidrees commited on
Commit
175aebd
Β·
verified Β·
1 Parent(s): 9d63291

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -28
app.py CHANGED
@@ -47,20 +47,20 @@ def extract_name(text):
47
  # =======================================================
48
  def doctor_response(user_message):
49
  global session
50
- user_message = user_message.strip()
51
 
52
- # Intro
53
  if session["stage"] == "intro":
54
  session["stage"] = "ask_name"
55
  return "πŸ‘¨β€βš•οΈ Hello! I’m Dr. Aiden. May I know your name, please?"
56
 
57
- # Ask Name
58
  elif session["stage"] == "ask_name":
59
  session["name"] = extract_name(user_message)
60
  session["stage"] = "ask_age"
61
  return f"Nice to meet you, {session['name']}! How old are you?"
62
 
63
- # Ask Age
64
  elif session["stage"] == "ask_age":
65
  words = user_message.split()
66
  for w in words:
@@ -70,54 +70,102 @@ def doctor_response(user_message):
70
  return f"Got it, {session['name']}. Are you male or female?"
71
  return "Please tell me your age in numbers, like 20 or 25."
72
 
73
- # Ask Gender
74
  elif session["stage"] == "ask_gender":
75
- if "male" in user_message.lower():
76
  session["gender"] = "male"
77
- elif "female" in user_message.lower():
78
  session["gender"] = "female"
79
  else:
80
  return "Could you please specify whether you are male or female?"
81
  session["stage"] = "ask_symptoms"
82
  return f"Thanks, {session['name']}! So you're a {session['age']}-year-old {session['gender']}. What symptoms are you experiencing?"
83
 
84
- # Ask Symptoms
85
  elif session["stage"] == "ask_symptoms":
86
  session["symptoms"] = user_message
87
  session["stage"] = "ask_duration"
88
  return "Since when have you been feeling this way?"
89
 
90
- # Ask Duration
91
  elif session["stage"] == "ask_duration":
92
  session["duration"] = user_message
93
- session["stage"] = "consult"
94
  return "Got it. Are you taking any medications or treatments currently?"
95
 
96
- # Consultation
 
 
 
 
 
 
97
  elif session["stage"] == "consult":
98
  name = session["name"]
99
  age = session["age"]
100
  gender = session["gender"]
101
- symptoms = session["symptoms"]
102
- duration = session["duration"]
103
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  prompt = f"""
105
- You are Dr. Aiden β€” a warm, friendly, and professional doctor having an interview-style consultation.
106
- The patient is a {age}-year-old {gender} named {name}.
107
- They have been feeling {symptoms} for {duration}.
108
- They said: "{user_message}"
109
 
110
- Respond like a real doctor β€” show empathy, analyze the symptoms, suggest likely causes, give simple medication and home care advice.
 
 
 
111
 
112
- Include:
113
- 1. Acknowledge their discomfort.
114
- 2. Explain possible causes in simple terms.
115
- 3. Recommend over-the-counter medicines (if safe).
116
- 4. Suggest food, hydration, and rest tips.
117
- 5. Warn when to visit a real doctor.
118
- 6. End with gentle reassurance.
119
 
120
- Doctor:"""
 
 
 
 
 
 
 
 
121
 
122
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
123
  gen_cfg = GenerationConfig(
@@ -134,13 +182,15 @@ Doctor:"""
134
 
135
  output_text = tokenizer.decode(output[0], skip_special_tokens=True)
136
  output_text = output_text.split("Doctor:")[-1].strip()
 
137
  if not output_text.endswith((".", "!", "?")):
138
  output_text += "."
139
- output_text
140
 
 
141
  return output_text
142
 
143
 
 
144
  # =======================================================
145
  # Gradio Interface
146
  # =======================================================
 
47
  # =======================================================
48
  def doctor_response(user_message):
49
  global session
50
+ user_message = user_message.strip().lower()
51
 
52
+ # Step 1: Greeting
53
  if session["stage"] == "intro":
54
  session["stage"] = "ask_name"
55
  return "πŸ‘¨β€βš•οΈ Hello! I’m Dr. Aiden. May I know your name, please?"
56
 
57
+ # Step 2: Get Name
58
  elif session["stage"] == "ask_name":
59
  session["name"] = extract_name(user_message)
60
  session["stage"] = "ask_age"
61
  return f"Nice to meet you, {session['name']}! How old are you?"
62
 
63
+ # Step 3: Get Age
64
  elif session["stage"] == "ask_age":
65
  words = user_message.split()
66
  for w in words:
 
70
  return f"Got it, {session['name']}. Are you male or female?"
71
  return "Please tell me your age in numbers, like 20 or 25."
72
 
73
+ # Step 4: Get Gender
74
  elif session["stage"] == "ask_gender":
75
+ if "male" in user_message:
76
  session["gender"] = "male"
77
+ elif "female" in user_message:
78
  session["gender"] = "female"
79
  else:
80
  return "Could you please specify whether you are male or female?"
81
  session["stage"] = "ask_symptoms"
82
  return f"Thanks, {session['name']}! So you're a {session['age']}-year-old {session['gender']}. What symptoms are you experiencing?"
83
 
84
+ # Step 5: Get Symptoms
85
  elif session["stage"] == "ask_symptoms":
86
  session["symptoms"] = user_message
87
  session["stage"] = "ask_duration"
88
  return "Since when have you been feeling this way?"
89
 
90
+ # Step 6: Duration
91
  elif session["stage"] == "ask_duration":
92
  session["duration"] = user_message
93
+ session["stage"] = "ask_medicine"
94
  return "Got it. Are you taking any medications or treatments currently?"
95
 
96
+ # Step 7: Medicine intake
97
+ elif session["stage"] == "ask_medicine":
98
+ session["medication"] = user_message
99
+ session["stage"] = "consult"
100
+ return "Thank you. Let’s discuss what could be going on and how you can manage it safely."
101
+
102
+ # Step 8: Personalized Consultation
103
  elif session["stage"] == "consult":
104
  name = session["name"]
105
  age = session["age"]
106
  gender = session["gender"]
107
+ symptoms = session.get("symptoms", "")
108
+ duration = session.get("duration", "")
109
+ medication = session.get("medication", "")
110
+
111
+ # ------------- Handle diet-related questions -----------------
112
+ if any(word in user_message for word in ["diet", "food", "meal", "eat", "nutrition"]):
113
+ # Diet suggestions tailored to the condition
114
+ if "fever" in symptoms:
115
+ diet = (
116
+ f"🍎 {name}, since you have a fever, try keeping your meals light and hydrating.\n\n"
117
+ "- πŸ₯£ **Breakfast:** Oatmeal or boiled egg with fruit (like banana or apple)\n"
118
+ "- 🍲 **Lunch:** Rice with lentil soup or boiled vegetables\n"
119
+ "- πŸ› **Dinner:** Light soup or grilled chicken with plain rice\n"
120
+ "- πŸ’§ **Hydration:** Drink 8–10 glasses of water, coconut water, or clear soups\n"
121
+ "- 🚫 **Avoid:** Fried, oily, or spicy foods\n\n"
122
+ "Eat small portions often β€” this helps your body recover faster."
123
+ )
124
+ elif "stomach" in symptoms or "vomit" in symptoms:
125
+ diet = (
126
+ f"πŸ₯— {name}, for stomach discomfort, stick to a **bland diet**:\n\n"
127
+ "- 🍞 Toast, plain rice, boiled potatoes, or bananas\n"
128
+ "- πŸ’§ Sip water, oral rehydration solution, or herbal tea\n"
129
+ "- 🚫 Avoid milk, fried, or spicy foods\n"
130
+ "- 🍌 Eat small meals to avoid nausea"
131
+ )
132
+ elif "cold" in symptoms or "flu" in symptoms or "cough" in symptoms:
133
+ diet = (
134
+ f"🍊 {name}, for flu or cold, focus on **immunity-boosting foods**:\n\n"
135
+ "- πŸ‹ Citrus fruits, honey with warm water, and soups\n"
136
+ "- 🍲 Chicken soup helps clear congestion\n"
137
+ "- β˜• Ginger or green tea for throat relief\n"
138
+ "- 🚫 Avoid sugary and chilled drinks"
139
+ )
140
+ else:
141
+ diet = (
142
+ f"πŸ₯— {name}, eat a balanced diet: fruits, vegetables, lean protein, and whole grains. "
143
+ "Stay hydrated and avoid processed or fried foods."
144
+ )
145
+
146
+ return diet + "\n\nβš•οΈ *Note: This is general advice β€” not a substitute for medical care.*"
147
+
148
+ # ----------------- Normal medical consultation ----------------
149
  prompt = f"""
150
+ You are Dr. Aiden β€” a warm, caring, and professional doctor.
151
+ You are consulting a {age}-year-old {gender} named {name}.
 
 
152
 
153
+ Patient details:
154
+ - Symptoms: {symptoms}
155
+ - Duration: {duration}
156
+ - Current medications: {medication}
157
 
158
+ Now the patient says: "{user_message}"
 
 
 
 
 
 
159
 
160
+ Respond as a real doctor would β€” empathetic, clear, and personalized.
161
+ Include:
162
+ 1. Acknowledge their condition
163
+ 2. Possible causes
164
+ 3. Simple home remedies or OTC medicines (if safe)
165
+ 4. Diet, rest, and hydration tips
166
+ 5. When to visit a real doctor
167
+ 6. End with a reassuring tone
168
+ """
169
 
170
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
171
  gen_cfg = GenerationConfig(
 
182
 
183
  output_text = tokenizer.decode(output[0], skip_special_tokens=True)
184
  output_text = output_text.split("Doctor:")[-1].strip()
185
+
186
  if not output_text.endswith((".", "!", "?")):
187
  output_text += "."
 
188
 
189
+ output_text += "\n\nβš•οΈ *Note: This advice is AI-generated and not a substitute for professional medical care.*"
190
  return output_text
191
 
192
 
193
+
194
  # =======================================================
195
  # Gradio Interface
196
  # =======================================================