Vision-CAIR commited on
Commit
c236c27
1 Parent(s): e436189

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -191,7 +191,7 @@ 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 +207,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 +372,9 @@ 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,27 +395,27 @@ 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
 
@@ -438,14 +438,14 @@ def gradio_ask(user_message, chatbot, chat_state, gr_img, img_list, upload_flag,
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,7 +457,7 @@ 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
 
@@ -475,9 +475,9 @@ def gradio_answer(chatbot, chat_state, img_list, temperature):
475
 
476
 
477
  def gradio_stream_answer(chatbot, chat_state, img_list, temperature):
478
- print('chat state', chat_state.get_prompt())
479
- if not isinstance(img_list[0], torch.Tensor):
480
- chat.encode_img(img_list)
481
  streamer = chat.stream_answer(conv=chat_state,
482
  img_list=img_list,
483
  temperature=temperature,
@@ -501,7 +501,7 @@ 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 +659,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(share=True, enable_queue=True)
 
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
  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
  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
  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
 
 
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
  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
 
 
475
 
476
 
477
  def gradio_stream_answer(chatbot, chat_state, img_list, temperature):
478
+ if len(img_list) > 0:
479
+ if not isinstance(img_list[0], torch.Tensor):
480
+ chat.encode_img(img_list)
481
  streamer = chat.stream_answer(conv=chat_state,
482
  img_list=img_list,
483
  temperature=temperature,
 
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
 
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)