Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,3 @@
|
|
1 |
-
import asyncio
|
2 |
-
import mimetypes
|
3 |
-
import os
|
4 |
-
import tempfile
|
5 |
-
import glob
|
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 |
-
|
19 |
-
# Khởi tạo client OpenAI với API key từ biến môi trường
|
20 |
-
client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
|
21 |
-
|
22 |
-
def create_docx(content, output_path):
|
23 |
-
"""
|
24 |
-
Tạo file docx từ nội dung.
|
25 |
-
"""
|
26 |
-
doc = Document()
|
27 |
-
doc.add_paragraph(content)
|
28 |
-
doc.save(output_path)
|
29 |
-
|
30 |
-
def process_pdf(file_path):
|
31 |
-
"""
|
32 |
-
Xử lý file PDF và trích xuất nội dung.
|
33 |
-
"""
|
34 |
-
doc = fitz.open(file_path)
|
35 |
-
text = ""
|
36 |
-
for page in doc:
|
37 |
-
text += page.get_text()
|
38 |
-
return text
|
39 |
-
|
40 |
-
def process_docx(file_path):
|
41 |
-
"""
|
42 |
-
Xử lý file DOCX và trích xuất nội dung.
|
43 |
-
"""
|
44 |
-
doc = Document(file_path)
|
45 |
-
text = ""
|
46 |
-
for para in doc.paragraphs:
|
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 |
-
# ... existing imports and functions ...
|
78 |
-
|
79 |
-
# Giao diện Gradio
|
80 |
def interface():
|
81 |
with gr.Blocks() as app:
|
82 |
gr.Markdown("# Ứng dụng Tạo Nội dung và Video")
|
@@ -142,7 +63,7 @@ def interface():
|
|
142 |
outputs=[download_docx, download_audio, status_message])
|
143 |
|
144 |
# Định nghĩa danh sách giọng đọc
|
145 |
-
VOICES = ["
|
146 |
|
147 |
with gr.Tab("Tạo Âm thanh"):
|
148 |
text_input = gr.Textbox(label="Nhập văn bản để chuyển đổi")
|
@@ -153,7 +74,7 @@ def interface():
|
|
153 |
|
154 |
def text_to_speech_func(text, voice):
|
155 |
try:
|
156 |
-
audio_path =
|
157 |
return audio_path, audio_path
|
158 |
except Exception as e:
|
159 |
print(f"Lỗi khi chuyển đổi văn bản thành giọng nói: {e}")
|
@@ -224,4 +145,4 @@ def interface():
|
|
224 |
# Khởi chạy ứng dụng
|
225 |
if __name__ == "__main__":
|
226 |
app = interface()
|
227 |
-
app.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def interface():
|
2 |
with gr.Blocks() as app:
|
3 |
gr.Markdown("# Ứng dụng Tạo Nội dung và Video")
|
|
|
63 |
outputs=[download_docx, download_audio, status_message])
|
64 |
|
65 |
# Định nghĩa danh sách giọng đọc
|
66 |
+
VOICES = ["vi-VN-HoaiMyNeural"]
|
67 |
|
68 |
with gr.Tab("Tạo Âm thanh"):
|
69 |
text_input = gr.Textbox(label="Nhập văn bản để chuyển đổi")
|
|
|
74 |
|
75 |
def text_to_speech_func(text, voice):
|
76 |
try:
|
77 |
+
audio_path = await async_text_to_speech(text, voice, "Tiếng Việt")
|
78 |
return audio_path, audio_path
|
79 |
except Exception as e:
|
80 |
print(f"Lỗi khi chuyển đổi văn bản thành giọng nói: {e}")
|
|
|
145 |
# Khởi chạy ứng dụng
|
146 |
if __name__ == "__main__":
|
147 |
app = interface()
|
148 |
+
app.launch(share=True)
|