File size: 12,124 Bytes
55bd1de 087ad85 85260dc e816a63 55bd1de 7347b55 55bd1de 087ad85 cb8b8d6 087ad85 55bd1de 96b597d 55bd1de 96b597d 55bd1de 96b597d 55bd1de 96b597d 55bd1de 96b597d 55bd1de e7d588c 96b597d e7d588c 96b597d e7d588c 96b597d e7d588c 96b597d e7d588c 968ba2a 55bd1de 96b597d 087ad85 968ba2a 087ad85 abda649 96b597d abda649 4943180 087ad85 e7d588c c7ec472 96b597d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
import asyncio
import mimetypes
import os
import tempfile
import glob
import random
import gradio as gr
from docx import Document
from audio_processing import async_text_to_speech, text_to_speech
from content_generation import create_content, CONTENT_TYPES
from video_processing import create_video_func
from moviepy.editor import AudioFileClip, CompositeAudioClip
from utils import (combine_videos, get_pexels_image, get_bgm_file)
from video_processing import create_video
# Danh sách giọng đọc
VOICES = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
# Danh sách ngôn ngữ (chưa được sử dụng trong mã)
LANGUAGES = ["Tiếng Anh", "Tiếng Việt", "Tiếng Hindi"]
# Danh sách loại nội dung và hướng dẫn mặc định cho từng loại
CONTENT_TYPES = ["podcast", "giới thiệu", "triết lý sống", "Phổ biến kiến thức thống kê"]
CONTENT_TYPE_INSTRUCTIONS = {
"podcast": """
Tone giọng: Gần gũi, thân thiện nhưng chuyên sâu, thể hiện sự am hiểu về chủ đề.
Cấu trúc:
- Bắt đầu bằng một câu hỏi kích thích tư duy hoặc một câu chuyện mở màn gây tò mò.
- Triển khai các luận điểm theo từng bước. Sử dụng câu từ mạnh mẽ, ví dụ điển hình hoặc những câu nói nổi tiếng.
- Xây dựng các phần chuyển tiếp mượt mà giữa các ý.
- Kết thúc podcast với một thông điệp sâu sắc, để lại sự suy ngẫm cho thính giả.
Mục tiêu: Mang lại kiến thức giá trị, lôi cuốn thính giả tham gia suy nghĩ và cảm nhận sâu sắc về chủ đề.
""",
"giới thiệu": """
Tone giọng: Chuyên nghiệp, gãy gọn nhưng vẫn có sự truyền cảm.
Cấu trúc:
- Bắt đầu với một câu khẳng định mạnh mẽ về đối tượng được giới thiệu.
- Giải thích mục tiêu của phần giới thiệu, nhấn mạnh tầm quan trọng hoặc sự khác biệt.
- Kết thúc với một lời kêu gọi hành động, khích lệ người nghe tiếp tục lắng nghe hoặc tham gia.
Mục tiêu: Đưa ra thông tin cô đọng, hấp dẫn, khiến người nghe cảm thấy bị thu hút và muốn tìm hiểu thêm.
""",
"triết lý sống": """
Tone giọng: Sâu sắc, truyền cảm hứng, mang tính chiêm nghiệm.
Cấu trúc:
- Bắt đầu bằng một câu hỏi sâu sắc hoặc ẩn dụ về cuộc sống.
- Triển khai các luận điểm chặt chẽ, xen lẫn cảm xúc và những ví dụ đời thực hoặc những câu nói triết lý.
- Kết thúc với một thông điệp sâu sắc, khơi dậy suy ngẫm cho người nghe.
Mục tiêu: Khơi gợi suy nghĩ sâu sắc về cuộc sống, khiến người nghe tìm thấy ý nghĩa hoặc giá trị trong câu chuyện.
""",
"Phổ biến kiến thức Thống kê": """
Tone giọng: Thân thiện, dễ hiểu, và mang tính giáo dục.
Cấu trúc:
- Bắt đầu với một câu hỏi hoặc một tình huống thực tế để thu hút sự chú ý.
- Giải thích các khái niệm thống kê cơ bản một cách đơn giản và dễ hiểu, sử dụng ví dụ thực tế để minh họa.
- Đưa ra các ứng dụng thực tế của thống kê trong đời sống hàng ngày hoặc trong các lĩnh vực cụ thể.
- Kết thúc với một thông điệp khuyến khích người nghe áp dụng kiến thức thống kê vào cuộc sống.
Mục tiêu: Giúp người nghe hiểu và yêu thích thống kê, thấy được giá trị và ứng dụng của nó trong cuộc sống.
"""
}
def create_docx(content, output_path):
"""
Tạo file docx từ nội dung.
"""
doc = Document()
doc.add_paragraph(content)
doc.save(output_path)
def process_pdf(file_path):
"""
Xử lý file PDF và trích xuất nội dung.
"""
doc = fitz.open(file_path)
text = ""
for page in doc:
text += page.get_text()
return text
def process_docx(file_path):
"""
Xử lý file DOCX và trích xuất nội dung.
"""
doc = Document(file_path)
text = ""
for para in doc.paragraphs:
text += para.text
return text
def get_bgm_file_list():
"""
Trả về danh sách các tệp nhạc nền.
"""
# Giả sử bạn có một thư mục chứa các tệp nhạc nền
song_dir = "/data/bg-music"
return [os.path.basename(file) for file in glob.glob(os.path.join(song_dir, "*.mp3"))]
# Giao diện Gradio
def interface():
with gr.Blocks() as app:
gr.Markdown("# Ứng dụng Tạo Nội dung và Video")
with gr.Tab("Tạo Nội dung"):
prompt = gr.Textbox(label="Nhập yêu cầu nội dung")
file_upload = gr.File(label="Tải lên file kèm theo", type="filepath")
# Sử dụng gr.Radio thay vì gr.CheckboxGroup
content_type = gr.Radio(label="Chọn loại nội dung",
choices=CONTENT_TYPES,
value=None) # Giá trị mặc định là không có gì được chọn
content_button = gr.Button("Tạo Nội dung")
content_output = gr.Textbox(label="Nội dung tạo ra", interactive=True)
confirm_button = gr.Button("Xác nhận nội dung")
download_docx = gr.File(label="Tải xuống file DOCX", interactive=False)
download_audio = gr.File(label="Tải xuống file âm thanh", interactive=False)
status_message = gr.Label(label="Trạng thái")
def generate_content(prompt, file, content_type):
try:
status = "Đang xử lý..."
if file and os.path.exists(file):
mime_type, _ = mimetypes.guess_type(file)
if mime_type == "application/pdf":
file_content = process_pdf(file)
prompt = f"{prompt}\n\nDưới đây là nội dung của file tài liệu:\n\n{file_content}"
elif mime_type in (
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/msword"):
file_content = process_docx(file)
prompt = f"{prompt}\n\nDưới đây là nội dung của file tài liệu:\n\n{file_content}"
else:
raise ValueError("Định dạng file không được hỗ trợ.")
if not content_type:
raise ValueError("Vui lòng chọn một loại nội dung")
script_content = create_content(prompt, content_type, "Tiếng Việt")
docx_path = "script.docx"
create_docx(script_content, docx_path)
status = "Đã tạo nội dung thành công!"
return script_content, docx_path, status
except Exception as e:
status = f"Đã xảy ra lỗi: {str(e)}"
return "", None, status
async def confirm_content(content):
docx_path = "script.docx"
create_docx(content, docx_path)
audio_path = await async_text_to_speech(content, "alloy", "Tiếng Việt")
return docx_path, audio_path, "Nội dung đã được xác nhận và âm thanh đã được tạo!"
content_button.click(generate_content,
inputs=[prompt, file_upload, content_type],
outputs=[content_output, download_docx, status_message])
confirm_button.click(lambda x: asyncio.run(confirm_content(x)),
inputs=[content_output],
outputs=[download_docx, download_audio, status_message])
with gr.Tab("Tạo Âm thanh"):
text_input = gr.Textbox(label="Nhập văn bản để chuyển đổi")
voice_select = gr.Dropdown(label="Chọn giọng đọc",
choices=VOICES) # Dropdown cho voice_select
audio_button = gr.Button("Tạo Âm thanh")
audio_output = gr.Audio(label="Âm thanh tạo ra")
download_audio = gr.File(label="Tải xuống file âm thanh", interactive=False)
def text_to_speech_func(text, voice):
audio_path = text_to_speech(text, voice, "Tiếng Việt")
return audio_path, audio_path
audio_button.click(text_to_speech_func,
inputs=[text_input, voice_select],
outputs=[audio_output, download_audio])
with gr.Tab("Tạo Video"):
script_input = gr.Textbox(label="Nhập kịch bản")
audio_file = gr.File(label="Chọn file âm thanh", type="filepath")
keywords_output = gr.Textbox(label="Từ khóa", interactive=True)
max_clip_duration = gr.Slider(minimum=2, maximum=5, step=1, label="Thời lượng tối đa mỗi video (giây)")
join_order = gr.Checkbox(label="Ghép ngẫu nhiên", value=True) # Mặc định là ghép ngẫu nhiên
bgm_files = gr.Dropdown(choices=get_bgm_file_list(), label="Chọn nhạc nền")
video_output = gr.Video(label="Video tạo ra")
# Thêm nút để tạo video
video_button = gr.Button("Tạo Video")
def create_video_func(script, audio_file, keywords, max_clip_duration, join_order, bgm_file):
""" Tạo video từ các thông tin đầu vào. """
try:
# 1. Tính toán thời lượng video
audio_clip = AudioFileClip(audio_file)
video_duration = audio_clip.duration
# 2. Tìm kiếm và tải video từ Pexels
video_paths = []
for keyword in keywords:
for _ in range(int(video_duration // max_clip_duration)): # Chia thời lượng video cho thời lượng tối đa mỗi clip
image_url = get_pexels_image(keyword)
if image_url:
video_path = download_video_from_pexels(image_url) # Cần định nghĩa hàm download_video_from_pexels
video_paths.append(video_path)
# 3. Ghép video
temp_dir = tempfile.mkdtemp()
if join_order: # Ghép ngẫu nhiên
random.shuffle(video_paths)
combined_video_path = os.path.join(temp_dir, "combined_video.mp4")
combine_videos(combined_video_path, video_paths, audio_file, max_clip_duration)
# 4. Gộp audio và nhạc nền
final_video_path = "final_video.mp4"
bgm_clip = AudioFileClip(bgm_file)
final_audio = CompositeAudioClip([audio_clip, bgm_clip])
final_video = VideoFileClip(combined_video_path).set_audio(final_audio)
final_video.write_videofile(final_video_path)
return final_video_path
except Exception as e:
print(f"Lỗi khi tạo video: {e}")
return None
# Liên kết nút với hàm xử lý video
video_button.click(create_video_func,
inputs=[script_input, audio_file, keywords_output, max_clip_duration, join_order, bgm_files],
outputs=video_output)
return app
# Khởi chạy ứng dụng
if __name__ == "__main__":
app = interface()
app.launch() |