Spaces:
Running
Running
Update model_loader.py
Browse files- model_loader.py +67 -1
model_loader.py
CHANGED
|
@@ -189,6 +189,72 @@ def load_skin_models():
|
|
| 189 |
print(f"[Skin] Ensemble ready β {len(_skin_models)} models")
|
| 190 |
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
def predict_skin(image: Image.Image) -> dict:
|
| 193 |
import torch
|
| 194 |
import torch.nn.functional as F
|
|
@@ -211,4 +277,4 @@ def predict_skin(image: Image.Image) -> dict:
|
|
| 211 |
"confidence": round(float(avg[top]), 4),
|
| 212 |
"probabilities": {c: round(float(p), 4) for c, p in zip(SKIN_CLASSES, avg)},
|
| 213 |
"model_count": len(_skin_models),
|
| 214 |
-
}
|
|
|
|
| 189 |
print(f"[Skin] Ensemble ready β {len(_skin_models)} models")
|
| 190 |
|
| 191 |
|
| 192 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 193 |
+
# DIABETES β Keras ANN
|
| 194 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 195 |
+
|
| 196 |
+
DIABETES_MODEL_PATH = "/app/diabetes_model.h5"
|
| 197 |
+
DIABETES_SCALER_PATH = "/app/diabetes_scaler.json"
|
| 198 |
+
DIABETES_FEATURE_COLS = [
|
| 199 |
+
"Pregnancies", "Glucose", "BloodPressure", "SkinThickness",
|
| 200 |
+
"Insulin", "BMI", "DiabetesPedigreeFunction", "Age"
|
| 201 |
+
]
|
| 202 |
+
_diabetes_model = None
|
| 203 |
+
_diabetes_scaler = None
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def load_diabetes_model():
|
| 207 |
+
global _diabetes_model, _diabetes_scaler
|
| 208 |
+
if _diabetes_model is not None:
|
| 209 |
+
return
|
| 210 |
+
import json
|
| 211 |
+
import tensorflow as tf
|
| 212 |
+
|
| 213 |
+
print("[Diabetes] Loading model...")
|
| 214 |
+
_diabetes_model = tf.keras.models.load_model(DIABETES_MODEL_PATH)
|
| 215 |
+
|
| 216 |
+
with open(DIABETES_SCALER_PATH) as f:
|
| 217 |
+
params = json.load(f)
|
| 218 |
+
_diabetes_scaler = {
|
| 219 |
+
"mean": np.array(params["mean"]),
|
| 220 |
+
"scale": np.array(params["scale"]),
|
| 221 |
+
}
|
| 222 |
+
print("[Diabetes] β Ready")
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def predict_diabetes(input_data: dict) -> dict:
|
| 226 |
+
load_diabetes_model()
|
| 227 |
+
|
| 228 |
+
# Map frontend keys to training feature names
|
| 229 |
+
key_map = {
|
| 230 |
+
"pregnancies": "Pregnancies",
|
| 231 |
+
"glucose": "Glucose",
|
| 232 |
+
"blood_pressure": "BloodPressure",
|
| 233 |
+
"skin_thickness": "SkinThickness",
|
| 234 |
+
"insulin": "Insulin",
|
| 235 |
+
"bmi": "BMI",
|
| 236 |
+
"diabetes_pedigree": "DiabetesPedigreeFunction",
|
| 237 |
+
"age": "Age",
|
| 238 |
+
}
|
| 239 |
+
arr = np.array([[
|
| 240 |
+
float(input_data[k]) for k in key_map.keys()
|
| 241 |
+
]], dtype=np.float32)
|
| 242 |
+
|
| 243 |
+
# Scale using saved scaler params
|
| 244 |
+
arr = (arr - _diabetes_scaler["mean"]) / _diabetes_scaler["scale"]
|
| 245 |
+
|
| 246 |
+
prob = float(_diabetes_model.predict(arr, verbose=0)[0][0])
|
| 247 |
+
|
| 248 |
+
return {
|
| 249 |
+
"label": "DIABETIC" if prob >= 0.5 else "NON-DIABETIC",
|
| 250 |
+
"confidence": round(prob if prob >= 0.5 else 1 - prob, 4),
|
| 251 |
+
"probabilities": {
|
| 252 |
+
"NON-DIABETIC": round(1 - prob, 4),
|
| 253 |
+
"DIABETIC": round(prob, 4),
|
| 254 |
+
},
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
|
| 258 |
def predict_skin(image: Image.Image) -> dict:
|
| 259 |
import torch
|
| 260 |
import torch.nn.functional as F
|
|
|
|
| 277 |
"confidence": round(float(avg[top]), 4),
|
| 278 |
"probabilities": {c: round(float(p), 4) for c, p in zip(SKIN_CLASSES, avg)},
|
| 279 |
"model_count": len(_skin_models),
|
| 280 |
+
}
|