rdezwart commited on
Commit
331994a
·
1 Parent(s): 29ebbdc

Update UI and lock out buttons when no images present

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -82,15 +82,15 @@ def gallery_selected(evt: gr.SelectData) -> tuple[int, gr.Button]:
82
  return evt.index, gr.Button(interactive=evt.selected)
83
 
84
 
85
- def to_moondream(images: list[tuple[Image.Image, str | None]], index: int) -> tuple[gr.Tabs, Image.Image]:
86
  """
87
  Listener that sends selected gallery image to the moondream model.
88
 
89
  :param images: list of images from yolos_gallery
90
  :param index: index of selected gallery image
91
- :return: new selected tab and selected image (no caption)
92
  """
93
- return gr.Tabs(selected='moondream'), images[index][0]
94
 
95
 
96
  if __name__ == "__main__":
@@ -105,33 +105,37 @@ if __name__ == "__main__":
105
  selected_image = gr.Number(visible=False, precision=0)
106
 
107
  # Referenced: https://github.com/gradio-app/gradio/issues/7726#issuecomment-2028051431
108
- with gr.Tabs(selected='yolos') as tabs:
109
  with gr.Tab("Object Detection", id='yolos'):
110
- with gr.Row():
111
- with gr.Column():
112
- yolos_input = gr.Image(type="pil", scale=1)
113
- yolos_submit = gr.Button("Detect Objects")
114
-
115
- with gr.Column():
116
  yolos_gallery = gr.Gallery(label="Detected Objects", object_fit="scale-down", columns=3,
117
- scale=2, show_share_button=False, selected_index=None,
118
- allow_preview=False, type="pil", interactive=False)
119
  proceed_button = gr.Button("To Moondream", interactive=False)
120
 
121
  with gr.Tab("Inference", id='moondream'):
122
  with gr.Row():
123
- moon_prompt = gr.Textbox(label="Input", value="Describe this image.")
124
- moon_submit = gr.Button("Submit")
125
- with gr.Row():
126
- moon_img = gr.Image(label="Image", type="pil", interactive=True)
127
- moon_output = gr.TextArea(label="Output")
128
 
129
  # --- YOLOS --- #
 
 
 
130
  yolos_submit.click(detect_objects, [yolos_input], yolos_gallery)
131
  yolos_gallery.select(gallery_selected, None, [selected_image, proceed_button])
132
- proceed_button.click(to_moondream, [yolos_gallery, selected_image], [tabs, moon_img])
133
 
134
  # --- Moondream --- #
 
 
135
  moon_submit.click(answer_question, [moon_img, moon_prompt], moon_output)
136
 
137
  app.queue().launch()
 
82
  return evt.index, gr.Button(interactive=evt.selected)
83
 
84
 
85
+ def to_moondream(images: list[tuple[Image.Image, str | None]], index: int) -> tuple[gr.Tabs, Image.Image, gr.Button]:
86
  """
87
  Listener that sends selected gallery image to the moondream model.
88
 
89
  :param images: list of images from yolos_gallery
90
  :param index: index of selected gallery image
91
+ :return: selected tab, selected image (no caption), and an enabled button
92
  """
93
+ return gr.Tabs(selected='moondream'), images[index][0], gr.Button(interactive=True)
94
 
95
 
96
  if __name__ == "__main__":
 
105
  selected_image = gr.Number(visible=False, precision=0)
106
 
107
  # Referenced: https://github.com/gradio-app/gradio/issues/7726#issuecomment-2028051431
108
+ with gr.Tabs() as tabs:
109
  with gr.Tab("Object Detection", id='yolos'):
110
+ with gr.Row(equal_height=False):
111
+ with gr.Group():
112
+ yolos_input = gr.Image(label="Input Image", type="pil")
113
+ yolos_submit = gr.Button("Detect Objects", interactive=False)
114
+ with gr.Group():
 
115
  yolos_gallery = gr.Gallery(label="Detected Objects", object_fit="scale-down", columns=3,
116
+ show_share_button=False, selected_index=None, allow_preview=False,
117
+ type="pil", interactive=False)
118
  proceed_button = gr.Button("To Moondream", interactive=False)
119
 
120
  with gr.Tab("Inference", id='moondream'):
121
  with gr.Row():
122
+ with gr.Group():
123
+ moon_img = gr.Image(label="Image", type="pil", interactive=True)
124
+ moon_prompt = gr.Textbox(label="Question", value="What is this food item?")
125
+ moon_submit = gr.Button("Submit", interactive=False)
126
+ moon_output = gr.TextArea(label="Output", interactive=False)
127
 
128
  # --- YOLOS --- #
129
+ yolos_input.upload(lambda: gr.Button(interactive=True), None, yolos_submit)
130
+ yolos_input.clear(lambda: gr.Button(interactive=False), None, yolos_submit)
131
+
132
  yolos_submit.click(detect_objects, [yolos_input], yolos_gallery)
133
  yolos_gallery.select(gallery_selected, None, [selected_image, proceed_button])
134
+ proceed_button.click(to_moondream, [yolos_gallery, selected_image], [tabs, moon_img, moon_submit])
135
 
136
  # --- Moondream --- #
137
+ moon_img.upload(lambda: gr.Button(interactive=True), None, moon_submit)
138
+ moon_img.clear(lambda: gr.Button(interactive=False), None, moon_submit)
139
  moon_submit.click(answer_question, [moon_img, moon_prompt], moon_output)
140
 
141
  app.queue().launch()