openfree commited on
Commit
f2f0666
Β·
verified Β·
1 Parent(s): 75c3863

Update app-backup1.py

Browse files
Files changed (1) hide show
  1. app-backup1.py +122 -52
app-backup1.py CHANGED
@@ -557,36 +557,67 @@ def analyze_scenario(scenario):
557
  {"role": "user", "content": f"""
558
  λ‹€μŒ 슀크립트의 λΆ„μœ„κΈ°μ™€ 감성을 ν‘œν˜„ν•  수 μžˆλŠ” λ°°κ²½ μ˜μƒ ν”„λ‘¬ν”„νŠΈλ₯Ό μƒμ„±ν•΄μ£Όμ„Έμš”:
559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
  {scenario}
561
 
562
  각 μ„Ήμ…˜λ³„λ‘œ 직접적인 μ œν’ˆ λ¬˜μ‚¬λŠ” ν”Όν•˜κ³ , 슀크립트의 감성을 ν‘œν˜„ν•˜λŠ” λ°°κ²½ μ˜μƒμ— μ§‘μ€‘ν•΄μ£Όμ„Έμš”."""},
563
  ]
564
 
565
- try:
566
  response = client.chat.completions.create(
567
  model="gpt-4-1106-preview",
568
  messages=messages,
569
  max_tokens=2000,
570
  )
571
- prompts = response.choices[0].message.content.strip().split("\n\n")
572
 
573
- # ν”„λ‘¬ν”„νŠΈ 처리 λ‘œμ§μ€ 동일
574
- section_prompts = []
575
  current_section = ""
576
- for line in prompts:
577
- if line.strip():
578
- if any(section in line for section in ["1.", "2.", "3.", "4.", "5."]):
579
- if current_section:
580
- section_prompts.append(current_section)
581
- current_section = line
582
- else:
583
- current_section += "\n" + line
584
  if current_section:
585
- section_prompts.append(current_section)
586
 
587
- while len(section_prompts) < 5:
588
- section_prompts.append("μΆ”κ°€ μ„Ήμ…˜μ΄ ν•„μš”ν•©λ‹ˆλ‹€.")
589
- return section_prompts[:5]
 
 
 
590
  except Exception as e:
591
  print(f"Error during scenario analysis: {e}")
592
  return ["Error occurred during analysis"] * 5
@@ -612,7 +643,6 @@ def generate_section_video(prompt, preset, section_number=1, base_seed=171198, p
612
  print(f"Error in section {section_number}: {e}")
613
  raise gr.Error(f"μ„Ήμ…˜ {section_number} 생성 쀑 였λ₯˜: {str(e)}")
614
 
615
-
616
  # κ°œλ³„ μ„Ήμ…˜ ν”„λ‘¬ν”„νŠΈ 생성 ν•¨μˆ˜ μΆ”κ°€
617
  def generate_single_section_prompt(scenario, section_number):
618
  """κ°œλ³„ μ„Ήμ…˜μ— λŒ€ν•œ ν”„λ‘¬ν”„νŠΈ 생성"""
@@ -683,13 +713,50 @@ def combine_videos(video_paths, output_path):
683
 
684
  def merge_section_videos(section1, section2, section3, section4, section5):
685
  """μ„Ήμ…˜ λΉ„λ””μ˜€λ“€μ„ ν•˜λ‚˜λ‘œ κ²°ν•©"""
686
- videos = [section1, section2, section3, section4, section5]
687
 
688
- if not all(videos):
689
- raise gr.Error("λͺ¨λ“  μ„Ήμ…˜μ˜ μ˜μƒμ΄ λ¨Όμ € μƒμ„±λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.")
 
 
 
 
690
 
691
- output_path = tempfile.mktemp(suffix=".mp4")
692
- return combine_videos(videos, output_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
693
 
694
 
695
  # Gradio Interface Definition
@@ -973,6 +1040,39 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
973
  ]
974
  )
975
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
976
  # μ„Ήμ…˜ 생성 이벀트 ν•Έλ“€λŸ¬
