Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import list_models
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Fetch timm models from Hugging Face Hub
|
| 6 |
+
timm_models = list_models(filter="timm", sort="downloads", limit=20) # Fetch top 20 based on downloads
|
| 7 |
+
model_ids = [model.modelId for model in timm_models]
|
| 8 |
+
|
| 9 |
+
# Initialize a pipeline with a default model
|
| 10 |
+
default_model = model_ids[0]
|
| 11 |
+
pipe = pipeline("image-classification", model=default_model)
|
| 12 |
+
|
| 13 |
+
# Function for classification
|
| 14 |
+
def classify(image, model_name):
|
| 15 |
+
pipe.model = model_name # Update model dynamically
|
| 16 |
+
results = pipe(image)
|
| 17 |
+
return {result["label"]: round(result["score"], 2) for result in results}
|
| 18 |
+
|
| 19 |
+
# Gradio Interface
|
| 20 |
+
demo = gr.Interface(
|
| 21 |
+
fn=classify,
|
| 22 |
+
inputs=[
|
| 23 |
+
gr.Image(type="pil", label="Upload an Image"),
|
| 24 |
+
gr.Dropdown(choices=model_ids, label="Select timm Model", value=default_model)
|
| 25 |
+
],
|
| 26 |
+
outputs=gr.Label(num_top_classes=3, label="Top Predictions"),
|
| 27 |
+
title="timm Model Image Classifier",
|
| 28 |
+
description="Select a timm model and upload an image for classification."
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
demo.launch()
|