Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# 1. Choose a Hugging Face image classification model
|
| 5 |
+
# You can change this later (for digits, beans, general images etc.)
|
| 6 |
+
MODEL_ID = "nateraw/vit-base-beans" # demo model trained on 3 types of beans
|
| 7 |
+
|
| 8 |
+
# 2. Create a pipeline (this downloads and loads the model)
|
| 9 |
+
classifier = pipeline("image-classification", model=MODEL_ID)
|
| 10 |
+
|
| 11 |
+
# 3. Define a prediction function Gradio will use
|
| 12 |
+
def predict(image):
|
| 13 |
+
"""
|
| 14 |
+
image: PIL.Image (Gradio handles conversion)
|
| 15 |
+
returns: dict {label: score} for the top classes
|
| 16 |
+
"""
|
| 17 |
+
results = classifier(image)
|
| 18 |
+
|
| 19 |
+
# Gradio's Label component likes a dict: {label: confidence}
|
| 20 |
+
output = {r["label"]: float(r["score"]) for r in results}
|
| 21 |
+
return output
|
| 22 |
+
|
| 23 |
+
# 4. Build the Gradio Interface
|
| 24 |
+
demo = gr.Interface(
|
| 25 |
+
fn=predict,
|
| 26 |
+
inputs=gr.Image(type="pil", label="Upload or draw an image"),
|
| 27 |
+
outputs=gr.Label(num_top_classes=3, label="Predictions"),
|
| 28 |
+
title="Vision Transformer Image Classifier",
|
| 29 |
+
description=(
|
| 30 |
+
"Upload a small image and this app will classify it using a pretrained "
|
| 31 |
+
"Vision Transformer model from Hugging Face."
|
| 32 |
+
),
|
| 33 |
+
examples=[], # you can add example images later
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# 5. Run the app (Spaces will call launch() automatically)
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
demo.launch()
|