rdezwart commited on
Commit
17317f8
·
1 Parent(s): 33f25f1

Took time to figure this out in Gradio's playground instead of committing here every three minutes, lol

Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -51,6 +51,11 @@ def answer_question(img, prompt):
51
 
52
 
53
  def detect_objects(img: Image.Image):
 
 
 
 
 
54
  inputs = yolos_processor(img, return_tensors="pt")
55
  outputs = yolos_model(**inputs)
56
 
@@ -115,22 +120,27 @@ if __name__ == "__main__":
115
  with gr.Group():
116
  yolos_gallery = gr.Gallery(label="Detected Objects", object_fit="scale-down", columns=3,
117
  show_share_button=False, selected_index=None, allow_preview=False,
118
- type="pil", interactive=False, show_download_button=True)
119
  proceed_button = gr.Button("To Moondream", interactive=False)
120
 
121
  with gr.Tab("Inference", id='moondream'):
122
  with gr.Row(equal_height=False):
123
- with gr.Group():
124
  moon_img = gr.Image(label="Image", type="pil", interactive=True, mirror_webcam=False)
125
- moon_prompt = gr.Textbox(label="Question", value="What is this food item?")
126
- moon_submit = gr.Button("Submit", interactive=False)
127
- moon_output = gr.TextArea(label="Output", interactive=False)
 
 
128
 
129
  # --- YOLOS --- #
130
  yolos_input.upload(lambda: gr.Button(interactive=True), None, yolos_submit)
131
  yolos_input.clear(lambda: gr.Button(interactive=False), None, yolos_submit)
132
 
133
- yolos_submit.click(detect_objects, [yolos_input], yolos_gallery)
 
 
 
134
  yolos_gallery.select(gallery_selected, None, [selected_image, proceed_button])
135
  proceed_button.click(to_moondream, [yolos_gallery, selected_image], [tabs, moon_img, moon_submit])
136
 
 
51
 
52
 
53
  def detect_objects(img: Image.Image):
54
+ """
55
+ Submits an image to the YOLOS-Small-300 model for object detection.
56
+ :param img:
57
+ :return:
58
+ """
59
  inputs = yolos_processor(img, return_tensors="pt")
60
  outputs = yolos_model(**inputs)
61
 
 
120
  with gr.Group():
121
  yolos_gallery = gr.Gallery(label="Detected Objects", object_fit="scale-down", columns=3,
122
  show_share_button=False, selected_index=None, allow_preview=False,
123
+ type="pil", interactive=False)
124
  proceed_button = gr.Button("To Moondream", interactive=False)
125
 
126
  with gr.Tab("Inference", id='moondream'):
127
  with gr.Row(equal_height=False):
128
+ with gr.Column():
129
  moon_img = gr.Image(label="Image", type="pil", interactive=True, mirror_webcam=False)
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