TDN-M commited on
Commit
ed8d8b7
·
verified ·
1 Parent(s): cbb683a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -111
app.py CHANGED
@@ -7,12 +7,6 @@ import fitz # PyMuPDF
7
  import random
8
  import gradio as gr
9
  from docx import Document
10
- from audio_processing import async_text_to_speech, text_to_speech
11
- from content_generation import create_content, CONTENT_TYPES
12
- from video_processing import create_video_func
13
- from moviepy.editor import AudioFileClip, VideoFileClip, CompositeAudioClip
14
- from utils import (combine_videos, get_pexels_video, get_bgm_file, download_video)
15
- from video_processing import create_video
16
  from content_generation import create_content, CONTENT_TYPES
17
  from openai import OpenAI
18
 
@@ -47,33 +41,6 @@ def process_docx(file_path):
47
  text += para.text
48
  return text
49
 
50
- def get_bgm_file_list():
51
- """
52
- Trả về danh sách các tệp nhạc nền.
53
- """
54
- # Giả sử bạn có một thư mục chứa các tệp nhạc nền
55
- song_dir = "/data/bg-music"
56
- return [os.path.basename(file) for file in glob.glob(os.path.join(song_dir, "*.mp3"))]
57
-
58
- def extract_key_contents(script, num_contents=10):
59
- """
60
- Trích xuất các ý chính từ script.
61
- """
62
- try:
63
- response = client.chat.completions.create(
64
- model="gpt-3.5-turbo",
65
- messages=[
66
- {"role": "system", "content": f"Bạn là một chuyên gia phân tích nội dung. Hãy trích xuất chính xác {num_contents} ý chính quan trọng nhất từ đoạn văn sau, mỗi ý không quá 20 từ."},
67
- {"role": "user", "content": script}
68
- ]
69
- )
70
- # Sửa lỗi truy cập đối tượng không thể subscript
71
- key_contents = response.choices[0].message.content.split('\n')
72
- return key_contents[:num_contents]
73
- except Exception as e:
74
- print(f"Lỗi khi trích xuất nội dung: {str(e)}")
75
- return []
76
-
77
  def interface():
78
  with gr.Blocks() as app:
79
  gr.Markdown("# Ứng dụng Tạo Nội dung và Video")
@@ -131,84 +98,6 @@ def interface():
131
  inputs=[prompt, file_upload, content_type],
132
  outputs=[content_output, download_docx, status_message])
133
 
134
- # Định nghĩa danh sách giọng đọc
135
- VOICES = ["vi-VN-HoaiMyNeural - vi-VN (Female)", "vi-VN-NamMinhNeural - vi-VN (Male)"]
136
-
137
- with gr.Tab("Tạo Âm thanh"):
138
- text_input = gr.Textbox(label="Nhập văn bản để chuyển đổi")
139
- voice_select = gr.Dropdown(label="Chọn giọng đọc", choices=VOICES) # Dropdown cho voice_select
140
- audio_button = gr.Button("Tạo Âm thanh")
141
- audio_output = gr.Audio(label="Âm thanh tạo ra")
142
- download_audio = gr.File(label="Tải xuống file âm thanh", interactive=False)
143
-
144
- async def text_to_speech_func(text, voice):
145
- try:
146
- audio_path = await async_text_to_speech(text, voice)
147
- return audio_path, audio_path
148
- except Exception as e:
149
- print(f"Lỗi khi chuyển đổi văn bản thành giọng nói: {e}")
150
- return None, None
151
-
152
- audio_button.click(text_to_speech_func,
153
- inputs=[text_input, voice_select],
154
- outputs=[audio_output])
155
-
156
- with gr.Tab("Tạo Video"):
157
- script_input = gr.Textbox(label="Nhập kịch bản")
158
- audio_file = gr.File(label="Chọn file âm thanh", type="filepath")
159
- keywords_output = gr.Textbox(label="Từ khóa", interactive=True)
160
- max_clip_duration = gr.Slider(minimum=2, maximum=5, step=1, label="Thời lượng tối đa mỗi video (giây)")
161
- join_order = gr.Checkbox(label="Ghép ngẫu nhiên", value=True)
162
- bgm_files = gr.Dropdown(choices=get_bgm_file_list(), label="Chọn nhạc nền")
163
- video_output = gr.Video(label="Video tạo ra")
164
- video_button = gr.Button("Tạo Video")
165
- status_message = gr.Label(label="Trạng thái") # Thêm thông báo trạng thái
166
-
167
- def create_video_func(script, audio_file, max_clip_duration, join_order, bgm_file):
168
- """ Tạo video từ các thông tin đầu vào. """
169
- try:
170
- # Cập nhật trạng thái ban đầu
171
- status = "Đang xử lý..."
172
-
173
- # 1. Tính toán thời lượng video
174
- audio_clip = AudioFileClip(audio_file)
175
- video_duration = audio_clip.duration
176
-
177
- # 2. Trích xuất từ khóa từ kịch bản
178
- keywords = extract_key_contents(script)
179
- video_paths = []
180
- for keyword in keywords:
181
- video_url = get_pexels_video(keyword.strip())
182
- if video_url:
183
- video_path = download_video(video_url)
184
- video_paths.append(video_path)
185
-
186
- # 3. Ghép video
187
- temp_dir = tempfile.mkdtemp()
188
- if join_order:
189
- random.shuffle(video_paths)
190
- combined_video_path = os.path.join(temp_dir, "combined_video.mp4")
191
- combine_videos(combined_video_path, video_paths, audio_file, max_clip_duration)
192
-
193
- # 4. Gộp audio và nhạc nền
194
- final_video_path = "final_video.mp4"
195
- bgm_clip = AudioFileClip(bgm_file)
196
- final_audio = CompositeAudioClip([audio_clip, bgm_clip])
197
- final_video = VideoFileClip(combined_video_path).set_audio(final_audio)
198
- final_video.write_videofile(final_video_path)
199
-
200
- # Cập nhật trạng thái thành công
201
- status = "Video đã được tạo thành công!"
202
- return final_video_path, status
203
- except Exception as e:
204
- # Cập nhật trạng thái lỗi
205
- status = f"Lỗi khi tạo video: {e}"
206
- return None, status
207
-
208
- video_button.click(create_video_func,
209
- inputs=[script_input, audio_file, max_clip_duration, join_order, bgm_files],
210
- outputs=[video_output, status_message])
211
-
212
  return app
213
 
214
  # Khởi chạy ứng dụng
 
7
  import random
8
  import gradio as gr
9
  from docx import Document
 
 
 
 
 
 
10
  from content_generation import create_content, CONTENT_TYPES
11
  from openai import OpenAI
12
 
 
41
  text += para.text
42
  return text
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def interface():
45
  with gr.Blocks() as app:
46
  gr.Markdown("# Ứng dụng Tạo Nội dung và Video")
 
98
  inputs=[prompt, file_upload, content_type],
99
  outputs=[content_output, download_docx, status_message])
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  return app
102
 
103
  # Khởi chạy ứng dụng