Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
classifier = pipeline(
|
| 5 |
+
"zero-shot-image-classification",
|
| 6 |
+
model="google/siglip2-base-patch16-224",
|
| 7 |
+
device=-1
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
def classify_image(image, candidate_labels):
|
| 11 |
+
"""
|
| 12 |
+
Takes an image and a comma-separated string of candidate labels,
|
| 13 |
+
and returns the classification scores.
|
| 14 |
+
"""
|
| 15 |
+
labels = [label.strip() for label in candidate_labels.split(",") if label.strip()]
|
| 16 |
+
|
| 17 |
+
results = classifier(image, candidate_labels=labels)
|
| 18 |
+
return results[0]
|
| 19 |
+
|
| 20 |
+
iface = gr.Interface(
|
| 21 |
+
fn=classify_image,
|
| 22 |
+
inputs=[
|
| 23 |
+
gr.Image(type="pil", label="Input Image"),
|
| 24 |
+
gr.Textbox(value="cat, dog, bird, car, airplane", label="Candidate Labels (comma separated)")
|
| 25 |
+
],
|
| 26 |
+
outputs=gr.JSON(label="Classification Results"),
|
| 27 |
+
title="SigLIP Zero-Shot Image Classifier",
|
| 28 |
+
description="This app uses the Google SigLIP model (siglip2-base-patch16-224) for zero-shot image classification on CPU. "
|
| 29 |
+
"Enter an image and a set of candidate labels to see the prediction scores."
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
iface.launch()
|