Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Load the pre-trained model and feature extractor
|
7 |
+
model_name = "google/vit-base-patch16-224"
|
8 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
9 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
10 |
+
|
11 |
+
# Define the prediction function
|
12 |
+
def predict(image):
|
13 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
14 |
+
outputs = model(**inputs)
|
15 |
+
logits = outputs.logits
|
16 |
+
predicted_class_idx = logits.argmax(-1).item()
|
17 |
+
return model.config.id2label[predicted_class_idx]
|
18 |
+
|
19 |
+
# Create the Gradio interface
|
20 |
+
iface = gr.Interface(
|
21 |
+
fn=predict,
|
22 |
+
inputs=gr.inputs.Sketchpad(),
|
23 |
+
outputs=gr.outputs.Label(),
|
24 |
+
title="Drawing Classifier",
|
25 |
+
description="Draw something and the model will try to identify it!"
|
26 |
+
)
|
27 |
+
|
28 |
+
# Launch the interface
|
29 |
+
iface.launch()
|