Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
-
from transformers import
|
4 |
import torch
|
5 |
|
6 |
# Load Jina CLIP model
|
7 |
model_name = "jinaai/jina-clip-v1"
|
8 |
model = CLIPModel.from_pretrained(model_name)
|
9 |
-
|
|
|
10 |
|
11 |
def compute_similarity(input1, input2, type1, type2):
|
12 |
inputs = []
|
@@ -14,28 +15,28 @@ def compute_similarity(input1, input2, type1, type2):
|
|
14 |
# Process input1
|
15 |
if type1 == "Image":
|
16 |
image1 = Image.open(input1).convert("RGB")
|
17 |
-
inputs.append(processor(images=image1, return_tensors="pt"))
|
18 |
else:
|
19 |
-
inputs.append(
|
20 |
|
21 |
# Process input2
|
22 |
if type2 == "Image":
|
23 |
image2 = Image.open(input2).convert("RGB")
|
24 |
-
inputs.append(processor(images=image2, return_tensors="pt"))
|
25 |
else:
|
26 |
-
inputs.append(
|
27 |
|
28 |
# Compute embeddings
|
29 |
with torch.no_grad():
|
30 |
if type1 == "Image":
|
31 |
-
embedding1 = model.get_image_features(
|
32 |
else:
|
33 |
-
embedding1 = model.get_text_features(
|
34 |
|
35 |
if type2 == "Image":
|
36 |
-
embedding2 = model.get_image_features(
|
37 |
else:
|
38 |
-
embedding2 = model.get_text_features(
|
39 |
|
40 |
# Compute similarity
|
41 |
similarity = torch.nn.functional.cosine_similarity(embedding1, embedding2)
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
+
from transformers import CLIPModel, AutoTokenizer, AutoProcessor
|
4 |
import torch
|
5 |
|
6 |
# Load Jina CLIP model
|
7 |
model_name = "jinaai/jina-clip-v1"
|
8 |
model = CLIPModel.from_pretrained(model_name)
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
11 |
|
12 |
def compute_similarity(input1, input2, type1, type2):
|
13 |
inputs = []
|
|
|
15 |
# Process input1
|
16 |
if type1 == "Image":
|
17 |
image1 = Image.open(input1).convert("RGB")
|
18 |
+
inputs.append(processor(images=image1, return_tensors="pt")["pixel_values"])
|
19 |
else:
|
20 |
+
inputs.append(tokenizer(input1, return_tensors="pt")["input_ids"])
|
21 |
|
22 |
# Process input2
|
23 |
if type2 == "Image":
|
24 |
image2 = Image.open(input2).convert("RGB")
|
25 |
+
inputs.append(processor(images=image2, return_tensors="pt")["pixel_values"])
|
26 |
else:
|
27 |
+
inputs.append(tokenizer(input2, return_tensors="pt")["input_ids"])
|
28 |
|
29 |
# Compute embeddings
|
30 |
with torch.no_grad():
|
31 |
if type1 == "Image":
|
32 |
+
embedding1 = model.get_image_features(pixel_values=inputs[0])
|
33 |
else:
|
34 |
+
embedding1 = model.get_text_features(input_ids=inputs[0])
|
35 |
|
36 |
if type2 == "Image":
|
37 |
+
embedding2 = model.get_image_features(pixel_values=inputs[1])
|
38 |
else:
|
39 |
+
embedding2 = model.get_text_features(input_ids=inputs[1])
|
40 |
|
41 |
# Compute similarity
|
42 |
similarity = torch.nn.functional.cosine_similarity(embedding1, embedding2)
|