Update app.py
Browse files
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,52 +41,25 @@ 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")
|
80 |
|
81 |
with gr.Tab("Tạo Nội dung"):
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
|
97 |
def generate_content(prompt, file, content_type):
|
98 |
try:
|
@@ -127,98 +94,15 @@ def interface():
|
|
127 |
docx_path = "script.docx"
|
128 |
create_docx(content, docx_path)
|
129 |
|
130 |
-
audio_path = await async_text_to_speech(content, "alloy", "Tiếng Việt")
|
131 |
-
return docx_path, audio_path, "Nội dung đã được xác nhận và âm thanh đã được tạo!"
|
132 |
-
|
133 |
content_button.click(generate_content,
|
134 |
inputs=[prompt, file_upload, content_type],
|
135 |
outputs=[content_output, download_docx, status_message])
|
136 |
|
137 |
-
|
138 |
-
inputs=[content_output],
|
139 |
-
outputs=[download_docx, download_audio, status_message])
|
140 |
-
|
141 |
-
# Định nghĩa danh sách giọng đọc
|
142 |
-
VOICES = ["vi-VN-HoaiMyNeural"]
|
143 |
-
|
144 |
-
with gr.Tab("Tạo Âm thanh"):
|
145 |
-
text_input = gr.Textbox(label="Nhập văn bản để chuyển đổi")
|
146 |
-
voice_select = gr.Dropdown(label="Chọn giọng đọc", choices=VOICES) # Dropdown cho voice_select
|
147 |
-
audio_button = gr.Button("Tạo Âm thanh")
|
148 |
-
audio_output = gr.Audio(label="Âm thanh tạo ra")
|
149 |
-
download_audio = gr.File(label="Tải xuống file âm thanh", interactive=False)
|
150 |
-
|
151 |
-
def text_to_speech_func(text, voice):
|
152 |
-
try:
|
153 |
-
audio_path = await async_text_to_speech(text, voice, "Tiếng Việt")
|
154 |
-
return audio_path, audio_path
|
155 |
-
except Exception as e:
|
156 |
-
print(f"Lỗi khi chuyển đổi văn bản thành giọng nói: {e}")
|
157 |
-
return None, None
|
158 |
-
|
159 |
-
audio_button.click(text_to_speech_func,
|
160 |
-
inputs=[text_input, voice_select],
|
161 |
-
outputs=[audio_output, download_audio])
|
162 |
-
|
163 |
-
with gr.Tab("Tạo Video"):
|
164 |
-
script_input = gr.Textbox(label="Nhập kịch bản")
|
165 |
-
audio_file = gr.File(label="Chọn file âm thanh", type="filepath")
|
166 |
-
keywords_output = gr.Textbox(label="Từ khóa", interactive=True)
|
167 |
-
max_clip_duration = gr.Slider(minimum=2, maximum=5, step=1, label="Thời lượng tối đa mỗi video (giây)")
|
168 |
-
join_order = gr.Checkbox(label="Ghép ngẫu nhiên", value=True)
|
169 |
-
bgm_files = gr.Dropdown(choices=get_bgm_file_list(), label="Chọn nhạc nền")
|
170 |
-
video_output = gr.Video(label="Video tạo ra")
|
171 |
-
video_button = gr.Button("Tạo Video")
|
172 |
-
status_message = gr.Label(label="Trạng thái") # Thêm thông báo trạng thái
|
173 |
-
|
174 |
-
def create_video_func(script, audio_file, max_clip_duration, join_order, bgm_file):
|
175 |
-
""" Tạo video từ các thông tin đầu vào. """
|
176 |
-
try:
|
177 |
-
# Cập nhật trạng thái ban đầu
|
178 |
-
status = "Đang xử lý..."
|
179 |
-
|
180 |
-
# 1. Tính toán thời lượng video
|
181 |
-
audio_clip = AudioFileClip(audio_file)
|
182 |
-
video_duration = audio_clip.duration
|
183 |
-
|
184 |
-
# 2. Trích xuất từ khóa từ kịch bản
|
185 |
-
keywords = extract_key_contents(script)
|
186 |
-
video_paths = []
|
187 |
-
for keyword in keywords:
|
188 |
-
video_url = get_pexels_video(keyword.strip())
|
189 |
-
if video_url:
|
190 |
-
video_path = download_video(video_url)
|
191 |
-
video_paths.append(video_path)
|
192 |
-
|
193 |
-
# 3. Ghép video
|
194 |
-
temp_dir = tempfile.mkdtemp()
|
195 |
-
if join_order:
|
196 |
-
random.shuffle(video_paths)
|
197 |
-
combined_video_path = os.path.join(temp_dir, "combined_video.mp4")
|
198 |
-
combine_videos(combined_video_path, video_paths, audio_file, max_clip_duration)
|
199 |
-
|
200 |
-
# 4. Gộp audio và nhạc nền
|
201 |
-
final_video_path = "final_video.mp4"
|
202 |
-
bgm_clip = AudioFileClip(bgm_file)
|
203 |
-
final_audio = CompositeAudioClip([audio_clip, bgm_clip])
|
204 |
-
final_video = VideoFileClip(combined_video_path).set_audio(final_audio)
|
205 |
-
final_video.write_videofile(final_video_path)
|
206 |
-
|
207 |
-
# Cập nhật trạng thái thành công
|
208 |
-
status = "Video đã được tạo thành công!"
|
209 |
-
return final_video_path, status
|
210 |
-
except Exception as e:
|
211 |
-
# Cập nhật trạng thái lỗi
|
212 |
-
status = f"Lỗi khi tạo video: {e}"
|
213 |
-
return None, status
|
214 |
|
215 |
-
video_button.click(create_video_func,
|
216 |
-
inputs=[script_input, audio_file, max_clip_duration, join_order, bgm_files],
|
217 |
-
outputs=[video_output, status_message])
|
218 |
|
219 |
-
return app
|
220 |
|
221 |
# Khởi chạy ứng dụng
|
222 |
if __name__ == "__main__":
|
223 |
app = interface()
|
224 |
-
app.launch(share=True)
|
|
|
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")
|
47 |
|
48 |
with gr.Tab("Tạo Nội dung"):
|
49 |
+
with gr.Row():
|
50 |
+
with gr.Column():
|
51 |
+
prompt = gr.Textbox(label="Nhập yêu cầu nội dung")
|
52 |
+
file_upload = gr.File(label="Tải lên file kèm theo", type="filepath")
|
53 |
+
content_type = gr.Radio(label="Chọn loại nội dung",
|
54 |
+
choices=CONTENT_TYPES,
|
55 |
+
value=None) # Giá trị mặc định là không có gì được chọn
|
56 |
+
content_button = gr.Button("Tạo Nội dung")
|
57 |
+
|
58 |
+
with gr.Column():
|
59 |
+
content_output = gr.Textbox(label="Nội dung tạo ra", interactive=True)
|
60 |
+
confirm_button = gr.Button("Xác nhận nội dung")
|
61 |
+
download_docx = gr.File(label="Tải xuống file DOCX", interactive=False)
|
62 |
+
status_message = gr.Label(label="Trạng thái")
|
63 |
|
64 |
def generate_content(prompt, file, content_type):
|
65 |
try:
|
|
|
94 |
docx_path = "script.docx"
|
95 |
create_docx(content, docx_path)
|
96 |
|
|
|
|
|
|
|
97 |
content_button.click(generate_content,
|
98 |
inputs=[prompt, file_upload, content_type],
|
99 |
outputs=[content_output, download_docx, status_message])
|
100 |
|
101 |
+
return app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
|
|
|
|
|
|
103 |
|
|
|
104 |
|
105 |
# Khởi chạy ứng dụng
|
106 |
if __name__ == "__main__":
|
107 |
app = interface()
|
108 |
+
app.launch(share=True)
|