openfree commited on
Commit
c78a3dc
ยท
verified ยท
1 Parent(s): e24b657

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -15
app.py CHANGED
@@ -22,12 +22,16 @@ from transformers import pipeline
22
  from gradio_client import Client
23
  from gradio_client.utils import encode_file_to_base64
24
  import tempfile
25
-
26
  from PIL import Image as PILImage
27
  import base64
28
  import io
29
  from transformers import pipeline as translation_pipeline
30
  from gradio_client.utils import encode_file_to_base64
 
 
 
 
 
31
 
32
  translator = translation_pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
33
 
@@ -415,45 +419,59 @@ def process_image(image):
415
 
416
  img = img.resize((720, 480), PILImage.LANCZOS)
417
 
418
- # ์ด๋ฏธ์ง€๋ฅผ base64๋กœ ์ธ์ฝ”๋”ฉ
419
- buffered = io.BytesIO()
420
- img.save(buffered, format="PNG")
421
- img_str = base64.b64encode(buffered.getvalue()).decode()
422
-
423
- # JSON ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
424
- img_json = json.dumps({"image": img_str})
425
-
426
- return img_json, None
427
  except Exception as e:
 
428
  return None, f"์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
429
 
430
  def image_to_video(image, prompt, seed, high_resolution, frame_interpolation):
 
 
 
 
431
  if not prompt:
432
  return None, None, None, None, "ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."
433
 
434
  # ํ”„๋กฌํ”„ํŠธ๋ฅผ ์˜์–ด๋กœ ๋ฒˆ์—ญ
435
  prompt = translate_to_english(prompt)
 
436
 
437
- image_json, error = process_image(image)
438
  if error:
 
439
  return None, None, None, None, error
440
 
441
  client = Client("http://hugpu.ai:7916/", auth=("gini", "pick"))
442
 
443
  try:
 
 
 
 
444
  result = client.predict(
445
  prompt,
446
- image_json, # JSON ํ˜•์‹์˜ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ „์†ก
447
  seed,
448
  high_resolution,
449
  frame_interpolation,
450
  api_name="/lambda_2"
451
  )
 
 
 
 
452
 
453
  return result[0]['video'], result[1], result[2], result[3], prompt
454
  except Exception as e:
455
- print(f"API ํ˜ธ์ถœ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
456
- return None, None, None, None, f"๋น„๋””์˜ค ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
 
 
457
 
458
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css="""
459
  footer {
@@ -729,7 +747,9 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css="""
729
  outputs=[prompt_input]
730
  )
731
 
 
732
  def handle_generation(image, prompt, seed, high_resolution, frame_interpolation, use_enhanced):
 
733
  if use_enhanced:
734
  prompt = enhance_prompt(prompt)
735
 
