Vision-CAIR commited on
Commit
f1bfd5b
1 Parent(s): 4120bef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -23
app.py CHANGED
@@ -191,7 +191,6 @@ def visualize_all_bbox_together(image, generation):
191
  return None, ''
192
 
193
  generation = html.unescape(generation)
194
- # print('gen begin', generation)
195
 
196
  image_width, image_height = image.size
197
  image = image.resize([500, int(500 / image_width * image_height)])
@@ -207,7 +206,7 @@ def visualize_all_bbox_together(image, generation):
207
  try:
208
  obj, string = string.split('</p>')
209
  except ValueError:
210
- # print('wrong string: ', string)
211
  continue
212
  bbox_list = string.split('<delim>')
213
  flag = False
@@ -372,9 +371,7 @@ def visualize_all_bbox_together(image, generation):
372
  color = next(color_gen)
373
  return f'<span style="color:rgb{color}">{phrase}</span>'
374
 
375
- # print('gen before', generation)
376
  generation = re.sub(r'{<\d+><\d+><\d+><\d+>}|<delim>', '', generation)
377
- # print('gen after', generation)
378
  generation_colored = re.sub(r'<p>(.*?)</p>', colored_phrases, generation)
379
  else:
380
  generation_colored = ''
@@ -395,31 +392,28 @@ def gradio_reset(chat_state, img_list):
395
  def image_upload_trigger(upload_flag, replace_flag, img_list):
396
  # set the upload flag to true when receive a new image.
397
  # if there is an old image (and old conversation), set the replace flag to true to reset the conv later.
398
- # print('flag', upload_flag, replace_flag)
399
- # print("SET UPLOAD FLAG!")
400
  upload_flag = 1
401
  if img_list:
402
- # print("SET REPLACE FLAG!")
403
  replace_flag = 1
404
- # print('flag', upload_flag, replace_flag)
405
  return upload_flag, replace_flag
406
 
407
 
408
  def example_trigger(text_input, image, upload_flag, replace_flag, img_list):
409
  # set the upload flag to true when receive a new image.
410
  # if there is an old image (and old conversation), set the replace flag to true to reset the conv later.
411
- # print('flag', upload_flag, replace_flag)
412
- # print("SET UPLOAD FLAG!")
413
  upload_flag = 1
414
  if img_list or replace_flag == 1:
415
- # print("SET REPLACE FLAG!")
416
  replace_flag = 1
417
 
418
- # print('flag', upload_flag, replace_flag)
419
  return upload_flag, replace_flag
420
 
421
 
422
  def gradio_ask(user_message, chatbot, chat_state, gr_img, img_list, upload_flag, replace_flag):
 
 
 
 
 
423
  if isinstance(gr_img, dict):
424
  gr_img, mask = gr_img['image'], gr_img['mask']
425
  else:
