Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import ViTForImageClassification, ViTImageProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# মডেল ও প্রসেসর লোড করো
|
| 7 |
+
model = ViTForImageClassification.from_pretrained("rakib730/vit-base-oxford-iiit-pets")
|
| 8 |
+
processor = ViTImageProcessor.from_pretrained("rakib730/vit-base-oxford-iiit-pets")
|
| 9 |
+
|
| 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 |
+
label = model.config.id2label[predicted_class_idx]
|
| 18 |
+
return label
|
| 19 |
+
|
| 20 |
+
# Gradio UI
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
fn=classify_image,
|
| 23 |
+
inputs=gr.Image(type="pil"),
|
| 24 |
+
outputs="label",
|
| 25 |
+
title="Oxford-IIIT Pets Classifier",
|
| 26 |
+
description="Upload a pet image to classify its breed using ViT."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
iface.launch()
|