Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,7 @@
|
|
| 1 |
-
"""
|
| 2 |
-
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 3 |
-
β Cross-Medical-System Drug Recommendation Engine β
|
| 4 |
-
β Master's Thesis Project β Hugging Face Gradio App β
|
| 5 |
-
β Medical Systems: Allopathic | Ayurvedic | Unani | β
|
| 6 |
-
β Homeopathic | Herbal β
|
| 7 |
-
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 8 |
-
|
| 9 |
-
HOW TO DEPLOY ON HUGGING FACE SPACES:
|
| 10 |
-
1. Create a new Space β SDK: Gradio
|
| 11 |
-
2. Upload: app.py, requirements.txt, and the entire models/ folder
|
| 12 |
-
3. The Space will auto-install requirements and launch
|
| 13 |
-
|
| 14 |
-
FOLDER STRUCTURE on HF Space:
|
| 15 |
-
app.py
|
| 16 |
-
requirements.txt
|
| 17 |
-
models/
|
| 18 |
-
tfidf_vectorizer.pkl
|
| 19 |
-
tfidf_matrix.pkl
|
| 20 |
-
svd_reducer.pkl
|
| 21 |
-
kmeans_model.pkl
|
| 22 |
-
drug_database.csv
|
| 23 |
-
model_metadata.json
|
| 24 |
-
"""
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
import gradio as gr
|
| 27 |
import pandas as pd
|
| 28 |
import numpy as np
|
|
@@ -31,22 +10,147 @@ import json
|
|
| 31 |
import os
|
| 32 |
import re
|
| 33 |
import warnings
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
warnings.filterwarnings("ignore")
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def load_models():
|
| 42 |
-
print("Loading models
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
with open(os.path.join(MODEL_DIR, "model_metadata.json")) as f:
|
| 47 |
-
|
| 48 |
-
print(f"β
|
| 49 |
-
return
|
| 50 |
|
| 51 |
|
| 52 |
try:
|
|
@@ -54,338 +158,841 @@ try:
|
|
| 54 |
vectorizer, tfidf_matrix, drug_db, metadata = load_models()
|
| 55 |
MEDICAL_SYSTEMS = ["All Systems"] + sorted(drug_db["medical_system"].unique().tolist())
|
| 56 |
MODEL_LOADED = True
|
| 57 |
-
except Exception as
|
| 58 |
-
print(f"Model load
|
| 59 |
MODEL_LOADED = False
|
| 60 |
MEDICAL_SYSTEMS = ["All Systems"]
|
|
|
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
-
#
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
def
|
| 66 |
-
"""
|
| 67 |
-
|
| 68 |
-
Returns a formatted DataFrame of recommendations.
|
| 69 |
-
"""
|
| 70 |
-
if not MODEL_LOADED:
|
| 71 |
-
return pd.DataFrame({"Error": ["Models not loaded. Check /models folder."]}), "β Models not loaded"
|
| 72 |
|
| 73 |
-
if not query or not query.strip():
|
| 74 |
-
return pd.DataFrame({"Info": ["Please enter a drug name or query."]}), "β οΈ Empty query"
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
sims = cosine_similarity(q_vec, tfidf_matrix).flatten()
|
| 83 |
-
|
| 84 |
-
# Apply system filter
|
| 85 |
-
if medical_system and medical_system != "All Systems":
|
| 86 |
-
mask = drug_db["medical_system"] == medical_system
|
| 87 |
sims_work = sims.copy()
|
| 88 |
-
sims_work[~mask] = 0
|
| 89 |
else:
|
| 90 |
sims_work = sims
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
"brand_name", "generic_name", "dosage_form",
|
| 105 |
-
"medical_system", "manufacturer"
|
| 106 |
]].copy()
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
# Rename columns for display
|
| 113 |
-
results.columns = ["Brand Name", "Generic Name", "Dosage Form",
|
| 114 |
-
"Strength", "Medical System", "Manufacturer", "Score"]
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
summary = (
|
| 118 |
-
f"
|
| 119 |
-
f"{
|
| 120 |
-
f"
|
| 121 |
)
|
| 122 |
-
return
|
| 123 |
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
if not MODEL_LOADED:
|
| 171 |
return "Models not loaded."
|
| 172 |
-
sys_dist
|
| 173 |
-
dosage_dist = drug_db["dosage_form"].value_counts().head(
|
| 174 |
-
|
| 175 |
-
stats_md = f"""
|
| 176 |
-
## π Dataset Statistics
|
| 177 |
|
| 178 |
| Metric | Value |
|
| 179 |
|--------|-------|
|
| 180 |
-
| Total Drugs | {len(drug_db):,} |
|
| 181 |
-
| Medical Systems | {drug_db[
|
| 182 |
-
| Unique Manufacturers | {drug_db[
|
| 183 |
-
| Unique Brand Names | {drug_db[
|
| 184 |
-
| TF-IDF Features | {metadata.get(
|
| 185 |
-
| Silhouette Score | {metadata.get(
|
| 186 |
-
|
| 187 |
-
|
|
|
|
| 188 |
"""
|
| 189 |
-
for
|
| 190 |
-
pct =
|
| 191 |
-
bar = "β" * int(pct /
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
stats_md += "\n\n### π Top Dosage Forms\n"
|
| 195 |
-
for dosage, cnt in dosage_dist.items():
|
| 196 |
-
stats_md += f"\n- {dosage}: {cnt:,}"
|
| 197 |
|
| 198 |
-
|
|
|
|
|
|
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
|
| 212 |
-
|
| 213 |
-
["Azithromycin antibiotic tablet", 3],
|
| 214 |
-
["fever pain relief", 2],
|
| 215 |
-
["digestive stomach", 2],
|
| 216 |
-
["blood pressure hypertension", 2],
|
| 217 |
-
]
|
| 218 |
|
| 219 |
-
|
| 220 |
-
.gradio-container { max-width: 1100px; margin: auto; font-family: 'Segoe UI', sans-serif; }
|
| 221 |
-
.header-box { background: linear-gradient(135deg, #1a237e, #283593);
|
| 222 |
-
color: white; padding: 24px; border-radius: 12px; margin-bottom: 16px; }
|
| 223 |
-
.stat-box { background: #f8f9fa; border-radius: 8px; padding: 12px; }
|
| 224 |
-
footer { display: none !important; }
|
| 225 |
-
"""
|
| 226 |
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
gr.HTML("""
|
| 230 |
-
<div
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
</div>
|
| 237 |
""")
|
| 238 |
|
| 239 |
with gr.Tabs():
|
| 240 |
|
| 241 |
-
#
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
lines=1
|
| 251 |
-
)
|
| 252 |
-
with gr.Column(scale=2):
|
| 253 |
-
system_filter = gr.Dropdown(
|
| 254 |
-
choices=MEDICAL_SYSTEMS,
|
| 255 |
-
value="All Systems",
|
| 256 |
-
label="π₯ Medical System Filter"
|
| 257 |
-
)
|
| 258 |
|
| 259 |
with gr.Row():
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
recommend_btn = gr.Button("π Get Recommendations", variant="primary", size="lg")
|
| 270 |
-
summary_box = gr.Markdown(label="Summary")
|
| 271 |
-
results_table = gr.DataFrame(
|
| 272 |
label="π Recommended Drugs",
|
| 273 |
-
wrap=True,
|
| 274 |
-
interactive=False
|
| 275 |
)
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
inputs=[
|
| 280 |
-
|
| 281 |
)
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
| 288 |
|
| 289 |
-
#
|
| 290 |
-
|
|
|
|
|
|
|
| 291 |
gr.Markdown("""
|
| 292 |
-
###
|
| 293 |
-
|
|
|
|
| 294 |
""")
|
| 295 |
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
lines=1,
|
| 301 |
-
scale=4
|
| 302 |
-
)
|
| 303 |
-
top_per_sys = gr.Slider(
|
| 304 |
-
minimum=1, maximum=5, value=3, step=1,
|
| 305 |
-
label="Results per System",
|
| 306 |
-
scale=2
|
| 307 |
-
)
|
| 308 |
-
|
| 309 |
-
compare_btn = gr.Button("π Compare Across All Systems", variant="primary", size="lg")
|
| 310 |
cross_summary = gr.Markdown()
|
| 311 |
-
cross_table = gr.DataFrame(
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
examples=CROSS_SYSTEM_EXAMPLES,
|
| 315 |
-
inputs=[cross_query, top_per_sys],
|
| 316 |
-
label="π Quick Examples"
|
| 317 |
)
|
| 318 |
|
| 319 |
compare_btn.click(
|
| 320 |
fn=cross_system_compare,
|
| 321 |
-
inputs=[
|
| 322 |
-
outputs=[cross_table, cross_summary]
|
| 323 |
)
|
| 324 |
|
| 325 |
-
#
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
-
# ββ Tab 4: About βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 332 |
-
with gr.TabItem("π About / Thesis"):
|
| 333 |
gr.Markdown("""
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
-
|
| 337 |
-
|
|
|
|
| 338 |
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
multiple medical systems coexist. No unified digital tool existed for this task.
|
| 343 |
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
### Dataset
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
-
|
| 358 |
-
|
| 359 |
-
### Key Contributions
|
| 360 |
-
1. **First unified cross-medical-system recommender** for South Asian drug registry
|
| 361 |
-
2. **NLP-driven**: TF-IDF bigrams handle compound drug names (e.g., "Diphenhydramine + Zinc Acetate")
|
| 362 |
-
3. **Clustering analysis** reveals natural drug groupings across cultural medical traditions
|
| 363 |
-
4. **Deployable**: Fast PKL-based inference, <100ms per query
|
| 364 |
-
|
| 365 |
-
### Model Files
|
| 366 |
-
```
|
| 367 |
-
models/
|
| 368 |
-
βββ tfidf_vectorizer.pkl β Fitted TF-IDF transformer
|
| 369 |
-
βββ tfidf_matrix.pkl β Pre-computed drug feature matrix
|
| 370 |
-
βββ svd_reducer.pkl β SVD dimensionality reducer
|
| 371 |
-
βββ kmeans_model.pkl β K-Means cluster assignments
|
| 372 |
-
βββ drug_database.csv β Processed drug database
|
| 373 |
-
```
|
| 374 |
-
|
| 375 |
-
### How to Cite
|
| 376 |
-
```
|
| 377 |
-
Author, (2024). Cross-Medical-System Drug Recommendation Engine.
|
| 378 |
-
Master's Thesis, [University Name].
|
| 379 |
-
Dataset: https://www.kaggle.com/datasets/shuvokumarbasak2030/drug-pharma-new-dataset
|
| 380 |
-
```
|
| 381 |
""")
|
| 382 |
|
| 383 |
gr.HTML("""
|
| 384 |
-
<div style="text-align:center;
|
| 385 |
-
|
| 386 |
-
|
|
|
|
| 387 |
</div>
|
| 388 |
""")
|
| 389 |
|
|
|
|
|
|
|
| 390 |
if __name__ == "__main__":
|
| 391 |
-
demo.launch(server_name="0.0.0.0", server_port=7860,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 3 |
+
# IMPORTS
|
| 4 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
import gradio as gr
|
| 6 |
import pandas as pd
|
| 7 |
import numpy as np
|
|
|
|
| 10 |
import os
|
| 11 |
import re
|
| 12 |
import warnings
|
| 13 |
+
import requests
|
| 14 |
+
import plotly.graph_objects as go
|
| 15 |
+
import plotly.express as px
|
| 16 |
+
from plotly.subplots import make_subplots
|
| 17 |
|
| 18 |
warnings.filterwarnings("ignore")
|
| 19 |
|
| 20 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
+
# CONSTANTS
|
| 22 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 23 |
+
|
| 24 |
+
OPENFDA_BASE = "https://api.fda.gov/drug"
|
| 25 |
+
MODEL_DIR = os.path.join(os.path.dirname(__file__), "models")
|
| 26 |
+
|
| 27 |
+
# System colour palette β used consistently across all charts
|
| 28 |
+
SYSTEM_COLORS = {
|
| 29 |
+
"Allopathic": "#3b82f6", # blue
|
| 30 |
+
"Unani": "#f97316", # orange
|
| 31 |
+
"Ayurvedic": "#22c55e", # green
|
| 32 |
+
"Homeopathic": "#a855f7", # purple
|
| 33 |
+
"Herbal": "#ef4444", # red
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# βββ 30 Curated Drug Options βββββββββββββββββββββββββββββββββββββ
|
| 37 |
+
# Key = display label shown in the Gradio Dropdown
|
| 38 |
+
# Value = exact TF-IDF search query passed to recommend()
|
| 39 |
+
# Changing this dict is the ONLY place you need to add/remove options.
|
| 40 |
+
DRUG_OPTIONS = {
|
| 41 |
+
# ββ Antibiotics ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 42 |
+
"π¦ Azithromycin β Antibiotic (Respiratory)": "Azithromycin 500mg tablet",
|
| 43 |
+
"π¦ Amoxicillin β Antibiotic (Broad Spectrum)": "Amoxicillin 500mg capsule",
|
| 44 |
+
"π¦ Ciprofloxacin β Antibiotic (UTI/Infection)": "Ciprofloxacin 500mg tablet",
|
| 45 |
+
"π¦ Metronidazole β Antibiotic (Anaerobic)": "Metronidazole 400mg tablet",
|
| 46 |
+
"π¦ Ceftriaxone β Antibiotic (Injection)": "Ceftriaxone 1gm injection",
|
| 47 |
+
"π¦ Levofloxacin β Antibiotic (Pneumonia)": "Levofloxacin 500mg tablet",
|
| 48 |
+
# ββ Pain & Fever βββββββββββββββββββββββββββββββββββββββββββββ
|
| 49 |
+
"π€ Paracetamol β Fever & Pain Relief": "Paracetamol 500mg tablet",
|
| 50 |
+
"π€ Diclofenac β Anti-inflammatory": "Diclofenac Sodium 50mg tablet",
|
| 51 |
+
"π€ Naproxen β Pain Relief (Joints)": "Naproxen 250mg tablet",
|
| 52 |
+
"π€ Ketorolac β Strong Painkiller (Injection)": "Ketorolac 30mg injection",
|
| 53 |
+
# ββ Heart & Blood Pressure βββββββββββββββββββββββββββββββββββ
|
| 54 |
+
"π Amlodipine β Blood Pressure": "Amlodipine 5mg tablet",
|
| 55 |
+
"π Atorvastatin β Cholesterol": "Atorvastatin 20mg tablet",
|
| 56 |
+
"π Losartan β Hypertension": "Losartan Potassium 50mg tablet",
|
| 57 |
+
"π Metoprolol β Heart Rate / Beta Blocker": "Metoprolol 50mg tablet",
|
| 58 |
+
# ββ Diabetes βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
+
"π©Ί Metformin β Type 2 Diabetes": "Metformin Hydrochloride 500mg tablet",
|
| 60 |
+
"π©Ί Glibenclamide β Blood Sugar Control": "Glibenclamide 5mg tablet",
|
| 61 |
+
# ββ Respiratory & Allergy ββββββββββββββββββββββββββββββββββββ
|
| 62 |
+
"π« Salbutamol β Asthma / Bronchospasm": "Salbutamol 2mg tablet syrup",
|
| 63 |
+
"π« Montelukast β Asthma / Allergy": "Montelukast 10mg tablet",
|
| 64 |
+
"π« Fexofenadine β Allergy / Antihistamine": "Fexofenadine Hydrochloride 120mg tablet",
|
| 65 |
+
"π« Cetirizine β Allergy / Antihistamine": "Cetirizine Dihydrochloride 10mg tablet",
|
| 66 |
+
# ββ Neuro / Mental Health ββββββββββββββββββββββββββββββββββββ
|
| 67 |
+
"π§ Pregabalin β Nerve Pain / Anxiety": "Pregabalin 75mg capsule",
|
| 68 |
+
"π§ Clonazepam β Anxiety / Seizure": "Clonazepam 0.5mg tablet",
|
| 69 |
+
# ββ GI / Stomach βββββββββββββββββββββββββββββββββββββββββββββ
|
| 70 |
+
"π« Omeprazole β Acid Reflux / Ulcer": "Omeprazole 20mg capsule",
|
| 71 |
+
"π« Esomeprazole β GERD / Acid": "Esomeprazole 40mg capsule",
|
| 72 |
+
"π« Domperidone β Nausea / Vomiting": "Domperidone 10mg tablet",
|
| 73 |
+
"π« Ondansetron β Nausea (Chemotherapy)": "Ondansetron 4mg tablet",
|
| 74 |
+
# ββ Anti-infective / Antifungal ββββββββββββββββββββββββββββββ
|
| 75 |
+
"πΏ Albendazole β Deworming": "Albendazole 400mg tablet",
|
| 76 |
+
"πΏ Fluconazole β Antifungal": "Fluconazole 150mg capsule",
|
| 77 |
+
# ββ Vitamins & Supplements βββββββββββββββββββββββββββββββββββ
|
| 78 |
+
"π Vitamin D3 β Bone / Immunity": "Cholecalciferol Vitamin D3 tablet",
|
| 79 |
+
"π Zinc + Multivitamin β Immunity": "Zinc Nicotinamide Pyridoxine vitamin tablet",
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
DROPDOWN_LABELS = list(DRUG_OPTIONS.keys())
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 86 |
+
# β
BUG FIX β build_drug_text
|
| 87 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 88 |
+
# THIS FUNCTION IS THE CORE FIX.
|
| 89 |
+
#
|
| 90 |
+
# OLD behaviour (buggy):
|
| 91 |
+
# All systems used: GenericName + Dosage + Strength + System
|
| 92 |
+
# For Ayurvedic/Unani/Homeopathic/Herbal, Generic Name is NULL in
|
| 93 |
+
# the dataset, so the code fell back to Brand Name.
|
| 94 |
+
# Brand names like "Feverfit", "Paincap", "Paralead" contain tokens
|
| 95 |
+
# like "fever", "pain", "para" β TF-IDF wrongly matched these when
|
| 96 |
+
# a user searched "paracetamol fever tablet".
|
| 97 |
+
#
|
| 98 |
+
# NEW behaviour (fixed):
|
| 99 |
+
# Allopathic β GenericName + Dosage + Strength + "allopathic"
|
| 100 |
+
# (uses the real pharmaceutical compound)
|
| 101 |
+
# Non-allopathic β Dosage + Strength + SystemName ONLY
|
| 102 |
+
# (brand name noise removed entirely)
|
| 103 |
+
#
|
| 104 |
+
# Result: "paracetamol fever tablet" now returns ONLY Allopathic
|
| 105 |
+
# compounds like Paracetamol, Acetaminophen β no more "Feverfit".
|
| 106 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 107 |
+
|
| 108 |
+
def _clean(t) -> str:
|
| 109 |
+
"""Lowercase, remove special chars, collapse whitespace."""
|
| 110 |
+
if pd.isna(t):
|
| 111 |
+
return ""
|
| 112 |
+
t = re.sub(r"[^a-z0-9\s\+\-\.]", " ", str(t).lower())
|
| 113 |
+
return re.sub(r"\s+", " ", t).strip()
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def build_drug_text(row) -> str:
|
| 117 |
+
"""
|
| 118 |
+
β
FIXED version of drug_text construction.
|
| 119 |
+
|
| 120 |
+
Allopathic β rich text: compound + dosage + strength + system
|
| 121 |
+
All others β lean text: dosage + strength + system (NO brand name)
|
| 122 |
+
"""
|
| 123 |
+
if row["medical_system"] == "Allopathic":
|
| 124 |
+
return " ".join(filter(None, [
|
| 125 |
+
_clean(row.get("Generic Name", "")),
|
| 126 |
+
_clean(row.get("Dosages Description", "")),
|
| 127 |
+
_clean(str(row.get("Strength", ""))),
|
| 128 |
+
"allopathic",
|
| 129 |
+
]))
|
| 130 |
+
else:
|
| 131 |
+
# Non-allopathic: Generic Name is always NULL in this dataset.
|
| 132 |
+
# Using Brand Name as fallback was the source of the bug.
|
| 133 |
+
# We intentionally exclude it here.
|
| 134 |
+
return " ".join(filter(None, [
|
| 135 |
+
_clean(row.get("Dosages Description", "")),
|
| 136 |
+
_clean(str(row.get("Strength", ""))),
|
| 137 |
+
_clean(row.get("medical_system", "")),
|
| 138 |
+
]))
|
| 139 |
+
|
| 140 |
|
| 141 |
+
# ββββββββββββββββοΏ½οΏ½οΏ½ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 142 |
+
# LOAD PKL MODELS
|
| 143 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 144 |
|
| 145 |
def load_models():
|
| 146 |
+
print("Loading models β¦")
|
| 147 |
+
vec = joblib.load(os.path.join(MODEL_DIR, "tfidf_vectorizer.pkl"))
|
| 148 |
+
mat = joblib.load(os.path.join(MODEL_DIR, "tfidf_matrix.pkl"))
|
| 149 |
+
db = pd.read_csv(os.path.join(MODEL_DIR, "drug_database.csv"))
|
| 150 |
with open(os.path.join(MODEL_DIR, "model_metadata.json")) as f:
|
| 151 |
+
meta = json.load(f)
|
| 152 |
+
print(f"β
{len(db):,} drugs Β· {mat.shape[1]:,} features loaded")
|
| 153 |
+
return vec, mat, db, meta
|
| 154 |
|
| 155 |
|
| 156 |
try:
|
|
|
|
| 158 |
vectorizer, tfidf_matrix, drug_db, metadata = load_models()
|
| 159 |
MEDICAL_SYSTEMS = ["All Systems"] + sorted(drug_db["medical_system"].unique().tolist())
|
| 160 |
MODEL_LOADED = True
|
| 161 |
+
except Exception as exc:
|
| 162 |
+
print(f"Model load failed: {exc}")
|
| 163 |
MODEL_LOADED = False
|
| 164 |
MEDICAL_SYSTEMS = ["All Systems"]
|
| 165 |
+
drug_db = pd.DataFrame()
|
| 166 |
+
metadata = {}
|
| 167 |
|
| 168 |
|
| 169 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 170 |
+
# CORE RECOMMENDER
|
| 171 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 172 |
|
| 173 |
+
def _get_query(drug_label: str) -> str:
|
| 174 |
+
"""Map dropdown label β TF-IDF search query."""
|
| 175 |
+
return DRUG_OPTIONS.get(drug_label, drug_label)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
|
|
|
|
|
|
| 177 |
|
| 178 |
+
def _run_similarity(query: str, system_filter: str, top_n: int, min_score: float):
|
| 179 |
+
"""Inner similarity search. Returns (indices, scores)."""
|
| 180 |
+
q_clean = _clean(query)
|
| 181 |
+
q_vec = vectorizer.transform([q_clean])
|
| 182 |
+
sims = cosine_similarity(q_vec, tfidf_matrix).flatten()
|
| 183 |
|
| 184 |
+
if system_filter and system_filter != "All Systems":
|
| 185 |
+
mask = drug_db["medical_system"] == system_filter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
sims_work = sims.copy()
|
| 187 |
+
sims_work[~mask] = 0.0
|
| 188 |
else:
|
| 189 |
sims_work = sims
|
| 190 |
|
| 191 |
+
candidate_idx = sims_work.argsort()[-(top_n * 4):][::-1]
|
| 192 |
+
filtered_idx = [i for i in candidate_idx if sims[i] >= min_score][:top_n]
|
| 193 |
+
return filtered_idx, sims
|
| 194 |
|
| 195 |
+
|
| 196 |
+
def recommend_from_selection(drug_label: str, system_filter: str,
|
| 197 |
+
top_n: int, min_score: float):
|
| 198 |
+
"""Tab 1 β Dataset recommendations from PKL model."""
|
| 199 |
+
if not MODEL_LOADED:
|
| 200 |
+
return None, "β Models not loaded. Ensure `/models` folder is present."
|
| 201 |
+
if not drug_label:
|
| 202 |
+
return None, "β οΈ Please select a drug from the dropdown."
|
| 203 |
+
|
| 204 |
+
query = _get_query(drug_label)
|
| 205 |
+
idx, sims = _run_similarity(query, system_filter, top_n, min_score)
|
| 206 |
+
|
| 207 |
+
if not idx:
|
| 208 |
+
return None, (
|
| 209 |
+
f"β οΈ No results above similarity score **{min_score}**. "
|
| 210 |
+
"Try lowering the threshold slider."
|
| 211 |
)
|
| 212 |
|
| 213 |
+
out = drug_db.iloc[idx][[
|
| 214 |
+
"brand_name", "generic_name", "dosage_form",
|
| 215 |
+
"strength", "medical_system", "manufacturer",
|
| 216 |
]].copy()
|
| 217 |
+
out["similarity_score"] = [round(float(sims[i]), 4) for i in idx]
|
| 218 |
+
out = out.sort_values("similarity_score", ascending=False).reset_index(drop=True)
|
| 219 |
+
out.index = range(1, len(out) + 1)
|
| 220 |
+
out.index.name = "Rank"
|
| 221 |
+
out.columns = [
|
| 222 |
+
"Brand Name", "Generic Name", "Dosage Form",
|
| 223 |
+
"Strength", "Medical System", "Manufacturer", "Score",
|
| 224 |
+
]
|
| 225 |
+
|
| 226 |
+
sys_counts = out["Medical System"].value_counts()
|
| 227 |
+
sys_str = " Β· ".join(f"**{k}** {v}" for k, v in sys_counts.items())
|
| 228 |
+
label_short = drug_label.split("β")[0].strip()
|
| 229 |
+
summary = (
|
| 230 |
+
f"### β
{len(out)} results for {label_short}\n\n"
|
| 231 |
+
f"{sys_str}\n\n"
|
| 232 |
+
f"*Query used: `{query}`*"
|
| 233 |
+
)
|
| 234 |
+
return out, summary
|
| 235 |
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
+
def cross_system_compare(drug_label: str, top_per_system: int):
|
| 238 |
+
"""Tab 2 β Best N drugs from every system side by side."""
|
| 239 |
+
if not MODEL_LOADED:
|
| 240 |
+
return None, "β Models not loaded."
|
| 241 |
+
if not drug_label:
|
| 242 |
+
return None, "β οΈ Select a drug first."
|
| 243 |
+
|
| 244 |
+
query = _get_query(drug_label)
|
| 245 |
+
q_clean = _clean(query)
|
| 246 |
+
q_vec = vectorizer.transform([q_clean])
|
| 247 |
+
sims = cosine_similarity(q_vec, tfidf_matrix).flatten()
|
| 248 |
+
|
| 249 |
+
rows = []
|
| 250 |
+
for system in sorted(drug_db["medical_system"].unique()):
|
| 251 |
+
mask = drug_db["medical_system"] == system
|
| 252 |
+
s = sims.copy(); s[~mask] = 0.0
|
| 253 |
+
idx = [i for i in s.argsort()[-top_per_system:][::-1] if sims[i] > 0.01]
|
| 254 |
+
for i in idx:
|
| 255 |
+
r = drug_db.iloc[i]
|
| 256 |
+
rows.append({
|
| 257 |
+
"Medical System": r["medical_system"],
|
| 258 |
+
"Brand Name": r["brand_name"],
|
| 259 |
+
"Generic Name": r["generic_name"],
|
| 260 |
+
"Dosage Form": r["dosage_form"],
|
| 261 |
+
"Strength": r["strength"],
|
| 262 |
+
"Score": round(float(sims[i]), 4),
|
| 263 |
+
})
|
| 264 |
+
|
| 265 |
+
if not rows:
|
| 266 |
+
return None, "No cross-system results found."
|
| 267 |
+
|
| 268 |
+
df = pd.DataFrame(rows).sort_values(
|
| 269 |
+
["Medical System", "Score"], ascending=[True, False]
|
| 270 |
+
).reset_index(drop=True)
|
| 271 |
+
df.index = range(1, len(df) + 1)
|
| 272 |
+
df.index.name = "Rank"
|
| 273 |
+
|
| 274 |
+
label_short = drug_label.split("β")[0].strip()
|
| 275 |
summary = (
|
| 276 |
+
f"### π Cross-system: {label_short}\n\n"
|
| 277 |
+
f"Top **{top_per_system}** per system Β· {len(df)} total drugs Β· "
|
| 278 |
+
f"{df['Medical System'].nunique()} systems"
|
| 279 |
)
|
| 280 |
+
return df, summary
|
| 281 |
|
| 282 |
|
| 283 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 284 |
+
# OPENFA API HELPERS
|
| 285 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 286 |
+
|
| 287 |
+
def _openfda(endpoint: str, params: dict, timeout: int = 10) -> dict:
|
| 288 |
+
try:
|
| 289 |
+
r = requests.get(
|
| 290 |
+
f"{OPENFDA_BASE}/{endpoint}.json",
|
| 291 |
+
params=params, timeout=timeout,
|
| 292 |
+
headers={"User-Agent": "DrugRecommenderThesis/3.0"},
|
| 293 |
+
)
|
| 294 |
+
if r.status_code == 200:
|
| 295 |
+
return r.json()
|
| 296 |
+
return {"error": f"HTTP {r.status_code}", "message": r.text[:200]}
|
| 297 |
+
except requests.exceptions.Timeout:
|
| 298 |
+
return {"error": "timeout", "message": "OpenFDA timed out β try again."}
|
| 299 |
+
except requests.exceptions.ConnectionError:
|
| 300 |
+
return {"error": "connection", "message": "Cannot reach OpenFDA. Check internet."}
|
| 301 |
+
except Exception as e:
|
| 302 |
+
return {"error": "unknown", "message": str(e)}
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
def _extract_generic(drug_label: str) -> str:
|
| 306 |
+
"""'π¦ Azithromycin β Antibiotic' β 'Azithromycin'"""
|
| 307 |
+
raw = drug_label.split("β")[0]
|
| 308 |
+
cleaned = re.sub(r"[^\w\s]", "", raw).strip()
|
| 309 |
+
words = cleaned.split()
|
| 310 |
+
return words[0] if words else cleaned
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
# βββ Tab 3: FDA Drug Label βββββββββββββββββββββββββββββββββββββββ
|
| 314 |
+
def get_fda_label(drug_label: str) -> str:
|
| 315 |
+
if not drug_label:
|
| 316 |
+
return "β οΈ Select a drug first."
|
| 317 |
+
generic = _extract_generic(drug_label)
|
| 318 |
+
data = _openfda("label", {"search": f"openfda.generic_name:{generic}", "limit": 1})
|
| 319 |
+
|
| 320 |
+
if "error" in data:
|
| 321 |
+
return (
|
| 322 |
+
f"### β οΈ OpenFDA: {data['message']}\n\n"
|
| 323 |
+
f"*`{generic}` may not be in the US FDA database β "
|
| 324 |
+
"OpenFDA covers US-approved drugs only.*"
|
| 325 |
+
)
|
| 326 |
+
|
| 327 |
+
results = data.get("results", [])
|
| 328 |
+
if not results:
|
| 329 |
+
return f"βΉοΈ No FDA label found for **{generic}**."
|
| 330 |
+
|
| 331 |
+
r = results[0]
|
| 332 |
+
ofd = r.get("openfda", {})
|
| 333 |
+
lines = [
|
| 334 |
+
f"## π FDA Label: {generic.title()}",
|
| 335 |
+
"_Source: U.S. Food & Drug Administration Β· OpenFDA_\n",
|
| 336 |
+
]
|
| 337 |
+
|
| 338 |
+
def _add(key, title):
|
| 339 |
+
v = ofd.get(key, [])
|
| 340 |
+
if v:
|
| 341 |
+
lines.append(f"**{title}:** {', '.join(v[:5])}")
|
| 342 |
+
|
| 343 |
+
_add("brand_name", "Brand Names (US)")
|
| 344 |
+
_add("manufacturer_name", "Manufacturer")
|
| 345 |
+
_add("route", "Route")
|
| 346 |
+
lines.append("")
|
| 347 |
+
|
| 348 |
+
SECTIONS = [
|
| 349 |
+
("indications_and_usage", "π Indications & Usage", 700),
|
| 350 |
+
("warnings", "β οΈ Warnings", 500),
|
| 351 |
+
("dosage_and_administration", "π Dosage & Administration", 500),
|
| 352 |
+
("adverse_reactions", "π΄ Adverse Reactions", 400),
|
| 353 |
+
("drug_interactions", "π Drug Interactions", 400),
|
| 354 |
+
("contraindications", "π« Contraindications", 400),
|
| 355 |
+
]
|
| 356 |
+
for field, heading, limit in SECTIONS:
|
| 357 |
+
val = r.get(field, [])
|
| 358 |
+
if val:
|
| 359 |
+
lines.append(f"### {heading}")
|
| 360 |
+
lines.append(val[0][:limit] + ("β¦" if len(val[0]) > limit else "") + "\n")
|
| 361 |
+
|
| 362 |
+
lines.append("---")
|
| 363 |
+
lines.append(
|
| 364 |
+
"*Data from [OpenFDA](https://open.fda.gov) Β· "
|
| 365 |
+
"For research purposes only Β· Not clinical advice*"
|
| 366 |
+
)
|
| 367 |
+
return "\n".join(lines)
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
# βββ Tab 4: FAERS Adverse Events ββββββββββββββββββββββββββββββββ
|
| 371 |
+
def get_fda_adverse_events(drug_label: str):
|
| 372 |
+
if not drug_label:
|
| 373 |
+
return None, "β οΈ Select a drug first."
|
| 374 |
+
generic = _extract_generic(drug_label)
|
| 375 |
+
data = _openfda("event", {
|
| 376 |
+
"search": f"patient.drug.medicinalproduct:{generic}",
|
| 377 |
+
"count": "patient.reaction.reactionmeddrapt.exact",
|
| 378 |
+
"limit": 15,
|
| 379 |
+
})
|
| 380 |
+
if "error" in data:
|
| 381 |
+
return None, f"### β οΈ FAERS: {data['message']}"
|
| 382 |
+
|
| 383 |
+
results = data.get("results", [])
|
| 384 |
+
if not results:
|
| 385 |
+
return None, f"βΉοΈ No FAERS data for **{generic}**."
|
| 386 |
+
|
| 387 |
+
df = pd.DataFrame(results, columns=["Adverse Reaction", "Report Count"])
|
| 388 |
+
df = df.sort_values("Report Count", ascending=False).reset_index(drop=True)
|
| 389 |
+
df.index = range(1, len(df) + 1)
|
| 390 |
+
df.index.name = "Rank"
|
| 391 |
+
|
| 392 |
+
total = df["Report Count"].sum()
|
| 393 |
+
summary = (
|
| 394 |
+
f"### π FAERS Adverse Events: **{generic.title()}**\n\n"
|
| 395 |
+
f"Top 15 reactions Β· **{total:,} total reports** in FDA database\n\n"
|
| 396 |
+
f"*Source: FDA Adverse Event Reporting System (FAERS) via OpenFDA*"
|
| 397 |
+
)
|
| 398 |
+
return df, summary
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
# βββ Tab 4: NDC Lookup ββββββββββββββββββββββββββββββββββββββββββ
|
| 402 |
+
def get_fda_ndc(drug_label: str):
|
| 403 |
+
if not drug_label:
|
| 404 |
+
return None, "β οΈ Select a drug."
|
| 405 |
+
generic = _extract_generic(drug_label)
|
| 406 |
+
data = _openfda("ndc", {"search": f"generic_name:{generic}", "limit": 10})
|
| 407 |
+
if "error" in data:
|
| 408 |
+
return None, f"### β οΈ NDC: {data['message']}"
|
| 409 |
+
|
| 410 |
+
results = data.get("results", [])
|
| 411 |
+
if not results:
|
| 412 |
+
return None, f"βΉοΈ No NDC data for **{generic}**."
|
| 413 |
+
|
| 414 |
+
rows = [{
|
| 415 |
+
"Brand Name": r.get("brand_name", "β"),
|
| 416 |
+
"Generic Name": r.get("generic_name", "β"),
|
| 417 |
+
"Dosage Form": r.get("dosage_form", "β"),
|
| 418 |
+
"Route": ", ".join(r.get("route", [])),
|
| 419 |
+
"Manufacturer": r.get("labeler_name", "β"),
|
| 420 |
+
"Product Type": r.get("product_type", "β"),
|
| 421 |
+
"NDC Code": r.get("product_ndc", "β"),
|
| 422 |
+
} for r in results]
|
| 423 |
+
|
| 424 |
+
df = pd.DataFrame(rows)
|
| 425 |
+
df.index = range(1, len(df) + 1)
|
| 426 |
+
df.index.name = "#"
|
| 427 |
+
|
| 428 |
+
summary = (
|
| 429 |
+
f"### π·οΈ NDC Registry: **{generic.title()}**\n\n"
|
| 430 |
+
f"**{len(df)} products** in US National Drug Code directory\n\n"
|
| 431 |
+
f"*Source: FDA NDC Database via OpenFDA*"
|
| 432 |
+
)
|
| 433 |
+
return df, summary
|
| 434 |
+
|
| 435 |
+
|
| 436 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 437 |
+
# β¨ CHARTS β 5 unique visuals for the medical system overview tab
|
| 438 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 439 |
+
|
| 440 |
+
# Precompute all chart data at startup (fast, in-memory)
|
| 441 |
+
if MODEL_LOADED and not drug_db.empty:
|
| 442 |
+
_sys_counts = drug_db["medical_system"].value_counts()
|
| 443 |
+
_dosage_top10 = drug_db["dosage_form"].value_counts().head(10)
|
| 444 |
+
_mfr_top15 = drug_db["manufacturer"].value_counts().head(15)
|
| 445 |
+
_sys_dosage = pd.crosstab(drug_db["medical_system"], drug_db["dosage_form"])
|
| 446 |
+
_sys_dosage = _sys_dosage[_dosage_top10.index[:8]]
|
| 447 |
+
else:
|
| 448 |
+
_sys_counts = pd.Series({"No data": 1})
|
| 449 |
+
_dosage_top10 = pd.Series({"No data": 1})
|
| 450 |
+
_mfr_top15 = pd.Series({"No data": 1})
|
| 451 |
+
_sys_dosage = pd.DataFrame()
|
| 452 |
+
|
| 453 |
+
|
| 454 |
+
def _sys_colors(labels):
|
| 455 |
+
return [SYSTEM_COLORS.get(l, "#64748b") for l in labels]
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
# ββ Chart 1: Donut β Drug share per medical system βββββββββββββββ
|
| 459 |
+
def chart_donut():
|
| 460 |
+
labels = _sys_counts.index.tolist()
|
| 461 |
+
values = _sys_counts.values.tolist()
|
| 462 |
+
colors = _sys_colors(labels)
|
| 463 |
+
|
| 464 |
+
fig = go.Figure(go.Pie(
|
| 465 |
+
labels=labels,
|
| 466 |
+
values=values,
|
| 467 |
+
hole=0.55,
|
| 468 |
+
marker=dict(colors=colors, line=dict(color="#ffffff", width=2.5)),
|
| 469 |
+
textinfo="label+percent",
|
| 470 |
+
textfont=dict(size=13),
|
| 471 |
+
hovertemplate="<b>%{label}</b><br>%{value:,} drugs<br>%{percent}<extra></extra>",
|
| 472 |
+
))
|
| 473 |
+
fig.update_layout(
|
| 474 |
+
title=dict(
|
| 475 |
+
text="<b>Drug Distribution Across 5 Medical Systems</b>",
|
| 476 |
+
x=0.5, xanchor="center", font=dict(size=17)
|
| 477 |
+
),
|
| 478 |
+
annotations=[dict(
|
| 479 |
+
text=f"<b>{_sys_counts.sum():,}</b><br>Total Drugs",
|
| 480 |
+
x=0.5, y=0.5, font=dict(size=15), showarrow=False
|
| 481 |
+
)],
|
| 482 |
+
legend=dict(orientation="h", y=-0.08, x=0.5, xanchor="center"),
|
| 483 |
+
height=420,
|
| 484 |
+
margin=dict(t=60, b=40, l=20, r=20),
|
| 485 |
+
paper_bgcolor="white", plot_bgcolor="white",
|
| 486 |
+
)
|
| 487 |
+
return fig
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
# ββ Chart 2: Horizontal bar β Top 10 dosage forms ββββββββββββββββ
|
| 491 |
+
def chart_dosage_bar():
|
| 492 |
+
labels = _dosage_top10.index.tolist()[::-1]
|
| 493 |
+
values = _dosage_top10.values.tolist()[::-1]
|
| 494 |
+
colors = px.colors.sequential.Blues[2:][:len(labels)][::-1]
|
| 495 |
+
|
| 496 |
+
fig = go.Figure(go.Bar(
|
| 497 |
+
y=labels, x=values,
|
| 498 |
+
orientation="h",
|
| 499 |
+
marker=dict(color=colors),
|
| 500 |
+
text=[f" {v:,}" for v in values],
|
| 501 |
+
textposition="outside",
|
| 502 |
+
hovertemplate="<b>%{y}</b>: %{x:,} drugs<extra></extra>",
|
| 503 |
+
))
|
| 504 |
+
fig.update_layout(
|
| 505 |
+
title=dict(
|
| 506 |
+
text="<b>Top 10 Dosage Forms</b>",
|
| 507 |
+
x=0.5, xanchor="center", font=dict(size=17)
|
| 508 |
+
),
|
| 509 |
+
xaxis=dict(title="Number of Drugs", showgrid=True, gridcolor="#f0f0f0"),
|
| 510 |
+
yaxis=dict(title=""),
|
| 511 |
+
height=420,
|
| 512 |
+
margin=dict(t=60, b=40, l=160, r=60),
|
| 513 |
+
paper_bgcolor="white", plot_bgcolor="white",
|
| 514 |
+
)
|
| 515 |
+
return fig
|
| 516 |
+
|
| 517 |
+
|
| 518 |
+
# ββ Chart 3: Grouped bar β Dosage form per system ββββββββββββββββ
|
| 519 |
+
def chart_system_dosage_grouped():
|
| 520 |
+
if _sys_dosage.empty:
|
| 521 |
+
return go.Figure()
|
| 522 |
+
|
| 523 |
+
fig = go.Figure()
|
| 524 |
+
dosage_cols = _sys_dosage.columns.tolist()
|
| 525 |
+
palette = px.colors.qualitative.Pastel[:len(dosage_cols)]
|
| 526 |
+
|
| 527 |
+
for col, color in zip(dosage_cols, palette):
|
| 528 |
+
fig.add_trace(go.Bar(
|
| 529 |
+
name=col,
|
| 530 |
+
x=_sys_dosage.index.tolist(),
|
| 531 |
+
y=_sys_dosage[col].tolist(),
|
| 532 |
+
marker_color=color,
|
| 533 |
+
hovertemplate=f"<b>{col}</b><br>%{{x}}: %{{y:,}}<extra></extra>",
|
| 534 |
+
))
|
| 535 |
+
|
| 536 |
+
fig.update_layout(
|
| 537 |
+
barmode="group",
|
| 538 |
+
title=dict(
|
| 539 |
+
text="<b>Dosage Form Breakdown per Medical System</b>",
|
| 540 |
+
x=0.5, xanchor="center", font=dict(size=17)
|
| 541 |
+
),
|
| 542 |
+
xaxis=dict(title="Medical System"),
|
| 543 |
+
yaxis=dict(title="Drug Count", showgrid=True, gridcolor="#f0f0f0"),
|
| 544 |
+
legend=dict(title="Dosage Form", orientation="h", y=-0.22, x=0.5, xanchor="center"),
|
| 545 |
+
height=460,
|
| 546 |
+
margin=dict(t=60, b=100, l=60, r=20),
|
| 547 |
+
paper_bgcolor="white", plot_bgcolor="white",
|
| 548 |
+
)
|
| 549 |
+
return fig
|
| 550 |
+
|
| 551 |
+
|
| 552 |
+
# ββ Chart 4: Treemap β Manufacturer Γ System βββββββββββββββββββββ
|
| 553 |
+
def chart_treemap():
|
| 554 |
+
top_mfr = drug_db.groupby(["medical_system", "manufacturer"]).size().reset_index(name="count")
|
| 555 |
+
top_mfr = top_mfr.sort_values("count", ascending=False)
|
| 556 |
+
# Keep top 5 manufacturers per system
|
| 557 |
+
top_mfr = top_mfr.groupby("medical_system").head(5).reset_index(drop=True)
|
| 558 |
+
|
| 559 |
+
fig = px.treemap(
|
| 560 |
+
top_mfr,
|
| 561 |
+
path=["medical_system", "manufacturer"],
|
| 562 |
+
values="count",
|
| 563 |
+
color="medical_system",
|
| 564 |
+
color_discrete_map=SYSTEM_COLORS,
|
| 565 |
+
custom_data=["count"],
|
| 566 |
+
)
|
| 567 |
+
fig.update_traces(
|
| 568 |
+
hovertemplate="<b>%{label}</b><br>Products: %{customdata[0]:,}<extra></extra>",
|
| 569 |
+
textfont=dict(size=12),
|
| 570 |
+
)
|
| 571 |
+
fig.update_layout(
|
| 572 |
+
title=dict(
|
| 573 |
+
text="<b>Top Manufacturers by Medical System (Treemap)</b>",
|
| 574 |
+
x=0.5, xanchor="center", font=dict(size=17)
|
| 575 |
+
),
|
| 576 |
+
height=480,
|
| 577 |
+
margin=dict(t=60, b=20, l=20, r=20),
|
| 578 |
+
paper_bgcolor="white",
|
| 579 |
+
)
|
| 580 |
+
return fig
|
| 581 |
+
|
| 582 |
+
|
| 583 |
+
# ββ Chart 5: Radar β System profile across dosage dimensions βββββ
|
| 584 |
+
def chart_radar():
|
| 585 |
+
dosage_categories = ["Tablet", "Capsule", "Liquid", "Injection", "Syrup"]
|
| 586 |
+
available_cats = [c for c in dosage_categories if c in _sys_dosage.columns]
|
| 587 |
+
if not available_cats:
|
| 588 |
+
return go.Figure()
|
| 589 |
+
|
| 590 |
+
sub = _sys_dosage[available_cats]
|
| 591 |
+
# Normalise each system to 0-100
|
| 592 |
+
sub_n = sub.div(sub.max(axis=0), axis=1).fillna(0) * 100
|
| 593 |
+
|
| 594 |
+
fig = go.Figure()
|
| 595 |
+
for system in sub_n.index:
|
| 596 |
+
vals = sub_n.loc[system].tolist()
|
| 597 |
+
color = SYSTEM_COLORS.get(system, "#64748b")
|
| 598 |
+
fig.add_trace(go.Scatterpolar(
|
| 599 |
+
r=vals + [vals[0]],
|
| 600 |
+
theta=available_cats + [available_cats[0]],
|
| 601 |
+
fill="toself",
|
| 602 |
+
fillcolor=color.replace(")", ",0.15)").replace("rgb", "rgba")
|
| 603 |
+
if "rgb" in color else color + "28",
|
| 604 |
+
line=dict(color=color, width=2),
|
| 605 |
+
name=system,
|
| 606 |
+
hovertemplate="<b>" + system + "</b><br>%{theta}: %{r:.0f}%<extra></extra>",
|
| 607 |
+
))
|
| 608 |
+
fig.update_layout(
|
| 609 |
+
polar=dict(
|
| 610 |
+
radialaxis=dict(
|
| 611 |
+
visible=True, range=[0, 110],
|
| 612 |
+
tickfont=dict(size=10), gridcolor="#e5e7eb",
|
| 613 |
+
),
|
| 614 |
+
angularaxis=dict(tickfont=dict(size=12)),
|
| 615 |
+
bgcolor="white",
|
| 616 |
+
),
|
| 617 |
+
title=dict(
|
| 618 |
+
text="<b>Medical System Profile β Dosage Form Radar</b>",
|
| 619 |
+
x=0.5, xanchor="center", font=dict(size=17)
|
| 620 |
+
),
|
| 621 |
+
showlegend=True,
|
| 622 |
+
legend=dict(orientation="h", y=-0.12, x=0.5, xanchor="center"),
|
| 623 |
+
height=460,
|
| 624 |
+
margin=dict(t=60, b=80, l=60, r=60),
|
| 625 |
+
paper_bgcolor="white", plot_bgcolor="white",
|
| 626 |
+
)
|
| 627 |
+
return fig
|
| 628 |
+
|
| 629 |
+
|
| 630 |
+
def build_all_charts():
|
| 631 |
+
"""Called once when the Charts tab is first opened."""
|
| 632 |
+
return (
|
| 633 |
+
chart_donut(),
|
| 634 |
+
chart_dosage_bar(),
|
| 635 |
+
chart_system_dosage_grouped(),
|
| 636 |
+
chart_treemap(),
|
| 637 |
+
chart_radar(),
|
| 638 |
+
)
|
| 639 |
+
|
| 640 |
+
|
| 641 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 642 |
+
# STATS TEXT
|
| 643 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 644 |
+
def get_stats() -> str:
|
| 645 |
if not MODEL_LOADED:
|
| 646 |
return "Models not loaded."
|
| 647 |
+
sys_dist = drug_db["medical_system"].value_counts()
|
| 648 |
+
dosage_dist = drug_db["dosage_form"].value_counts().head(10)
|
| 649 |
+
md = f"""## π Dataset Statistics
|
|
|
|
|
|
|
| 650 |
|
| 651 |
| Metric | Value |
|
| 652 |
|--------|-------|
|
| 653 |
+
| **Total Drugs** | {len(drug_db):,} |
|
| 654 |
+
| **Medical Systems** | {drug_db["medical_system"].nunique()} |
|
| 655 |
+
| **Unique Manufacturers** | {drug_db["manufacturer"].nunique():,} |
|
| 656 |
+
| **Unique Brand Names** | {drug_db["brand_name"].nunique():,} |
|
| 657 |
+
| **TF-IDF Features** | {metadata.get("n_features", 10000):,} |
|
| 658 |
+
| **Silhouette Score** | {metadata.get("silhouette_score", "N/A")} |
|
| 659 |
+
| **Bug Fix Applied** | Non-allopathic brand names excluded from TF-IDF |
|
| 660 |
+
|
| 661 |
+
### π₯ Medical Systems
|
| 662 |
"""
|
| 663 |
+
for s, c in sys_dist.items():
|
| 664 |
+
pct = c / len(drug_db) * 100
|
| 665 |
+
bar = "β" * int(pct / 3)
|
| 666 |
+
md += f"\n- **{s}**: {c:,} ({pct:.1f}%) `{bar}`"
|
| 667 |
+
md += "\n\n### π Top 10 Dosage Forms\n"
|
| 668 |
+
for d, c in dosage_dist.items():
|
| 669 |
+
md += f"\n- {d}: {c:,}"
|
| 670 |
+
return md
|
| 671 |
|
|
|
|
|
|
|
|
|
|
| 672 |
|
| 673 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 674 |
+
# GRADIO UI
|
| 675 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 676 |
|
| 677 |
+
CSS = """
|
| 678 |
+
.gradio-container {
|
| 679 |
+
max-width: 1080px !important;
|
| 680 |
+
margin: auto !important;
|
| 681 |
+
font-family: 'Segoe UI', system-ui, sans-serif !important;
|
| 682 |
+
}
|
| 683 |
+
.hero {
|
| 684 |
+
background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 55%, #0f172a 100%);
|
| 685 |
+
border: 1px solid rgba(99,102,241,0.35);
|
| 686 |
+
border-radius: 16px;
|
| 687 |
+
padding: 28px 32px 22px;
|
| 688 |
+
margin-bottom: 18px;
|
| 689 |
+
text-align: center;
|
| 690 |
+
}
|
| 691 |
+
.sbadge {
|
| 692 |
+
display: inline-block; border-radius: 999px;
|
| 693 |
+
padding: 4px 13px; font-size: 12px; margin: 3px;
|
| 694 |
+
}
|
| 695 |
+
.fix-note {
|
| 696 |
+
background: rgba(34,197,94,0.08);
|
| 697 |
+
border: 1px solid rgba(34,197,94,0.25);
|
| 698 |
+
border-radius: 10px; padding: 11px 16px;
|
| 699 |
+
font-size: 13px; margin: 8px 0 12px;
|
| 700 |
+
}
|
| 701 |
+
footer { display: none !important; }
|
| 702 |
+
"""
|
| 703 |
|
| 704 |
+
HEADER_HTML = """
|
| 705 |
+
<div class="hero">
|
| 706 |
+
<h1 style="color:white;font-size:2em;margin:0 0 8px;font-weight:800;letter-spacing:-0.5px;">
|
| 707 |
+
π Cross-Medical-System Drug Recommender
|
| 708 |
+
</h1>
|
| 709 |
+
<p style="color:#94a3b8;margin:0 0 14px;font-size:1rem;">
|
| 710 |
+
53,581 drugs Β· NLP-Powered Β· Master's Thesis Β· + Live OpenFDA API
|
| 711 |
+
</p>
|
| 712 |
+
<div>
|
| 713 |
+
<span class="sbadge" style="background:rgba(59,130,246,.15);border:1px solid rgba(59,130,246,.3);color:#93c5fd;">π΅ Allopathic 36,251</span>
|
| 714 |
+
<span class="sbadge" style="background:rgba(249,115,22,.12);border:1px solid rgba(249,115,22,.3);color:#fdba74;">π Unani 8,460</span>
|
| 715 |
+
<span class="sbadge" style="background:rgba(34,197,94,.12);border:1px solid rgba(34,197,94,.3);color:#86efac;">π’ Ayurvedic 5,262</span>
|
| 716 |
+
<span class="sbadge" style="background:rgba(168,85,247,.12);border:1px solid rgba(168,85,247,.3);color:#d8b4fe;">π£ Homeopathic 2,580</span>
|
| 717 |
+
<span class="sbadge" style="background:rgba(239,68,68,.1);border:1px solid rgba(239,68,68,.3);color:#fca5a5;">π΄ Herbal 1,028</span>
|
| 718 |
+
<span class="sbadge" style="background:rgba(16,185,129,.1);border:1px solid rgba(16,185,129,.3);color:#6ee7b7;">πΊπΈ + OpenFDA API</span>
|
| 719 |
+
</div>
|
| 720 |
+
</div>
|
| 721 |
+
"""
|
| 722 |
|
| 723 |
+
FIX_NOTE_HTML = """
|
| 724 |
+
<div class="fix-note">
|
| 725 |
+
<strong>β
Bug fix applied:</strong> Non-allopathic drugs (Ayurvedic, Unani, Homeopathic, Herbal)
|
| 726 |
+
no longer appear in Allopathic compound searches.
|
| 727 |
+
Brand names like <em>"Feverfit"</em> or <em>"Paincap"</em> are no longer used as TF-IDF tokens β
|
| 728 |
+
only the pharmaceutical compound name (Generic Name) drives matching for Allopathic drugs.
|
| 729 |
+
</div>
|
| 730 |
+
"""
|
| 731 |
|
| 732 |
+
with gr.Blocks(css=CSS, title="π Drug Recommender v3", theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 733 |
|
| 734 |
+
gr.HTML(HEADER_HTML)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 735 |
|
| 736 |
+
# ββ Global selector β shared by all 4 data tabs ββββββββββββββ
|
| 737 |
+
gr.Markdown("### π Step 1: Select a drug β then use any tab below")
|
| 738 |
+
|
| 739 |
+
with gr.Row():
|
| 740 |
+
with gr.Column(scale=5):
|
| 741 |
+
drug_selector = gr.Dropdown(
|
| 742 |
+
choices=DROPDOWN_LABELS,
|
| 743 |
+
value=DROPDOWN_LABELS[0],
|
| 744 |
+
label="π Select Drug / Category (30 options)",
|
| 745 |
+
info="Antibiotics Β· Pain Β· Heart Β· Diabetes Β· Respiratory Β· Neuro Β· GI Β· Antifungal Β· Vitamins",
|
| 746 |
+
interactive=True,
|
| 747 |
+
)
|
| 748 |
+
with gr.Column(scale=2):
|
| 749 |
+
system_filter = gr.Dropdown(
|
| 750 |
+
choices=MEDICAL_SYSTEMS,
|
| 751 |
+
value="All Systems",
|
| 752 |
+
label="π₯ Medical System Filter",
|
| 753 |
+
info="Optional β narrows results",
|
| 754 |
+
)
|
| 755 |
|
| 756 |
gr.HTML("""
|
| 757 |
+
<div style="background:#f8fafc;border:1px solid #e2e8f0;border-radius:10px;
|
| 758 |
+
padding:11px 16px;font-size:13px;margin:6px 0 14px;color:#475569;">
|
| 759 |
+
<strong>Two data sources:</strong>
|
| 760 |
+
Tabs 1β2 query your <strong>local PKL model</strong> (53k drugs). |
|
| 761 |
+
Tabs 3β4 call <strong>OpenFDA live API</strong> for real-time FDA data.
|
| 762 |
+
Both use the same dropdown above.
|
| 763 |
</div>
|
| 764 |
""")
|
| 765 |
|
| 766 |
with gr.Tabs():
|
| 767 |
|
| 768 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 769 |
+
# TAB 1 β Dataset Recommendations
|
| 770 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 771 |
+
with gr.TabItem("π Dataset Recommendations"):
|
| 772 |
+
gr.HTML(FIX_NOTE_HTML)
|
| 773 |
+
gr.Markdown("""
|
| 774 |
+
Finds similar drugs using **TF-IDF cosine similarity** across all 53,581 records.
|
| 775 |
+
The dropdown selection maps to a precise search query β no typing needed.
|
| 776 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 777 |
|
| 778 |
with gr.Row():
|
| 779 |
+
top_n = gr.Slider(3, 25, value=10, step=1,
|
| 780 |
+
label="π Number of Results")
|
| 781 |
+
min_score = gr.Slider(0.01, 0.50, value=0.05, step=0.01,
|
| 782 |
+
label="π― Min Similarity Score")
|
| 783 |
+
|
| 784 |
+
rec_btn = gr.Button("π Get Recommendations", variant="primary", size="lg")
|
| 785 |
+
rec_summary = gr.Markdown()
|
| 786 |
+
rec_table = gr.DataFrame(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 787 |
label="π Recommended Drugs",
|
| 788 |
+
wrap=True, interactive=False,
|
|
|
|
| 789 |
)
|
| 790 |
|
| 791 |
+
rec_btn.click(
|
| 792 |
+
fn=recommend_from_selection,
|
| 793 |
+
inputs=[drug_selector, system_filter, top_n, min_score],
|
| 794 |
+
outputs=[rec_table, rec_summary],
|
| 795 |
)
|
| 796 |
|
| 797 |
+
gr.Markdown("""
|
| 798 |
+
---
|
| 799 |
+
**How matching works per system:**
|
| 800 |
+
- **Allopathic** β matched by *Generic Name* compound (e.g. "Paracetamol") β
|
| 801 |
+
- **Ayurvedic / Unani / Homeopathic / Herbal** β matched by *dosage form + system*
|
| 802 |
+
(no generic compound data exists in this dataset for these systems)
|
| 803 |
+
""")
|
| 804 |
|
| 805 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 806 |
+
# TAB 2 β Cross-System Comparison
|
| 807 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 808 |
+
with gr.TabItem("π Cross-System Compare"):
|
| 809 |
gr.Markdown("""
|
| 810 |
+
### π Core Thesis Feature
|
| 811 |
+
Best results from **every medical tradition** side by side.
|
| 812 |
+
Bridges Allopathic β Ayurvedic β Unani β Homeopathic β Herbal.
|
| 813 |
""")
|
| 814 |
|
| 815 |
+
top_per_sys = gr.Slider(1, 5, value=3, step=1,
|
| 816 |
+
label="Results per Medical System")
|
| 817 |
+
compare_btn = gr.Button("π Compare All 5 Systems",
|
| 818 |
+
variant="primary", size="lg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
cross_summary = gr.Markdown()
|
| 820 |
+
cross_table = gr.DataFrame(
|
| 821 |
+
label="π All 5 Medical Systems β Side by Side",
|
| 822 |
+
wrap=True, interactive=False,
|
|
|
|
|
|
|
|
|
|
| 823 |
)
|
| 824 |
|
| 825 |
compare_btn.click(
|
| 826 |
fn=cross_system_compare,
|
| 827 |
+
inputs=[drug_selector, top_per_sys],
|
| 828 |
+
outputs=[cross_table, cross_summary],
|
| 829 |
)
|
| 830 |
|
| 831 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 832 |
+
# TAB 3 β OpenFDA Drug Label (Live)
|
| 833 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 834 |
+
with gr.TabItem("πΊπΈ FDA Label (Live)"):
|
| 835 |
+
gr.Markdown("""
|
| 836 |
+
### Official FDA Drug Label β fetched live from OpenFDA
|
| 837 |
+
Returns indications, warnings, dosage, adverse reactions, and drug interactions
|
| 838 |
+
directly from the US Food & Drug Administration.
|
| 839 |
+
|
| 840 |
+
> π **API:** [OpenFDA /drug/label](https://open.fda.gov/apis/drug/label/) Β· Free Β· No key required
|
| 841 |
+
""")
|
| 842 |
+
|
| 843 |
+
fda_label_btn = gr.Button("π Fetch FDA Drug Label",
|
| 844 |
+
variant="primary", size="lg")
|
| 845 |
+
fda_label_result = gr.Markdown()
|
| 846 |
+
|
| 847 |
+
fda_label_btn.click(
|
| 848 |
+
fn=get_fda_label,
|
| 849 |
+
inputs=[drug_selector],
|
| 850 |
+
outputs=[fda_label_result],
|
| 851 |
+
)
|
| 852 |
|
|
|
|
|
|
|
| 853 |
gr.Markdown("""
|
| 854 |
+
---
|
| 855 |
+
β οΈ *OpenFDA covers US-approved drugs. Bangladesh dataset drugs may use
|
| 856 |
+
different brand names or may not be in FDA records β this is expected.*
|
| 857 |
+
""")
|
| 858 |
+
|
| 859 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 860 |
+
# TAB 4 β FDA Adverse Events + NDC (Live)
|
| 861 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 862 |
+
with gr.TabItem("β οΈ Adverse Events + NDC (Live)"):
|
| 863 |
+
gr.Markdown("""
|
| 864 |
+
### FDA FAERS Adverse Events + National Drug Code Registry
|
| 865 |
+
- **FAERS** β real patient-reported side effects from millions of reports
|
| 866 |
+
- **NDC** β manufacturer, packaging, and product type data
|
| 867 |
+
""")
|
| 868 |
+
|
| 869 |
+
with gr.Row():
|
| 870 |
+
ae_btn = gr.Button("π Fetch Adverse Events (FAERS)",
|
| 871 |
+
variant="primary")
|
| 872 |
+
ndc_btn = gr.Button("π·οΈ Lookup NDC Directory",
|
| 873 |
+
variant="secondary")
|
| 874 |
+
|
| 875 |
+
ae_summary = gr.Markdown()
|
| 876 |
+
ae_table = gr.DataFrame(
|
| 877 |
+
label="β οΈ Top Adverse Reactions (Real FDA Data)",
|
| 878 |
+
wrap=True, interactive=False,
|
| 879 |
+
)
|
| 880 |
+
|
| 881 |
+
gr.HTML("<hr style='margin:14px 0;border-color:#e2e8f0;'>")
|
| 882 |
+
|
| 883 |
+
ndc_summary = gr.Markdown()
|
| 884 |
+
ndc_table = gr.DataFrame(
|
| 885 |
+
label="π·οΈ NDC Product Registry",
|
| 886 |
+
wrap=True, interactive=False,
|
| 887 |
+
)
|
| 888 |
+
|
| 889 |
+
ae_btn.click(
|
| 890 |
+
fn=get_fda_adverse_events,
|
| 891 |
+
inputs=[drug_selector],
|
| 892 |
+
outputs=[ae_table, ae_summary],
|
| 893 |
+
)
|
| 894 |
+
ndc_btn.click(
|
| 895 |
+
fn=get_fda_ndc,
|
| 896 |
+
inputs=[drug_selector],
|
| 897 |
+
outputs=[ndc_table, ndc_summary],
|
| 898 |
+
)
|
| 899 |
+
|
| 900 |
+
gr.Markdown("""
|
| 901 |
+
---
|
| 902 |
+
> **APIs:** [OpenFDA FAERS](https://open.fda.gov/apis/drug/event/) Β·
|
| 903 |
+
> [OpenFDA NDC](https://open.fda.gov/apis/drug/ndc/) Β· Both free, no key.
|
| 904 |
+
""")
|
| 905 |
+
|
| 906 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 907 |
+
# TAB 5 β Visual Charts (5 unique plots)
|
| 908 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 909 |
+
with gr.TabItem("π Visual Charts"):
|
| 910 |
+
gr.Markdown("""
|
| 911 |
+
### π Five unique visualisations of the 53,581-drug dataset
|
| 912 |
+
Click **Load All Charts** to render the full dashboard.
|
| 913 |
+
""")
|
| 914 |
+
|
| 915 |
+
load_charts_btn = gr.Button("π Load All Charts",
|
| 916 |
+
variant="primary", size="lg")
|
| 917 |
|
| 918 |
+
with gr.Row():
|
| 919 |
+
p1 = gr.Plot(label="β Drug Share by Medical System (Donut)")
|
| 920 |
+
p2 = gr.Plot(label="β‘ Top 10 Dosage Forms (Bar)")
|
| 921 |
|
| 922 |
+
with gr.Row():
|
| 923 |
+
p3 = gr.Plot(label="β’ Dosage Form per System (Grouped Bar)")
|
| 924 |
+
p4 = gr.Plot(label="β£ Top Manufacturers Treemap")
|
|
|
|
| 925 |
|
| 926 |
+
with gr.Row():
|
| 927 |
+
p5 = gr.Plot(label="β€ System Profile β Dosage Radar")
|
| 928 |
+
|
| 929 |
+
load_charts_btn.click(
|
| 930 |
+
fn=build_all_charts,
|
| 931 |
+
inputs=[],
|
| 932 |
+
outputs=[p1, p2, p3, p4, p5],
|
| 933 |
+
)
|
| 934 |
+
|
| 935 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 936 |
+
# TAB 6 β Stats text
|
| 937 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 938 |
+
with gr.TabItem("π Dataset Stats"):
|
| 939 |
+
load_stats_btn = gr.Button("π Load Statistics", variant="secondary")
|
| 940 |
+
stats_output = gr.Markdown()
|
| 941 |
+
load_stats_btn.click(fn=get_stats, inputs=[], outputs=[stats_output])
|
| 942 |
+
|
| 943 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 944 |
+
# TAB 7 β About / Code Reference
|
| 945 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 946 |
+
with gr.TabItem("π About / Code"):
|
| 947 |
+
gr.Markdown("""
|
| 948 |
+
## π About This Project
|
| 949 |
+
|
| 950 |
+
**Thesis:** Intelligent Cross-Medical-System Drug Recommendation Using NLP
|
| 951 |
+
|
| 952 |
+
### Where the key code changes live in `app.py`
|
| 953 |
+
|
| 954 |
+
| What changed | Function / location |
|
| 955 |
+
|---|---|
|
| 956 |
+
| β
Bug fix β non-allopathic brand name excluded | `build_drug_text()` ~line 100 |
|
| 957 |
+
| β¨ 30-option dropdown | `DRUG_OPTIONS` dict ~line 60 |
|
| 958 |
+
| πΊπΈ FDA Drug Label API | `get_fda_label()` |
|
| 959 |
+
| β οΈ FDA FAERS adverse events | `get_fda_adverse_events()` |
|
| 960 |
+
| π·οΈ FDA NDC lookup | `get_fda_ndc()` |
|
| 961 |
+
| π Donut chart | `chart_donut()` |
|
| 962 |
+
| π Bar chart | `chart_dosage_bar()` |
|
| 963 |
+
| π Grouped bar | `chart_system_dosage_grouped()` |
|
| 964 |
+
| π Treemap | `chart_treemap()` |
|
| 965 |
+
| π Radar chart | `chart_radar()` |
|
| 966 |
+
|
| 967 |
+
### Technical Stack
|
| 968 |
+
|
| 969 |
+
| Layer | Technology |
|
| 970 |
+
|---|---|
|
| 971 |
+
| NLP | TF-IDF bigrams, 10,000 features, sublinear TF |
|
| 972 |
+
| Similarity | Cosine Similarity |
|
| 973 |
+
| Clustering | SVD (50d) + K-Means (K=10) |
|
| 974 |
+
| External APIs | OpenFDA label Β· FAERS Β· NDC |
|
| 975 |
+
| Visualisation | Plotly (donut, bar, grouped bar, treemap, radar) |
|
| 976 |
+
| Deployment | Hugging Face Spaces Β· Gradio 4 |
|
| 977 |
|
| 978 |
### Dataset
|
| 979 |
+
53,581 records Β· Bangladesh National Drug Registry Β·
|
| 980 |
+
[Kaggle link](https://www.kaggle.com/datasets/shuvokumarbasak2030/drug-pharma-new-dataset)
|
| 981 |
+
|
| 982 |
+
---
|
| 983 |
+
β οΈ *Research and educational purposes only. Not clinical advice.*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 984 |
""")
|
| 985 |
|
| 986 |
gr.HTML("""
|
| 987 |
+
<div style="text-align:center;padding:14px;color:#94a3b8;font-size:12px;
|
| 988 |
+
border-top:1px solid #e2e8f0;margin-top:12px;">
|
| 989 |
+
π Cross-Medical-System Drug Recommender v3.0 Β· Master's Thesis Β·
|
| 990 |
+
53,581 drugs Β· TF-IDF + Cosine Similarity Β· OpenFDA API Β· Plotly Charts
|
| 991 |
</div>
|
| 992 |
""")
|
| 993 |
|
| 994 |
+
|
| 995 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 996 |
if __name__ == "__main__":
|
| 997 |
+
demo.launch(server_name="0.0.0.0", server_port=7860,
|
| 998 |
+
share=False, show_error=True)
|