rapacious commited on
Commit
51fc0b4
·
verified ·
1 Parent(s): 59b9fd9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import ViTHybridImageProcessor, ViTHybridForImageClassification
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Load model and processor
7
+ model_name = "google/vit-hybrid-base-bit-384"
8
+ feature_extractor = ViTHybridImageProcessor.from_pretrained(model_name)
9
+ model = ViTHybridForImageClassification.from_pretrained(model_name)
10
+
11
+ # Function for prediction
12
+ def classify_image(image):
13
+ inputs = feature_extractor(images=image, return_tensors="pt")
14
+ with torch.no_grad():
15
+ outputs = model(**inputs)
16
+ logits = outputs.logits
17
+ predicted_class_idx = logits.argmax(-1).item()
18
+ return model.config.id2label[predicted_class_idx]
19
+
20
+ # Gradio UI
21
+ iface = gr.Interface(
22
+ fn=classify_image,
23
+ inputs=gr.Image(type="pil"),
24
+ outputs="text",
25
+ title="ViT-Hybrid Image Classifier",
26
+ description="Upload an image to classify it using the ViT-Hybrid model.",
27
+ )
28
+
29
+ if __name__ == "__main__":
30
+ iface.launch()