Arpit1234 commited on
Commit
2ea9cf6
·
verified ·
1 Parent(s): 81713bd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import ViTFeatureExtractor, ViTForImageClassification
2
+ from PIL import Image
3
+ import torch
4
+ import gradio as gr
5
+
6
+ # Load the model and feature extractor
7
+ model_name = 'google/vit-base-patch16-224'
8
+ feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
9
+ model = ViTForImageClassification.from_pretrained(model_name)
10
+
11
+ # Function to load and preprocess the image
12
+ def preprocess_image(image):
13
+ inputs = feature_extractor(images=image, return_tensors="pt")
14
+ return inputs['pixel_values']
15
+
16
+ # Function to predict the class of the image
17
+ def predict_image(image):
18
+ pixel_values = preprocess_image(image)
19
+ with torch.no_grad():
20
+ outputs = model(pixel_values)
21
+ logits = outputs.logits
22
+ predicted_class_idx = logits.argmax(-1).item()
23
+ return model.config.id2label[predicted_class_idx]
24
+
25
+ # Define the Gradio interface
26
+ image_input = gr.inputs.Image(type="pil")
27
+ label_output = gr.outputs.Label(num_top_classes=3)
28
+
29
+ interface = gr.Interface(
30
+ fn=predict_image,
31
+ inputs=image_input,
32
+ outputs=label_output,
33
+ title="Image Classification with ViT",
34
+ description="Upload an image and get the predicted label using Vision Transformer (ViT)."
35
+ )
36
+
37
+ # Launch the interface
38
+ if __name__ == "__main__":
39
+ interface.launch()