Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the pipeline once
|
| 5 |
+
pipe = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 6 |
+
|
| 7 |
+
# Define function for Gradio
|
| 8 |
+
def classify_image(img):
|
| 9 |
+
results = pipe(img)
|
| 10 |
+
return {res["label"]: float(res["score"]) for res in results}
|
| 11 |
+
|
| 12 |
+
# Gradio interface
|
| 13 |
+
demo = gr.Interface(
|
| 14 |
+
fn=classify_image,
|
| 15 |
+
inputs=gr.Image(type="filepath"), # user uploads/selects image
|
| 16 |
+
outputs=gr.Label(num_top_classes=5), # show top 5 predictions with scores
|
| 17 |
+
title="Image Classification with ViT",
|
| 18 |
+
description="Upload an image (e.g., SHrek.png) and see predicted labels."
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Launch
|
| 22 |
+
demo.launch()
|