Arghya Ghosh commited on
Commit
5f85430
·
verified ·
1 Parent(s): cf90603

Update api/alzheimers.py

Browse files
Files changed (1) hide show
  1. 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
- router = APIRouter()
10
-
11
- BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
12
- MODEL_PATH = os.path.join(BASE_DIR, "models", "alzheimers.h5")
13
-
14
- CLASS_LABELS = [
15
- "Mild Dementia",
16
- "Moderate Dementia",
17
- "Non Demented",
18
- "Very Mild Dementia",
19
- ]
20
-
21
-
22
- @lru_cache(maxsize=1)
23
- def load_model():
24
- return tf.keras.models.load_model(MODEL_PATH) # type: ignore
25
-
26
-
27
- @router.post(
28
- "/analyze", name="smritiyaan", description="Analyze Alzheimer's from MRI images"
29
- )
30
- async def analyze(file: UploadFile = File(...)):
31
- contents = await file.read()
32
- if file.content_type not in ["image/jpeg", "image/png"]:
33
- raise HTTPException(status_code=400, detail="Only JPG/PNG supported.")
34
-
35
- if len(contents) > 5 * 1024 * 1024:
36
- raise HTTPException(status_code=413, detail="Image too large. Max size 5MB.")
37
-
38
- input_tensor = preprocess_image_tf(contents)
39
- model = load_model()
40
- predictions = model.predict(input_tensor)
41
- confidences = predictions[0].tolist()
42
- predicted_class = CLASS_LABELS[np.argmax(confidences)]
43
- confidence_dict = {
44
- label: float(conf) for label, conf in zip(CLASS_LABELS, confidences)
45
- }
46
-
47
- insights = await generate_gemini_insights(
48
- predicted_class, confidence_dict, mode="alzheimers"
49
- )
50
- return {
51
- "predictedClass": predicted_class,
52
- "confidences": confidence_dict,
53
- "insights": insights,
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
+ }