Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
2 |
+
from PIL import Image
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load the model and feature extractor
|
6 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
|
7 |
+
model = AutoModelForImageClassification.from_pretrained("google/vit-base-patch16-224")
|
8 |
+
|
9 |
+
# Define prediction function
|
10 |
+
def classify_image(image):
|
11 |
+
image = Image.fromarray(image).convert("RGB")
|
12 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
13 |
+
outputs = model(**inputs)
|
14 |
+
predicted_class = outputs.logits.argmax(-1).item()
|
15 |
+
return model.config.id2label[predicted_class]
|
16 |
+
|
17 |
+
# Create a Gradio app
|
18 |
+
app = gr.Interface(
|
19 |
+
fn=classify_image,
|
20 |
+
inputs=gr.Image(type="numpy"),
|
21 |
+
outputs="text",
|
22 |
+
title="Image Classifier"
|
23 |
+
)
|
24 |
+
|
25 |
+
app.launch()
|