Spaces:
Sleeping
Sleeping
Arghya Ghosh commited on
Update api/alzheimers.py
Browse files- api/alzheimers.py +57 -54
api/alzheimers.py
CHANGED
|
@@ -1,54 +1,57 @@
|
|
| 1 |
-
# app/api/alzheimers.py
|
| 2 |
-
from fastapi import APIRouter, UploadFile, File, HTTPException
|
| 3 |
-
from core.utils import preprocess_image_tf, generate_gemini_insights
|
| 4 |
-
from functools import lru_cache
|
| 5 |
-
import numpy as np
|
| 6 |
-
import tensorflow as tf
|
| 7 |
-
import os
|
| 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 |
-
if
|
| 36 |
-
raise HTTPException(status_code=
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/api/alzheimers.py
|
| 2 |
+
from fastapi import APIRouter, UploadFile, File, HTTPException
|
| 3 |
+
from core.utils import preprocess_image_tf, generate_gemini_insights
|
| 4 |
+
from functools import lru_cache
|
| 5 |
+
import numpy as np
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
import os
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
|
| 10 |
+
router = APIRouter()
|
| 11 |
+
|
| 12 |
+
CLASS_LABELS = [
|
| 13 |
+
"Mild Dementia",
|
| 14 |
+
"Moderate Dementia",
|
| 15 |
+
"Non Demented",
|
| 16 |
+
"Very Mild Dementia",
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@lru_cache(maxsize=1)
|
| 21 |
+
def load_model():
|
| 22 |
+
model_path = hf_hub_download(
|
| 23 |
+
repo_id="uiuxarghya/test-store",
|
| 24 |
+
filename="models/alzheimers.h5",
|
| 25 |
+
repo_type="dataset"
|
| 26 |
+
)
|
| 27 |
+
return tf.keras.models.load_model(model_path) # type: ignore
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@router.post(
|
| 31 |
+
"/analyze", name="smritiyaan", description="Analyze Alzheimer's from MRI images"
|
| 32 |
+
)
|
| 33 |
+
async def analyze(file: UploadFile = File(...)):
|
| 34 |
+
contents = await file.read()
|
| 35 |
+
if file.content_type not in ["image/jpeg", "image/png"]:
|
| 36 |
+
raise HTTPException(status_code=400, detail="Only JPG/PNG supported.")
|
| 37 |
+
|
| 38 |
+
if len(contents) > 5 * 1024 * 1024:
|
| 39 |
+
raise HTTPException(status_code=413, detail="Image too large. Max size 5MB.")
|
| 40 |
+
|
| 41 |
+
input_tensor = preprocess_image_tf(contents)
|
| 42 |
+
model = load_model()
|
| 43 |
+
predictions = model.predict(input_tensor)
|
| 44 |
+
confidences = predictions[0].tolist()
|
| 45 |
+
predicted_class = CLASS_LABELS[np.argmax(confidences)]
|
| 46 |
+
confidence_dict = {
|
| 47 |
+
label: float(conf) for label, conf in zip(CLASS_LABELS, confidences)
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
insights = await generate_gemini_insights(
|
| 51 |
+
predicted_class, confidence_dict, mode="alzheimers"
|
| 52 |
+
)
|
| 53 |
+
return {
|
| 54 |
+
"predictedClass": predicted_class,
|
| 55 |
+
"confidences": confidence_dict,
|
| 56 |
+
"insights": insights,
|
| 57 |
+
}
|