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