@@ -739,12 +759,14 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css="""
739
  try:
740
  video, video_file, gif_file, used_seed, message = image_to_video(image, prompt, seed, high_resolution, frame_interpolation)
741
  if video is None:
 
742
  return [None, None, None, None, None, gr.update(visible=True, value=message)]
743
  else:
 
744
  return [video, video_file, gif_file, used_seed, message, gr.update(visible=False)]
745
  except Exception as e:
746
  error_message = f"๋น„๋””์˜ค ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
747
- print(error_message)
748
  return [None, None, None, None, None, gr.update(visible=True, value=error_message)]
749
 
750
 
 
22
  from gradio_client import Client
23
  from gradio_client.utils import encode_file_to_base64
24
  import tempfile
 
25
  from PIL import Image as PILImage
26
  import base64
27
  import io
28
  from transformers import pipeline as translation_pipeline
29
  from gradio_client.utils import encode_file_to_base64
30
+ import logging
31
+ from gradio_client.utils import File
32
+
33
+ logging.basicConfig(level=logging.INFO)
34
+ logger = logging.getLogger(__name__)
35
 
36
  translator = translation_pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
37
 
 
419
 
420
  img = img.resize((720, 480), PILImage.LANCZOS)
421
 
422
+ # ์ด๋ฏธ์ง€๋ฅผ ์ž„์‹œ ํŒŒ์ผ๋กœ ์ €์žฅ
423
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
424
+ img.save(temp_file, format="PNG")
425
+ temp_file_path = temp_file.name
426
+
427
+ return temp_file_path, None
 
 
 
428
  except Exception as e:
429
+ logger.error(f"์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
430
  return None, f"์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
431
 
432
  def image_to_video(image, prompt, seed, high_resolution, frame_interpolation):
433
+ logger.info(f"์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ: {prompt}")
434
+ logger.info(f"์ด๋ฏธ์ง€ ํƒ€์ž…: {type(image)}")
435
+ logger.info(f"Seed: {seed}, ๊ณ ํ•ด์ƒ๋„: {high_resolution}, ํ”„๋ ˆ์ž„ ๋ณด๊ฐ„: {frame_interpolation}")
436
+
437
  if not prompt:
438
  return None, None, None, None, "ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."
439
 
440
  # ํ”„๋กฌํ”„ํŠธ๋ฅผ ์˜์–ด๋กœ ๋ฒˆ์—ญ
441
  prompt = translate_to_english(prompt)
442
+ logger.info(f"๋ฒˆ์—ญ๋œ ํ”„๋กฌํ”„ํŠธ: {prompt}")
443
 
444
+ image_path, error = process_image(image)
445
  if error:
446
+ logger.error(f"์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ์˜ค๋ฅ˜: {error}")
447
  return None, None, None, None, error
448
 
449
  client = Client("http://hugpu.ai:7916/", auth=("gini", "pick"))
450
 
451
  try:
452
+ # File ๊ฐ์ฒด ์ƒ์„ฑ
453
+ image_file = File(image_path)
454
+
455
+ logger.info("API ํ˜ธ์ถœ ์‹œ์ž‘")
456
  result = client.predict(
457
  prompt,
458
+ image_file,
459
  seed,
460
  high_resolution,
461
  frame_interpolation,
462
  api_name="/lambda_2"
463
  )
464
+ logger.info("API ํ˜ธ์ถœ ์™„๋ฃŒ")
465
+
466
+ # ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ
467
+ os.remove(image_path)
468
 
469
  return result[0]['video'], result[1], result[2], result[3], prompt
470
  except Exception as e:
471
+ logger.error(f"API ํ˜ธ์ถœ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
472
+ # ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ
473
+ os.remove(image_path)
474
+ return None, None, None, None, f"๋น„๋””์˜ค ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}
475
 
476
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css="""
477
  footer {
 
747
  outputs=[prompt_input]
748
  )
749
 
750
+
751
  def handle_generation(image, prompt, seed, high_resolution, frame_interpolation, use_enhanced):
752
+ logger.info("handle_generation ํ•จ์ˆ˜ ์‹œ์ž‘")
753
  if use_enhanced:
754
  prompt = enhance_prompt(prompt)
755
 
 
759
  try:
760
  video, video_file, gif_file, used_seed, message = image_to_video(image, prompt, seed, high_resolution, frame_interpolation)
761
  if video is None:
762
+ logger.warning(f"๋น„๋””์˜ค ์ƒ์„ฑ ์‹คํŒจ: {message}")
763
  return [None, None, None, None, None, gr.update(visible=True, value=message)]
764
  else:
765
+ logger.info("๋น„๋””์˜ค ์ƒ์„ฑ ์„ฑ๊ณต")
766
  return [video, video_file, gif_file, used_seed, message, gr.update(visible=False)]
767
  except Exception as e:
768
  error_message = f"๋น„๋””์˜ค ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
769
+ logger.error(error_message)
770
  return [None, None, None, None, None, gr.update(visible=True, value=error_message)]
771
 
772