Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,55 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from transformers import AutoModelForImageClassification
|
| 6 |
+
|
| 7 |
+
# Load the model (no AutoImageProcessor since it is unsupported)
|
| 8 |
+
model_name = "anismizi/skin-type-classifier"
|
| 9 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 10 |
+
model.eval() # Set model to eval mode
|
| 11 |
+
|
| 12 |
+
# Define manual preprocessing transforms similar to ResNet50 training
|
| 13 |
+
preprocess = transforms.Compose([
|
| 14 |
+
transforms.Resize(256),
|
| 15 |
+
transforms.CenterCrop(224),
|
| 16 |
+
transforms.ToTensor(),
|
| 17 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 18 |
+
std=[0.229, 0.224, 0.225]),
|
| 19 |
+
])
|
| 20 |
+
|
| 21 |
+
# Labels according to model info
|
| 22 |
+
labels = ["dry", "oily"]
|
| 23 |
+
|
| 24 |
+
def analyze_skin(image: Image.Image):
|
| 25 |
+
# Convert input image to RGB
|
| 26 |
+
image = image.convert("RGB")
|
| 27 |
+
# Preprocess image
|
| 28 |
+
input_tensor = preprocess(image)
|
| 29 |
+
# Add batch dimension
|
| 30 |
+
input_batch = input_tensor.unsqueeze(0)
|
| 31 |
+
|
| 32 |
+
# Run inference
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
outputs = model(input_batch)
|
| 35 |
+
logits = outputs.logits
|
| 36 |
+
probabilities = torch.nn.functional.softmax(logits, dim=1)
|
| 37 |
+
confidence, predicted_idx = torch.max(probabilities, dim=1)
|
| 38 |
+
|
| 39 |
+
predicted_label = labels[predicted_idx.item()]
|
| 40 |
+
confidence_score = confidence.item()
|
| 41 |
+
|
| 42 |
+
# Format results for display and API output
|
| 43 |
+
return {predicted_label: confidence_score}
|
| 44 |
+
|
| 45 |
+
# Create Gradio interface
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=analyze_skin,
|
| 48 |
+
inputs=gr.Image(type="pil"),
|
| 49 |
+
outputs=gr.Label(num_top_classes=2),
|
| 50 |
+
title="Skin Condition Analyzer",
|
| 51 |
+
description="Classify skin as dry or oily from an image."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
iface.launch()
|