Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
def classify(image, model_name):
|
5 |
+
try:
|
6 |
+
pipe = pipeline("image-classification", model=model_name)
|
7 |
+
results = pipe(image)
|
8 |
+
return {result["label"]: round(result["score"], 2) for result in results}
|
9 |
+
except Exception as e:
|
10 |
+
return {"Error": str(e)}
|
11 |
+
|
12 |
+
# Gradio Blocks Interface
|
13 |
+
with gr.Blocks() as demo:
|
14 |
+
gr.Markdown(
|
15 |
+
"""
|
16 |
+
# Custom timm Model Image Classifier 🚀
|
17 |
+
|
18 |
+
Explore the power of [timm](https://github.com/rwightman/pytorch-image-models) models for image classification using
|
19 |
+
the Hugging Face [Transformers pipeline](https://huggingface.co/docs/transformers/main_classes/pipelines).
|
20 |
+
|
21 |
+
With just a few lines of code, you can load any timm model hosted on the Hugging Face Hub and classify images effortlessly.
|
22 |
+
This application demonstrates how you can use the pipeline API to create a powerful yet minimalistic image classification tool.
|
23 |
+
|
24 |
+
## How to Use
|
25 |
+
|
26 |
+
1. Upload an image or use one of the provided examples.
|
27 |
+
2. Enter a valid timm model name from the Hugging Face Hub (e.g., `timm/resnet50.a1_in1k`).
|
28 |
+
3. View the top predictions and confidence scores!
|
29 |
+
"""
|
30 |
+
)
|
31 |
+
|
32 |
+
with gr.Row():
|
33 |
+
with gr.Column():
|
34 |
+
image_input = gr.Image(type="pil", label="Upload an Image")
|
35 |
+
model_name_input = gr.Textbox(
|
36 |
+
label="Enter timm Model Name",
|
37 |
+
placeholder="e.g., timm/mobilenetv3_large_100.ra_in1k"
|
38 |
+
)
|
39 |
+
with gr.Column():
|
40 |
+
output_label = gr.Label(num_top_classes=3, label="Top Predictions")
|
41 |
+
|
42 |
+
submit_button = gr.Button("Classify")
|
43 |
+
submit_button.click(fn=classify, inputs=[image_input, model_name_input], outputs=output_label)
|
44 |
+
|
45 |
+
gr.Examples(
|
46 |
+
examples=[
|
47 |
+
["cat.jpg", "timm/mobilenetv3_small_100.lamb_in1k"],
|
48 |
+
["cat.jpg", "timm/resnet50.a1_in1k"],
|
49 |
+
],
|
50 |
+
inputs=[image_input, model_name_input]
|
51 |
+
)
|
52 |
+
|
53 |
+
gr.Markdown(
|
54 |
+
"""
|
55 |
+
## Learn More
|
56 |
+
- Check out the implementation in the `app.py` file to see how easy it is to integrate timm models.
|
57 |
+
- Dive into the [official blog post on timm integration](https://huggingface.co/blog/timm-transformers) for more insights.
|
58 |
+
"""
|
59 |
+
)
|
60 |
+
demo.launch()
|