openfree commited on
Commit
7c4a3b2
·
verified ·
1 Parent(s): 3879190

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -52
app.py CHANGED
@@ -41,7 +41,9 @@ from moviepy.editor import (
41
  TextClip,
42
  concatenate_videoclips
43
  )
44
-
 
 
45
  # .env 파일에서 환경 변수 로드
46
  load_dotenv()
47
 
@@ -901,7 +903,7 @@ def analyze_top_spaces(progress=gr.Progress()) -> Tuple[str, str]:
901
 
902
  response = hf_client.chat_completion(
903
  messages,
904
- max_tokens=200,
905
  temperature=0.7
906
  )
907
 
@@ -1029,66 +1031,95 @@ def create_editable_space_analysis(progress=gr.Progress()) -> List[str]:
1029
  def generate_video(texts: List[str], progress=gr.Progress()) -> str:
1030
  """영상 생성"""
1031
  try:
 
1032
  temp_dir = tempfile.mkdtemp()
1033
  clips = []
1034
 
1035
- # 인트로 생성 (이미지 대신 텍스트로)
1036
- intro_clip = TextClip(
1037
- txt=texts[0],
1038
- fontsize=30,
1039
- color='white',
1040
- bg_color='black',
1041
- size=(800, 600)
1042
- ).set_duration(5)
1043
-
1044
- # 인트로 음성
1045
- intro_audio = gTTS(text=texts[0], lang='ko', slow=False)
1046
- intro_audio_path = os.path.join(temp_dir, "intro.mp3")
1047
- intro_audio.save(intro_audio_path)
1048
- intro_audio_clip = AudioFileClip(intro_audio_path)
1049
- intro_clip = intro_clip.set_audio(intro_audio_clip)
1050
- clips.append(intro_clip)
1051
-
1052
- # 각 스페이스별 클립 생성
1053
- for idx, text in enumerate(texts[1:], 1):
1054
- progress((idx / 24), desc=f"영상 생성 중... {idx}/24")
1055
-
1056
- # 텍스트 클립 생성
1057
- text_clip = TextClip(
1058
- txt=text,
1059
- fontsize=25,
1060
  color='white',
1061
  bg_color='black',
1062
- size=(800, 600)
1063
- ).set_duration(5)
 
1064
 
