Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +25 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load an image classification pipeline
|
| 6 |
+
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 7 |
+
|
| 8 |
+
def classify_image(img, top_k=3):
|
| 9 |
+
if img is None:
|
| 10 |
+
return {"Error": 1.0}
|
| 11 |
+
results = classifier(img, top_k=top_k)
|
| 12 |
+
# Return as {label: score} for Gradio Label component
|
| 13 |
+
return {r["label"]: float(r["score"]) for r in results}
|
| 14 |
+
|
| 15 |
+
# Gradio interface
|
| 16 |
+
demo = gr.Interface(
|
| 17 |
+
fn=classify_image,
|
| 18 |
+
inputs=[gr.Image(type="pil", label="Upload Image"), gr.Slider(1, 5, value=3, label="Top K Predictions")],
|
| 19 |
+
outputs=gr.Label(num_top_classes=5, label="Predictions"),
|
| 20 |
+
title="Image Classification App",
|
| 21 |
+
description="Upload an image and the model will predict the top objects in it."
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
if __name__ == "__main__":
|
| 25 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.30
|
| 2 |
+
transformers>=4.40
|
| 3 |
+
torch>=1.13
|
| 4 |
+
pillow
|