Upload backend.py with huggingface_hub
Browse files- backend.py +23 -2
backend.py
CHANGED
|
@@ -315,6 +315,12 @@ async def analyze_xray(
|
|
| 315 |
threshold: float = Form(0.5),
|
| 316 |
age_group: str = Form("Adult (18-64)"),
|
| 317 |
force_offline: bool = Form(False),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
explainer: MistralExplainer = Depends(get_explainer),
|
| 319 |
request_id: str = Depends(get_request_id),
|
| 320 |
client_ip: str = Depends(check_rate_limit)
|
|
@@ -330,7 +336,8 @@ async def analyze_xray(
|
|
| 330 |
"""
|
| 331 |
|
| 332 |
logger.info(f"[REQ {request_id}] Analyze request: symptoms={len(symptoms)} chars, "
|
| 333 |
-
f"threshold={threshold}, age_group={age_group}, force_offline={force_offline}"
|
|
|
|
| 334 |
|
| 335 |
start_time = time.time()
|
| 336 |
|
|
@@ -407,6 +414,18 @@ async def analyze_xray(
|
|
| 407 |
# ===== Model Inference =====
|
| 408 |
logger.info(f"[REQ {request_id}] Running inference...")
|
| 409 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
# Issue #6: Non-blocking inference call
|
| 411 |
result = await asyncio.to_thread(
|
| 412 |
explainer.explain,
|
|
@@ -414,7 +433,9 @@ async def analyze_xray(
|
|
| 414 |
symptoms=symptoms,
|
| 415 |
threshold=threshold,
|
| 416 |
age_group=age_group,
|
| 417 |
-
force_offline=force_offline
|
|
|
|
|
|
|
| 418 |
)
|
| 419 |
|
| 420 |
# ===== Response Construction =====
|
|
|
|
| 315 |
threshold: float = Form(0.5),
|
| 316 |
age_group: str = Form("Adult (18-64)"),
|
| 317 |
force_offline: bool = Form(False),
|
| 318 |
+
report_type: str = Form("clinical"),
|
| 319 |
+
mrn: str = Form(""),
|
| 320 |
+
patient_name: str = Form(""),
|
| 321 |
+
age: str = Form(""),
|
| 322 |
+
sex: str = Form(""),
|
| 323 |
+
institution: str = Form(""),
|
| 324 |
explainer: MistralExplainer = Depends(get_explainer),
|
| 325 |
request_id: str = Depends(get_request_id),
|
| 326 |
client_ip: str = Depends(check_rate_limit)
|
|
|
|
| 336 |
"""
|
| 337 |
|
| 338 |
logger.info(f"[REQ {request_id}] Analyze request: symptoms={len(symptoms)} chars, "
|
| 339 |
+
f"threshold={threshold}, age_group={age_group}, force_offline={force_offline}, "
|
| 340 |
+
f"report_type={report_type}")
|
| 341 |
|
| 342 |
start_time = time.time()
|
| 343 |
|
|
|
|
| 414 |
# ===== Model Inference =====
|
| 415 |
logger.info(f"[REQ {request_id}] Running inference...")
|
| 416 |
|
| 417 |
+
# Build patient metadata dict
|
| 418 |
+
patient_metadata = None
|
| 419 |
+
if mrn or patient_name or age or sex or institution:
|
| 420 |
+
patient_metadata = {
|
| 421 |
+
"mrn": mrn.strip() if mrn else "Not provided",
|
| 422 |
+
"name": patient_name.strip() if patient_name else "Anonymous",
|
| 423 |
+
"age": age.strip() if age else "Unknown",
|
| 424 |
+
"sex": sex.strip() if sex else "Unknown",
|
| 425 |
+
"institution": institution.strip() if institution else "TB-Guard Clinic",
|
| 426 |
+
"study_date": datetime.utcnow().strftime("%Y-%m-%d")
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
# Issue #6: Non-blocking inference call
|
| 430 |
result = await asyncio.to_thread(
|
| 431 |
explainer.explain,
|
|
|
|
| 433 |
symptoms=symptoms,
|
| 434 |
threshold=threshold,
|
| 435 |
age_group=age_group,
|
| 436 |
+
force_offline=force_offline,
|
| 437 |
+
patient_metadata=patient_metadata,
|
| 438 |
+
report_type=report_type
|
| 439 |
)
|
| 440 |
|
| 441 |
# ===== Response Construction =====
|