Update retina_api_multi.py
Browse files- retina_api_multi.py +620 -650
retina_api_multi.py
CHANGED
|
@@ -1,650 +1,620 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
# Retina/eye multi-task inference API (
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
import
|
| 29 |
-
import
|
| 30 |
-
import
|
| 31 |
-
|
| 32 |
-
from
|
| 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 |
-
def
|
| 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 |
-
return
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
if
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
torch.
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
new_state =
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
)
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
return
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
return
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
mode
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
""
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
)
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
"
|
| 394 |
-
|
| 395 |
-
"
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
@app.get("/
|
| 446 |
-
def
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
"
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
qc
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
)
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
try:
|
| 623 |
-
raw = file.file.read()
|
| 624 |
-
except Exception:
|
| 625 |
-
raise HTTPException(status_code=400, detail="Invalid file")
|
| 626 |
-
|
| 627 |
-
if tm.model is None:
|
| 628 |
-
form = {"patient_name": patient_name, "exam_date": exam_date, "eye": eye}
|
| 629 |
-
return _proxy_report_task(task, raw, form, filename=getattr(file, "filename", "image.jpg"))
|
| 630 |
-
|
| 631 |
-
try:
|
| 632 |
-
im = Image.open(io.BytesIO(raw))
|
| 633 |
-
except Exception:
|
| 634 |
-
raise HTTPException(status_code=400, detail="Invalid image")
|
| 635 |
-
|
| 636 |
-
qc = _simple_qc(im)
|
| 637 |
-
probs = predict_with_task(tm, im)
|
| 638 |
-
items_sorted, top1 = _items_from_probs(task, probs)
|
| 639 |
-
rep = _format_report(task, probs, patient_name=patient_name, exam_date=exam_date, eye=eye)
|
| 640 |
-
return {
|
| 641 |
-
"task": task,
|
| 642 |
-
"patient": {"name": patient_name, "exam_date": exam_date, "eye": eye},
|
| 643 |
-
"qc": qc, "top1": top1, "probs": items_sorted,
|
| 644 |
-
"report": rep
|
| 645 |
-
}
|
| 646 |
-
|
| 647 |
-
@app.post("/predict_strict")
|
| 648 |
-
def predict_strict(file: UploadFile = File(...), tta: int = 1):
|
| 649 |
-
"""Alias برای سازگاری؛ مثل /predict عمل میکند (پارامتر tta نادیده گرفته میشود)."""
|
| 650 |
-
return predict(file)
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# Retina/eye multi-task inference API (single-port, Torch-optional)
|
| 3 |
+
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
import base64
|
| 7 |
+
import glob
|
| 8 |
+
import hashlib
|
| 9 |
+
import tempfile
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from dataclasses import dataclass
|
| 12 |
+
from typing import Dict, List, Optional, Tuple, Any
|
| 13 |
+
|
| 14 |
+
import requests
|
| 15 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, Query, Form
|
| 16 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 17 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 18 |
+
from pydantic import BaseModel
|
| 19 |
+
from PIL import Image
|
| 20 |
+
|
| 21 |
+
# -------------------- Torch / Torchvision (optional) --------------------
|
| 22 |
+
TORCH_AVAILABLE = False
|
| 23 |
+
_TV_WEIGHTS_ENUM = False
|
| 24 |
+
try:
|
| 25 |
+
import torch # type: ignore
|
| 26 |
+
TORCH_AVAILABLE = True
|
| 27 |
+
try:
|
| 28 |
+
# import torchvision only if torch is OK
|
| 29 |
+
from torchvision import transforms as T # type: ignore
|
| 30 |
+
from torchvision.models import resnet50, mobilenet_v3_large # type: ignore
|
| 31 |
+
try:
|
| 32 |
+
from torchvision.models import ResNet50_Weights, MobileNet_V3_Large_Weights # type: ignore
|
| 33 |
+
_TV_WEIGHTS_ENUM = True
|
| 34 |
+
except Exception:
|
| 35 |
+
ResNet50_Weights = None # type: ignore
|
| 36 |
+
MobileNet_V3_Large_Weights = None # type: ignore
|
| 37 |
+
_TV_WEIGHTS_ENUM = False
|
| 38 |
+
except Exception:
|
| 39 |
+
# torchvision هم در دسترس نبود
|
| 40 |
+
T = None # type: ignore
|
| 41 |
+
resnet50 = mobilenet_v3_large = None # type: ignore
|
| 42 |
+
except Exception:
|
| 43 |
+
torch = None # type: ignore
|
| 44 |
+
T = None # type: ignore
|
| 45 |
+
resnet50 = mobilenet_v3_large = None # type: ignore
|
| 46 |
+
|
| 47 |
+
# -------------------- Defaults per task --------------------
|
| 48 |
+
DEFAULT_TASKS = ["dr"]
|
| 49 |
+
TASK_DEFAULT_CLASSES_FA: Dict[str, List[str]] = {
|
| 50 |
+
"dr": ["بدون DR", "خفیف", "متوسط", "شدید", "پرولیفراکتیو"],
|
| 51 |
+
"oct_cme": ["بدون CME", "CME"],
|
| 52 |
+
"oct_csr": ["بدون CSR", "CSR"],
|
| 53 |
+
"oct_amd": ["بدون AMD", "خشک", "تر"],
|
| 54 |
+
"glaucoma": ["نرمال", "گلوکوم"],
|
| 55 |
+
"keratoconus": ["نرمال", "کراتوکونوس"],
|
| 56 |
+
}
|
| 57 |
+
TASK_DEFAULT_CLASSES_EN: Dict[str, List[str]] = {
|
| 58 |
+
"dr": ["No DR", "Mild", "Moderate", "Severe", "Proliferative DR"],
|
| 59 |
+
"oct_cme": ["No CME", "CME"],
|
| 60 |
+
"oct_csr": ["No CSR", "CSR"],
|
| 61 |
+
"oct_amd": ["No AMD", "Dry", "Wet"],
|
| 62 |
+
"glaucoma": ["Normal", "Glaucoma"],
|
| 63 |
+
"keratoconus": ["Normal", "Keratoconus"],
|
| 64 |
+
}
|
| 65 |
+
TASK_DEFAULT_IMG: Dict[str, int] = {
|
| 66 |
+
"dr": 448,
|
| 67 |
+
"oct_cme": 416,
|
| 68 |
+
"oct_csr": 416,
|
| 69 |
+
"oct_amd": 416,
|
| 70 |
+
"glaucoma": 416,
|
| 71 |
+
"keratoconus": 416,
|
| 72 |
+
}
|
| 73 |
+
TASK_DEFAULT_MODEL: Dict[str, str] = {
|
| 74 |
+
"dr": "resnet50",
|
| 75 |
+
"oct_cme": "resnet50",
|
| 76 |
+
"oct_csr": "resnet50",
|
| 77 |
+
"oct_amd": "resnet50",
|
| 78 |
+
"glaucoma": "resnet50",
|
| 79 |
+
"keratoconus": "resnet50",
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
# -------------------- Weights: autodiscovery / optional download --------------------
|
| 83 |
+
DEFAULT_WEIGHTS_DIR = os.getenv("RETINA_WEIGHTS_DIR", "/app/models")
|
| 84 |
+
WEIGHT_PATTERNS = {
|
| 85 |
+
"dr": ["runs_k80/phase2/best.pth", "dr/*.pth", "*.pth"],
|
| 86 |
+
"oct_cme": ["oct_cme/best.pth", "oct_cme/*.pth", "*.pth"],
|
| 87 |
+
"oct_csr": ["oct_csr/best.pth", "oct_csr/*.pth", "*.pth"],
|
| 88 |
+
"oct_amd": ["oct_amd/best.pth", "oct_amd/*.pth", "*.pth"],
|
| 89 |
+
"glaucoma": ["glaucoma/best.pth", "glaucoma/*.pth", "*.pth"],
|
| 90 |
+
"keratoconus": ["keratoconus/best.pth", "keratoconus/*.pth", "*.pth"],
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
def _find_candidate_weights(task: str) -> List[str]:
|
| 94 |
+
root = Path(DEFAULT_WEIGHTS_DIR)
|
| 95 |
+
pats = WEIGHT_PATTERNS.get(task, ["*.pth"])
|
| 96 |
+
found: List[str] = []
|
| 97 |
+
for p in pats:
|
| 98 |
+
found.extend(glob.glob(str(root / p)))
|
| 99 |
+
uniq = sorted(
|
| 100 |
+
set(found),
|
| 101 |
+
key=lambda p: Path(p).stat().st_mtime if Path(p).exists() else 0,
|
| 102 |
+
reverse=True,
|
| 103 |
+
)
|
| 104 |
+
return [f for f in uniq if Path(f).is_file()]
|
| 105 |
+
|
| 106 |
+
def _download(url: str, dest: Path, sha256: Optional[str] = None) -> Path:
|
| 107 |
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
| 108 |
+
with requests.get(url, stream=True, timeout=60) as r:
|
| 109 |
+
r.raise_for_status()
|
| 110 |
+
h = hashlib.sha256()
|
| 111 |
+
with tempfile.NamedTemporaryFile(delete=False, dir=str(dest.parent), suffix=".part") as tmp:
|
| 112 |
+
for chunk in r.iter_content(chunk_size=1024*1024):
|
| 113 |
+
if not chunk:
|
| 114 |
+
continue
|
| 115 |
+
tmp.write(chunk)
|
| 116 |
+
h.update(chunk)
|
| 117 |
+
tmp_path = Path(tmp.name)
|
| 118 |
+
if sha256 and h.hexdigest().lower() != sha256.lower():
|
| 119 |
+
tmp_path.unlink(missing_ok=True)
|
| 120 |
+
raise RuntimeError(f"SHA256 mismatch for {url}")
|
| 121 |
+
tmp_path.replace(dest)
|
| 122 |
+
return dest
|
| 123 |
+
|
| 124 |
+
def _pick_weight(task: str) -> Tuple[Optional[str], List[str]]:
|
| 125 |
+
env_path = os.getenv(f"RETINA_WEIGHTS_{task}")
|
| 126 |
+
if env_path and Path(env_path).is_file():
|
| 127 |
+
return env_path, [env_path]
|
| 128 |
+
cands = _find_candidate_weights(task)
|
| 129 |
+
if cands:
|
| 130 |
+
return cands[0], cands
|
| 131 |
+
url = os.getenv(f"RETINA_WEIGHTS_URL_{task}")
|
| 132 |
+
sha = os.getenv(f"RETINA_WEIGHTS_SHA256_{task}")
|
| 133 |
+
if url:
|
| 134 |
+
dest = Path(DEFAULT_WEIGHTS_DIR) / task / "best.pth"
|
| 135 |
+
try:
|
| 136 |
+
print(f"[weights] downloading {task} from {url} → {dest}")
|
| 137 |
+
got = _download(url, dest, sha256=sha)
|
| 138 |
+
return str(got), [str(got)]
|
| 139 |
+
except Exception as e:
|
| 140 |
+
print(f"[weights] download failed for {task}: {e}")
|
| 141 |
+
return None, []
|
| 142 |
+
|
| 143 |
+
# -------------------- Utils (Torch-aware) --------------------
|
| 144 |
+
def device_setup() -> str:
|
| 145 |
+
if TORCH_AVAILABLE and torch.cuda.is_available(): # type: ignore
|
| 146 |
+
torch.backends.cudnn.enabled = False # type: ignore
|
| 147 |
+
return "cuda"
|
| 148 |
+
return "cpu"
|
| 149 |
+
|
| 150 |
+
def build_model(name: str, num_classes: int):
|
| 151 |
+
if not (TORCH_AVAILABLE and resnet50 and mobilenet_v3_large):
|
| 152 |
+
raise RuntimeError("Torch/torchvision not available in this runtime.")
|
| 153 |
+
name = name.lower()
|
| 154 |
+
if name in ("resnet50", "resnet"):
|
| 155 |
+
if _TV_WEIGHTS_ENUM:
|
| 156 |
+
m = resnet50(weights=None) # type: ignore
|
| 157 |
+
else:
|
| 158 |
+
m = resnet50(pretrained=False) # type: ignore
|
| 159 |
+
import torch.nn as nn # local import (only when torch exists)
|
| 160 |
+
m.fc = nn.Linear(m.fc.in_features, num_classes)
|
| 161 |
+
return m
|
| 162 |
+
elif name in ("mobilenetv3", "mobilenet_v3", "mbv3"):
|
| 163 |
+
if _TV_WEIGHTS_ENUM:
|
| 164 |
+
m = mobilenet_v3_large(weights=None) # type: ignore
|
| 165 |
+
else:
|
| 166 |
+
m = mobilenet_v3_large(pretrained=False) # type: ignore
|
| 167 |
+
import torch.nn as nn # local import
|
| 168 |
+
m.classifier[3] = nn.Linear(m.classifier[3].in_features, num_classes)
|
| 169 |
+
return m
|
| 170 |
+
else:
|
| 171 |
+
raise ValueError(f"Unknown model: {name}")
|
| 172 |
+
|
| 173 |
+
def make_transform(img_size: int):
|
| 174 |
+
if not (TORCH_AVAILABLE and T):
|
| 175 |
+
# در حالت بدون Torch اصلاً این مسیر استفاده نمیشود
|
| 176 |
+
def _noop(x): return x
|
| 177 |
+
return _noop
|
| 178 |
+
return T.Compose([
|
| 179 |
+
T.Resize(int(img_size * 1.15)),
|
| 180 |
+
T.CenterCrop(img_size),
|
| 181 |
+
T.ToTensor(),
|
| 182 |
+
T.Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225]),
|
| 183 |
+
])
|
| 184 |
+
|
| 185 |
+
def load_state(model, weights_path: str):
|
| 186 |
+
if not TORCH_AVAILABLE:
|
| 187 |
+
raise RuntimeError("Torch not available for loading state.")
|
| 188 |
+
ckpt = torch.load(weights_path, map_location='cpu') # type: ignore
|
| 189 |
+
state = ckpt.get("model", ckpt)
|
| 190 |
+
new_state = {}
|
| 191 |
+
for k, v in state.items():
|
| 192 |
+
nk = k[7:] if k.startswith("module.") else k
|
| 193 |
+
new_state[nk] = v
|
| 194 |
+
missing, unexpected = model.load_state_dict(new_state, strict=False)
|
| 195 |
+
return list(missing), list(unexpected)
|
| 196 |
+
|
| 197 |
+
@dataclass
|
| 198 |
+
class TaskModel:
|
| 199 |
+
name: str
|
| 200 |
+
model: Optional[Any]
|
| 201 |
+
device: str
|
| 202 |
+
img_size: int
|
| 203 |
+
classes_fa: List[str]
|
| 204 |
+
classes_en: List[str]
|
| 205 |
+
weights_path: Optional[str]
|
| 206 |
+
missing_keys: List[str]
|
| 207 |
+
unexpected_keys: List[str]
|
| 208 |
+
transform: Any
|
| 209 |
+
|
| 210 |
+
def env_list(key: str, default: Optional[List[str]] = None) -> List[str]:
|
| 211 |
+
raw = os.getenv(key)
|
| 212 |
+
if not raw:
|
| 213 |
+
return default or []
|
| 214 |
+
return [x.strip() for x in raw.split(",") if x.strip()]
|
| 215 |
+
|
| 216 |
+
def parse_classes_env(task: str) -> Optional[List[str]]:
|
| 217 |
+
key = f"RETINA_CLASSES_{task}"
|
| 218 |
+
raw = os.getenv(key)
|
| 219 |
+
if not raw:
|
| 220 |
+
return None
|
| 221 |
+
vals = [v.strip() for v in raw.split(",") if v.strip()]
|
| 222 |
+
return vals or None
|
| 223 |
+
|
| 224 |
+
def prepare_task(task: str, device: str) -> TaskModel:
|
| 225 |
+
model_name = os.getenv(f"RETINA_MODEL_{task}", TASK_DEFAULT_MODEL.get(task, "resnet50"))
|
| 226 |
+
img_size = int(os.getenv(f"RETINA_IMG_SIZE_{task}", str(TASK_DEFAULT_IMG.get(task, 416))))
|
| 227 |
+
classes_en = parse_classes_env(task) or TASK_DEFAULT_CLASSES_EN.get(task, ["Negative","Positive"])
|
| 228 |
+
classes_fa_default = TASK_DEFAULT_CLASSES_FA.get(task, ["منفی","مثبت"])
|
| 229 |
+
classes_fa = classes_fa_default if not parse_classes_env(task) else (
|
| 230 |
+
classes_fa_default if len(classes_fa_default)==len(classes_en) else classes_en
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
weights, all_cands = _pick_weight(task)
|
| 234 |
+
|
| 235 |
+
# اگر torch/torchvision نیست یا وزنی نداریم → مدل لوکال لود نشود
|
| 236 |
+
if (not TORCH_AVAILABLE) or (not weights) or (not os.path.isfile(weights)):
|
| 237 |
+
tm = TaskModel(task, None, device, img_size, classes_fa, classes_en, weights if weights else None,
|
| 238 |
+
[], [], make_transform(img_size))
|
| 239 |
+
tm._all_weight_candidates = all_cands # type: ignore
|
| 240 |
+
return tm
|
| 241 |
+
|
| 242 |
+
m = build_model(model_name, num_classes=len(classes_en))
|
| 243 |
+
missing, unexpected = load_state(m, weights)
|
| 244 |
+
m.eval().to(device)
|
| 245 |
+
if device == 'cuda':
|
| 246 |
+
m.to(memory_format=torch.channels_last) # type: ignore
|
| 247 |
+
|
| 248 |
+
tm = TaskModel(task, m, device, img_size, classes_fa, classes_en, weights, missing, unexpected, make_transform(img_size))
|
| 249 |
+
tm._all_weight_candidates = all_cands # type: ignore
|
| 250 |
+
return tm
|
| 251 |
+
|
| 252 |
+
def predict_with_task(task_obj: TaskModel, pil_im: Image.Image) -> List[float]:
|
| 253 |
+
if (not TORCH_AVAILABLE) or (task_obj.model is None):
|
| 254 |
+
raise RuntimeError("Local model not available.")
|
| 255 |
+
x = task_obj.transform(pil_im.convert("RGB")).unsqueeze(0)
|
| 256 |
+
x = x.to(task_obj.device, non_blocking=True)
|
| 257 |
+
with torch.no_grad(): # type: ignore
|
| 258 |
+
logits = task_obj.model(x)
|
| 259 |
+
probs = torch.softmax(logits, dim=1)[0].detach().cpu().numpy().tolist() # type: ignore
|
| 260 |
+
return probs
|
| 261 |
+
|
| 262 |
+
# -------------------- Remote proxy helpers --------------------
|
| 263 |
+
def _remote_base_for(task: str) -> Optional[str]:
|
| 264 |
+
return os.getenv(f"RETINA_REMOTE_{task}")
|
| 265 |
+
|
| 266 |
+
def _remote_auth_header_for(task: str) -> dict:
|
| 267 |
+
token = os.getenv(f"RETINA_REMOTE_AUTH_{task}") or os.getenv("RETINA_REMOTE_AUTH") or ""
|
| 268 |
+
return {"Authorization": token} if token.strip() else {}
|
| 269 |
+
|
| 270 |
+
def _remote_verify_ssl() -> bool:
|
| 271 |
+
v = (os.getenv("RETINA_REMOTE_VERIFY_SSL") or "true").strip().lower()
|
| 272 |
+
return v not in ("0", "false", "no")
|
| 273 |
+
|
| 274 |
+
def _remote_timeout() -> int:
|
| 275 |
+
try:
|
| 276 |
+
return int(os.getenv("RETINA_REMOTE_TIMEOUT", "90"))
|
| 277 |
+
except Exception:
|
| 278 |
+
return 90
|
| 279 |
+
|
| 280 |
+
def _remote_url(task: str, mode: str) -> Optional[str]:
|
| 281 |
+
base = _remote_base_for(task)
|
| 282 |
+
if not base:
|
| 283 |
+
return None
|
| 284 |
+
base = base.strip()
|
| 285 |
+
if base.endswith("/predict_task") or base.endswith("/report_task"):
|
| 286 |
+
return base
|
| 287 |
+
return f"{base.rstrip('/')}/{ 'predict_task' if mode == 'predict' else 'report_task'}?task={task}"
|
| 288 |
+
|
| 289 |
+
def _proxy_predict_task(task: str, file_bytes: bytes, filename: str = "image.jpg") -> JSONResponse:
|
| 290 |
+
url = _remote_url(task, "predict")
|
| 291 |
+
if not url:
|
| 292 |
+
raise HTTPException(status_code=501, detail=f"Task '{task}' not loaded and no remote set (RETINA_REMOTE_{task}).")
|
| 293 |
+
headers = _remote_auth_header_for(task)
|
| 294 |
+
try:
|
| 295 |
+
r = requests.post(
|
| 296 |
+
url,
|
| 297 |
+
files={"file": (filename, file_bytes, "image/jpeg")},
|
| 298 |
+
headers=headers,
|
| 299 |
+
timeout=_remote_timeout(),
|
| 300 |
+
verify=_remote_verify_ssl(),
|
| 301 |
+
)
|
| 302 |
+
if not (200 <= r.status_code < 300):
|
| 303 |
+
raise HTTPException(status_code=r.status_code, detail=f"Remote error: {r.text}")
|
| 304 |
+
try:
|
| 305 |
+
return JSONResponse(r.json())
|
| 306 |
+
except Exception:
|
| 307 |
+
return JSONResponse({"remote_raw": r.text})
|
| 308 |
+
except requests.RequestException as e:
|
| 309 |
+
raise HTTPException(status_code=502, detail=f"Remote proxy failed: {e}")
|
| 310 |
+
|
| 311 |
+
def _proxy_report_task(task: str, file_bytes: bytes, form: dict, filename: str = "image.jpg") -> JSONResponse:
|
| 312 |
+
url = _remote_url(task, "report")
|
| 313 |
+
if not url:
|
| 314 |
+
raise HTTPException(status_code=501, detail=f"Task '{task}' not loaded and no remote set (RETINA_REMOTE_{task}).")
|
| 315 |
+
headers = _remote_auth_header_for(task)
|
| 316 |
+
try:
|
| 317 |
+
r = requests.post(
|
| 318 |
+
url,
|
| 319 |
+
files={"file": (filename, file_bytes, "image/jpeg")},
|
| 320 |
+
data=form,
|
| 321 |
+
headers=headers,
|
| 322 |
+
timeout=_remote_timeout(),
|
| 323 |
+
verify=_remote_verify_ssl(),
|
| 324 |
+
)
|
| 325 |
+
if not (200 <= r.status_code < 300):
|
| 326 |
+
raise HTTPException(status_code=r.status_code, detail=f"Remote error: {r.text}")
|
| 327 |
+
try:
|
| 328 |
+
return JSONResponse(r.json())
|
| 329 |
+
except Exception:
|
| 330 |
+
return JSONResponse({"remote_raw": r.text})
|
| 331 |
+
except requests.RequestException as e:
|
| 332 |
+
raise HTTPException(status_code=502, detail=f"Remote proxy failed: {e}")
|
| 333 |
+
|
| 334 |
+
# -------------------- App --------------------
|
| 335 |
+
app = FastAPI(title="Retina Multi-Task Inference API (Unified)", version="1.3.1")
|
| 336 |
+
app.add_middleware(
|
| 337 |
+
CORSMiddleware,
|
| 338 |
+
allow_origins=["*"], allow_credentials=True,
|
| 339 |
+
allow_methods=["*"], allow_headers=["*"],
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
_DEVICE = device_setup()
|
| 343 |
+
_TASKS = env_list("RETINA_TASKS", DEFAULT_TASKS)
|
| 344 |
+
_TASK_MODELS: Dict[str, TaskModel] = {t: prepare_task(t, _DEVICE) for t in _TASKS}
|
| 345 |
+
DEFAULT_FALLBACK_TASK = os.getenv("RETINA_DEFAULT_TASK", "dr").strip().lower()
|
| 346 |
+
|
| 347 |
+
# -------------------- Helpers for QC/format --------------------
|
| 348 |
+
def _simple_qc(im: Image.Image) -> dict:
|
| 349 |
+
try:
|
| 350 |
+
import numpy as np # lazy
|
| 351 |
+
except Exception:
|
| 352 |
+
w, h = im.size
|
| 353 |
+
return {"width": w, "height": h, "mean_luma": None, "warnings": [], "ok": True}
|
| 354 |
+
w, h = im.size
|
| 355 |
+
mean_luma = float(np.array(im.convert("L")).mean())
|
| 356 |
+
warns: List[str] = []
|
| 357 |
+
if min(w, h) < 512: warns.append("low_resolution")
|
| 358 |
+
if mean_luma < 25: warns.append("too_dark")
|
| 359 |
+
if mean_luma > 230: warns.append("too_bright")
|
| 360 |
+
return {"width": w, "height": h, "mean_luma": round(mean_luma,1), "warnings": warns, "ok": len(warns)==0}
|
| 361 |
+
|
| 362 |
+
def _items_from_probs(task: str, probs: List[float]):
|
| 363 |
+
tm = _TASK_MODELS[task]
|
| 364 |
+
items = [{"index": i,
|
| 365 |
+
"class_en": tm.classes_en[i],
|
| 366 |
+
"class_fa": tm.classes_fa[i],
|
| 367 |
+
"prob": float(p)} for i, p in enumerate(probs)]
|
| 368 |
+
items_sorted = sorted(items, key=lambda d: d["prob"], reverse=True)
|
| 369 |
+
top1 = items_sorted[0]
|
| 370 |
+
return items_sorted, top1
|
| 371 |
+
|
| 372 |
+
def _format_report(task: str, probs: List[float], patient_name: str = "", exam_date: str = "", eye: str = "") -> str:
|
| 373 |
+
tm = _TASK_MODELS[task]
|
| 374 |
+
items, top = _items_from_probs(task, probs)
|
| 375 |
+
title_map = {
|
| 376 |
+
"dr": "گزارش رتینوپاتی دیابتی (DR)",
|
| 377 |
+
"oct_cme": "گزارش OCT - CME",
|
| 378 |
+
"oct_csr": "گزارش OCT - CSR",
|
| 379 |
+
"oct_amd": "گزارش OCT - AMD",
|
| 380 |
+
"glaucoma": "گزارش گلوکوم",
|
| 381 |
+
"keratoconus": "گزارش کراتوکونوس",
|
| 382 |
+
}
|
| 383 |
+
title = title_map.get(task, f"گزارش {task}")
|
| 384 |
+
lines: List[str] = []
|
| 385 |
+
lines.append(f"👁 {title} برای بیمار: {patient_name or '—'}")
|
| 386 |
+
lines.append(f"📅 تاریخ معاینه: {exam_date or '—'}")
|
| 387 |
+
if eye: lines.append(f"👓 چشم: {eye}")
|
| 388 |
+
lines.append("________________________________________")
|
| 389 |
+
lines.append("📌 نتیجه الگوریتم (Top-1):")
|
| 390 |
+
lines.append(f"• {top['class_fa']} ({top['class_en']}) — احتمال {top['prob']:.3f}")
|
| 391 |
+
lines.append("📊 توزیع احتمالات:")
|
| 392 |
+
for it in items:
|
| 393 |
+
lines.append(f"• {it['class_fa']} ({it['class_en']}) — {it['prob']:.4f}")
|
| 394 |
+
if task == "dr":
|
| 395 |
+
lines.append("🧠 یادداشت: نتیجه برای کمک به تصمیمگیری است؛ در موارد مثبت معاینه بالینی/تصویربرداری تکمیلی توصیه میشود.")
|
| 396 |
+
elif task.startswith("oct_"):
|
| 397 |
+
lines.append("🧠 یادداشت: تفسیر نهایی با همبستگی بالینی و تصاویر مکمل.")
|
| 398 |
+
elif task in ("glaucoma", "keratoconus"):
|
| 399 |
+
lines.append("🧠 یادداشت: جایگزین تشخیص پزشک نیست و باید با پاراکلینیک تلفیق شود.")
|
| 400 |
+
return "\n".join(lines)
|
| 401 |
+
|
| 402 |
+
# -------------------- Pages --------------------
|
| 403 |
+
@app.get("/", response_class=HTMLResponse)
|
| 404 |
+
def root():
|
| 405 |
+
li = "".join([f"<li>{t} — loaded={_TASK_MODELS[t].model is not None} — img={_TASK_MODELS[t].img_size}</li>" for t in _TASKS])
|
| 406 |
+
return f"""
|
| 407 |
+
<html><head><meta charset="utf-8"><title>Retina Unified API</title></head>
|
| 408 |
+
<body style="font-family:Tahoma,Arial,sans-serif">
|
| 409 |
+
<h2>Retina Multi-Task Predictor (Single Port)</h2>
|
| 410 |
+
<p>Device: <b>{_DEVICE}</b> | Tasks: {", ".join(_TASKS)}</p>
|
| 411 |
+
<ul>{li}</ul>
|
| 412 |
+
<h3>Quick Forms</h3>
|
| 413 |
+
<form action="/predict" method="post" enctype="multipart/form-data">
|
| 414 |
+
<div><b>Back-compat /predict (RETINA_DEFAULT_TASK = {DEFAULT_FALLBACK_TASK})</b></div>
|
| 415 |
+
<input type="file" name="file" accept="image/*" required />
|
| 416 |
+
<button type="submit">/predict</button>
|
| 417 |
+
</form>
|
| 418 |
+
<hr/>
|
| 419 |
+
<form action="/predict_task?task=oct_cme" method="post" enctype="multipart/form-data">
|
| 420 |
+
<div><b>OCT - CME</b></div>
|
| 421 |
+
<input type="file" name="file" accept="image/*" required />
|
| 422 |
+
<button type="submit">/predict_task?task=oct_cme</button>
|
| 423 |
+
</form>
|
| 424 |
+
</body></html>
|
| 425 |
+
"""
|
| 426 |
+
|
| 427 |
+
# -------------------- Meta --------------------
|
| 428 |
+
@app.get("/tasks")
|
| 429 |
+
def tasks():
|
| 430 |
+
out = {}
|
| 431 |
+
for t, tm in _TASK_MODELS.items():
|
| 432 |
+
out[t] = {
|
| 433 |
+
"loaded": tm.model is not None,
|
| 434 |
+
"img_size": tm.img_size,
|
| 435 |
+
"classes_en": tm.classes_en,
|
| 436 |
+
"classes_fa": tm.classes_fa,
|
| 437 |
+
"weights_used": tm.weights_path,
|
| 438 |
+
"weights_candidates": getattr(tm, "_all_weight_candidates", []),
|
| 439 |
+
"missing_keys": tm.missing_keys,
|
| 440 |
+
"unexpected_keys": tm.unexpected_keys,
|
| 441 |
+
"remote_url": _remote_url(t, "predict"),
|
| 442 |
+
}
|
| 443 |
+
return out
|
| 444 |
+
|
| 445 |
+
@app.get("/health")
|
| 446 |
+
def health():
|
| 447 |
+
return {
|
| 448 |
+
"device": _DEVICE,
|
| 449 |
+
"cuda": bool(TORCH_AVAILABLE and torch and torch.cuda.is_available()), # type: ignore
|
| 450 |
+
"cudnn_enabled": bool(TORCH_AVAILABLE and torch and torch.backends.cudnn.enabled), # type: ignore
|
| 451 |
+
"tasks": list(_TASK_MODELS.keys()),
|
| 452 |
+
"loaded": {t: (_TASK_MODELS[t].model is not None) for t in _TASK_MODELS},
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
# -------------------- API: multi-task --------------------
|
| 456 |
+
@app.post("/predict_task")
|
| 457 |
+
def predict_task(
|
| 458 |
+
task: str = Query(..., description="dr, oct_cme, oct_csr, oct_amd, glaucoma, keratoconus"),
|
| 459 |
+
file: UploadFile = File(...)
|
| 460 |
+
):
|
| 461 |
+
task = task.strip().lower()
|
| 462 |
+
if task not in _TASK_MODELS:
|
| 463 |
+
raise HTTPException(status_code=404, detail=f"Unknown task: {task}")
|
| 464 |
+
tm = _TASK_MODELS[task]
|
| 465 |
+
|
| 466 |
+
try:
|
| 467 |
+
raw = file.file.read()
|
| 468 |
+
except Exception:
|
| 469 |
+
raise HTTPException(status_code=400, detail="Invalid file")
|
| 470 |
+
|
| 471 |
+
if tm.model is None:
|
| 472 |
+
return _proxy_predict_task(task, raw, filename=getattr(file, "filename", "image.jpg"))
|
| 473 |
+
|
| 474 |
+
try:
|
| 475 |
+
im = Image.open(io.BytesIO(raw))
|
| 476 |
+
except Exception:
|
| 477 |
+
raise HTTPException(status_code=400, detail="Invalid image")
|
| 478 |
+
|
| 479 |
+
qc = _simple_qc(im)
|
| 480 |
+
probs = predict_with_task(tm, im)
|
| 481 |
+
items_sorted, top1 = _items_from_probs(task, probs)
|
| 482 |
+
return JSONResponse({
|
| 483 |
+
"task": task,
|
| 484 |
+
"qc": qc,
|
| 485 |
+
"top1": top1,
|
| 486 |
+
"probs": items_sorted,
|
| 487 |
+
"weights_used": tm.weights_path,
|
| 488 |
+
"weights_candidates": getattr(tm, "_all_weight_candidates", []),
|
| 489 |
+
})
|
| 490 |
+
|
| 491 |
+
@app.post("/report_task")
|
| 492 |
+
def report_task(
|
| 493 |
+
task: str = Query(...),
|
| 494 |
+
file: UploadFile = File(...),
|
| 495 |
+
patient_name: str = Form(""),
|
| 496 |
+
exam_date: str = Form(""),
|
| 497 |
+
eye: str = Form("")
|
| 498 |
+
):
|
| 499 |
+
task = task.strip().lower()
|
| 500 |
+
if task not in _TASK_MODELS:
|
| 501 |
+
raise HTTPException(status_code=404, detail=f"Unknown task: {task}")
|
| 502 |
+
tm = _TASK_MODELS[task]
|
| 503 |
+
|
| 504 |
+
try:
|
| 505 |
+
raw = file.file.read()
|
| 506 |
+
except Exception:
|
| 507 |
+
raise HTTPException(status_code=400, detail="Invalid file")
|
| 508 |
+
|
| 509 |
+
if tm.model is None:
|
| 510 |
+
form = {"patient_name": patient_name, "exam_date": exam_date, "eye": eye}
|
| 511 |
+
return _proxy_report_task(task, raw, form, filename=getattr(file, "filename", "image.jpg"))
|
| 512 |
+
|
| 513 |
+
try:
|
| 514 |
+
im = Image.open(io.BytesIO(raw))
|
| 515 |
+
except Exception:
|
| 516 |
+
raise HTTPException(status_code=400, detail="Invalid image")
|
| 517 |
+
|
| 518 |
+
qc = _simple_qc(im)
|
| 519 |
+
probs = predict_with_task(tm, im)
|
| 520 |
+
items_sorted, top1 = _items_from_probs(task, probs)
|
| 521 |
+
report_fa = _format_report(task, probs, patient_name=patient_name, exam_date=exam_date, eye=eye)
|
| 522 |
+
|
| 523 |
+
return JSONResponse({
|
| 524 |
+
"task": task,
|
| 525 |
+
"patient": {"name": patient_name, "exam_date": exam_date, "eye": eye},
|
| 526 |
+
"qc": qc, "top1": top1, "probs": items_sorted,
|
| 527 |
+
"report": report_fa,
|
| 528 |
+
"weights_used": tm.weights_path,
|
| 529 |
+
"weights_candidates": getattr(tm, "_all_weight_candidates", []),
|
| 530 |
+
})
|
| 531 |
+
|
| 532 |
+
# -------------------- Back-compat --------------------
|
| 533 |
+
class PredictJsonReq(BaseModel):
|
| 534 |
+
image_b64: str
|
| 535 |
+
|
| 536 |
+
def _get_fallback_task() -> str:
|
| 537 |
+
t = os.getenv("RETINA_DEFAULT_TASK", "dr").strip().lower()
|
| 538 |
+
if t not in _TASK_MODELS:
|
| 539 |
+
raise HTTPException(status_code=404, detail=f"Unknown default task: {t}")
|
| 540 |
+
return t
|
| 541 |
+
|
| 542 |
+
@app.post("/predict")
|
| 543 |
+
def predict(file: UploadFile = File(...)):
|
| 544 |
+
task = _get_fallback_task()
|
| 545 |
+
tm = _TASK_MODELS[task]
|
| 546 |
+
try:
|
| 547 |
+
raw = file.file.read()
|
| 548 |
+
except Exception:
|
| 549 |
+
raise HTTPException(status_code=400, detail="Invalid file")
|
| 550 |
+
|
| 551 |
+
if tm.model is None:
|
| 552 |
+
return _proxy_predict_task(task, raw, filename=getattr(file, "filename", "image.jpg"))
|
| 553 |
+
|
| 554 |
+
try:
|
| 555 |
+
im = Image.open(io.BytesIO(raw))
|
| 556 |
+
except Exception:
|
| 557 |
+
raise HTTPException(status_code=400, detail="Invalid image")
|
| 558 |
+
|
| 559 |
+
qc = _simple_qc(im)
|
| 560 |
+
probs = predict_with_task(tm, im)
|
| 561 |
+
items_sorted, top1 = _items_from_probs(task, probs)
|
| 562 |
+
return {"task": task, "qc": qc, "top1": top1, "probs": items_sorted}
|
| 563 |
+
|
| 564 |
+
@app.post("/predict_json")
|
| 565 |
+
def predict_json(req: PredictJsonReq):
|
| 566 |
+
task = _get_fallback_task()
|
| 567 |
+
tm = _TASK_MODELS[task]
|
| 568 |
+
try:
|
| 569 |
+
data = base64.b64decode(req.image_b64)
|
| 570 |
+
except Exception:
|
| 571 |
+
raise HTTPException(status_code=400, detail="Invalid base64 image")
|
| 572 |
+
|
| 573 |
+
if tm.model is None:
|
| 574 |
+
return _proxy_predict_task(task, data, filename="image.jpg")
|
| 575 |
+
|
| 576 |
+
try:
|
| 577 |
+
im = Image.open(io.BytesIO(data))
|
| 578 |
+
except Exception:
|
| 579 |
+
raise HTTPException(status_code=400, detail="Invalid image data")
|
| 580 |
+
|
| 581 |
+
qc = _simple_qc(im)
|
| 582 |
+
probs = predict_with_task(tm, im)
|
| 583 |
+
items_sorted, top1 = _items_from_probs(task, probs)
|
| 584 |
+
return {"task": task, "qc": qc, "top1": top1, "probs": items_sorted}
|
| 585 |
+
|
| 586 |
+
@app.post("/report")
|
| 587 |
+
def report(
|
| 588 |
+
file: UploadFile = File(...),
|
| 589 |
+
patient_name: str = Form(""),
|
| 590 |
+
exam_date: str = Form(""),
|
| 591 |
+
eye: str = Form("OD")
|
| 592 |
+
):
|
| 593 |
+
task = _get_fallback_task()
|
| 594 |
+
tm = _TASK_MODELS[task]
|
| 595 |
+
try:
|
| 596 |
+
raw = file.file.read()
|
| 597 |
+
except Exception:
|
| 598 |
+
raise HTTPException(status_code=400, detail="Invalid file")
|
| 599 |
+
|
| 600 |
+
if tm.model is None:
|
| 601 |
+
form = {"patient_name": patient_name, "exam_date": exam_date, "eye": eye}
|
| 602 |
+
return _proxy_report_task(task, raw, form, filename=getattr(file, "filename", "image.jpg"))
|
| 603 |
+
|
| 604 |
+
try:
|
| 605 |
+
im = Image.open(io.BytesIO(raw))
|
| 606 |
+
except Exception:
|
| 607 |
+
raise HTTPException(status_code=400, detail="Invalid image")
|
| 608 |
+
|
| 609 |
+
qc = _simple_qc(im)
|
| 610 |
+
probs = predict_with_task(tm, im)
|
| 611 |
+
items_sorted, top1 = _items_from_probs(task, probs)
|
| 612 |
+
rep = _format_report(task, probs, patient_name=patient_name, exam_date=exam_date, eye=eye)
|
| 613 |
+
return {"task": task,
|
| 614 |
+
"patient": {"name": patient_name, "exam_date": exam_date, "eye": eye},
|
| 615 |
+
"qc": qc, "top1": top1, "probs": items_sorted,
|
| 616 |
+
"report": rep}
|
| 617 |
+
|
| 618 |
+
@app.post("/predict_strict")
|
| 619 |
+
def predict_strict(file: UploadFile = File(...), tta: int = 1):
|
| 620 |
+
return predict(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|