Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
processor = AutoImageProcessor.from_pretrained("anasmkh/customied_vit")
|
| 7 |
+
model = AutoModelForImageClassification.from_pretrained("anasmkh/customied_vit")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def classify_image(image):
|
| 11 |
+
|
| 12 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
logits = outputs.logits
|
| 18 |
+
probs = torch.softmax(logits, dim=1)[0]
|
| 19 |
+
best_idx = torch.argmax(probs).item()
|
| 20 |
+
label = model.config.id2label[best_idx]
|
| 21 |
+
score = float(probs[best_idx])
|
| 22 |
+
|
| 23 |
+
return {label: score}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
demo = gr.Interface(
|
| 27 |
+
fn=classify_image,
|
| 28 |
+
inputs=gr.Image(type="pil"),
|
| 29 |
+
outputs=gr.Label(num_top_classes=3),
|
| 30 |
+
title="Custom Vision Transformer Classifier",
|
| 31 |
+
description="Upload an image to get classification results from the custom ViT model."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch(share=True)
|