Spaces:
Sleeping
Sleeping
Honey Bee Society
commited on
Commit
·
a31153b
1
Parent(s):
b152a7e
init push
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
# 1. Load a pretrained ResNet-50 from the Hugging Face Hub
|
8 |
+
model_id = "halictus/resnet50_honeybee"
|
9 |
+
classifier = pipeline("image-classification", model=model_id)
|
10 |
+
|
11 |
+
# 2. Define an inference function
|
12 |
+
def classify_image_from_url(image_url: str):
|
13 |
+
"""
|
14 |
+
Downloads an image from a public URL and runs it through
|
15 |
+
the ResNet-50 image-classification pipeline, returning the top predictions.
|
16 |
+
"""
|
17 |
+
try:
|
18 |
+
# Fetch the image
|
19 |
+
response = requests.get(image_url)
|
20 |
+
response.raise_for_status()
|
21 |
+
image = Image.open(BytesIO(response.content)).convert("RGB")
|
22 |
+
|
23 |
+
# Run inference
|
24 |
+
results = classifier(image)
|
25 |
+
|
26 |
+
# You can return raw results or format them as desired
|
27 |
+
return results
|
28 |
+
|
29 |
+
except Exception as e:
|
30 |
+
return {"error": str(e)}
|
31 |
+
|
32 |
+
# 3. Create a Gradio interface
|
33 |
+
# - We accept a single Textbox input (the public image URL)
|
34 |
+
# - We return the classification results in JSON format
|
35 |
+
demo = gr.Interface(
|
36 |
+
fn=classify_image_from_url,
|
37 |
+
inputs=gr.Textbox(lines=1, label="Image URL"),
|
38 |
+
outputs="json",
|
39 |
+
title="ResNet-50 Image Classifier",
|
40 |
+
description="Enter a public image URL to get top predictions."
|
41 |
+
)
|
42 |
+
|
43 |
+
# 4. Launch the app
|
44 |
+
if __name__ == "__main__":
|
45 |
+
demo.launch()
|
46 |
+
|