wilwork commited on
Commit
aa04fb9
·
verified ·
1 Parent(s): f0b1903

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -8,19 +8,19 @@ model = AutoModel.from_pretrained("jinaai/jina-clip-v1", trust_remote_code=True)
8
 
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])
26
  inputs.append(text1_embedding)
@@ -28,7 +28,6 @@ def compute_similarity(input1, input2, input1_type, input2_type):
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])
34
  inputs.append(text2_embedding)
@@ -38,16 +37,15 @@ def compute_similarity(input1, input2, input1_type, input2_type):
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
@@ -59,19 +57,22 @@ with gr.Blocks() as demo:
59
  input2_type = gr.Radio(["Text", "Image"], label="Input 2 Type", value="Image")
60
 
61
  with gr.Row():
62
- input1 = gr.Textbox(label="Text Input 1", visible=True)
63
- image1 = gr.Image(type="numpy", label="Image Input 1", visible=False)
64
-
65
  with gr.Row():
66
- input2 = gr.Textbox(label="Text Input 2", visible=False)
67
- image2 = gr.Image(type="numpy", label="Image Input 2", visible=True)
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
 
74
  btn = gr.Button("Compute Similarity")
75
- btn.click(compute_similarity, inputs=[input1, input2, input1_type, input2_type], outputs=output)
76
 
77
  demo.launch()
 
8
 
9
  # Function to compute similarity
10
  def compute_similarity(input1, input2, input1_type, input2_type):
11
+ # Ensure inputs are valid
12
+ if input1_type == "Text" and (not input1 or input1.strip() == ""):
13
  return "Error: Input 1 is empty!"
14
+ if input2_type == "Text" and (not input2 or 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
+ # Encode inputs
22
  inputs = []
23
+
 
24
  if input1_type == "Text":
25
  text1_embedding = model.encode_text([input1])
26
  inputs.append(text1_embedding)
 
28
  image1_embedding = model.encode_image([Image.fromarray(input1)])
29
  inputs.append(image1_embedding)
30
 
 
31
  if input2_type == "Text":
32
  text2_embedding = model.encode_text([input2])
33
  inputs.append(text2_embedding)
 
37
 
38
  # Compute cosine similarity
39
  similarity_score = (inputs[0] @ inputs[1].T).item()
40
+ return f"Similarity Score: {similarity_score:.4f}"
41
 
42
+ # Function to toggle input fields dynamically
 
 
43
  def update_visibility(input1_type, input2_type):
44
  return (
45
+ gr.update(visible=(input1_type == "Text")), # Show text input if Text is selected
46
+ gr.update(visible=(input1_type == "Image")), # Show image input if Image is selected
47
+ gr.update(visible=(input2_type == "Text")),
48
+ gr.update(visible=(input2_type == "Image"))
49
  )
50
 
51
  # Gradio UI
 
57
  input2_type = gr.Radio(["Text", "Image"], label="Input 2 Type", value="Image")
58
 
59
  with gr.Row():
60
+ input1_text = gr.Textbox(label="Text Input 1", visible=True)
61
+ input1_image = gr.Image(type="numpy", label="Image Input 1", visible=False)
62
+
63
  with gr.Row():
64
+ input2_text = gr.Textbox(label="Text Input 2", visible=False)
65
+ input2_image = gr.Image(type="numpy", label="Image Input 2", visible=True)
66
 
67
  output = gr.Textbox(label="Similarity Score / Error", interactive=False)
68
 
69
+ # Toggle visibility of inputs dynamically
70
+ input1_type.change(update_visibility, inputs=[input1_type, input2_type],
71
+ outputs=[input1_text, input1_image, input2_text, input2_image])
72
+ input2_type.change(update_visibility, inputs=[input1_type, input2_type],
73
+ outputs=[input1_text, input1_image, input2_text, input2_image])
74
 
75
  btn = gr.Button("Compute Similarity")
76
+ btn.click(compute_similarity, inputs=[input1_text, input2_text, input1_type, input2_type], outputs=output)
77
 
78
  demo.launch()