977
  section1_generate.click(
978
  fn=generate_section_video,
@@ -1011,36 +1111,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
1011
 
1012
 
1013
 
1014
- # μ„Ήμ…˜ 생성 이벀트 ν•Έλ“€λŸ¬
1015
- section1_generate.click(
1016
- fn=lambda p, pr: generate_section_video(p, pr, 1),
1017
- inputs=[section1_prompt, scenario_preset],
1018
- outputs=section1_video
1019
- )
1020
-
1021
- section2_generate.click(
1022
- fn=lambda p, pr: generate_section_video(p, pr, 2),
1023
- inputs=[section2_prompt, scenario_preset],
1024
- outputs=section2_video
1025
- )
1026
-
1027
- section3_generate.click(
1028
- fn=lambda p, pr: generate_section_video(p, pr, 3),
1029
- inputs=[section3_prompt, scenario_preset],
1030
- outputs=section3_video
1031
- )
1032
-
1033
- section4_generate.click(
1034
- fn=lambda p, pr: generate_section_video(p, pr, 4),
1035
- inputs=[section4_prompt, scenario_preset],
1036
- outputs=section4_video
1037
- )
1038
-
1039
- section5_generate.click(
1040
- fn=lambda p, pr: generate_section_video(p, pr, 5),
1041
- inputs=[section5_prompt, scenario_preset],
1042
- outputs=section5_video
1043
- )
1044
 
1045
 
1046
  # 이벀트 ν•Έλ“€λŸ¬ μΆ”κ°€
 
557
  {"role": "user", "content": f"""
558
  λ‹€μŒ 슀크립트의 λΆ„μœ„κΈ°μ™€ 감성을 ν‘œν˜„ν•  수 μžˆλŠ” λ°°κ²½ μ˜μƒ ν”„λ‘¬ν”„νŠΈλ₯Ό μƒμ„±ν•΄μ£Όμ„Έμš”:
559
 
560
+ μ£Όμ–΄μ§„ 슀크립트의 λΆ„μœ„κΈ°μ™€ λ§₯락을 μ‹œκ°μ  배경으둜 ν‘œν˜„ν•˜λ˜, λ‹€μŒ 원칙을 λ°˜λ“œμ‹œ μ€€μˆ˜ν•˜μ„Έμš”:
561
+
562
+ 1. μ œν’ˆμ΄λ‚˜ μ„œλΉ„μŠ€λ₯Ό μ§μ ‘μ μœΌλ‘œ λ¬˜μ‚¬ν•˜μ§€ 말 것
563
+ 2. 슀크립트의 감성과 ν†€μ•€λ§€λ„ˆλ₯Ό ν‘œν˜„ν•˜λŠ” λ°°κ²½ μ˜μƒμ— 집쀑할 것
564
+ 3. 5개 μ„Ήμ…˜μ΄ ν•˜λ‚˜μ˜ μ΄μ•ΌκΈ°μ²˜λŸΌ μžμ—°μŠ€λŸ½κ²Œ μ—°κ²°λ˜λ„λ‘ ν•  것
565
+ 4. 좔상적이고 μ€μœ μ μΈ μ‹œκ° ν‘œν˜„μ„ ν™œμš©ν•  것
566
+
567
+ 각 μ„Ήμ…˜λ³„ ν”„λ‘¬ν”„νŠΈ μž‘μ„± κ°€μ΄λ“œ:
568
+ 1. λ°°κ²½ 및 ν•„μš”μ„±: 주제의 μ „λ°˜μ μΈ λΆ„μœ„κΈ°λ₯Ό ν‘œν˜„ν•˜λŠ” λ°°κ²½ 씬
569
+ 2. ν₯λ―Έ 유발: κΈ΄μž₯κ°μ΄λ‚˜ κ°ˆλ“±μ„ μ•”μ‹œν•˜λŠ” λΆ„μœ„κΈ° μžˆλŠ” λ°°κ²½
570
+ 3. ν•΄κ²°μ±… μ œμ‹œ: 희망적이고 밝은 ν†€μ˜ λ°°κ²½ μ „ν™˜
571
+ 4. λ³Έλ‘ : μ•ˆμ •κ° 있고 신뒰도λ₯Ό λ†’μ΄λŠ” λ°°κ²½
572
+ 5. κ²°λ‘ : μž„νŒ©νŠΈ μžˆλŠ” 마무리λ₯Ό μœ„ν•œ 역동적인 λ°°κ²½
573
+
574
+ λͺ¨λ“  μ„Ήμ…˜μ΄ μΌκ΄€λœ μŠ€νƒ€μΌκ³Ό 톀을 μœ μ§€ν•˜λ©΄μ„œλ„ μžμ—°μŠ€λŸ½κ²Œ 이어지도둝 κ΅¬μ„±ν•˜μ„Έμš”.
575
+
576
+ 각 μ„Ήμ…˜μ˜ ν”„λ‘¬ν”„νŠΈ μž‘μ„±μ‹œ λ°˜λ“œμ‹œ λ‹€μŒ ꡬ쑰에 맞게 κ°œμ„ ν•΄μ£Όμ„Έμš”:
577
+ 1. μ£Όμš” λ™μž‘μ„ λͺ…ν™•ν•œ ν•œ λ¬Έμž₯으둜 μ‹œμž‘
578
+ 2. ꡬ체적인 λ™μž‘κ³Ό 제슀처λ₯Ό μ‹œκ°„ μˆœμ„œλŒ€λ‘œ μ„€λͺ…
579
+ 3. 캐릭터/객체의 μ™Έλͺ¨λ₯Ό μƒμ„Ένžˆ λ¬˜μ‚¬
580
+ 4. λ°°κ²½κ³Ό ν™˜κ²½ μ„ΈλΆ€ 사항을 ꡬ체적으둜 포함
581
+ 5. 카메라 각도와 μ›€μ§μž„μ„ λͺ…μ‹œ
582
+ 6. μ‘°λͺ…κ³Ό 색상을 μžμ„Ένžˆ μ„€λͺ…
583
+ 7. λ³€ν™”λ‚˜ κ°‘μž‘μŠ€λŸ¬μš΄ 사건을 μžμ—°μŠ€λŸ½κ²Œ 포함
584
+ λͺ¨λ“  μ„€λͺ…은 ν•˜λ‚˜μ˜ μžμ—°μŠ€λŸ¬μš΄ λ¬Έλ‹¨μœΌλ‘œ μž‘μ„±ν•˜κ³ ,
585
+ 촬영 감독이 촬영 λͺ©λ‘μ„ μ„€λͺ…ν•˜λŠ” κ²ƒμ²˜λŸΌ ꡬ체적이고 μ‹œκ°μ μœΌλ‘œ μž‘μ„±ν•˜μ„Έμš”.
586
+ 200단어λ₯Ό λ„˜μ§€ μ•Šλ„λ‘ ν•˜λ˜, μ΅œλŒ€ν•œ μƒμ„Έν•˜κ²Œ μž‘μ„±ν•˜μ„Έμš”.
587
+
588
  {scenario}
589
 
590
  각 μ„Ήμ…˜λ³„λ‘œ 직접적인 μ œν’ˆ λ¬˜μ‚¬λŠ” ν”Όν•˜κ³ , 슀크립트의 감성을 ν‘œν˜„ν•˜λŠ” λ°°κ²½ μ˜μƒμ— μ§‘μ€‘ν•΄μ£Όμ„Έμš”."""},
591
  ]
