Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,12 @@ import mimetypes
|
|
3 |
import gradio as gr
|
4 |
from docx import Document
|
5 |
from content_generation import create_content, CONTENT_TYPES
|
|
|
6 |
from tts import generate_speech
|
7 |
|
|
|
|
|
|
|
8 |
# Đường dẫn đến thư mục chứa các file âm thanh
|
9 |
VOICES_DIR = "voices"
|
10 |
|
@@ -29,28 +33,21 @@ def process_pdf(file_path):
|
|
29 |
"""
|
30 |
Xử lý file PDF và trích xuất nội dung.
|
31 |
"""
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
text
|
36 |
-
|
37 |
-
text += page.get_text()
|
38 |
-
return text
|
39 |
-
except Exception as e:
|
40 |
-
raise ValueError(f"Lỗi khi xử lý file PDF: {str(e)}")
|
41 |
|
42 |
def process_docx(file_path):
|
43 |
"""
|
44 |
Xử lý file DOCX và trích xuất nội dung.
|
45 |
"""
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
return text
|
52 |
-
except Exception as e:
|
53 |
-
raise ValueError(f"Lỗi khi xử lý file DOCX: {str(e)}")
|
54 |
|
55 |
def text_to_speech(content, voice_file):
|
56 |
"""
|
@@ -60,6 +57,7 @@ def text_to_speech(content, voice_file):
|
|
60 |
print(f"Đường dẫn file âm thanh mẫu: {voice_file}") # Log đường dẫn file
|
61 |
if voice_file is None or not os.path.exists(voice_file):
|
62 |
return f"Lỗi: File âm thanh mẫu không tồn tại hoặc không hợp lệ. Đường dẫn: {voice_file}"
|
|
|
63 |
print(f"Chuyển đổi nội dung thành giọng nói: {content}") # Log nội dung trước khi chuyển đổi
|
64 |
output_audio = generate_speech(content, language="vi", speaker_wav=voice_file)
|
65 |
print(f"File âm thanh đã được tạo: {output_audio}") # Log file âm thanh
|
@@ -81,6 +79,7 @@ def convert_content_to_speech(content, voice_file):
|
|
81 |
def interface():
|
82 |
with gr.Blocks() as app:
|
83 |
gr.Markdown("# TTV@tdnm")
|
|
|
84 |
with gr.Tab("Tạo Nội dung"):
|
85 |
with gr.Row():
|
86 |
with gr.Column():
|
@@ -95,6 +94,7 @@ def interface():
|
|
95 |
else:
|
96 |
voice_selector = gr.Dropdown(label="Chọn giọng đọc", choices=[], value=None) # Nếu không có file, để trống
|
97 |
content_button = gr.Button("Tạo Nội dung")
|
|
|
98 |
with gr.Column():
|
99 |
content_output = gr.Textbox(label="Nội dung tạo ra", interactive=True)
|
100 |
download_docx = gr.File(label="Tải xuống file DOCX", interactive=False)
|
@@ -111,20 +111,24 @@ def interface():
|
|
111 |
file_content = process_pdf(file)
|
112 |
prompt = f"{prompt}\n\nDưới đây là nội dung của file tài liệu:\n\n{file_content}"
|
113 |
elif mime_type in (
|
114 |
-
|
115 |
-
|
116 |
file_content = process_docx(file)
|
117 |
prompt = f"{prompt}\n\nDưới đây là nội dung của file tài liệu:\n\n{file_content}"
|
118 |
else:
|
119 |
raise ValueError("Định dạng file không được hỗ trợ.")
|
|
|
120 |
if not content_type:
|
121 |
raise ValueError("Vui lòng chọn một loại nội dung")
|
|
|
122 |
script_content = create_content(prompt, content_type, "Tiếng Việt")
|
123 |
print(f"Nội dung từ LLM: {script_content}") # Log nội dung từ LLM
|
124 |
if script_content is None:
|
125 |
raise ValueError("Nội dung từ LLM là None. Vui lòng kiểm tra lại hàm create_content.")
|
|
|
126 |
docx_path = "script.docx"
|
127 |
create_docx(script_content, docx_path)
|
|
|
128 |
status = "Đã tạo nội dung thành công!"
|
129 |
return script_content, docx_path, status
|
130 |
except Exception as e:
|
@@ -138,6 +142,7 @@ def interface():
|
|
138 |
content_button.click(generate_content,
|
139 |
inputs=[prompt, file_upload, content_type],
|
140 |
outputs=[content_output, download_docx, status_message])
|
|
|
141 |
convert_to_speech_button.click(convert_content_to_speech,
|
142 |
inputs=[content_output, voice_selector],
|
143 |
outputs=[audio_output])
|
|
|
3 |
import gradio as gr
|
4 |
from docx import Document
|
5 |
from content_generation import create_content, CONTENT_TYPES
|
6 |
+
from openai import OpenAI
|
7 |
from tts import generate_speech
|
8 |
|
9 |
+
# Khởi tạo client OpenAI với API key từ biến môi trường
|
10 |
+
client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
|
11 |
+
|
12 |
# Đường dẫn đến thư mục chứa các file âm thanh
|
13 |
VOICES_DIR = "voices"
|
14 |
|
|
|
33 |
"""
|
34 |
Xử lý file PDF và trích xuất nội dung.
|
35 |
"""
|
36 |
+
doc = fitz.open(file_path)
|
37 |
+
text = ""
|
38 |
+
for page in doc:
|
39 |
+
text += page.get_text()
|
40 |
+
return text
|
|
|
|
|
|
|
|
|
41 |
|
42 |
def process_docx(file_path):
|
43 |
"""
|
44 |
Xử lý file DOCX và trích xuất nội dung.
|
45 |
"""
|
46 |
+
doc = Document(file_path)
|
47 |
+
text = ""
|
48 |
+
for para in doc.paragraphs:
|
49 |
+
text += para.text
|
50 |
+
return text
|
|
|
|
|
|
|
51 |
|
52 |
def text_to_speech(content, voice_file):
|
53 |
"""
|
|
|
57 |
print(f"Đường dẫn file âm thanh mẫu: {voice_file}") # Log đường dẫn file
|
58 |
if voice_file is None or not os.path.exists(voice_file):
|
59 |
return f"Lỗi: File âm thanh mẫu không tồn tại hoặc không hợp lệ. Đường dẫn: {voice_file}"
|
60 |
+
|
61 |
print(f"Chuyển đổi nội dung thành giọng nói: {content}") # Log nội dung trước khi chuyển đổi
|
62 |
output_audio = generate_speech(content, language="vi", speaker_wav=voice_file)
|
63 |
print(f"File âm thanh đã được tạo: {output_audio}") # Log file âm thanh
|
|
|
79 |
def interface():
|
80 |
with gr.Blocks() as app:
|
81 |
gr.Markdown("# TTV@tdnm")
|
82 |
+
|
83 |
with gr.Tab("Tạo Nội dung"):
|
84 |
with gr.Row():
|
85 |
with gr.Column():
|
|
|
94 |
else:
|
95 |
voice_selector = gr.Dropdown(label="Chọn giọng đọc", choices=[], value=None) # Nếu không có file, để trống
|
96 |
content_button = gr.Button("Tạo Nội dung")
|
97 |
+
|
98 |
with gr.Column():
|
99 |
content_output = gr.Textbox(label="Nội dung tạo ra", interactive=True)
|
100 |
download_docx = gr.File(label="Tải xuống file DOCX", interactive=False)
|
|
|
111 |
file_content = process_pdf(file)
|
112 |
prompt = f"{prompt}\n\nDưới đây là nội dung của file tài liệu:\n\n{file_content}"
|
113 |
elif mime_type in (
|
114 |
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
115 |
+
"application/msword"):
|
116 |
file_content = process_docx(file)
|
117 |
prompt = f"{prompt}\n\nDưới đây là nội dung của file tài liệu:\n\n{file_content}"
|
118 |
else:
|
119 |
raise ValueError("Định dạng file không được hỗ trợ.")
|
120 |
+
|
121 |
if not content_type:
|
122 |
raise ValueError("Vui lòng chọn một loại nội dung")
|
123 |
+
|
124 |
script_content = create_content(prompt, content_type, "Tiếng Việt")
|
125 |
print(f"Nội dung từ LLM: {script_content}") # Log nội dung từ LLM
|
126 |
if script_content is None:
|
127 |
raise ValueError("Nội dung từ LLM là None. Vui lòng kiểm tra lại hàm create_content.")
|
128 |
+
|
129 |
docx_path = "script.docx"
|
130 |
create_docx(script_content, docx_path)
|
131 |
+
|
132 |
status = "Đã tạo nội dung thành công!"
|
133 |
return script_content, docx_path, status
|
134 |
except Exception as e:
|
|
|
142 |
content_button.click(generate_content,
|
143 |
inputs=[prompt, file_upload, content_type],
|
144 |
outputs=[content_output, download_docx, status_message])
|
145 |
+
|
146 |
convert_to_speech_button.click(convert_content_to_speech,
|
147 |
inputs=[content_output, voice_selector],
|
148 |
outputs=[audio_output])
|