paralym commited on
Commit
a35f45a
1 Parent(s): 7e37767

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -34
app.py CHANGED
@@ -292,48 +292,63 @@ def bot(history, temperature, top_p, max_output_tokens):
292
  text_this_term = ""
293
 
294
  num_new_images = 0
295
- previous_image = False
296
  for i, message in enumerate(history[:-1]):
297
  if type(message[0]) is tuple:
298
- if previous_image:
299
- gr.Warning("Only one image can be uploaded in a conversation. Please reduce the number of images and start a new conversation.")
300
- our_chatbot.conversation = conv_templates[our_chatbot.conv_mode].copy()
301
- return None
302
 
303
  images_this_term.append(message[0][0])
304
  if is_valid_video_filename(message[0][0]):
305
- raise ValueError("Video is not supported")
306
  num_new_images += our_chatbot.num_frames
307
  elif is_valid_image_filename(message[0][0]):
308
  print("#### Load image from local file",message[0][0])
309
  num_new_images += 1
310
  else:
311
- raise ValueError("Invalid image file")
312
- previous_image = True
313
  else:
314
  num_new_images = 0
315
- previous_image = False
316
 
317
  all_image_hash = []
318
  all_image_path = []
319
- for image_path in images_this_term:
320
- with open(image_path, "rb") as image_file:
321
- image_data = image_file.read()
322
- image_hash = hashlib.md5(image_data).hexdigest()
323
- all_image_hash.append(image_hash)
324
- image = PIL.Image.open(image_path).convert("RGB")
325
  t = datetime.datetime.now()
326
- filename = os.path.join(
327
  LOGDIR,
328
- "serve_images",
329
- f"{t.year}-{t.month:02d}-{t.day:02d}",
330
- f"{image_hash}.jpg",
331
  )
332
- all_image_path.append(filename)
333
- if not os.path.isfile(filename):
334
- os.makedirs(os.path.dirname(filename), exist_ok=True)
335
- print("image save to",filename)
336
- image.save(filename)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
 
338
  image_list = []
339
  for f in images_this_term:
@@ -493,7 +508,7 @@ with gr.Blocks(
493
 
494
  chat_input = gr.MultimodalTextbox(
495
  interactive=True,
496
- file_types=["image"],
497
  placeholder="Enter message or upload file...",
498
  show_label=False,
499
  submit_btn="🚀"
@@ -571,18 +586,25 @@ with gr.Blocks(
571
  "text": "Based on the five species in the images, draw a food chain. Explain the role of each species in the food chain.",
572
  }
573
  ],
574
- [
575
- {
576
- "files": [
577
- f"{cur_dir}/examples/realcase_video.mp4",
578
- ],
579
- "text": "Please describe the video in detail.",
580
- },
581
- ]
582
  ],
583
  inputs=[chat_input],
584
- label="RealWorld Cases",
585
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586
 
587
  gr.Markdown(tos_markdown)
588
  gr.Markdown(learn_more_markdown)
 
292
  text_this_term = ""
293
 
294
  num_new_images = 0
295
+ # previous_image = False
296
  for i, message in enumerate(history[:-1]):
297
  if type(message[0]) is tuple:
298
+ # if previous_image:
299
+ # gr.Warning("Only one image can be uploaded in a conversation. Please reduce the number of images and start a new conversation.")
300
+ # our_chatbot.conversation = conv_templates[our_chatbot.conv_mode].copy()
301
+ # return None
302
 
303
  images_this_term.append(message[0][0])
304
  if is_valid_video_filename(message[0][0]):
305
+ # raise ValueError("Video is not supported")
306
  num_new_images += our_chatbot.num_frames
307
  elif is_valid_image_filename(message[0][0]):
308
  print("#### Load image from local file",message[0][0])
309
  num_new_images += 1
310
  else:
311
+ raise ValueError("Invalid file format")
312
+ # previous_image = True
313
  else:
314
  num_new_images = 0
315
+ # previous_image = False
316
 
317
  all_image_hash = []
318
  all_image_path = []
319
+ for file_path in images_this_term:
320
+ with open(file_path, "rb") as file:
321
+ file_data = file.read()
322
+ file_hash = hashlib.md5(file_data).hexdigest()
323
+ all_file_hash.append(file_hash)
324
+
325
  t = datetime.datetime.now()
326
+ output_dir = os.path.join(
327
  LOGDIR,
328
+ "serve_files",
329
+ f"{t.year}-{t.month:02d}-{t.day:02d}"
 
330
  )
331
+ os.makedirs(output_dir, exist_ok=True)
332
+
333
+ if is_valid_image_filename(file_path):
334
+ # Process and save images
335
+ image = Image.open(file_path).convert("RGB")
336
+ filename = os.path.join(output_dir, f"{file_hash}.jpg")
337
+ all_file_path.append(filename)
338
+ if not os.path.isfile(filename):
339
+ print("Image saved to", filename)
340
+ image.save(filename)
341
+
342
+ elif is_valid_video_filename(file_path):
343
+ # Simplified video saving
344
+ filename = os.path.join(output_dir, f"{file_hash}.mp4")
345
+ all_file_path.append(filename)
346
+ if not os.path.isfile(filename):
347
+ print("Video saved to", filename)
348
+ os.makedirs(os.path.dirname(filename), exist_ok=True)
349
+ # Directly copy the video file
350
+ with open(file_path, "rb") as src, open(filename, "wb") as dst:
351
+ dst.write(src.read())
352
 
353
  image_list = []
354
  for f in images_this_term:
 
508
 
509
  chat_input = gr.MultimodalTextbox(
510
  interactive=True,
511
+ file_types=["image", "video"],
512
  placeholder="Enter message or upload file...",
513
  show_label=False,
514
  submit_btn="🚀"
 
586
  "text": "Based on the five species in the images, draw a food chain. Explain the role of each species in the food chain.",
587
  }
588
  ],
 
 
 
 
 
 
 
 
589
  ],
590
  inputs=[chat_input],
591
+ label="Real World Image Cases",
592
  )
593
+ gr.Examples(
594
+ label="Video",
595
+ examples=[
596
+ [
597
+ {
598
+ "files": [
599
+ f"{cur_dir}/examples/realcase_video.mp4",
600
+ ],
601
+ "text": "Please describe the video in detail.",
602
+ },
603
+ ]
604
+ ]
605
+ inputs=[chat_input],
606
+ label="Real World Video Case"
607
+ )
608
 
609
  gr.Markdown(tos_markdown)
610
  gr.Markdown(learn_more_markdown)