rdezwart commited on
Commit
8db35f3
·
1 Parent(s): 17317f8

Tidy up event listeners

Browse files

Separated returning button states from functions that were more meant for their own thing

Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -78,25 +78,42 @@ def detect_objects(img: Image.Image):
78
  return box_images
79
 
80
 
81
- def gallery_selected(evt: gr.SelectData) -> tuple[int, gr.Button]:
82
  """
83
  Listener for the gallery selection event.
84
 
85
  :return: index of the currently selected image
86
  """
87
- print(f"Index: {evt.index}, Value: {evt.value}, Selected: {evt.selected}")
88
- return evt.index, gr.Button(interactive=evt.selected)
89
 
90
 
91
- def to_moondream(images: list[tuple[Image.Image, str | None]], index: int) -> tuple[gr.Tabs, Image.Image, gr.Button]:
92
  """
93
  Listener that sends selected gallery image to the moondream model.
94
 
95
  :param images: list of images from yolos_gallery
96
  :param index: index of selected gallery image
97
- :return: selected tab, selected image (no caption), and an enabled button
98
  """
99
- return gr.Tabs(selected='moondream'), images[index][0], gr.Button(interactive=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
 
102
  if __name__ == "__main__":
@@ -130,23 +147,22 @@ if __name__ == "__main__":
130
  with gr.Group():
131
  moon_prompt = gr.Textbox(label="Ask a question about the image:",
132
  value="What is this food item? Include any text on labels.")
133
- moon_submit = gr.Button("Submit", interactive=False, variant="primary")
134
  moon_output = gr.TextArea(label="Answer", interactive=False)
135
 
136
  # --- YOLOS --- #
137
- yolos_input.upload(lambda: gr.Button(interactive=True), None, yolos_submit)
138
- yolos_input.clear(lambda: gr.Button(interactive=False), None, yolos_submit)
139
-
140
  yolos_submit.click(detect_objects, yolos_input, yolos_gallery)
141
- yolos_submit.click(lambda: (gr.Button(variant="secondary"), gr.Button(variant="primary")), None,
142
- [yolos_submit, proceed_button])
143
 
144
- yolos_gallery.select(gallery_selected, None, [selected_image, proceed_button])
145
- proceed_button.click(to_moondream, [yolos_gallery, selected_image], [tabs, moon_img, moon_submit])
 
 
146
 
147
  # --- Moondream --- #
148
- moon_img.upload(lambda: gr.Button(interactive=True), None, moon_submit)
149
- moon_img.clear(lambda: gr.Button(interactive=False), None, moon_submit)
150
  moon_submit.click(answer_question, [moon_img, moon_prompt], moon_output)
151
 
152
  app.queue().launch()
 
78
  return box_images
79
 
80
 
81
+ def get_selected_index(evt: gr.SelectData) -> int:
82
  """
83
  Listener for the gallery selection event.
84
 
85
  :return: index of the currently selected image
86
  """
87
+ return evt.index
 
88
 
89
 
90
+ def to_moondream(images: list[tuple[Image.Image, str | None]], index: int) -> tuple[gr.Tabs, Image.Image]:
91
  """
92
  Listener that sends selected gallery image to the moondream model.
93
 
94
  :param images: list of images from yolos_gallery
95
  :param index: index of selected gallery image
96
+ :return: selected tab and selected image (no caption)
97
  """
98
+ return gr.Tabs(selected='moondream'), images[index][0]
99
+
100
+
101
+ def enable_button() -> gr.Button:
102
+ """
103
+ Helper function for Gradio event listeners.
104
+
105
+ :return: a button with ``interactive=True`` and ``variant="primary"``
106
+ """
107
+ return gr.Button(interactive=True, variant="primary")
108
+
109
+
110
+ def disable_button() -> gr.Button:
111
+ """
112
+ Helper function for Gradio event listeners.
113
+
114
+ :return: a button with ``interactive=False`` and ``variant="secondary"``
115
+ """
116
+ return gr.Button(interactive=False, variant="secondary")
117
 
118
 
119
  if __name__ == "__main__":
 
147
  with gr.Group():
148
  moon_prompt = gr.Textbox(label="Ask a question about the image:",
149
  value="What is this food item? Include any text on labels.")
150
+ moon_submit = gr.Button("Submit", interactive=False)
151
  moon_output = gr.TextArea(label="Answer", interactive=False)
152
 
153
  # --- YOLOS --- #
154
+ yolos_input.upload(enable_button, None, yolos_submit)
155
+ yolos_input.clear(disable_button, None, yolos_submit)
 
156
  yolos_submit.click(detect_objects, yolos_input, yolos_gallery)
 
 
157
 
158
+ yolos_gallery.select(get_selected_index, None, selected_image)
159
+ yolos_gallery.select(enable_button, None, proceed_button)
160
+ proceed_button.click(to_moondream, [yolos_gallery, selected_image], [tabs, moon_img])
161
+ proceed_button.click(enable_button, None, moon_submit)
162
 
163
  # --- Moondream --- #
164
+ moon_img.upload(enable_button, None, moon_submit)
165
+ moon_img.clear(disable_button, None, moon_submit)
166
  moon_submit.click(answer_question, [moon_img, moon_prompt], moon_output)
167
 
168
  app.queue().launch()