Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ import torch.nn as nn
|
|
| 11 |
import torchvision.models as models
|
| 12 |
|
| 13 |
from fastapi import FastAPI, UploadFile, File
|
|
|
|
| 14 |
from fastapi.responses import JSONResponse
|
| 15 |
|
| 16 |
# =========================================
|
|
@@ -38,6 +39,17 @@ app = FastAPI(
|
|
| 38 |
version="1.0"
|
| 39 |
)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
# =========================================
|
| 42 |
# LOAD NIFTI
|
| 43 |
# =========================================
|
|
@@ -346,6 +358,7 @@ model3d = load_model(
|
|
| 346 |
|
| 347 |
# =========================================
|
| 348 |
# SINGLE PREDICTION
|
|
|
|
| 349 |
# =========================================
|
| 350 |
def predict_model(model, tensor):
|
| 351 |
|
|
@@ -372,12 +385,13 @@ def predict_model(model, tensor):
|
|
| 372 |
"class_id":
|
| 373 |
pred_idx,
|
| 374 |
|
|
|
|
| 375 |
"confidence":
|
| 376 |
round(
|
| 377 |
float(
|
| 378 |
probs[pred_idx]
|
| 379 |
-
)
|
| 380 |
-
|
| 381 |
),
|
| 382 |
|
| 383 |
"probabilities": {
|
|
@@ -386,8 +400,8 @@ def predict_model(model, tensor):
|
|
| 386 |
round(
|
| 387 |
float(
|
| 388 |
probs[i]
|
| 389 |
-
)
|
| 390 |
-
|
| 391 |
)
|
| 392 |
|
| 393 |
for i in range(NUM_CLASSES)
|
|
@@ -396,6 +410,25 @@ def predict_model(model, tensor):
|
|
| 396 |
|
| 397 |
return result, probs
|
| 398 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
# =========================================
|
| 400 |
# ROOT
|
| 401 |
# =========================================
|
|
@@ -408,7 +441,16 @@ def home():
|
|
| 408 |
"Parkinson DATSCAN Ensemble API Running",
|
| 409 |
|
| 410 |
"classes":
|
| 411 |
-
LABELS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
}
|
| 413 |
|
| 414 |
# =========================================
|
|
@@ -505,6 +547,7 @@ async def predict_cnn3d(
|
|
| 505 |
|
| 506 |
# =========================================
|
| 507 |
# ENSEMBLE ENDPOINT
|
|
|
|
| 508 |
# =========================================
|
| 509 |
@app.post("/predict/ensemble")
|
| 510 |
async def predict_ensemble(
|
|
@@ -552,27 +595,29 @@ async def predict_ensemble(
|
|
| 552 |
avg_probs
|
| 553 |
).item()
|
| 554 |
|
|
|
|
| 555 |
final_result = {
|
| 556 |
|
| 557 |
-
"
|
| 558 |
LABELS[pred_idx],
|
| 559 |
|
| 560 |
-
|
|
|
|
| 561 |
round(
|
| 562 |
float(
|
| 563 |
avg_probs[pred_idx]
|
| 564 |
-
)
|
| 565 |
-
|
| 566 |
),
|
| 567 |
|
| 568 |
-
"
|
| 569 |
|
| 570 |
LABELS[i]:
|
| 571 |
round(
|
| 572 |
float(
|
| 573 |
avg_probs[i]
|
| 574 |
-
)
|
| 575 |
-
|
| 576 |
)
|
| 577 |
|
| 578 |
for i in range(NUM_CLASSES)
|
|
|
|
| 11 |
import torchvision.models as models
|
| 12 |
|
| 13 |
from fastapi import FastAPI, UploadFile, File
|
| 14 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
from fastapi.responses import JSONResponse
|
| 16 |
|
| 17 |
# =========================================
|
|
|
|
| 39 |
version="1.0"
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# =========================================
|
| 43 |
+
# CORS
|
| 44 |
+
# =========================================
|
| 45 |
+
app.add_middleware(
|
| 46 |
+
CORSMiddleware,
|
| 47 |
+
allow_origins=["*"],
|
| 48 |
+
allow_credentials=True,
|
| 49 |
+
allow_methods=["*"],
|
| 50 |
+
allow_headers=["*"],
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
# =========================================
|
| 54 |
# LOAD NIFTI
|
| 55 |
# =========================================
|
|
|
|
| 358 |
|
| 359 |
# =========================================
|
| 360 |
# SINGLE PREDICTION
|
| 361 |
+
# NOTE: All confidence and probability values are 0.0–1.0 (NOT percentages)
|
| 362 |
# =========================================
|
| 363 |
def predict_model(model, tensor):
|
| 364 |
|
|
|
|
| 385 |
"class_id":
|
| 386 |
pred_idx,
|
| 387 |
|
| 388 |
+
# Confidence as 0.0–1.0 decimal (NOT percentage)
|
| 389 |
"confidence":
|
| 390 |
round(
|
| 391 |
float(
|
| 392 |
probs[pred_idx]
|
| 393 |
+
),
|
| 394 |
+
4
|
| 395 |
),
|
| 396 |
|
| 397 |
"probabilities": {
|
|
|
|
| 400 |
round(
|
| 401 |
float(
|
| 402 |
probs[i]
|
| 403 |
+
),
|
| 404 |
+
4
|
| 405 |
)
|
| 406 |
|
| 407 |
for i in range(NUM_CLASSES)
|
|
|
|
| 410 |
|
| 411 |
return result, probs
|
| 412 |
|
| 413 |
+
# =========================================
|
| 414 |
+
# HEALTH ENDPOINT
|
| 415 |
+
# =========================================
|
| 416 |
+
@app.get("/health")
|
| 417 |
+
def health():
|
| 418 |
+
|
| 419 |
+
return {
|
| 420 |
+
"status": "running",
|
| 421 |
+
"service": "Parkinson DaTscan Ensemble API",
|
| 422 |
+
"models_loaded": {
|
| 423 |
+
"densenet121": True,
|
| 424 |
+
"densenet169": True,
|
| 425 |
+
"densenet201": True,
|
| 426 |
+
"cnn3d": True,
|
| 427 |
+
},
|
| 428 |
+
"device": str(DEVICE),
|
| 429 |
+
"classes": LABELS,
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
# =========================================
|
| 433 |
# ROOT
|
| 434 |
# =========================================
|
|
|
|
| 441 |
"Parkinson DATSCAN Ensemble API Running",
|
| 442 |
|
| 443 |
"classes":
|
| 444 |
+
LABELS,
|
| 445 |
+
|
| 446 |
+
"endpoints": [
|
| 447 |
+
"/health",
|
| 448 |
+
"/predict/densenet121",
|
| 449 |
+
"/predict/densenet169",
|
| 450 |
+
"/predict/densenet201",
|
| 451 |
+
"/predict/cnn3d",
|
| 452 |
+
"/predict/ensemble",
|
| 453 |
+
]
|
| 454 |
}
|
| 455 |
|
| 456 |
# =========================================
|
|
|
|
| 547 |
|
| 548 |
# =========================================
|
| 549 |
# ENSEMBLE ENDPOINT
|
| 550 |
+
# Returns ensemble confidence as 0.0-1.0 with individual model breakdown
|
| 551 |
# =========================================
|
| 552 |
@app.post("/predict/ensemble")
|
| 553 |
async def predict_ensemble(
|
|
|
|
| 595 |
avg_probs
|
| 596 |
).item()
|
| 597 |
|
| 598 |
+
# All probability values as 0.0–1.0
|
| 599 |
final_result = {
|
| 600 |
|
| 601 |
+
"prediction":
|
| 602 |
LABELS[pred_idx],
|
| 603 |
|
| 604 |
+
# Ensemble confidence as 0.0–1.0 (NOT percentage)
|
| 605 |
+
"confidence":
|
| 606 |
round(
|
| 607 |
float(
|
| 608 |
avg_probs[pred_idx]
|
| 609 |
+
),
|
| 610 |
+
4
|
| 611 |
),
|
| 612 |
|
| 613 |
+
"probabilities": {
|
| 614 |
|
| 615 |
LABELS[i]:
|
| 616 |
round(
|
| 617 |
float(
|
| 618 |
avg_probs[i]
|
| 619 |
+
),
|
| 620 |
+
4
|
| 621 |
)
|
| 622 |
|
| 623 |
for i in range(NUM_CLASSES)
|