Spaces:
Runtime error
Runtime error
Tantawi65 commited on
Commit ·
84fdd79
1
Parent(s): 2822f6c
Fix: Embed model loading and prediction directly in main.py
Browse files
main.py
CHANGED
|
@@ -5,18 +5,97 @@ from fastapi import FastAPI, File, UploadFile, HTTPException
|
|
| 5 |
from fastapi.responses import JSONResponse
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
|
| 8 |
-
#
|
| 9 |
import sys
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
app = FastAPI(
|
| 22 |
title="GP-Tea Skin Analysis API",
|
|
|
|
| 5 |
from fastapi.responses import JSONResponse
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
|
| 8 |
+
# Embedded prediction function with model loading
|
| 9 |
import sys
|
| 10 |
+
import numpy as np
|
| 11 |
+
import tensorflow as tf
|
| 12 |
+
from tensorflow.keras.preprocessing import image
|
| 13 |
+
from huggingface_hub import hf_hub_download
|
| 14 |
|
| 15 |
+
# Model configuration
|
| 16 |
+
MODEL_PATH = "app/model/efficientnetv2s.h5"
|
| 17 |
+
REPO_ID = "Miguel764/efficientnetv2s-skin-cancer-classifier"
|
| 18 |
+
FILENAME = "efficientnetv2s.h5"
|
| 19 |
+
TEMPERATURE = 2.77
|
| 20 |
+
|
| 21 |
+
class_names_mapping = {
|
| 22 |
+
0: "AKIEC",
|
| 23 |
+
1: "BCC",
|
| 24 |
+
2: "BKL",
|
| 25 |
+
3: "DF",
|
| 26 |
+
4: "MEL",
|
| 27 |
+
5: "NV",
|
| 28 |
+
6: "VASC"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
full_names = {
|
| 32 |
+
"AKIEC": "Actinic keratoses and intraepithelial carcinoma",
|
| 33 |
+
"BCC": "Basal cell carcinoma",
|
| 34 |
+
"BKL": "Benign keratosis-like lesions",
|
| 35 |
+
"DF": "Dermatofibroma",
|
| 36 |
+
"MEL": "Melanoma",
|
| 37 |
+
"NV": "Melanocytic nevi",
|
| 38 |
+
"VASC": "Vascular lesions"
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
# Global model variable
|
| 42 |
+
model = None
|
| 43 |
+
|
| 44 |
+
def load_model():
|
| 45 |
+
global model
|
| 46 |
+
try:
|
| 47 |
+
if not os.path.exists(MODEL_PATH):
|
| 48 |
+
print("Model not found locally. Downloading from Hugging Face...")
|
| 49 |
+
os.makedirs(os.path.dirname(MODEL_PATH), exist_ok=True)
|
| 50 |
+
hf_hub_download(
|
| 51 |
+
repo_id=REPO_ID,
|
| 52 |
+
filename=FILENAME,
|
| 53 |
+
local_dir="app/model"
|
| 54 |
+
)
|
| 55 |
+
else:
|
| 56 |
+
print("Model already exists locally.")
|
| 57 |
+
|
| 58 |
+
print("Loading TensorFlow model...")
|
| 59 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 60 |
+
print("Model loaded successfully!")
|
| 61 |
+
return model
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"Error loading model: {e}")
|
| 64 |
+
return None
|
| 65 |
+
|
| 66 |
+
def predict_image(image_path):
|
| 67 |
+
global model
|
| 68 |
+
try:
|
| 69 |
+
if model is None:
|
| 70 |
+
model = load_model()
|
| 71 |
+
if model is None:
|
| 72 |
+
return "Error: Model not loaded", 0.0, [{"label": "Error", "confidence": 0.0}]
|
| 73 |
+
|
| 74 |
+
# Load and preprocess image
|
| 75 |
+
img = image.load_img(image_path, target_size=(224, 224))
|
| 76 |
+
img_array = image.img_to_array(img)
|
| 77 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 78 |
+
img_array = tf.keras.applications.imagenet_utils.preprocess_input(img_array)
|
| 79 |
+
|
| 80 |
+
# Make prediction
|
| 81 |
+
logits = model.predict(img_array)
|
| 82 |
+
scaled_logits = logits / TEMPERATURE
|
| 83 |
+
scaled_probs = tf.nn.softmax(scaled_logits).numpy()[0]
|
| 84 |
+
|
| 85 |
+
class_idx = int(np.argmax(scaled_probs))
|
| 86 |
+
top_label = full_names[class_names_mapping[class_idx]]
|
| 87 |
+
top_confidence = float(scaled_probs[class_idx])
|
| 88 |
+
|
| 89 |
+
all_predictions = [
|
| 90 |
+
{"label": class_names_mapping[i], "confidence": float(pred)}
|
| 91 |
+
for i, pred in enumerate(scaled_probs)
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
return top_label, top_confidence, all_predictions
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"Prediction error: {e}")
|
| 98 |
+
return f"Error: {str(e)}", 0.0, [{"label": "Error", "confidence": 0.0}]
|
| 99 |
|
| 100 |
app = FastAPI(
|
| 101 |
title="GP-Tea Skin Analysis API",
|