File size: 1,080 Bytes
e074ee9
0e73f03
e074ee9
 
1cef0a7
 
 
b57f8d5
1cef0a7
cf604df
e074ee9
1cef0a7
 
ab10c56
1cef0a7
 
 
 
42a6be5
1cef0a7
 
7cc3d8e
1cef0a7
7cc3d8e
7151f63
1cef0a7
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import gradio as gr
from transformers import AutoModel
from PIL import Image
import torch
import torch.nn.functional as F
import requests
from io import BytesIO

# Load model with remote code support
model = AutoModel.from_pretrained('jinaai/jina-clip-v1', trust_remote_code=True)

def compute_similarity(image, text):
    image = Image.fromarray(image)  # Convert NumPy array to PIL Image

    with torch.no_grad():
        # Encode text and image using JinaAI CLIP model
        text_embeds = model.encode_text([text])  # Expecting list input
        image_embeds = model.encode_image([image])  # Expecting list input

        # Compute cosine similarity
        similarity_score = (text_embeds @ image_embeds.T).item()

    return similarity_score

# Gradio UI
demo = gr.Interface(
    fn=compute_similarity,
    inputs=[gr.Image(type="numpy"), gr.Textbox(label="Enter text")],
    outputs=gr.Number(label="Similarity Score"),
    title="JinaAI CLIP Image-Text Similarity",
    description="Upload an image and enter a text prompt to get the similarity score."
)

demo.launch()