taesiri commited on
Commit
f93e53d
1 Parent(s): 61b7eee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -23
app.py CHANGED
@@ -30,11 +30,8 @@ def calculate_score(image, text, model_name):
30
  model = models[model_name]
31
  processor = processors[model_name]
32
 
33
- # Get the correct image size for the model
34
- _, image_size = CLIP_MODELS[model_name]
35
-
36
  # Preprocess the image and text
37
- inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
38
  inputs = {k: v.to("cuda") for k, v in inputs.items()}
39
 
40
  # Calculate scores
@@ -43,7 +40,7 @@ def calculate_score(image, text, model_name):
43
 
44
  logits_per_image = outputs.logits_per_image.cpu().numpy()
45
 
46
- results_dict = {label: score / 100.0 for label, score in zip(labels, logits_per_image[0])}
47
  return results_dict
48
 
49
  with gr.Blocks() as demo:
@@ -63,23 +60,12 @@ with gr.Blocks() as demo:
63
  return None
64
  return calculate_score(image, text, model_name)
65
 
66
- image_input.change(
67
- fn=process_inputs,
68
- inputs=[image_input, text_input, model_dropdown],
69
- outputs=output_label
70
- )
71
-
72
- text_input.submit(
73
- fn=process_inputs,
74
- inputs=[image_input, text_input, model_dropdown],
75
- outputs=output_label
76
- )
77
 
78
- model_dropdown.change(
79
- fn=process_inputs,
80
- inputs=[image_input, text_input, model_dropdown],
81
- outputs=output_label
82
- )
83
 
84
  gr.Examples(
85
  examples=[
@@ -90,8 +76,8 @@ with gr.Blocks() as demo:
90
  ]
91
  ],
92
  fn=process_inputs,
93
- inputs=[image_input, text_input, model_dropdown],
94
- outputs=output_label,
95
  )
96
 
97
  demo.launch()
 
30
  model = models[model_name]
31
  processor = processors[model_name]
32
 
 
 
 
33
  # Preprocess the image and text
34
+ inputs = processor(text=labels, images=[image], return_tensors="pt", padding=True)
35
  inputs = {k: v.to("cuda") for k, v in inputs.items()}
36
 
37
  # Calculate scores
 
40
 
41
  logits_per_image = outputs.logits_per_image.cpu().numpy()
42
 
43
+ results_dict = {label: float(score) for label, score in zip(labels, logits_per_image[0])}
44
  return results_dict
45
 
46
  with gr.Blocks() as demo:
 
60
  return None
61
  return calculate_score(image, text, model_name)
62
 
63
+ inputs = [image_input, text_input, model_dropdown]
64
+ outputs = output_label
 
 
 
 
 
 
 
 
 
65
 
66
+ image_input.change(fn=process_inputs, inputs=inputs, outputs=outputs)
67
+ text_input.submit(fn=process_inputs, inputs=inputs, outputs=outputs)
68
+ model_dropdown.change(fn=process_inputs, inputs=inputs, outputs=outputs)
 
 
69
 
70
  gr.Examples(
71
  examples=[
 
76
  ]
77
  ],
78
  fn=process_inputs,
79
+ inputs=inputs,
80
+ outputs=outputs,
81
  )
82
 
83
  demo.launch()