ehsanwebdev99 commited on
Commit
b3bface
·
verified ·
1 Parent(s): a39dc0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -23
app.py CHANGED
@@ -1,23 +1,55 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
-
4
- # Load your Hugging Face model using transformers pipeline for image classification
5
- classifier = pipeline("image-classification", model="anismizi/skin-type-classifier")
6
-
7
- def analyze_skin(image):
8
- # Run inference
9
- results = classifier(image)
10
- # Format results for display or API response
11
- return {res['label']: float(res['score']) for res in results}
12
-
13
- # Create Gradio Interface
14
- iface = gr.Interface(
15
- fn=analyze_skin,
16
- inputs=gr.Image(type="pil"),
17
- outputs=gr.Label(num_top_classes=2),
18
- title="Skin Condition Analyzer",
19
- description="Classify skin as dry or oily from image."
20
- )
21
-
22
- if __name__ == "__main__":
23
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()