Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# 1. Install Required Libraries (
|
| 2 |
# pip install gradio transformers torch torchvision pillow
|
| 3 |
|
| 4 |
# 2. Import Libraries
|
|
@@ -7,44 +7,52 @@ from transformers import CLIPProcessor, CLIPModel
|
|
| 7 |
from PIL import Image
|
| 8 |
import torch
|
| 9 |
|
| 10 |
-
# 3. Load the
|
| 11 |
model_name = "openai/clip-vit-base-patch16"
|
| 12 |
processor = CLIPProcessor.from_pretrained(model_name)
|
| 13 |
model = CLIPModel.from_pretrained(model_name)
|
| 14 |
|
| 15 |
-
# 4. Define the
|
| 16 |
-
def
|
| 17 |
-
if not image or not
|
| 18 |
-
return "Please
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
| 24 |
with torch.no_grad():
|
| 25 |
outputs = model(**inputs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
probs = logits_per_image.softmax(dim=1) # shape: [1, 1]
|
| 30 |
-
score = probs[0][0].item() # Get scalar score
|
| 31 |
|
| 32 |
-
|
| 33 |
-
match_percentage = round(score * 100, 2)
|
| 34 |
-
return f"Match Confidence: {match_percentage}%"
|
| 35 |
|
| 36 |
-
# 5. Create the Gradio
|
| 37 |
iface = gr.Interface(
|
| 38 |
-
fn=
|
| 39 |
inputs=[
|
| 40 |
gr.Image(type="pil", label="Upload an Image"),
|
| 41 |
-
gr.Textbox(lines=
|
| 42 |
],
|
| 43 |
-
outputs=gr.Label(label="
|
| 44 |
-
title="
|
| 45 |
-
description="Upload an image and enter
|
| 46 |
-
allow_flagging="never"
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# 6. Launch the
|
| 50 |
iface.launch()
|
|
|
|
| 1 |
+
# 1. Install Required Libraries (if you haven't already)
|
| 2 |
# pip install gradio transformers torch torchvision pillow
|
| 3 |
|
| 4 |
# 2. Import Libraries
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
import torch
|
| 9 |
|
| 10 |
+
# 3. Load the CLIP model
|
| 11 |
model_name = "openai/clip-vit-base-patch16"
|
| 12 |
processor = CLIPProcessor.from_pretrained(model_name)
|
| 13 |
model = CLIPModel.from_pretrained(model_name)
|
| 14 |
|
| 15 |
+
# 4. Define the matching function
|
| 16 |
+
def match_image_with_descriptions(image, descriptions):
|
| 17 |
+
if not image or not descriptions.strip():
|
| 18 |
+
return "Please upload an image and enter at least one description."
|
| 19 |
+
|
| 20 |
+
# Convert multi-line string to list of captions
|
| 21 |
+
captions = [line.strip() for line in descriptions.strip().split('\n') if line.strip()]
|
| 22 |
|
| 23 |
+
if len(captions) < 2:
|
| 24 |
+
return "Please enter at least two descriptions to compare."
|
| 25 |
+
|
| 26 |
+
# Process inputs
|
| 27 |
+
inputs = processor(text=captions, images=image, return_tensors="pt", padding=True)
|
| 28 |
|
| 29 |
+
# Run model
|
| 30 |
with torch.no_grad():
|
| 31 |
outputs = model(**inputs)
|
| 32 |
+
|
| 33 |
+
# Calculate probabilities
|
| 34 |
+
logits_per_image = outputs.logits_per_image # shape: [1, num_captions]
|
| 35 |
+
probs = logits_per_image.softmax(dim=1)[0] # Convert to probabilities
|
| 36 |
+
|
| 37 |
+
# Build result dictionary
|
| 38 |
+
results = {captions[i]: f"{probs[i].item() * 100:.2f}%" for i in range(len(captions))}
|
| 39 |
|
| 40 |
+
# Sort by confidence (descending)
|
| 41 |
+
sorted_results = dict(sorted(results.items(), key=lambda item: float(item[1][:-1]), reverse=True))
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
return sorted_results
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# 5. Create the Gradio interface
|
| 46 |
iface = gr.Interface(
|
| 47 |
+
fn=match_image_with_descriptions,
|
| 48 |
inputs=[
|
| 49 |
gr.Image(type="pil", label="Upload an Image"),
|
| 50 |
+
gr.Textbox(lines=6, placeholder="Enter one description per line...", label="Descriptions")
|
| 51 |
],
|
| 52 |
+
outputs=gr.Label(label="Match Confidence"),
|
| 53 |
+
title="AI Image-Text Matcher",
|
| 54 |
+
description="Upload an image and enter multiple possible descriptions (one per line). The AI will tell you which one best matches the image."
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# 6. Launch the app
|
| 58 |
iface.launch()
|