|
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 |
|
|
|
|
|
model = AutoModel.from_pretrained('jinaai/jina-clip-v1', trust_remote_code=True) |
|
|
|
def compute_similarity(image, text): |
|
image = Image.fromarray(image) |
|
|
|
with torch.no_grad(): |
|
|
|
text_embeds = model.encode_text([text]) |
|
image_embeds = model.encode_image([image]) |
|
|
|
|
|
similarity_score = (text_embeds @ image_embeds.T).item() |
|
|
|
return similarity_score |
|
|
|
|
|
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() |