rdezwart commited on
Commit
e881756
·
1 Parent(s): b32931d

Hook selected image from gallery into Moondream input

Browse files
Files changed (1) hide show
  1. app.py +37 -22
app.py CHANGED
@@ -22,6 +22,8 @@ yolos_id = "hustvl/yolos-small-300"
22
  yolos_processor: YolosImageProcessor = YolosImageProcessor.from_pretrained(yolos_id)
23
  yolos_model: YolosForObjectDetection = YolosForObjectDetection.from_pretrained(yolos_id)
24
 
 
 
25
 
26
  def answer_question(img, prompt):
27
  """
@@ -72,7 +74,7 @@ def detect_objects(img: Image.Image):
72
  return box_images
73
 
74
 
75
- def gallery_selected(evt: gr.SelectData):
76
  """
77
  Listener for the gallery selection event.
78
 
@@ -82,6 +84,16 @@ def gallery_selected(evt: gr.SelectData):
82
  return evt.index, gr.Button(interactive=evt.selected)
83
 
84
 
 
 
 
 
 
 
 
 
 
 
85
  if __name__ == "__main__":
86
  with gr.Blocks() as app:
87
  gr.Markdown(
@@ -92,29 +104,32 @@ if __name__ == "__main__":
92
  """
93
  )
94
 
95
- with gr.Tab("Object Detection"):
96
- with gr.Row():
97
- with gr.Column():
98
- yolos_input = gr.Image(type="pil")
99
- yolos_submit = gr.Button("Submit")
100
- yolos_output = gr.Gallery(label="Detected Objects", object_fit="scale-down", columns=3, scale=1,
101
- show_share_button=False, selected_index=None, allow_preview=False)
102
-
103
- with gr.Row():
104
- yolos_selected = gr.TextArea(label="Selected Image Index")
105
- proceed_button = gr.Button("To Moondream", interactive=False)
106
-
107
- with gr.Tab("Inference"):
108
- with gr.Row():
109
- moon_prompt = gr.Textbox(label="Input", value="Describe this image.")
110
- moon_submit = gr.Button("Submit")
111
- with gr.Row():
112
- moon_img = gr.Image(label="Image", type="pil")
113
- moon_output = gr.TextArea(label="Output")
 
 
114
 
115
  # --- YOLOS --- #
116
- yolos_submit.click(detect_objects, [yolos_input], yolos_output)
117
- yolos_output.select(gallery_selected, None, [yolos_selected, proceed_button])
 
118
 
119
  # --- Moondream --- #
120
  moon_submit.click(answer_question, [moon_img, moon_prompt], moon_output)
 
22
  yolos_processor: YolosImageProcessor = YolosImageProcessor.from_pretrained(yolos_id)
23
  yolos_model: YolosForObjectDetection = YolosForObjectDetection.from_pretrained(yolos_id)
24
 
25
+ selected_image: int | None = None
26
+
27
 
28
  def answer_question(img, prompt):
29
  """
 
74
  return box_images
75
 
76
 
77
+ def gallery_selected(evt: gr.SelectData) -> tuple[int, gr.Button]:
78
  """
79
  Listener for the gallery selection event.
80
 
 
84
  return evt.index, gr.Button(interactive=evt.selected)
85
 
86
 
87
+ def to_moondream(images: list[tuple[Image.Image, str | None]]) -> tuple[gr.Tabs, Image.Image]:
88
+ """
89
+ Listener that sends selected gallery image to the moondream model.
90
+
91
+ :param images: list of images from yolos_gallery
92
+ :return: new selected tab and selected image (no caption)
93
+ """
94
+ return gr.Tabs(selected='moondream'), images[selected_image][0]
95
+
96
+
97
  if __name__ == "__main__":
98
  with gr.Blocks() as app:
99
  gr.Markdown(
 
104
  """
105
  )
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("Submit", scale=0)
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, [tabs, moon_img])
133
 
134
  # --- Moondream --- #
135
  moon_submit.click(answer_question, [moon_img, moon_prompt], moon_output)