Spaces:
Running
Running
import gradio as gr | |
from transformers import pipeline | |
# Function for image classification | |
def classify(image, model_name): | |
try: | |
pipe = pipeline("image-classification", model=model_name) | |
results = pipe(image) | |
return {result["label"]: round(result["score"], 2) for result in results} | |
except Exception as e: | |
# Handle errors gracefully, e.g., invalid model names | |
return {"Error": str(e)} | |
# Gradio Interface | |
demo = gr.Interface( | |
fn=classify, | |
inputs=[ | |
gr.Image(type="pil", label="Upload an Image"), | |
gr.Textbox(label="Enter timm Model Name", placeholder="e.g., timm/mobilenetv3_large_100.ra_in1k"), | |
], | |
outputs=gr.Label(num_top_classes=3, label="Top Predictions"), | |
title="Custom timm Model Image Classifier", | |
description="Enter a timm model name from Hugging Face, upload an image, and get predictions.", | |
) | |
demo.launch() |