592
 
593
+
594
  response = client.chat.completions.create(
595
  model="gpt-4-1106-preview",
596
  messages=messages,
597
  max_tokens=2000,
598
  )
599
+ content = response.choices[0].message.content.strip()
600
 
601
+ # μ„Ήμ…˜λ³„λ‘œ 뢄리
602
+ sections = []
603
  current_section = ""
604
+ for line in content.split('\n'):
605
+ if line.strip().startswith(('1.', '2.', '3.', '4.', '5.')):
606
+ if current_section:
607
+ sections.append(current_section.strip())
608
+ current_section = line
609
+ else:
610
+ current_section += "\n" + line
611
+
612
  if current_section:
613
+ sections.append(current_section.strip())
614
 
615
+ # λΆ€μ‘±ν•œ μ„Ήμ…˜ μ±„μš°κΈ°
616
+ while len(sections) < 5:
617
+ sections.append("μΆ”κ°€ μ„Ήμ…˜μ΄ ν•„μš”ν•©λ‹ˆλ‹€.")
618
+
619
+ return sections[:5]
620
+
621
  except Exception as e:
622
  print(f"Error during scenario analysis: {e}")
623
  return ["Error occurred during analysis"] * 5
 
643
  print(f"Error in section {section_number}: {e}")
644
  raise gr.Error(f"μ„Ήμ…˜ {section_number} 생성 쀑 였λ₯˜: {str(e)}")
645
 
 
646
  # κ°œλ³„ μ„Ήμ…˜ ν”„λ‘¬ν”„νŠΈ 생성 ν•¨μˆ˜ μΆ”κ°€
