Spaces:
Sleeping
Sleeping
File size: 7,923 Bytes
94fe463 f25eac2 94fe463 289d09a 94fe463 3f0c576 94fe463 3f0c576 289d09a 94fe463 3f0c576 289d09a 682f3ef 289d09a 3f0c576 94fe463 289d09a 94fe463 3f0c576 94fe463 3f0c576 289d09a 94fe463 |
1 2 3 4 5 6 7 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.middleware.cors import CORSMiddleware
import numpy as np
import tensorflow as tf
from PIL import Image
import io
import keras
import os
import gdown
from keras.saving import register_keras_serializable
# Enable unsafe deserialization for custom Lambda layers
keras.config.enable_unsafe_deserialization()
# --- CUSTOM FUNCTION ---
@register_keras_serializable(package="Custom", name="CustomMaxPool")
def custom_max_pool(x):
return tf.reduce_max(x, axis=[1, 2], keepdims=True)
# --- CONFIG ---
IMG_SIZE = 224
NUM_CLASSES = 23
USE_METADATA_ADJUSTMENT = True # Toggle on/off
MODEL_PATHS = {
"EfficientNetB3": "saved_models/EfficientNetB3_recovered.keras",
"ResNet50": "saved_models/ResNet50_recovered.keras",
"MobileNetV2": "saved_models/MobileNetV2_recovered.keras",
"DenseNet121": "saved_models/DenseNet121_recovered.keras"
}
MODEL_URLS = {
"EfficientNetB3": "https://drive.google.com/uc?id=1jP4-HoFFbGIFugqgRpVVt0V3LhOoKVkY",
"ResNet50": "https://drive.google.com/uc?id=1yv4duVkGHTyLEpw92CCJcUy9Y1VxC6Ec",
"MobileNetV2": "https://drive.google.com/uc?id=1fJtogp6fH7F2Wa2YvN_KTklgK2-ufqMN",
"DenseNet121": "https://drive.google.com/uc?id=1lJ0nlTP7cMTglEM6XIaTvAEZHVJ4dsN8"
}
MODEL_WEIGHTS = {
"EfficientNetB3": 0.260,
"ResNet50": 0.256,
"MobileNetV2": 0.222,
"DenseNet121": 0.261
}
PREPROCESS_FUNCS = {
"EfficientNetB3": tf.keras.applications.efficientnet.preprocess_input,
"ResNet50": tf.keras.applications.resnet.preprocess_input,
"MobileNetV2": tf.keras.applications.mobilenet_v2.preprocess_input,
"DenseNet121": tf.keras.applications.densenet.preprocess_input
}
# --- FASTAPI SETUP ---
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def root():
return {"status": "🩺 App is running."}
models = {}
@app.on_event("startup")
def load_models():
global models
os.makedirs("saved_models", exist_ok=True)
for name, path in MODEL_PATHS.items():
if not os.path.exists(path):
gdown.download(MODEL_URLS[name], path, quiet=False)
models[name] = tf.keras.models.load_model(path)
# --- UTILS ---
def read_imagefile(file) -> Image.Image:
image = Image.open(io.BytesIO(file))
return image.convert("RGB")
def predict_ensemble(image):
image = image.resize((IMG_SIZE, IMG_SIZE))
image_array = np.array(image)
ensemble_pred = np.zeros((NUM_CLASSES,))
for name, model in models.items():
preproc = PREPROCESS_FUNCS[name]
img_proc = preproc(image_array.copy())
img_proc = np.expand_dims(img_proc, axis=0)
pred = model.predict(img_proc, verbose=0)[0]
ensemble_pred += pred * MODEL_WEIGHTS[name]
return ensemble_pred
# --- ADJUST WITH METADATA ---
def adjust_with_metadata(predictions, metadata):
age = int(metadata.get("age", 30))
condition = metadata.get("condition", "").lower()
skin_type = metadata.get("skin_type", "").lower()
gender = metadata.get("gender", "").lower()
race = metadata.get("race", "").lower()
rules = [
{"keyword": "acne", "condition": age > 40, "factor": 0.6},
{"keyword": "acne", "condition": age < 25, "factor": 1.2},
{"keyword": "eczema", "condition": skin_type == "dry", "factor": 1.2},
{"keyword": "eczema", "condition": skin_type == "oily", "factor": 0.8},
{"keyword": "warts", "condition": age < 12, "factor": 1.3},
{"keyword": "fungal", "condition": "itchy" in condition, "factor": 1.2},
{"keyword": "hair loss", "condition": gender == "male" and age > 40, "factor": 1.3},
{"keyword": "lupus", "condition": gender == "female", "factor": 1.2},
{"keyword": "melanoma", "condition": skin_type == "light" and age > 50, "factor": 1.3},
{"keyword": "nail fungus", "condition": age > 60, "factor": 1.3},
{"keyword": "psoriasis", "condition": "flaky" in condition or "dry" in skin_type, "factor": 1.2},
{"keyword": "systemic", "condition": "fatigue" in condition or "pain" in condition, "factor": 1.2},
{"keyword": "urticaria", "condition": "allergy" in condition or "hives" in condition, "factor": 1.3},
{"keyword": "contact dermatitis", "condition": "red" in condition or "burning" in condition, "factor": 1.2},
{"keyword": "seborrheic", "condition": age > 60, "factor": 1.2},
{"keyword": "bullous", "condition": age > 60, "factor": 1.3},
{"keyword": "vasculitis", "condition": "joint" in condition or "swelling" in condition, "factor": 1.2},
{"keyword": "atopic dermatitis", "condition": age < 10, "factor": 1.2},
{"keyword": "pigmentation", "condition": skin_type == "dark", "factor": 1.3},
{"keyword": "hpv", "condition": age > 18, "factor": 1.2},
{"keyword": "vascular tumors", "condition": age < 5, "factor": 1.3},
{"keyword": "poison ivy", "condition": "rash" in condition or "camping" in condition, "factor": 1.3},
{"keyword": "bacterial", "condition": "pus" in condition or "fever" in condition, "factor": 1.3},
{"keyword": "drug eruption", "condition": "medication" in condition or "rash" in condition, "factor": 1.3},
{"keyword": "connective tissue", "condition": "joint" in condition or "fatigue" in condition, "factor": 1.3},
]
adjusted = []
for pred in predictions:
label = pred["label"]
score = pred["confidence"]
for rule in rules:
if rule["keyword"] in label.lower() and rule["condition"]:
score *= rule["factor"]
adjusted.append({"label": label, "confidence": score})
return sorted(adjusted, key=lambda x: x["confidence"], reverse=True)[:3]
# --- PREDICT ENDPOINT ---
@app.post("/predict/")
async def predict(
file: UploadFile = File(...),
age: int = Form(...),
race: str = Form(...),
gender: str = Form(...),
skin_color: str = Form(...),
skin_type: str = Form(...),
condition_description: str = Form(...)
):
image = read_imagefile(await file.read())
prediction = predict_ensemble(image)
class_labels = [
"Acne and Rosacea Photos",
"Actinic Keratosis Basal Cell Carcinoma and other Malignant Lesions",
"Atopic Dermatitis Photos",
"Bullous Disease Photos",
"Cellulitis Impetigo and other Bacterial Infections",
"Eczema Photos",
"Exanthems and Drug Eruptions",
"Hair Loss Photos Alopecia and other Hair Diseases",
"Herpes HPV and other STDs Photos",
"Light Diseases and Disorders of Pigmentation",
"Lupus and other Connective Tissue diseases",
"Melanoma Skin Cancer Nevi and Moles",
"Nail Fungus and other Nail Disease",
"Poison Ivy Photos and other Contact Dermatitis",
"Psoriasis pictures Lichen Planus and related diseases",
"Scabies Lyme Disease and other Infestations and Bites",
"Seborrheic Keratoses and other Benign Tumors",
"Systemic Disease",
"Tinea Ringworm Candidiasis and other Fungal Infections",
"Urticaria Hives",
"Vascular Tumors",
"Vasculitis Photos",
"Warts Molluscum and other Viral Infections"
]
top3_indices = prediction.argsort()[-3:][::-1]
top_preds = [
{"label": class_labels[i], "confidence": float(prediction[i])}
for i in top3_indices
]
metadata = {
"age": age,
"race": race,
"gender": gender,
"skin_color": skin_color,
"skin_type": skin_type,
"condition": condition_description
}
final_preds = adjust_with_metadata(top_preds, metadata) if USE_METADATA_ADJUSTMENT else top_preds
return {
"prediction": final_preds,
"metadata": metadata
}
|