1065
- # 음성 생성
1066
- tts = gTTS(text=text, lang='ko', slow=False)
1067
- audio_path = os.path.join(temp_dir, f"audio_{idx}.mp3")
1068
- tts.save(audio_path)
1069
- audio_clip = AudioFileClip(audio_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1070
 
1071
- # 결합
1072
- video_clip = text_clip.set_audio(audio_clip)
1073
- clips.append(video_clip)
1074
-
1075
- # 모든 클립 연결
1076
- final_clip = concatenate_videoclips(clips)
1077
-
1078
- # MP4로 저장
1079
- output_path = "output_video.mp4"
1080
- final_clip.write_videofile(output_path, fps=24, codec='libx264')
1081
-
1082
- # 임시 파일 정리
1083
- shutil.rmtree(temp_dir)
1084
-
1085
- return output_path
1086
-
1087
  except Exception as e:
1088
  print(f"Error generating video: {e}")
1089
- if 'temp_dir' in locals():
1090
- shutil.rmtree(temp_dir)
1091
  return ""
 
 
 
 
 
 
 
 
1092
 
1093
  def create_interface():
1094
  with gr.Blocks(title="HuggingFace Trending Board", css="""
 
41
  TextClip,
42
  concatenate_videoclips
43
  )
44
+ # MoviePy 설정
45
+ from moviepy.config import change_settings
46
+ change_settings({"IMAGEMAGICK_BINARY": "convert"}) # ImageMagick 바이너리 설정
47
  # .env 파일에서 환경 변수 로드
48
  load_dotenv()
49
 
 
903
 
904
  response = hf_client.chat_completion(
905
  messages,
906
+ max_tokens=1000,
907
  temperature=0.7
908
  )
909
 
 
1031
  def generate_video(texts: List[str], progress=gr.Progress()) -> str:
1032
  """영상 생성"""
1033
  try:
1034
+ # 임시 디렉토리 생성
1035
  temp_dir = tempfile.mkdtemp()
1036
  clips = []
1037
 
1038
+ # 작업 디렉토리를 임시 디렉토리로 변경
1039
+ current_dir = os.getcwd()
1040
+ os.chdir(temp_dir)
1041
+
1042
+ try:
1043
+ # 인트로 생성
1044
+ intro_text = texts[0]
1045
+ intro_clip = TextClip(
1046
+ txt=intro_text,
1047
+ fontsize=30,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1048
  color='white',
1049
  bg_color='black',
1050
+ size=(800, 600),
1051
+ method='label' # 'caption' 대신 'label' 사용
1052
+ ).set_duration(10) # 인트로는 좀 더 길게
1053
 
1054
+ # 인트로 음성
1055
+ intro_audio = gTTS(text=intro_text, lang='ko', slow=False)
1056
+ intro_audio_path = os.path.join(temp_dir, "intro.mp3")
1057
+ intro_audio.save(intro_audio_path)
1058
+ intro_audio_clip = AudioFileClip(intro_audio_path)
1059
+
1060
+ # 오디오 길이에 맞춰 비디오 길이 조정
1061
+ intro_clip = intro_clip.set_duration(intro_audio_clip.duration)
1062
+ intro_clip = intro_clip.set_audio(intro_audio_clip)
1063
+ clips.append(intro_clip)
1064
+
1065
+ # 각 스페이스별 클립 생성
1066
+ for idx, text in enumerate(texts[1:], 1):
1067
+ progress((idx / 24), desc=f"영상 생성 중... {idx}/24")
1068
+
1069
+ # 텍스트 클립 생성
1070
+ text_clip = TextClip(
1071
+ txt=text,
1072
+ fontsize=25,
1073
+ color='white',
1074
+ bg_color='black',
1075
+ size=(800, 600),
1076
+ method='label' # 'caption' 대신 'label' 사용
1077
+ ).set_duration(5)
1078
+
1079
+ # 음성 생성
1080
+ tts = gTTS(text=text, lang='ko', slow=False)
1081
+ audio_path = os.path.join(temp_dir, f"audio_{idx}.mp3")
1082
+ tts.save(audio_path)
1083
+ audio_clip = AudioFileClip(audio_path)
1084
+
1085
+ # 오디오 길이에 맞춰 비디오 길이 조정
1086
+ text_clip = text_clip.set_duration(audio_clip.duration)
1087
+ video_clip = text_clip.set_audio(audio_clip)
1088
+ clips.append(video_clip)
1089
+
1090
+ # 모든 클립 연결
1091
+ final_clip = concatenate_videoclips(clips)
1092
+
1093
+ # MP4로 저장
1094
+ output_path = os.path.join(temp_dir, "output_video.mp4")
1095
+ final_clip.write_videofile(
1096
+ output_path,
1097
+ fps=24,
1098
+ codec='libx264',
1099
+ audio_codec='aac'
1100
+ )
1101
+
1102
+ # 결과 파일을 현재 디렉토리로 복사
1103
+ final_output = os.path.join(current_dir, "output_video.mp4")
1104
+ shutil.copy2(output_path, final_output)
1105
+
1106
+ return final_output
1107
+
1108
+ finally:
1109
+ # 작업 디렉토리 복원
1110
+ os.chdir(current_dir)
1111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1112
  except Exception as e:
1113
  print(f"Error generating video: {e}")
 
 
1114
  return ""
1115
+
1116
+ finally:
1117
+ # 임시 파일 정리
1118
+ if 'temp_dir' in locals():
1119
+ try:
1120
+ shutil.rmtree(temp_dir)
1121
+ except Exception as e:
1122
+ print(f"Error cleaning up temp files: {e}")
1123
 
1124
  def create_interface():
1125
  with gr.Blocks(title="HuggingFace Trending Board", css="""