Spaces:
Running
Running
Tidy up event listeners
Browse filesSeparated returning button states from functions that were more meant for their own thing
app.py
CHANGED
@@ -78,25 +78,42 @@ def detect_objects(img: Image.Image):
|
|
78 |
return box_images
|
79 |
|
80 |
|
81 |
-
def
|
82 |
"""
|
83 |
Listener for the gallery selection event.
|
84 |
|
85 |
:return: index of the currently selected image
|
86 |
"""
|
87 |
-
|
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
|
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
|
98 |
"""
|
99 |
-
return gr.Tabs(selected='moondream'), images[index][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
134 |
moon_output = gr.TextArea(label="Answer", interactive=False)
|
135 |
|
136 |
# --- YOLOS --- #
|
137 |
-
yolos_input.upload(
|
138 |
-
yolos_input.clear(
|
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(
|
145 |
-
|
|
|
|
|
146 |
|
147 |
# --- Moondream --- #
|
148 |
-
moon_img.upload(
|
149 |
-
moon_img.clear(
|
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()
|