Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
from PIL import Image | |
from io import BytesIO | |
from transformers import pipeline | |
#new | |
# 1. Load a pretrained ResNet-50 from the Hugging Face Hub | |
model_id = "halictus/resnet50_honeybee" | |
classifier = pipeline("image-classification", model=model_id) | |
# 2. Define an inference function | |
def classify_image_from_url(image_url: str): | |
""" | |
Downloads an image from a public URL and runs it through | |
the ResNet-50 image-classification pipeline, returning the top predictions. | |
""" | |
try: | |
# Fetch the image | |
response = requests.get(image_url) | |
response.raise_for_status() | |
image = Image.open(BytesIO(response.content)).convert("RGB") | |
# Run inference | |
results = classifier(image) | |
# You can return raw results or format them as desired | |
return results | |
except Exception as e: | |
return {"error": str(e)} | |
# 3. Create a Gradio interface | |
# - We accept a single Textbox input (the public image URL) | |
# - We return the classification results in JSON format | |
demo = gr.Interface( | |
fn=classify_image_from_url, | |
inputs=gr.Textbox(lines=1, label="Image URL"), | |
outputs="json", | |
title="ResNet-50 Image Classifier", | |
description="Enter a public image URL to get top predictions." | |
) | |
# 4. Launch the app | |
if __name__ == "__main__": | |
demo.launch() | |