TDN-M commited on
Commit
e7d588c
·
verified ·
1 Parent(s): 4943180

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +191 -0
app.py CHANGED
@@ -89,6 +89,190 @@ def process_docx(file_path):
89
  text += para.text
90
  return text
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  # Giao diện Gradio
93
  def interface():
94
  with gr.Blocks() as app:
@@ -219,6 +403,13 @@ def interface():
219
 
220
  return app
221
 
 
 
 
 
 
 
 
222
  # Khởi chạy ứng dụng
223
  if __name__ == "__main__":
224
  app = interface()
 
89
  text += para.text
90
  return text
91
 
92
+ # Giao diện Gradio
93
+ def interface():
94
+ with gr.Blocks() as app:
95
+ gr.Markdown("# Ứng dụng Tạo Nội dung và Video")
96
+
97
+ with gr.Tab("Tạo Nội dung"):
98
+ prompt = gr.Textbox(label="Nhập yêu cầu nội dung")
99
+ file_upload = gr.File(label="Tải lên file kèm theo", type="filepath")
100
+
101
+ # Sử dụng gr.Radio thay vì gr.CheckboxGroup
102
+ content_type = gr.Radio(label="Chọn loại nội dung",
103
+ choices=CONTENT_TYPES,
104
+ value=None) # Giá trị mặc định là không có gì được chọn
105
+
106
+ content_button = gr.Button("Tạo Nội dung")
107
+ content_output = gr.Textbox(label="Nội dung tạo ra", interactive=True)
108
+ confirm_button = gr.Button("Xác nhận nội dung")
109
+ download_docx = gr.File(label="Tải xuống file DOCX", interactive=False)
110
+ download_audio = gr.File(label="Tải xuống file âm thanh",
111
+ interactive=False)
112
+ status_message = gr.Label(label="Trạng thái")
113
+
114
+ def generate_content(prompt, file, content_type):
115
+ try:
116
+ status = "Đang xử lý..."
117
+ if file and os.path.exists(file):
118
+ mime_type, _ = mimetypes.guess_type(file)
119
+ if mime_type == "application/pdf":
120
+ file_content = process_pdf(file)
121
+ prompt = f"{prompt}\n\nDưới đây là nội dung của file tài liệu:\n\n{file_content}"
122
+ elif mime_type in (
123
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
124
+ "application/msword"):
125
+ file_content = process_docx(file)
126
+ prompt = f"{prompt}\n\nDưới đây là nội dung của file tài liệu:\n\n{file_content}"
127
+ else:
128
+ raise ValueError("Định dạng file không được hỗ trợ.")
129
+
130
+ if not content_type:
131
+ raise ValueError("Vui lòng chọn một loại nội dung")
132
+
133
+ script_content = create_content(prompt, content_type, "Tiếng Việt")
134
+ docx_path = "script.docx"
135
+ create_docx(script_content, docx_path)
136
+
137
+ status = "Đã tạo nội dung thành công!"
138
+ return script_content, docx_path, status
139
+ except Exception as e:
140
+ status = f"Đã xảy ra lỗi: {str(e)}"
141
+ return "", None, status
142
+
143
+ async def confirm_content(content):
144
+ docx_path = "script.docx"
145
+ create_docx(content, docx_path)
146
+
147
+ audio_path = await async_text_to_speech(content, "alloy", "Tiếng Việt")
148
+ return docx_path, audio_path, "Nội dung đã được xác nhận và âm thanh đã được tạo!"
149
+
150
+ content_button.click(generate_content,
151
+ inputs=[prompt, file_upload, content_type],
152
+ outputs=[content_output, download_docx, status_message])
153
+
154
+ confirm_button.click(lambda x: asyncio.run(confirm_content(x)),
155
+ inputs=[content_output],
156
+ outputs=[download_docx, download_audio, status_message])
157
+
158
+ with gr.Tab("Tạo Âm thanh"):
159
+ text_input = gr.Textbox(label="Nhập văn bản để chuyển đổi")
160
+ voice_select = gr.Dropdown(label="Chọn giọng đọc",
161
+ choices=VOICES) # Dropdown cho voice_select
162
+ audio_button = gr.Button("Tạo Âm thanh")
163
+ audio_output = gr.Audio(label="Âm thanh tạo ra")
164
+ download_audio = gr.File(label="Tải xuống file âm thanh",
165
+ interactive=False)
166
+
167
+ def text_to_speech_func(text, voice):
168
+ audio_path = text_to_speech(text, voice, "Tiếng Việt")
169
+ return audio_path, audio_path
170
+
171
+ audio_button.click(text_to_speech_func,
172
+ inputs=[text_input, voice_select],
173
+ outputs=[audio_output, download_audio])
174
+
175
+ with gr.Tab("Tạo Video"):
176
+ script_input = gr.Textbox(label="Nhập kịch bản")
177
+ audio_file = gr.File(label="Chọn file âm thanh", type="filepath")
178
+ keywords_output = gr.Textbox(label="Từ khóa", interactive=True)
179
+ max_clip_duration = gr.Slider(minimum=2, maximum=5, step=1, label="Thời lượng tối đa mỗi video (giây)")
180
+ join_order = gr.Checkbox(label="Ghép ngẫu nhiên", value=True) # Mặc định là ghép ngẫu nhiên
181
+ bgm_files = gr.Dropdown(choices=get_bgm_file_list(), label="Chọn nhạc nền") # Hàm get_bgm_file_list() cần được định nghĩa
182
+ create_video_button = gr.Button("Tạo Video")
183
+ video_output = gr.Video(label="Video tạo ra")
184
+
185
+ import asyncio
186
+ import mimetypes
187
+ import os
188
+ import tempfile
189
+ import gradio as gr
190
+ from docx import Document
191
+ from audio_processing import async_text_to_speech, text_to_speech
192
+ from content_generation import create_content, CONTENT_TYPES
193
+ from video_processing import create_video_func
194
+ from moviepy.editor import AudioFileClip, CompositeAudioClip
195
+ from utils import (combine_videos, find_matching_image, get_pexels_image,split_audio, get_bgm_file)
196
+ from video_processing import create_video
197
+
198
+ # Danh sách giọng đọc
199
+ VOICES = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
200
+
201
+ # Danh sách ngôn ngữ (chưa được sử dụng trong mã)
202
+ LANGUAGES = ["Tiếng Anh", "Tiếng Việt", "Tiếng Hindi"]
203
+
204
+ # Danh sách loại nội dung và hướng dẫn mặc định cho từng loại
205
+ CONTENT_TYPES = ["podcast", "giới thiệu", "triết lý sống",
206
+ "Phổ biến kiến thức thống kê"]
207
+ CONTENT_TYPE_INSTRUCTIONS = {
208
+ "podcast":
209
+ """
210
+ 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ủ đề.
211
+ Cấu trúc:
212
+ - 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ò.
213
+ - 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.
214
+ - Xây dựng các phần chuyển tiếp mượt mà giữa các ý.
215
+ - 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ả.
216
+ 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ủ đề.
217
+ """,
218
+ "giới thiệu":
219
+ """
220
+ Tone giọng: Chuyên nghiệp, gãy gọn nhưng vẫn có sự truyền cảm.
221
+ Cấu trúc:
222
+ - Bắt đầu với một câu khẳng định mạnh mẽ về đối tượng được giới thiệu.
223
+ - 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.
224
+ - 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.
225
+ 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.
226
+ """,
227
+ "triết lý sống":
228
+ """
229
+ Tone giọng: Sâu sắc, truyền cảm hứng, mang tính chiêm nghiệm.
230
+ Cấu trúc:
231
+ - Bắt đầu bằng một câu hỏi sâu sắc hoặc ẩn dụ về cuộc sống.
232
+ - 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ý.
233
+ - 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.
234
+ 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.
235
+ """,
236
+ "Phổ biến kiến thức Thống kê":
237
+ """
238
+ Tone giọng: Thân thiện, dễ hiểu, và mang tính giáo dục.
239
+ Cấu trúc:
240
+ - 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ú ý.
241
+ - 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.
242
+ - Đư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ể.
243
+ - 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.
244
+ 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.
245
+ """
246
+ }
247
+
248
+ def create_docx(content, output_path):
249
+ """
250
+ Tạo file docx từ nội dung.
251
+ """
252
+ doc = Document()
253
+ doc.add_paragraph(content)
254
+ doc.save(output_path)
255
+
256
+ def process_pdf(file_path):
257
+ """
258
+ Xử lý file PDF và trích xuất nội dung.
259
+ """
260
+ doc = fitz.open(file_path)
261
+ text = ""
262
+ for page in doc:
263
+ text += page.get_text()
264
+ return text
265
+
266
+ def process_docx(file_path):
267
+ """
268
+ Xử lý file DOCX và trích xuất nội dung.
269
+ """
270
+ doc = Document(file_path)
271
+ text = ""
272
+ for para in doc.paragraphs:
273
+ text += para.text
274
+ return text
275
+
276
  # Giao diện Gradio
277
  def interface():
278
  with gr.Blocks() as app:
 
403
 
404
  return app
405
 
406
+ # Khởi chạy ứng dụng
407
+ if __name__ == "__main__":
408
+ app = interface()
409
+ app.launch()
410
+
411
+ return app
412
+
413
  # Khởi chạy ứng dụng
414
  if __name__ == "__main__":
415
  app = interface()