Update app.py
Browse files
app.py
CHANGED
|
@@ -12,9 +12,7 @@ def process_input(input_data, input_type):
|
|
| 12 |
if input_type == "Text":
|
| 13 |
return model.encode_text([input_data]) if input_data.strip() else None
|
| 14 |
elif input_type == "Image":
|
| 15 |
-
if isinstance(input_data,
|
| 16 |
-
image = Image.open(input_data).convert("RGB")
|
| 17 |
-
elif isinstance(input_data, np.ndarray): # If it's a NumPy array (Gradio default)
|
| 18 |
image = Image.fromarray(input_data)
|
| 19 |
else:
|
| 20 |
return None # Invalid input type
|
|
@@ -44,13 +42,13 @@ def compute_similarity(input1, input2, input1_type, input2_type):
|
|
| 44 |
similarity_score = (embedding1 @ embedding2.T).item()
|
| 45 |
return f"Similarity Score: {similarity_score:.4f}"
|
| 46 |
|
| 47 |
-
# Function to toggle input fields dynamically
|
| 48 |
def update_visibility(input1_type, input2_type):
|
| 49 |
return (
|
| 50 |
-
gr.update(visible=(input1_type == "Text")),
|
| 51 |
-
gr.update(visible=(input1_type == "Image")),
|
| 52 |
-
gr.update(visible=(input2_type == "Text")),
|
| 53 |
-
gr.update(visible=(input2_type == "Image"))
|
| 54 |
)
|
| 55 |
|
| 56 |
# Gradio UI
|
|
@@ -71,7 +69,7 @@ with gr.Blocks() as demo:
|
|
| 71 |
|
| 72 |
output = gr.Textbox(label="Similarity Score / Error", interactive=False)
|
| 73 |
|
| 74 |
-
# Toggle visibility of inputs dynamically
|
| 75 |
input1_type.change(update_visibility, inputs=[input1_type, input2_type],
|
| 76 |
outputs=[input1_text, input1_image, input2_text, input2_image])
|
| 77 |
input2_type.change(update_visibility, inputs=[input1_type, input2_type],
|
|
|
|
| 12 |
if input_type == "Text":
|
| 13 |
return model.encode_text([input_data]) if input_data.strip() else None
|
| 14 |
elif input_type == "Image":
|
| 15 |
+
if isinstance(input_data, np.ndarray): # If it's a NumPy array (Gradio default)
|
|
|
|
|
|
|
| 16 |
image = Image.fromarray(input_data)
|
| 17 |
else:
|
| 18 |
return None # Invalid input type
|
|
|
|
| 42 |
similarity_score = (embedding1 @ embedding2.T).item()
|
| 43 |
return f"Similarity Score: {similarity_score:.4f}"
|
| 44 |
|
| 45 |
+
# Function to toggle input fields dynamically and clear previous values
|
| 46 |
def update_visibility(input1_type, input2_type):
|
| 47 |
return (
|
| 48 |
+
gr.update(visible=(input1_type == "Text"), value="" if input1_type == "Image" else None),
|
| 49 |
+
gr.update(visible=(input1_type == "Image"), value=None),
|
| 50 |
+
gr.update(visible=(input2_type == "Text"), value="" if input2_type == "Image" else None),
|
| 51 |
+
gr.update(visible=(input2_type == "Image"), value=None)
|
| 52 |
)
|
| 53 |
|
| 54 |
# Gradio UI
|
|
|
|
| 69 |
|
| 70 |
output = gr.Textbox(label="Similarity Score / Error", interactive=False)
|
| 71 |
|
| 72 |
+
# Toggle visibility of inputs dynamically and clear values
|
| 73 |
input1_type.change(update_visibility, inputs=[input1_type, input2_type],
|
| 74 |
outputs=[input1_text, input1_image, input2_text, input2_image])
|
| 75 |
input2_type.change(update_visibility, inputs=[input1_type, input2_type],
|