647
  def generate_single_section_prompt(scenario, section_number):
648
  """κ°œλ³„ μ„Ήμ…˜μ— λŒ€ν•œ ν”„λ‘¬ν”„νŠΈ 생성"""
 
713
 
714
  def merge_section_videos(section1, section2, section3, section4, section5):
715
  """μ„Ήμ…˜ λΉ„λ””μ˜€λ“€μ„ ν•˜λ‚˜λ‘œ κ²°ν•©"""
716
+ videos = []
717
 
718
+ # 각 μ„Ήμ…˜ λΉ„λ””μ˜€ 확인 및 처리
719
+ for i, video in enumerate([section1, section2, section3, section4, section5], 1):
720
+ if video and os.path.exists(video):
721
+ videos.append(video)
722
+ else:
723
+ raise gr.Error(f"μ„Ήμ…˜ {i}의 μ˜μƒμ΄ μ—†κ±°λ‚˜ μ ‘κ·Όν•  수 μ—†μŠ΅λ‹ˆλ‹€.")
724
 
725
+ if not videos:
726
+ raise gr.Error("κ²°ν•©ν•  μ˜μƒμ΄ μ—†μŠ΅λ‹ˆλ‹€.")
727
+
728
+ try:
729
+ output_path = tempfile.mktemp(suffix=".mp4")
730
+
731
+ # 첫 번째 λΉ„λ””μ˜€μ˜ 속성 κ°€μ Έμ˜€κΈ°
732
+ cap = cv2.VideoCapture(videos[0])
733
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
734
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
735
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
736
+ cap.release()
737
+
738
+ # 좜λ ₯ λΉ„λ””μ˜€ μ„€μ •
739
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
740
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
741
+
742
+ # 각 λΉ„λ””μ˜€ 순차적으둜 κ²°ν•©
743
+ for video_path in videos:
744
+ cap = cv2.VideoCapture(video_path)
745
+ while True:
746
+ ret, frame = cap.read()
747
+ if not ret:
748
+ break
749
+ # ν•„μš”ν•œ 경우 ν”„λ ˆμž„ 크기 μ‘°μ •
750
+ if frame.shape[:2] != (height, width):
751
+ frame = cv2.resize(frame, (width, height))
752
+ out.write(frame)
753
+ cap.release()
754
+
755
+ out.release()
756
+ return output_path
757
+
758
+ except Exception as e:
759
+ raise gr.Error(f"λΉ„λ””μ˜€ κ²°ν•© 쀑 였λ₯˜ λ°œμƒ: {e}")
760
 
761
 
762
  # Gradio Interface Definition
 
1040
  ]
1041
  )
1042
 
1043
+
1044
+ # 각 μ„Ήμ…˜μ˜ ν”„λ‘¬ν”„νŠΈ μž¬μƒμ„± 이벀트 ν•Έλ“€λŸ¬ μΆ”κ°€
1045
+ section1_regenerate.click(
1046
+ fn=lambda x: generate_single_section_prompt(x, 1),
1047
+ inputs=[scenario_input],
1048
+ outputs=section1_prompt
1049
+ )
1050
+
1051
+ section2_regenerate.click(
1052
+ fn=lambda x: generate_single_section_prompt(x, 2),
1053
+ inputs=[scenario_input],
1054
+ outputs=section2_prompt
1055
+ )
1056
+
1057
+ section3_regenerate.click(
1058
+ fn=lambda x: generate_single_section_prompt(x, 3),
1059
+ inputs=[scenario_input],
1060
+ outputs=section3_prompt
1061
+ )
1062
+
1063
+ section4_regenerate.click(
1064
+ fn=lambda x: generate_single_section_prompt(x, 4),
1065
+ inputs=[scenario_input],
1066
+ outputs=section4_prompt
1067
+ )
1068
+
1069
+ section5_regenerate.click(
1070
+ fn=lambda x: generate_single_section_prompt(x, 5),
1071
+ inputs=[scenario_input],
1072
+ outputs=section5_prompt
1073
+ )
1074
+
1075
+
1076
  # μ„Ήμ…˜ 생성 이벀트 ν•Έλ“€λŸ¬
1077
  section1_generate.click(
1078
  fn=generate_section_video,
 
1111
 
1112
 
1113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1114
 
1115
 
1116
  # 이벀트 ν•Έλ“€λŸ¬ μΆ”κ°€