Update app.py
Browse files
app.py
CHANGED
|
@@ -9,13 +9,17 @@ model = AutoModel.from_pretrained("jinaai/jina-clip-v1", trust_remote_code=True)
|
|
| 9 |
# Function to compute similarity
|
| 10 |
def compute_similarity(input1, input2, input1_type, input2_type):
|
| 11 |
# Check if inputs are empty
|
| 12 |
-
if
|
| 13 |
return "Error: Input 1 is empty!"
|
| 14 |
-
if
|
| 15 |
return "Error: Input 2 is empty!"
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
inputs = []
|
| 18 |
-
|
| 19 |
# Process first input
|
| 20 |
if input1_type == "Text":
|
| 21 |
text1_embedding = model.encode_text([input1])
|
|
@@ -23,7 +27,7 @@ def compute_similarity(input1, input2, input1_type, input2_type):
|
|
| 23 |
elif input1_type == "Image":
|
| 24 |
image1_embedding = model.encode_image([Image.fromarray(input1)])
|
| 25 |
inputs.append(image1_embedding)
|
| 26 |
-
|
| 27 |
# Process second input
|
| 28 |
if input2_type == "Text":
|
| 29 |
text2_embedding = model.encode_text([input2])
|
|
@@ -31,12 +35,21 @@ def compute_similarity(input1, input2, input1_type, input2_type):
|
|
| 31 |
elif input2_type == "Image":
|
| 32 |
image2_embedding = model.encode_image([Image.fromarray(input2)])
|
| 33 |
inputs.append(image2_embedding)
|
| 34 |
-
|
| 35 |
# Compute cosine similarity
|
| 36 |
similarity_score = (inputs[0] @ inputs[1].T).item()
|
| 37 |
-
|
| 38 |
return similarity_score
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Gradio UI
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
gr.Markdown("## Multimodal Similarity: Text-Text, Text-Image, Image-Image")
|
|
@@ -55,15 +68,6 @@ with gr.Blocks() as demo:
|
|
| 55 |
|
| 56 |
output = gr.Textbox(label="Similarity Score / Error", interactive=False)
|
| 57 |
|
| 58 |
-
# Function to toggle visibility based on selected types
|
| 59 |
-
def update_visibility(input1_type, input2_type):
|
| 60 |
-
return (
|
| 61 |
-
input1_type == "Text",
|
| 62 |
-
input1_type == "Image",
|
| 63 |
-
input2_type == "Text",
|
| 64 |
-
input2_type == "Image"
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
input1_type.change(update_visibility, inputs=[input1_type, input2_type], outputs=[input1, image1, input2, image2])
|
| 68 |
input2_type.change(update_visibility, inputs=[input1_type, input2_type], outputs=[input1, image1, input2, image2])
|
| 69 |
|
|
|
|
| 9 |
# Function to compute similarity
|
| 10 |
def compute_similarity(input1, input2, input1_type, input2_type):
|
| 11 |
# Check if inputs are empty
|
| 12 |
+
if input1_type == "Text" and not input1.strip():
|
| 13 |
return "Error: Input 1 is empty!"
|
| 14 |
+
if input2_type == "Text" and not input2.strip():
|
| 15 |
return "Error: Input 2 is empty!"
|
| 16 |
+
if input1_type == "Image" and input1 is None:
|
| 17 |
+
return "Error: Image 1 is missing!"
|
| 18 |
+
if input2_type == "Image" and input2 is None:
|
| 19 |
+
return "Error: Image 2 is missing!"
|
| 20 |
+
|
| 21 |
inputs = []
|
| 22 |
+
|
| 23 |
# Process first input
|
| 24 |
if input1_type == "Text":
|
| 25 |
text1_embedding = model.encode_text([input1])
|
|
|
|
| 27 |
elif input1_type == "Image":
|
| 28 |
image1_embedding = model.encode_image([Image.fromarray(input1)])
|
| 29 |
inputs.append(image1_embedding)
|
| 30 |
+
|
| 31 |
# Process second input
|
| 32 |
if input2_type == "Text":
|
| 33 |
text2_embedding = model.encode_text([input2])
|
|
|
|
| 35 |
elif input2_type == "Image":
|
| 36 |
image2_embedding = model.encode_image([Image.fromarray(input2)])
|
| 37 |
inputs.append(image2_embedding)
|
| 38 |
+
|
| 39 |
# Compute cosine similarity
|
| 40 |
similarity_score = (inputs[0] @ inputs[1].T).item()
|
| 41 |
+
|
| 42 |
return similarity_score
|
| 43 |
|
| 44 |
+
# Function to update UI based on selected input types
|
| 45 |
+
def update_visibility(input1_type, input2_type):
|
| 46 |
+
return (
|
| 47 |
+
gr.update(visible=input1_type == "Text", value="" if input1_type == "Text" else None),
|
| 48 |
+
gr.update(visible=input1_type == "Image", value=None if input1_type == "Image" else None),
|
| 49 |
+
gr.update(visible=input2_type == "Text", value="" if input2_type == "Text" else None),
|
| 50 |
+
gr.update(visible=input2_type == "Image", value=None if input2_type == "Image" else None),
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
# Gradio UI
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
gr.Markdown("## Multimodal Similarity: Text-Text, Text-Image, Image-Image")
|
|
|
|
| 68 |
|
| 69 |
output = gr.Textbox(label="Similarity Score / Error", interactive=False)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
input1_type.change(update_visibility, inputs=[input1_type, input2_type], outputs=[input1, image1, input2, image2])
|
| 72 |
input2_type.change(update_visibility, inputs=[input1_type, input2_type], outputs=[input1, image1, input2, image2])
|
| 73 |
|