@@ -432,20 +426,14 @@ def gradio_ask(user_message, chatbot, chat_state, gr_img, img_list, upload_flag,
432
  bbox = mask2bbox(mask)
433
  user_message = user_message + bbox
434
 
435
- if len(user_message) == 0:
436
- return gr.update(interactive=True, placeholder='Input should not be empty!'), chatbot, chat_state
437
-
438
  if chat_state is None:
439
  chat_state = CONV_VISION.copy()
440
 
441
- # print('upload flag: {}'.format(upload_flag))
442
  if upload_flag:
443
  if replace_flag:
444
- print('RESET!!!!!!!')
445
  chat_state = CONV_VISION.copy() # new image, reset everything
446
  replace_flag = 0
447
  chatbot = []
448
- # print('UPLOAD IMAGE!!')
449
  img_list = []
450
  llm_message = chat.upload_img(gr_img, chat_state, img_list)
451
  upload_flag = 0
@@ -457,11 +445,10 @@ def gradio_ask(user_message, chatbot, chat_state, gr_img, img_list, upload_flag,
457
  if '[identify]' in user_message:
458
  visual_img, _ = visualize_all_bbox_together(gr_img, user_message)
459
  if visual_img is not None:
460
- # print('Visualizing the input')
461
  file_path = save_tmp_img(visual_img)
462
  chatbot = chatbot + [[(file_path,), None]]
463
 
464
- return '', chatbot, chat_state, img_list, upload_flag, replace_flag
465
 
466
 
467
  def gradio_answer(chatbot, chat_state, img_list, temperature):
@@ -489,7 +476,6 @@ def gradio_stream_answer(chatbot, chat_state, img_list, temperature):
489
  output += escapped
490
  chatbot[-1][1] = output
491
  yield chatbot, chat_state
492
- # print('message: ', chat_state.messages)
493
  chat_state.messages[-1][1] = '</s>'
494
  return chatbot, chat_state
495
 
@@ -501,7 +487,6 @@ def gradio_visualize(chatbot, gr_img):
501
  unescaped = reverse_escape(chatbot[-1][1])
502
  visual_img, generation_color = visualize_all_bbox_together(gr_img, unescaped)
503
  if visual_img is not None:
504
- # print('Visualizing the output')
505
  if len(generation_color):
506
  chatbot[-1][1] = generation_color
507
  file_path = save_tmp_img(visual_img)
@@ -659,4 +644,4 @@ with gr.Blocks() as demo:
659
 
660
  clear.click(gradio_reset, [chat_state, img_list], [chatbot, image, text_input, chat_state, img_list], queue=False)
661
 
662
- demo.launch(enable_queue=True)
 
191
  return None, ''
192
 
193
  generation = html.unescape(generation)
 
194
 
195
  image_width, image_height = image.size
196
  image = image.resize([500, int(500 / image_width * image_height)])
 
206
  try:
207
  obj, string = string.split('</p>')
208
  except ValueError:
209
+ print('wrong string: ', string)
210
  continue
211
  bbox_list = string.split('<delim>')
212
  flag = False
 
371
  color = next(color_gen)
372
  return f'<span style="color:rgb{color}">{phrase}</span>'
373
 
 
374
  generation = re.sub(r'{<\d+><\d+><\d+><\d+>}|<delim>', '', generation)
 
375
  generation_colored = re.sub(r'<p>(.*?)</p>', colored_phrases, generation)
376
  else:
377
  generation_colored = ''
 
392
  def image_upload_trigger(upload_flag, replace_flag, img_list):
393
  # set the upload flag to true when receive a new image.
394
  # if there is an old image (and old conversation), set the replace flag to true to reset the conv later.
 
 
395
  upload_flag = 1
396
  if img_list:
 
397
  replace_flag = 1
 
398
  return upload_flag, replace_flag
399
 
400
 
401
  def example_trigger(text_input, image, upload_flag, replace_flag, img_list):
402
  # set the upload flag to true when receive a new image.
403
  # if there is an old image (and old conversation), set the replace flag to true to reset the conv later.
 
 
404
  upload_flag = 1
405
  if img_list or replace_flag == 1:
 
406
  replace_flag = 1
407
 
 
408
  return upload_flag, replace_flag
409
 
410
 
411
  def gradio_ask(user_message, chatbot, chat_state, gr_img, img_list, upload_flag, replace_flag):
412
+ if len(user_message) == 0:
413
+ text_box_show = 'Input should not be empty!'
414
+ else:
415
+ text_box_show = ''
416
+
417
  if isinstance(gr_img, dict):
418
  gr_img, mask = gr_img['image'], gr_img['mask']
419
  else:
 
426
  bbox = mask2bbox(mask)
427
  user_message = user_message + bbox
428
 
 
 
 
429
  if chat_state is None:
430
  chat_state = CONV_VISION.copy()
431
 
 
432
  if upload_flag:
433
  if replace_flag:
 
434
  chat_state = CONV_VISION.copy() # new image, reset everything
435
  replace_flag = 0
436
  chatbot = []
 
437
  img_list = []
438
  llm_message = chat.upload_img(gr_img, chat_state, img_list)
439
  upload_flag = 0
 
445
  if '[identify]' in user_message:
446
  visual_img, _ = visualize_all_bbox_together(gr_img, user_message)
447
  if visual_img is not None:
 
448
  file_path = save_tmp_img(visual_img)
449
  chatbot = chatbot + [[(file_path,), None]]
450
 
451
+ return text_box_show, chatbot, chat_state, img_list, upload_flag, replace_flag
452
 
453
 
454
  def gradio_answer(chatbot, chat_state, img_list, temperature):
 
476
  output += escapped
477
  chatbot[-1][1] = output
478
  yield chatbot, chat_state
 
479
  chat_state.messages[-1][1] = '</s>'
480
  return chatbot, chat_state
481
 
 
487
  unescaped = reverse_escape(chatbot[-1][1])
488
  visual_img, generation_color = visualize_all_bbox_together(gr_img, unescaped)
489
  if visual_img is not None:
 
490
  if len(generation_color):
491
  chatbot[-1][1] = generation_color
492
  file_path = save_tmp_img(visual_img)
 
644
 
645
  clear.click(gradio_reset, [chat_state, img_list], [chatbot, image, text_input, chat_state, img_list], queue=False)
646
 
647
+ demo.launch(enable_queue=True)