Spaces:
Sleeping
Sleeping
You need to use YOUR ORIGINAL app.py that works with ChromaDB!
Browse files
app.py
CHANGED
|
@@ -1,436 +1,189 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
from
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 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 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
html += f"""
|
| 192 |
-
<div style="background: {color}; padding: 12px; margin: 8px 0; border-radius: 5px; border: 1px solid #ccc;">
|
| 193 |
-
{icon} <strong>{label}.</strong> {value}
|
| 194 |
-
</div>
|
| 195 |
-
"""
|
| 196 |
-
|
| 197 |
-
html += "</div>"
|
| 198 |
-
|
| 199 |
-
# Show correct answer
|
| 200 |
-
html += f"""
|
| 201 |
-
<div style="background: #d4edda; padding: 15px; border-radius: 8px; margin-bottom: 20px; border-left: 4px solid #28a745;">
|
| 202 |
-
<h3 style="margin-top: 0;">β
Correct Answer</h3>
|
| 203 |
-
<p style="font-size: 18px; font-weight: bold; margin: 0;">{correct_answer}</p>
|
| 204 |
-
</div>
|
| 205 |
-
"""
|
| 206 |
-
|
| 207 |
-
# Show metamap if available
|
| 208 |
-
metamap = q.get('metamap_phrases')
|
| 209 |
-
if metamap:
|
| 210 |
-
html += f"""
|
| 211 |
-
<div style="background: #fff3cd; padding: 15px; border-radius: 8px; border-left: 4px solid #ffc107;">
|
| 212 |
-
<h3 style="margin-top: 0;">π₯ Medical Concepts (MetaMap)</h3>
|
| 213 |
-
<p style="line-height: 1.6;">{', '.join(metamap)}</p>
|
| 214 |
-
</div>
|
| 215 |
-
"""
|
| 216 |
-
|
| 217 |
-
return html
|
| 218 |
-
|
| 219 |
-
def browse_questions(dataset: str, index: int) -> Tuple[str, str]:
|
| 220 |
-
"""Browse questions by index"""
|
| 221 |
-
total = len(db.data.get(dataset, []))
|
| 222 |
-
|
| 223 |
-
if total == 0:
|
| 224 |
-
return "β No questions in this dataset", f"Dataset: {dataset} (empty)"
|
| 225 |
-
|
| 226 |
-
# Clamp index to valid range
|
| 227 |
-
index = max(0, min(index, total - 1))
|
| 228 |
-
|
| 229 |
-
question = db.get_question(dataset, index)
|
| 230 |
-
html = format_question_display(question, dataset)
|
| 231 |
-
info = f"π Question {index + 1} of {total} | Dataset: {dataset}"
|
| 232 |
-
|
| 233 |
-
return html, info
|
| 234 |
-
|
| 235 |
-
def random_question(dataset: str) -> Tuple[str, str, int]:
|
| 236 |
-
"""Get a random question"""
|
| 237 |
-
total = len(db.data.get(dataset, []))
|
| 238 |
-
|
| 239 |
-
if total == 0:
|
| 240 |
-
return "β No questions in this dataset", f"Dataset: {dataset} (empty)", 0
|
| 241 |
-
|
| 242 |
-
index = random.randint(0, total - 1)
|
| 243 |
-
question = db.get_question(dataset, index)
|
| 244 |
-
html = format_question_display(question, dataset)
|
| 245 |
-
info = f"π² Random Question {index + 1} of {total} | Dataset: {dataset}"
|
| 246 |
-
|
| 247 |
-
return html, info, index
|
| 248 |
-
|
| 249 |
-
def search_interface(query: str, dataset: str) -> str:
|
| 250 |
-
"""Search interface"""
|
| 251 |
-
if not query.strip():
|
| 252 |
-
return "π‘ Enter a search query to find questions"
|
| 253 |
-
|
| 254 |
-
results = db.search_questions(query, dataset)
|
| 255 |
-
|
| 256 |
-
if not results:
|
| 257 |
-
return f"β No results found for '{query}' in {dataset}"
|
| 258 |
-
|
| 259 |
-
html = f"""
|
| 260 |
-
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 20px; border-radius: 10px; margin-bottom: 20px;">
|
| 261 |
-
<h2 style="color: white; margin: 0;">π Search Results: "{query}"</h2>
|
| 262 |
-
<p style="color: white; margin: 5px 0 0 0;">Found {len(results)} results in {dataset}</p>
|
| 263 |
-
</div>
|
| 264 |
-
"""
|
| 265 |
-
|
| 266 |
-
for ds, idx, preview in results[:20]: # Show top 20
|
| 267 |
-
dataset_name = ds.replace('_', ' ').title()
|
| 268 |
-
html += f"""
|
| 269 |
-
<div style="background: #fff; padding: 15px; margin: 10px 0; border-radius: 8px; border-left: 4px solid #667eea;">
|
| 270 |
-
<p style="margin: 0; color: #666; font-size: 12px;"><strong>{dataset_name}</strong> - Question #{idx + 1}</p>
|
| 271 |
-
<p style="margin: 5px 0 0 0;">{preview}</p>
|
| 272 |
-
</div>
|
| 273 |
-
"""
|
| 274 |
-
|
| 275 |
-
if len(results) > 20:
|
| 276 |
-
html += f"<p>... and {len(results) - 20} more results</p>"
|
| 277 |
-
|
| 278 |
-
return html
|
| 279 |
-
|
| 280 |
-
# ============================================================================
|
| 281 |
-
# GRADIO APP
|
| 282 |
-
# ============================================================================
|
| 283 |
-
|
| 284 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="MedQA Database Explorer") as app:
|
| 285 |
-
|
| 286 |
-
gr.Markdown("""
|
| 287 |
-
# π₯ MedQA Database Explorer
|
| 288 |
-
|
| 289 |
-
Explore medical question-answering databases including **Med-Gemini** and **MedQA USMLE**.
|
| 290 |
-
""")
|
| 291 |
-
|
| 292 |
-
# Statistics
|
| 293 |
-
with gr.Accordion("π Database Statistics", open=False):
|
| 294 |
-
gr.Markdown(db.get_stats())
|
| 295 |
-
|
| 296 |
-
# Main interface
|
| 297 |
-
with gr.Tabs():
|
| 298 |
-
|
| 299 |
-
# Browse Tab
|
| 300 |
-
with gr.Tab("π Browse Questions"):
|
| 301 |
-
with gr.Row():
|
| 302 |
-
with gr.Column(scale=1):
|
| 303 |
-
dataset_dropdown = gr.Dropdown(
|
| 304 |
-
choices=['medgemini', 'medqa_train', 'medqa_dev', 'medqa_test'],
|
| 305 |
-
value='medgemini',
|
| 306 |
-
label="Select Database"
|
| 307 |
-
)
|
| 308 |
-
|
| 309 |
-
question_slider = gr.Slider(
|
| 310 |
-
minimum=0,
|
| 311 |
-
maximum=len(db.data['medgemini']) - 1,
|
| 312 |
-
value=0,
|
| 313 |
-
step=1,
|
| 314 |
-
label="Question Number"
|
| 315 |
-
)
|
| 316 |
-
|
| 317 |
-
with gr.Row():
|
| 318 |
-
prev_btn = gr.Button("β¬
οΈ Previous", size="sm")
|
| 319 |
-
random_btn = gr.Button("π² Random", size="sm", variant="primary")
|
| 320 |
-
next_btn = gr.Button("Next β‘οΈ", size="sm")
|
| 321 |
-
|
| 322 |
-
info_text = gr.Textbox(label="Info", interactive=False)
|
| 323 |
-
|
| 324 |
-
with gr.Column(scale=2):
|
| 325 |
-
question_display = gr.HTML()
|
| 326 |
-
|
| 327 |
-
# Update slider max when dataset changes
|
| 328 |
-
def update_slider(dataset):
|
| 329 |
-
max_val = len(db.data.get(dataset, [])) - 1
|
| 330 |
-
return gr.Slider(maximum=max_val, value=0)
|
| 331 |
-
|
| 332 |
-
dataset_dropdown.change(
|
| 333 |
-
fn=update_slider,
|
| 334 |
-
inputs=[dataset_dropdown],
|
| 335 |
-
outputs=[question_slider]
|
| 336 |
-
)
|
| 337 |
-
|
| 338 |
-
# Browse functions
|
| 339 |
-
def show_question(dataset, index):
|
| 340 |
-
return browse_questions(dataset, int(index))
|
| 341 |
-
|
| 342 |
-
question_slider.change(
|
| 343 |
-
fn=show_question,
|
| 344 |
-
inputs=[dataset_dropdown, question_slider],
|
| 345 |
-
outputs=[question_display, info_text]
|
| 346 |
-
)
|
| 347 |
-
|
| 348 |
-
dataset_dropdown.change(
|
| 349 |
-
fn=show_question,
|
| 350 |
-
inputs=[dataset_dropdown, question_slider],
|
| 351 |
-
outputs=[question_display, info_text]
|
| 352 |
-
)
|
| 353 |
-
|
| 354 |
-
# Navigation buttons
|
| 355 |
-
def prev_question(dataset, index):
|
| 356 |
-
new_index = max(0, int(index) - 1)
|
| 357 |
-
html, info = browse_questions(dataset, new_index)
|
| 358 |
-
return html, info, new_index
|
| 359 |
-
|
| 360 |
-
def next_question(dataset, index):
|
| 361 |
-
max_idx = len(db.data.get(dataset, [])) - 1
|
| 362 |
-
new_index = min(max_idx, int(index) + 1)
|
| 363 |
-
html, info = browse_questions(dataset, new_index)
|
| 364 |
-
return html, info, new_index
|
| 365 |
-
|
| 366 |
-
prev_btn.click(
|
| 367 |
-
fn=prev_question,
|
| 368 |
-
inputs=[dataset_dropdown, question_slider],
|
| 369 |
-
outputs=[question_display, info_text, question_slider]
|
| 370 |
-
)
|
| 371 |
-
|
| 372 |
-
next_btn.click(
|
| 373 |
-
fn=next_question,
|
| 374 |
-
inputs=[dataset_dropdown, question_slider],
|
| 375 |
-
outputs=[question_display, info_text, question_slider]
|
| 376 |
-
)
|
| 377 |
-
|
| 378 |
-
random_btn.click(
|
| 379 |
-
fn=random_question,
|
| 380 |
-
inputs=[dataset_dropdown],
|
| 381 |
-
outputs=[question_display, info_text, question_slider]
|
| 382 |
-
)
|
| 383 |
-
|
| 384 |
-
# Load first question on start
|
| 385 |
-
app.load(
|
| 386 |
-
fn=show_question,
|
| 387 |
-
inputs=[dataset_dropdown, question_slider],
|
| 388 |
-
outputs=[question_display, info_text]
|
| 389 |
-
)
|
| 390 |
-
|
| 391 |
-
# Search Tab
|
| 392 |
-
with gr.Tab("π Search"):
|
| 393 |
-
with gr.Row():
|
| 394 |
-
search_query = gr.Textbox(
|
| 395 |
-
label="Search Query",
|
| 396 |
-
placeholder="Enter keywords (e.g., 'diabetes', 'heart failure', 'treatment')...",
|
| 397 |
-
scale=3
|
| 398 |
-
)
|
| 399 |
-
search_dataset = gr.Dropdown(
|
| 400 |
-
choices=['all', 'medgemini', 'medqa_train', 'medqa_dev', 'medqa_test'],
|
| 401 |
-
value='all',
|
| 402 |
-
label="Search In",
|
| 403 |
-
scale=1
|
| 404 |
-
)
|
| 405 |
-
|
| 406 |
-
search_btn = gr.Button("π Search", variant="primary")
|
| 407 |
-
search_results = gr.HTML()
|
| 408 |
-
|
| 409 |
-
search_btn.click(
|
| 410 |
-
fn=search_interface,
|
| 411 |
-
inputs=[search_query, search_dataset],
|
| 412 |
-
outputs=[search_results]
|
| 413 |
-
)
|
| 414 |
-
|
| 415 |
-
# Also search on Enter key
|
| 416 |
-
search_query.submit(
|
| 417 |
-
fn=search_interface,
|
| 418 |
-
inputs=[search_query, search_dataset],
|
| 419 |
-
outputs=[search_results]
|
| 420 |
-
)
|
| 421 |
-
|
| 422 |
-
gr.Markdown("""
|
| 423 |
-
---
|
| 424 |
-
### π About the Databases
|
| 425 |
-
|
| 426 |
-
**Med-Gemini**: Expert-relabeled medical questions with detailed explanations from Google's Med-Gemini project.
|
| 427 |
-
|
| 428 |
-
**MedQA**: Original USMLE-style medical questions from the MedQA dataset.
|
| 429 |
-
|
| 430 |
-
### π Sources
|
| 431 |
-
- [Med-Gemini Paper](https://arxiv.org/abs/2404.18416)
|
| 432 |
-
- [MedQA Dataset](https://github.com/jind11/MedQA)
|
| 433 |
-
""")
|
| 434 |
-
|
| 435 |
-
if __name__ == "__main__":
|
| 436 |
-
app.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ['ANONYMIZED_TELEMETRY'] = 'False'
|
| 3 |
+
|
| 4 |
+
import zipfile
|
| 5 |
+
import chromadb
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from fastapi import FastAPI
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
+
|
| 11 |
+
# Extract and load database
|
| 12 |
+
DB_PATH = "./medqa_db"
|
| 13 |
+
if not os.path.exists(DB_PATH) and os.path.exists("./medqa_db.zip"):
|
| 14 |
+
print("π¦ Extracting database...")
|
| 15 |
+
with zipfile.ZipFile("./medqa_db.zip", 'r') as z:
|
| 16 |
+
z.extractall(".")
|
| 17 |
+
print("β
Database extracted")
|
| 18 |
+
|
| 19 |
+
print("π Loading ChromaDB...")
|
| 20 |
+
client = chromadb.PersistentClient(path=DB_PATH)
|
| 21 |
+
collection = client.get_collection("medqa")
|
| 22 |
+
print(f"β
Loaded {collection.count()} questions")
|
| 23 |
+
|
| 24 |
+
print("π§ Loading MedCPT model...")
|
| 25 |
+
model = SentenceTransformer('ncbi/MedCPT-Query-Encoder')
|
| 26 |
+
print("β
Model ready")
|
| 27 |
+
|
| 28 |
+
# Search function
|
| 29 |
+
def search(query, num_results=3, source_filter=None):
|
| 30 |
+
emb = model.encode(query).tolist()
|
| 31 |
+
|
| 32 |
+
# Apply source filter if specified
|
| 33 |
+
where_clause = None
|
| 34 |
+
if source_filter and source_filter != "all":
|
| 35 |
+
where_clause = {"source": source_filter}
|
| 36 |
+
|
| 37 |
+
return collection.query(
|
| 38 |
+
query_embeddings=[emb],
|
| 39 |
+
n_results=int(num_results),
|
| 40 |
+
where=where_clause
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Enhanced Gradio UI
|
| 44 |
+
def ui_search(query, num_results=3, source_filter="all"):
|
| 45 |
+
if not query.strip():
|
| 46 |
+
return "π‘ Enter a medical query to search"
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
r = search(query, num_results, source_filter if source_filter != "all" else None)
|
| 50 |
+
|
| 51 |
+
if not r['documents'][0]:
|
| 52 |
+
return "β No results found"
|
| 53 |
+
|
| 54 |
+
out = f"π Found {len(r['documents'][0])} results\n\n"
|
| 55 |
+
|
| 56 |
+
for i in range(len(r['documents'][0])):
|
| 57 |
+
source = r['metadatas'][0][i].get('source', 'unknown')
|
| 58 |
+
distance = r['distances'][0][i]
|
| 59 |
+
similarity = 1 - distance
|
| 60 |
+
|
| 61 |
+
# Source emoji
|
| 62 |
+
if source == 'medgemini':
|
| 63 |
+
source_icon = "π¬"
|
| 64 |
+
source_name = "Med-Gemini"
|
| 65 |
+
elif source.startswith('medqa_'):
|
| 66 |
+
source_icon = "π"
|
| 67 |
+
split = source.replace('medqa_', '').upper()
|
| 68 |
+
source_name = f"MedQA {split}"
|
| 69 |
+
else:
|
| 70 |
+
source_icon = "π"
|
| 71 |
+
source_name = source.upper()
|
| 72 |
+
|
| 73 |
+
out += f"\n{'='*70}\n"
|
| 74 |
+
out += f"{source_icon} Result {i+1} | {source_name} | Similarity: {similarity:.3f}\n"
|
| 75 |
+
out += f"{'='*70}\n\n"
|
| 76 |
+
out += r['documents'][0][i]
|
| 77 |
+
|
| 78 |
+
# Show answer
|
| 79 |
+
answer = r['metadatas'][0][i].get('answer', 'N/A')
|
| 80 |
+
out += f"\n\nβ
CORRECT ANSWER: {answer}\n"
|
| 81 |
+
|
| 82 |
+
# Show explanation if available (Med-Gemini)
|
| 83 |
+
explanation = r['metadatas'][0][i].get('explanation', '')
|
| 84 |
+
if explanation and explanation.strip():
|
| 85 |
+
out += f"\nπ‘ EXPLANATION:\n{explanation}\n"
|
| 86 |
+
|
| 87 |
+
out += "\n"
|
| 88 |
+
|
| 89 |
+
return out
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"β Error: {e}"
|
| 93 |
+
|
| 94 |
+
# Create Gradio interface
|
| 95 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="MedQA Search") as demo:
|
| 96 |
+
gr.Markdown("""
|
| 97 |
+
# π₯ MedQA Semantic Search
|
| 98 |
+
|
| 99 |
+
Search across **Med-Gemini** (expert explanations) and **MedQA** (USMLE questions) databases.
|
| 100 |
+
Uses medical-specific embeddings (MedCPT) for accurate retrieval.
|
| 101 |
+
""")
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
with gr.Column(scale=3):
|
| 105 |
+
query_input = gr.Textbox(
|
| 106 |
+
label="Medical Query",
|
| 107 |
+
placeholder="e.g., hyponatremia, myocardial infarction, diabetes management...",
|
| 108 |
+
lines=2
|
| 109 |
+
)
|
| 110 |
+
with gr.Column(scale=1):
|
| 111 |
+
num_results = gr.Slider(
|
| 112 |
+
minimum=1,
|
| 113 |
+
maximum=10,
|
| 114 |
+
value=3,
|
| 115 |
+
step=1,
|
| 116 |
+
label="Number of Results"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
source_filter = gr.Radio(
|
| 121 |
+
choices=["all", "medgemini", "medqa_train", "medqa_dev", "medqa_test"],
|
| 122 |
+
value="all",
|
| 123 |
+
label="Filter by Source"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
search_btn = gr.Button("π Search", variant="primary", size="lg")
|
| 127 |
+
|
| 128 |
+
output = gr.Textbox(
|
| 129 |
+
label="Search Results",
|
| 130 |
+
lines=25,
|
| 131 |
+
max_lines=50
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
search_btn.click(
|
| 135 |
+
fn=ui_search,
|
| 136 |
+
inputs=[query_input, num_results, source_filter],
|
| 137 |
+
outputs=output
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
query_input.submit(
|
| 141 |
+
fn=ui_search,
|
| 142 |
+
inputs=[query_input, num_results, source_filter],
|
| 143 |
+
outputs=output
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
gr.Markdown("""
|
| 147 |
+
### π Database Info
|
| 148 |
+
|
| 149 |
+
**Med-Gemini**: Expert-relabeled questions with detailed explanations
|
| 150 |
+
**MedQA**: USMLE-style questions (Train/Dev/Test splits)
|
| 151 |
+
|
| 152 |
+
**Total Questions**: Use the database you built with `build_combined_db.py`
|
| 153 |
+
""")
|
| 154 |
+
|
| 155 |
+
gr.Examples(
|
| 156 |
+
examples=[
|
| 157 |
+
["hyponatremia", 3, "all"],
|
| 158 |
+
["myocardial infarction treatment", 2, "medgemini"],
|
| 159 |
+
["diabetes complications", 3, "all"],
|
| 160 |
+
["antibiotics for pneumonia", 2, "medqa_train"]
|
| 161 |
+
],
|
| 162 |
+
inputs=[query_input, num_results, source_filter]
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# FastAPI
|
| 166 |
+
app = FastAPI()
|
| 167 |
+
|
| 168 |
+
class SearchRequest(BaseModel):
|
| 169 |
+
query: str
|
| 170 |
+
num_results: int = 3
|
| 171 |
+
source_filter: str = None
|
| 172 |
+
|
| 173 |
+
@app.post("/search_medqa")
|
| 174 |
+
def api_search(req: SearchRequest):
|
| 175 |
+
r = search(req.query, req.num_results, req.source_filter)
|
| 176 |
+
return {"results": [{
|
| 177 |
+
"result_number": i+1,
|
| 178 |
+
"question": r['documents'][0][i],
|
| 179 |
+
"answer": r['metadatas'][0][i].get('answer', 'N/A'),
|
| 180 |
+
"source": r['metadatas'][0][i].get('source', 'unknown'),
|
| 181 |
+
"similarity": 1 - r['distances'][0][i]
|
| 182 |
+
} for i in range(len(r['documents'][0]))]}
|
| 183 |
+
|
| 184 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 185 |
+
|
| 186 |
+
# Launch
|
| 187 |
+
if __name__ == "__main__":
|
| 188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