h-siyuan commited on
Commit
2e769a9
·
verified ·
1 Parent(s): d07cadd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -25
app.py CHANGED
@@ -125,7 +125,7 @@ def run_showui(image, query, session_id):
125
 
126
  def save_and_upload_data(image_path, query, session_id, is_example_image, votes=None):
127
  """Save the data to a JSON file and upload to S3."""
128
- if is_example_image:
129
  return
130
 
131
  votes = votes or {"upvotes": 0, "downvotes": 0}
@@ -148,7 +148,7 @@ def save_and_upload_data(image_path, query, session_id, is_example_image, votes=
148
 
149
  def update_vote(vote_type, session_id, is_example_image):
150
  """Update the vote count and re-upload the JSON file."""
151
- if is_example_image:
152
  return "Example image used. No vote recorded."
153
 
154
  local_file_name = f"{session_id}.json"
@@ -183,22 +183,10 @@ examples = [
183
  ["./examples/safari_google.png", "Click on search bar.", True],
184
  ]
185
 
186
- def check_if_example(query):
187
- """Check if the query matches an example query."""
188
- print(f"Checking query: {query}")
189
- for _, example_query, is_example in examples:
190
- print(f"Comparing with example: {example_query}")
191
- if query.strip() == example_query.strip():
192
- print("Match found!")
193
- return is_example
194
- print("No match found.")
195
- return False
196
-
197
  def build_demo(embed_mode, concurrency_count=1):
198
  with gr.Blocks(title="ShowUI Demo", theme=gr.themes.Default()) as demo:
199
  state_image_path = gr.State(value=None)
200
  state_session_id = gr.State(value=None)
201
- state_is_example_image = gr.State(value=False)
202
 
203
  if not embed_mode:
204
  gr.HTML(
@@ -222,12 +210,33 @@ def build_demo(embed_mode, concurrency_count=1):
222
  )
223
  submit_btn = gr.Button(value="Submit", variant="primary")
224
 
 
225
  gr.Examples(
226
  examples=[[e[0], e[1]] for e in examples],
227
  inputs=[imagebox, textbox],
228
- outputs=[state_is_example_image],
229
- fn=lambda img, query: check_if_example(query),
230
- examples_per_page=3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  )
232
 
233
  with gr.Column(scale=8):
@@ -249,31 +258,31 @@ def build_demo(embed_mode, concurrency_count=1):
249
 
250
  save_and_upload_data(image_path, query, session_id, is_example_image)
251
 
252
- return result_image, click_coords, image_path, session_id, is_example_image
253
 
254
  submit_btn.click(
255
  on_submit,
256
- [imagebox, textbox, state_is_example_image],
257
- [output_img, output_coords, state_image_path, state_session_id, state_is_example_image],
258
  )
259
 
260
  clear_btn.click(
261
- lambda: (None, None, None, None, None),
262
  inputs=None,
263
- outputs=[imagebox, textbox, output_img, output_coords, state_image_path, state_session_id, state_is_example_image],
264
  queue=False
265
  )
266
 
267
  upvote_btn.click(
268
  lambda session_id, is_example_image: update_vote("upvote", session_id, is_example_image),
269
- inputs=[state_session_id, state_is_example_image],
270
  outputs=[],
271
  queue=False
272
  )
273
 
274
  downvote_btn.click(
275
  lambda session_id, is_example_image: update_vote("downvote", session_id, is_example_image),
276
- inputs=[state_session_id, state_is_example_image],
277
  outputs=[],
278
  queue=False
279
  )
@@ -287,4 +296,4 @@ if __name__ == "__main__":
287
  server_port=7860,
288
  ssr_mode=False,
289
  debug=True,
290
- )
 
125
 
126
  def save_and_upload_data(image_path, query, session_id, is_example_image, votes=None):
127
  """Save the data to a JSON file and upload to S3."""
128
+ if is_example_image == "True":
129
  return
130
 
131
  votes = votes or {"upvotes": 0, "downvotes": 0}
 
148
 
149
  def update_vote(vote_type, session_id, is_example_image):
150
  """Update the vote count and re-upload the JSON file."""
151
+ if is_example_image == "True":
152
  return "Example image used. No vote recorded."
153
 
154
  local_file_name = f"{session_id}.json"
 
183
  ["./examples/safari_google.png", "Click on search bar.", True],
184
  ]
185
 
 
 
 
 
 
 
 
 
 
 
 
186
  def build_demo(embed_mode, concurrency_count=1):
187
  with gr.Blocks(title="ShowUI Demo", theme=gr.themes.Default()) as demo:
188
  state_image_path = gr.State(value=None)
189
  state_session_id = gr.State(value=None)
 
190
 
191
  if not embed_mode:
192
  gr.HTML(
 
210
  )
211
  submit_btn = gr.Button(value="Submit", variant="primary")
212
 
213
+ # Examples component
214
  gr.Examples(
215
  examples=[[e[0], e[1]] for e in examples],
216
  inputs=[imagebox, textbox],
217
+ outputs=[textbox], # Only update the query textbox
218
+ examples_per_page=3,
219
+ )
220
+
221
+ # Add a hidden dropdown to pass the `is_example` flag
222
+ is_example_dropdown = gr.Dropdown(
223
+ choices=["True", "False"],
224
+ value="False",
225
+ visible=False,
226
+ label="Is Example Image",
227
+ )
228
+
229
+ def set_is_example(query):
230
+ # Find the example and return its `is_example` flag
231
+ for _, example_query, is_example in examples:
232
+ if query.strip() == example_query.strip():
233
+ return str(is_example) # Return as string for Dropdown compatibility
234
+ return "False"
235
+
236
+ textbox.change(
237
+ set_is_example,
238
+ inputs=[textbox],
239
+ outputs=[is_example_dropdown],
240
  )
241
 
242
  with gr.Column(scale=8):
 
258
 
259
  save_and_upload_data(image_path, query, session_id, is_example_image)
260
 
261
+ return result_image, click_coords, image_path, session_id
262
 
263
  submit_btn.click(
264
  on_submit,
265
+ [imagebox, textbox, is_example_dropdown],
266
+ [output_img, output_coords, state_image_path, state_session_id],
267
  )
268
 
269
  clear_btn.click(
270
+ lambda: (None, None, None, None),
271
  inputs=None,
272
+ outputs=[imagebox, textbox, output_img, output_coords, state_image_path, state_session_id],
273
  queue=False
274
  )
275
 
276
  upvote_btn.click(
277
  lambda session_id, is_example_image: update_vote("upvote", session_id, is_example_image),
278
+ inputs=[state_session_id, is_example_dropdown],
279
  outputs=[],
280
  queue=False
281
  )
282
 
283
  downvote_btn.click(
284
  lambda session_id, is_example_image: update_vote("downvote", session_id, is_example_image),
285
+ inputs=[state_session_id, is_example_dropdown],
286
  outputs=[],
287
  queue=False
288
  )
 
296
  server_port=7860,
297
  ssr_mode=False,
298
  debug=True,
299
+ )