Spaces:
Sleeping
Sleeping
File size: 2,240 Bytes
b3bface a0dcc35 523df69 a0dcc35 b3bface a0dcc35 b3bface a0dcc35 b3bface a0dcc35 b3bface a0dcc35 b3bface a0dcc35 b3bface |
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 |
import gradio as gr
from PIL import Image
import torch
from torchvision import transforms
from transformers import AutoModelForImageClassification
# Define model names and corresponding labels
MODEL_CONFIGS = [
{
"name": "anismizi/skin-type-classifier",
"labels": ["dry", "oily"],
"key": "oil_vs_dry"
},
{
"name": "imfarzanansari/skintelligent-acne",
"labels": ["no_acne", "acne"],
"key": "acne"
},
{
"name": "imfarzanansari/skintelligent-wrinkles",
"labels": ["no_wrinkles", "wrinkles"],
"key": "wrinkles"
},
]
# Load all models at startup
MODELS = []
for config in MODEL_CONFIGS:
model = AutoModelForImageClassification.from_pretrained(config["name"])
model.eval()
MODELS.append(model)
# Common preprocessing (adjust if any model requires different input specs)
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
def analyze_skin(image: Image.Image):
image = image.convert("RGB")
input_tensor = preprocess(image)
input_batch = input_tensor.unsqueeze(0) # add batch dimension
results = {}
with torch.no_grad():
for idx, config in enumerate(MODEL_CONFIGS):
model, labels, key = MODELS[idx], config["labels"], config["key"]
outputs = model(input_batch)
logits = outputs.logits
probs = torch.softmax(logits, dim=1)
confidence, pred_idx = torch.max(probs, dim=1)
predicted_label = labels[pred_idx.item()]
confidence_score = confidence.item()
results[key] = {
"label": predicted_label,
"confidence": f"{confidence_score:.2%}"
}
return results
iface = gr.Interface(
fn=analyze_skin,
inputs=gr.Image(type="pil"),
outputs=gr.JSON(label="Skin Analysis Results"),
title="Comprehensive Skin Condition Analyzer",
description="Classifies skin image for oily/dry, acne, redness, wrinkles using multiple models."
)
if __name__ == "__main__":
iface.launch()
|