DumbledoreWiz commited on
Commit
85205f1
·
verified ·
1 Parent(s): 538c0a4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import ViTForImageClassification, ViTFeatureExtractor
3
+ import gradio as gr
4
+ from PIL import Image
5
+
6
+ # Define the class labels as used during training
7
+ labels = ['Leggings', 'Jogger', 'Palazzo', 'Cargo', 'Dresspants', 'Chinos']
8
+
9
+ # Load the ViT model and feature extractor
10
+ model = ViTForImageClassification.from_pretrained("DumbledoreWiz/PantsShape")
11
+ feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224-in21k")
12
+
13
+ # Set the model to evaluation mode
14
+ model.eval()
15
+
16
+ # Define the prediction function
17
+ def predict(image):
18
+ # Preprocess the image
19
+ inputs = feature_extractor(images=image, return_tensors="pt")
20
+
21
+ with torch.no_grad():
22
+ outputs = model(**inputs)
23
+ logits = outputs.logits
24
+ probabilities = torch.nn.functional.softmax(logits[0], dim=0)
25
+
26
+ # Prepare the output dictionary
27
+ result = {labels[i]: float(probabilities[i]) for i in range(len(labels))}
28
+ return result
29
+
30
+ # Set up the Gradio Interface
31
+ gradio_app = gr.Interface(
32
+ fn=predict,
33
+ inputs=gr.Image(type="pil"),
34
+ outputs=gr.Label(num_top_classes=6),
35
+ title="Pants Shape Classifier"
36
+ )
37
+
38
+ # Launch the app
39
+ if __name__ == "__main__":
40
+ gradio_app.launch()