Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import CLIPModel, CLIPProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load model and processor
|
| 7 |
+
model_name = "jinaai/jina-clip-v1"
|
| 8 |
+
model = CLIPModel.from_pretrained(model_name)
|
| 9 |
+
processor = CLIPProcessor.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
def compute_similarity(image, text):
|
| 12 |
+
image = Image.fromarray(image) # Convert NumPy array to PIL image
|
| 13 |
+
|
| 14 |
+
# Process inputs
|
| 15 |
+
inputs = processor(text=[text], images=image, return_tensors="pt", padding=True, truncation=True)
|
| 16 |
+
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
outputs = model(**inputs)
|
| 19 |
+
logits_per_image = outputs.logits_per_image # Image-to-text similarity
|
| 20 |
+
similarity_score = logits_per_image.item()
|
| 21 |
+
|
| 22 |
+
return similarity_score
|
| 23 |
+
|
| 24 |
+
# Gradio UI
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=compute_similarity,
|
| 27 |
+
inputs=[gr.Image(type="numpy"), gr.Textbox(label="Enter text")],
|
| 28 |
+
outputs=gr.Number(label="Similarity Score"),
|
| 29 |
+
title="CLIP Image-Text Similarity",
|
| 30 |
+
description="Upload an image and enter a text prompt to get the similarity score."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
demo.launch()
|