Spaces:
Sleeping
Sleeping
Commit ·
7dd345f
1
Parent(s): 78ec628
feat: add app code
Browse files- app.py +47 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# 1. Initialize the zero-shot image classification pipeline using CLIP
|
| 6 |
+
print("Loading OpenAI CLIP model...")
|
| 7 |
+
classifier = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")
|
| 8 |
+
|
| 9 |
+
def classify_image(image, labels_text):
|
| 10 |
+
# Safe checks for missing inputs
|
| 11 |
+
if image is None or not labels_text.strip():
|
| 12 |
+
return {"Please upload an image and provide labels.": 1.0}
|
| 13 |
+
|
| 14 |
+
# Clean up the comma-separated labels from the textbox input
|
| 15 |
+
candidate_labels = [label.strip() for label in labels_text.split(",") if label.strip()]
|
| 16 |
+
|
| 17 |
+
if not candidate_labels:
|
| 18 |
+
return {"Please enter at least one valid label.": 1.0}
|
| 19 |
+
|
| 20 |
+
# 2. Run inference through CLIP
|
| 21 |
+
# The pipeline automatically coordinates text tokens and image tensors
|
| 22 |
+
predictions = classifier(image, candidate_labels=candidate_labels)
|
| 23 |
+
|
| 24 |
+
# 3. Format the response dictionary so Gradio's gr.Label can display it
|
| 25 |
+
# Format looks like: {"label_name": score_float}
|
| 26 |
+
return {pred["label"]: float(pred["score"]) for pred in predictions}
|
| 27 |
+
|
| 28 |
+
# 4. Define the User Interface
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=classify_image,
|
| 31 |
+
inputs=[
|
| 32 |
+
gr.Image(type="pil", label="1. Upload your Image"),
|
| 33 |
+
gr.Textbox(
|
| 34 |
+
label="2. Candidate Labels (Separate with commas)",
|
| 35 |
+
placeholder="e.g., a sunny beach, a cozy rainy day, a cute animal, corporate office",
|
| 36 |
+
value="a playful dog, a quiet cat, an outdoor landscape, indoor architecture"
|
| 37 |
+
)
|
| 38 |
+
],
|
| 39 |
+
outputs=gr.Label(num_top_classes=5, label="Matching Confidence"),
|
| 40 |
+
title="CLIP Zero-Shot Image Matcher",
|
| 41 |
+
description="Type any descriptive phrases or labels you can think of, separate them with commas, and see how well OpenAI's CLIP aligns them to your uploaded photo.",
|
| 42 |
+
flagging_mode="never"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Launch the app
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
transformers
|