Upload 2 files
Browse files- app.py +29 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import ViTImageProcessor, ViTForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load model and processor
|
| 7 |
+
model_name = "google/vit-base-patch16-224"
|
| 8 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
| 9 |
+
processor = ViTImageProcessor.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
def classify_image(image):
|
| 12 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
logits = outputs.logits
|
| 16 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 17 |
+
predicted_label = model.config.id2label[predicted_class_idx]
|
| 18 |
+
return predicted_label
|
| 19 |
+
|
| 20 |
+
demo = gr.Interface(
|
| 21 |
+
fn=classify_image,
|
| 22 |
+
inputs=gr.Image(type="pil"),
|
| 23 |
+
outputs="text",
|
| 24 |
+
title="ViT Image Classifier",
|
| 25 |
+
description="Upload an image to classify it using Vision Transformer (ViT)."
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
gradio
|
| 3 |
+
torch
|
| 4 |
+
Pillow
|
| 5 |
+
datasets
|