BhoomiAgrawal commited on
Commit
978bdb5
·
verified ·
1 Parent(s): 5718511

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import ViTImageProcessor, ViTForImageClassification
2
+ from PIL import Image
3
+ import gradio as gr
4
+
5
+ # Load the model and processor
6
+ processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224')
7
+ model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224')
8
+
9
+ def predict(image):
10
+ inputs = processor(images=image, return_tensors="pt")
11
+ outputs = model(**inputs)
12
+ logits = outputs.logits
13
+ predicted_class_idx = logits.argmax(-1).item()
14
+ return model.config.id2label[predicted_class_idx]
15
+
16
+ def classify_image(image):
17
+ image = Image.fromarray(image.astype('uint8'), 'RGB')
18
+ label = predict(image)
19
+ return label
20
+
21
+ iface = gr.Interface(
22
+ fn=classify_image,
23
+ inputs=gr.Image(type="numpy", label="Upload an Image"),
24
+ outputs=gr.Textbox(label="Predicted Class"),
25
+ title="Image Classification with ViT",
26
+ description="Upload an image to classify it using the Vision Transformer (ViT) model."
27
+ )
28
+
29
+ if __name__ == "__main__":
30
+ iface.launch()