Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,6 @@ from huggingface_hub import HfApi, hf_hub_download, snapshot_download
|
|
14 |
from TTS.tts.configs.xtts_config import XttsConfig
|
15 |
from TTS.tts.models.xtts import Xtts
|
16 |
from vinorm import TTSnorm
|
17 |
-
from content_generation import create_content # Nhập hàm create_content từ file content_generation.py
|
18 |
from PIL import Image
|
19 |
from pathlib import Path
|
20 |
import requests
|
@@ -196,14 +195,19 @@ def create_video(image_path, audio_path, output_path):
|
|
196 |
subprocess.run(command, check=True)
|
197 |
|
198 |
# Hàm xử lý sự kiện khi nhấn nút "Tạo Video"
|
199 |
-
def generate_video(
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
|
|
|
|
204 |
image_description = generate_image_description(prompt)
|
205 |
-
|
206 |
-
# Bước
|
207 |
try:
|
208 |
image = txt2img(image_description, width=800, height=600)
|
209 |
if isinstance(image, str): # Nếu có lỗi từ API
|
@@ -214,14 +218,14 @@ def generate_video(audio_file, prompt):
|
|
214 |
image.save(image_path)
|
215 |
except Exception as e:
|
216 |
return None, f"Error generating image: {str(e)}"
|
217 |
-
|
218 |
-
# Bước
|
219 |
video_output_path = os.path.join(SAVE_DIR, "output_video.mp4")
|
220 |
try:
|
221 |
create_video(image_path, audio_file, video_output_path)
|
222 |
except Exception as e:
|
223 |
return None, f"Error creating video: {str(e)}"
|
224 |
-
|
225 |
return video_output_path, "Video created successfully!"
|
226 |
|
227 |
# Thư mục lưu trữ ảnh và video
|
@@ -406,11 +410,11 @@ with gr.Blocks(analytics_enabled=False) as demo:
|
|
406 |
visible=True,
|
407 |
variant="primary",
|
408 |
)
|
|
|
409 |
|
410 |
with gr.Column():
|
411 |
audio_gr = gr.Audio(label="Synthesised Audio", autoplay=True)
|
412 |
out_text_gr = gr.Text(label="Metrics")
|
413 |
-
video_button = gr.Button("Tạo Video 🎥", visible=False)
|
414 |
video_output = gr.Video(label="Generated Video", visible=False)
|
415 |
video_status = gr.Text(label="Video Status")
|
416 |
|
@@ -426,14 +430,18 @@ with gr.Blocks(analytics_enabled=False) as demo:
|
|
426 |
],
|
427 |
outputs=[audio_gr, out_text_gr],
|
428 |
api_name="predict",
|
429 |
-
).then(
|
430 |
-
lambda: [gr.update(visible=True)],
|
431 |
-
outputs=[video_button]
|
432 |
)
|
433 |
|
434 |
video_button.click(
|
435 |
generate_video,
|
436 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
437 |
outputs=[video_output, video_status],
|
438 |
)
|
439 |
|
|
|
14 |
from TTS.tts.configs.xtts_config import XttsConfig
|
15 |
from TTS.tts.models.xtts import Xtts
|
16 |
from vinorm import TTSnorm
|
|
|
17 |
from PIL import Image
|
18 |
from pathlib import Path
|
19 |
import requests
|
|
|
195 |
subprocess.run(command, check=True)
|
196 |
|
197 |
# Hàm xử lý sự kiện khi nhấn nút "Tạo Video"
|
198 |
+
def generate_video(prompt, language, audio_file_pth, normalize_text, use_llm, content_type):
|
199 |
+
# Bước 1: Tạo audio nếu chưa có
|
200 |
+
if not os.path.exists("output.wav"):
|
201 |
+
audio_file, metrics_text = predict(prompt, language, audio_file_pth, normalize_text, use_llm, content_type)
|
202 |
+
if not audio_file:
|
203 |
+
return None, metrics_text
|
204 |
+
else:
|
205 |
+
audio_file = "output.wav"
|
206 |
+
|
207 |
+
# Bước 2: Tạo mô tả ảnh
|
208 |
image_description = generate_image_description(prompt)
|
209 |
+
|
210 |
+
# Bước 3: Gọi API tạo ảnh
|
211 |
try:
|
212 |
image = txt2img(image_description, width=800, height=600)
|
213 |
if isinstance(image, str): # Nếu có lỗi từ API
|
|
|
218 |
image.save(image_path)
|
219 |
except Exception as e:
|
220 |
return None, f"Error generating image: {str(e)}"
|
221 |
+
|
222 |
+
# Bước 4: Tạo video từ ảnh và audio
|
223 |
video_output_path = os.path.join(SAVE_DIR, "output_video.mp4")
|
224 |
try:
|
225 |
create_video(image_path, audio_file, video_output_path)
|
226 |
except Exception as e:
|
227 |
return None, f"Error creating video: {str(e)}"
|
228 |
+
|
229 |
return video_output_path, "Video created successfully!"
|
230 |
|
231 |
# Thư mục lưu trữ ảnh và video
|
|
|
410 |
visible=True,
|
411 |
variant="primary",
|
412 |
)
|
413 |
+
video_button = gr.Button("Tạo Video 🎥", visible=True) # Nút tạo video luôn hiển thị
|
414 |
|
415 |
with gr.Column():
|
416 |
audio_gr = gr.Audio(label="Synthesised Audio", autoplay=True)
|
417 |
out_text_gr = gr.Text(label="Metrics")
|
|
|
418 |
video_output = gr.Video(label="Generated Video", visible=False)
|
419 |
video_status = gr.Text(label="Video Status")
|
420 |
|
|
|
430 |
],
|
431 |
outputs=[audio_gr, out_text_gr],
|
432 |
api_name="predict",
|
|
|
|
|
|
|
433 |
)
|
434 |
|
435 |
video_button.click(
|
436 |
generate_video,
|
437 |
+
inputs=[
|
438 |
+
input_text_gr,
|
439 |
+
language_gr,
|
440 |
+
ref_gr,
|
441 |
+
normalize_text,
|
442 |
+
use_llm_checkbox,
|
443 |
+
content_type_dropdown,
|
444 |
+
],
|
445 |
outputs=[video_output, video_status],
|
446 |
)
|
447 |
|