Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -16,24 +16,32 @@ model = CLIPModel.from_pretrained(model_name, trust_remote_code=True)
|
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
17 |
processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
|
18 |
|
19 |
-
def compute_similarity(input1, input2, type1, type2):
|
20 |
# Process input1
|
21 |
if type1 == "Image":
|
|
|
|
|
22 |
image1 = Image.open(input1).convert("RGB")
|
23 |
input1_tensor = processor(images=image1, return_tensors="pt")["pixel_values"]
|
24 |
-
elif
|
25 |
-
|
|
|
|
|
26 |
else:
|
27 |
-
return "Error: Invalid
|
28 |
|
29 |
# Process input2
|
30 |
if type2 == "Image":
|
|
|
|
|
31 |
image2 = Image.open(input2).convert("RGB")
|
32 |
input2_tensor = processor(images=image2, return_tensors="pt")["pixel_values"]
|
33 |
-
elif
|
34 |
-
|
|
|
|
|
35 |
else:
|
36 |
-
return "Error: Invalid
|
37 |
|
38 |
# Compute embeddings
|
39 |
with torch.no_grad():
|
@@ -72,6 +80,8 @@ with gr.Blocks() as demo:
|
|
72 |
inputs=[
|
73 |
input1,
|
74 |
input2,
|
|
|
|
|
75 |
type1,
|
76 |
type2
|
77 |
],
|
|
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
17 |
processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
|
18 |
|
19 |
+
def compute_similarity(input1, input2, text1, text2, type1, type2):
|
20 |
# Process input1
|
21 |
if type1 == "Image":
|
22 |
+
if not input1:
|
23 |
+
return "Error: No image provided for Input 1"
|
24 |
image1 = Image.open(input1).convert("RGB")
|
25 |
input1_tensor = processor(images=image1, return_tensors="pt")["pixel_values"]
|
26 |
+
elif type1 == "Text":
|
27 |
+
if not text1.strip():
|
28 |
+
return "Error: No text provided for Input 1"
|
29 |
+
input1_tensor = tokenizer(text1, return_tensors="pt")["input_ids"]
|
30 |
else:
|
31 |
+
return "Error: Invalid input type for Input 1"
|
32 |
|
33 |
# Process input2
|
34 |
if type2 == "Image":
|
35 |
+
if not input2:
|
36 |
+
return "Error: No image provided for Input 2"
|
37 |
image2 = Image.open(input2).convert("RGB")
|
38 |
input2_tensor = processor(images=image2, return_tensors="pt")["pixel_values"]
|
39 |
+
elif type2 == "Text":
|
40 |
+
if not text2.strip():
|
41 |
+
return "Error: No text provided for Input 2"
|
42 |
+
input2_tensor = tokenizer(text2, return_tensors="pt")["input_ids"]
|
43 |
else:
|
44 |
+
return "Error: Invalid input type for Input 2"
|
45 |
|
46 |
# Compute embeddings
|
47 |
with torch.no_grad():
|
|
|
80 |
inputs=[
|
81 |
input1,
|
82 |
input2,
|
83 |
+
text1,
|
84 |
+
text2,
|
85 |
type1,
|
86 |
type2
|
87 |
],
|