MMuzamilAI commited on
Commit
e673c68
·
verified ·
1 Parent(s): 5eaacc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -37
app.py CHANGED
@@ -1,6 +1,15 @@
1
  # app.py
 
 
 
 
2
  import gradio as gr
3
 
 
 
 
 
 
4
  forward_rules = {
5
  "cold": ["Aconitum napellus", "Allium cepa"],
6
  "fever": ["Gelsemium", "Belladonna", "Ferrum phosphoricum"],
@@ -80,10 +89,10 @@ forward_rules = {
80
  "copd": ["Arsenicum album", "Bryonia alba"],
81
  "pneumonia": ["Antimonium tartaricum", "Bryonia alba"],
82
  "influenza": ["Influenzinum", "Gelsemium sempervirens"],
83
- "tonsillitis": ["Belladonna", "Mercurius solubilis"],
84
- "gingivitis": ["Mercurius solubilis", "Arsenicum album"],
85
- "halitosis": ["Mercurius solubilis", "Carbo vegetabilis"],
86
- "oral thrush": ["Borax", "Mercurius solubilis"],
87
  "shingles": ["Rhus toxicodendron", "Mezereum"],
88
  "chickenpox": ["Rhus toxicodendron", "Antimonium tartaricum"],
89
  "measles": ["Euphrasia officinalis", "Aconitum napellus"],
@@ -104,54 +113,74 @@ forward_rules = {
104
  }
105
 
106
  def make_tag(text, bgcolor="#e0f7fa", fgcolor="#006064"):
107
- return f'<span style="display:inline-block; background-color:{bgcolor}; '\
108
- f'color:{fgcolor}; padding:4px 8px; border-radius:12px; '\
109
- f'margin:2px; font-weight:600;">{text}</span>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
- def homeopathy_bot(user_input):
112
- ui = user_input.strip().lower()
113
- found = False
 
 
 
 
114
  html_chunks = []
115
 
116
- # Forward lookup
117
  for symptom, meds in forward_rules.items():
118
- if symptom in ui:
119
  html_chunks.append(f"<p>💊 Suggested medicines for {make_tag(symptom)}:</p>")
120
- med_tags = " ".join([make_tag(m, bgcolor="#e8f5e9", fgcolor="#1b5e20") for m in meds])
 
 
121
  html_chunks.append(f"<p>{med_tags}</p>")
122
- found = True
123
- break
124
 
125
- # Reverse lookup
126
- if not found:
127
- related = []
128
- med_found = ""
129
- for symptom, meds in forward_rules.items():
130
- for med in meds:
131
- if med.lower() in ui:
132
- related.append(symptom)
133
- med_found = med
134
- if related:
135
- html_chunks.append(f"<p>{make_tag(med_found, bgcolor='#ffe0b2', fgcolor='#e65100')} is used for:</p>")
136
- unique_symptoms = sorted(set(related))
137
- symptom_tags = " ".join([make_tag(s) for s in unique_symptoms])
138
- html_chunks.append(f"<p>{symptom_tags}</p>")
139
- found = True
140
-
141
- if not found:
142
  html_chunks.append(
143
- "<p style='color:#b71c1c; font-weight:600;'>"
144
- "❌ Sorry, I couldn’t find any matches. Try another symptom or medicine.</p>"
145
  )
 
 
 
 
146
 
 
 
 
 
 
147
  return "\n".join(html_chunks)
148
 
149
-
150
  demo = gr.Interface(
151
  fn=homeopathy_bot,
152
  inputs=gr.Textbox(
153
  lines=2,
154
- placeholder="Type a symptom/disease (e.g., 'cold', 'headache') or a medicine name...",
155
  label="Your Query"
156
  ),
157
  outputs=gr.HTML(label="Response"),
@@ -159,9 +188,11 @@ demo = gr.Interface(
159
  description=(
160
  "Enter a symptom/disease to get suggested homeopathic remedies, "
161
  "or enter a medicine name to see which conditions it’s used for. "
 
162
  ),
163
  theme="huggingface",
164
  allow_flagging="never" # ← disable the 🏴 flag button
165
  )
166
 
167
- demo.launch()
 
 
1
  # app.py
2
+
3
+ import nltk
4
+ from nltk.tokenize import word_tokenize
5
+ from nltk.stem import WordNetLemmatizer
6
  import gradio as gr
7
 
8
+ # — Download NLTK resources if they’re missing —
9
+ # (Only needs to be run once per environment)
10
+ nltk.download("punkt")
11
+ nltk.download("wordnet")
12
+
13
  forward_rules = {
14
  "cold": ["Aconitum napellus", "Allium cepa"],
15
  "fever": ["Gelsemium", "Belladonna", "Ferrum phosphoricum"],
 
89
  "copd": ["Arsenicum album", "Bryonia alba"],
90
  "pneumonia": ["Antimonium tartaricum", "Bryonia alba"],
91
  "influenza": ["Influenzinum", "Gelsemium sempervirens"],
92
+ "tonsillitis": ["Belladonna", "Mercurius solubil"],
93
+ "gingivitis": ["Mercurius solubil", "Arsenicum album"],
94
+ "halitosis": ["Mercurius solubil", "Carbo vegetabilis"],
95
+ "oral thrush": ["Borax", "Mercurius solubil"],
96
  "shingles": ["Rhus toxicodendron", "Mezereum"],
97
  "chickenpox": ["Rhus toxicodendron", "Antimonium tartaricum"],
98
  "measles": ["Euphrasia officinalis", "Aconitum napellus"],
 
113
  }
114
 
115
  def make_tag(text, bgcolor="#e0f7fa", fgcolor="#006064"):
116
+ return (
117
+ f'<span style="display:inline-block; background-color:{bgcolor}; '
118
+ f'color:{fgcolor}; padding:4px 8px; border-radius:12px; '
119
+ f'margin:2px; font-weight:600;">{text}</span>'
120
+ )
121
+
122
+ lemmatizer = WordNetLemmatizer()
123
+
124
+ def preprocess(text: str):
125
+ """
126
+ Tokenize + lemmatize, just in case you want lemmatized tokens
127
+ for future extensions. We won’t use this for exact phrase matching,
128
+ but it may help if you want to add more advanced NLP logic later.
129
+ """
130
+ tokens = word_tokenize(text.lower())
131
+ lemmas = [lemmatizer.lemmatize(token) for token in tokens]
132
+ return lemmas
133
 
134
+ def homeopathy_bot(user_input: str) -> str:
135
+ """
136
+ 1. Try forward lookup: check if any symptom‐phrase is literally a substring
137
+ of user_input.lower()
138
+ 2. If not found, do reverse lookup: check if any remedy‐phrase is in user_input.lower()
139
+ """
140
+ raw = user_input.lower()
141
  html_chunks = []
142
 
143
+ # 1) Forward lookup (symptom → remedies)
144
  for symptom, meds in forward_rules.items():
145
+ if symptom in raw:
146
  html_chunks.append(f"<p>💊 Suggested medicines for {make_tag(symptom)}:</p>")
147
+ med_tags = " ".join(
148
+ make_tag(m, bgcolor="#e8f5e9", fgcolor="#1b5e20") for m in meds
149
+ )
150
  html_chunks.append(f"<p>{med_tags}</p>")
151
+ return "\n".join(html_chunks) # Once matched, return immediately
 
152
 
153
+ # 2) Reverse lookup (remedy → symptoms)
154
+ # Loop through every (symptom, [meds]) pair and see if any med is in raw.
155
+ related_symptoms = []
156
+ med_found = ""
157
+ for symptom, meds in forward_rules.items():
158
+ for med in meds:
159
+ if med.lower() in raw:
160
+ related_symptoms.append(symptom)
161
+ med_found = med
162
+ if related_symptoms:
 
 
 
 
 
 
 
163
  html_chunks.append(
164
+ f"<p>{make_tag(med_found, bgcolor='#ffe0b2', fgcolor='#e65100')} is used for:</p>"
 
165
  )
166
+ unique_symptoms = sorted(set(related_symptoms))
167
+ symptom_tags = " ".join(make_tag(s) for s in unique_symptoms)
168
+ html_chunks.append(f"<p>{symptom_tags}</p>")
169
+ return "\n".join(html_chunks)
170
 
171
+ # 3) If nothing matches
172
+ html_chunks.append(
173
+ "<p style='color:#b71c1c; font-weight:600;'>"
174
+ "❌ Sorry, I couldn’t find any matches. Try another symptom or medicine.</p>"
175
+ )
176
  return "\n".join(html_chunks)
177
 
178
+ # Build the Gradio interface
179
  demo = gr.Interface(
180
  fn=homeopathy_bot,
181
  inputs=gr.Textbox(
182
  lines=2,
183
+ placeholder="Type a symptom/disease (e.g., 'cold', 'headache') or a medicine name",
184
  label="Your Query"
185
  ),
186
  outputs=gr.HTML(label="Response"),
 
188
  description=(
189
  "Enter a symptom/disease to get suggested homeopathic remedies, "
190
  "or enter a medicine name to see which conditions it’s used for. "
191
+ "Results appear as colorful tags!"
192
  ),
193
  theme="huggingface",
194
  allow_flagging="never" # ← disable the 🏴 flag button
195
  )
196
 
197
+ if __name__ == "__main__":
198
+ demo.launch()