Spaces:
Sleeping
Sleeping
vuxuanhoan
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,8 @@ import io
|
|
4 |
import os
|
5 |
import time
|
6 |
from gtts.lang import _main_langs
|
7 |
-
from docx import Document #
|
|
|
8 |
|
9 |
AUDIO_DIR = 'audio_files'
|
10 |
MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
|
@@ -48,6 +49,15 @@ def docx_to_speech(file, lang, tld):
|
|
48 |
text = "\n".join([para.text for para in doc.paragraphs]) # Lấy tất cả văn bản từ các đoạn
|
49 |
return text_to_speech(text, lang, tld)
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
# Tạo giao diện Gradio với tab
|
52 |
with gr.Blocks() as iface:
|
53 |
with gr.Tab("Text to Speech"):
|
@@ -83,4 +93,15 @@ with gr.Blocks() as iface:
|
|
83 |
inputs=[docx_file_input, lang_input_docx, tld_input_docx],
|
84 |
outputs=[audio_output_docx, file_output_docx])
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
iface.launch(enable_queue=True)
|
|
|
4 |
import os
|
5 |
import time
|
6 |
from gtts.lang import _main_langs
|
7 |
+
from docx import Document # Thư viện để làm việc với tệp .docx
|
8 |
+
from ebooklib import epub # Thư viện để làm việc với tệp .epub
|
9 |
|
10 |
AUDIO_DIR = 'audio_files'
|
11 |
MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
|
|
|
49 |
text = "\n".join([para.text for para in doc.paragraphs]) # Lấy tất cả văn bản từ các đoạn
|
50 |
return text_to_speech(text, lang, tld)
|
51 |
|
52 |
+
# Hàm chuyển đổi file .epub thành giọng nói
|
53 |
+
def epub_to_speech(file, lang, tld):
|
54 |
+
book = epub.read_epub(file.name)
|
55 |
+
text = ""
|
56 |
+
|
57 |
+
for item in book.get_items_of_type(epub.EpubHtml):
|
58 |
+
text += item.get_body_content_str().decode('utf-8') # Lấy nội dung của mỗi trang
|
59 |
+
return text_to_speech(text, lang, tld)
|
60 |
+
|
61 |
# Tạo giao diện Gradio với tab
|
62 |
with gr.Blocks() as iface:
|
63 |
with gr.Tab("Text to Speech"):
|
|
|
93 |
inputs=[docx_file_input, lang_input_docx, tld_input_docx],
|
94 |
outputs=[audio_output_docx, file_output_docx])
|
95 |
|
96 |
+
with gr.Tab("EPUB to Speech"):
|
97 |
+
gr.Markdown("### Convert .epub file to speech")
|
98 |
+
epub_file_input = gr.File(label="Upload your .epub file")
|
99 |
+
lang_input_epub = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
|
100 |
+
tld_input_epub = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD
|
101 |
+
|
102 |
+
audio_output_epub, file_output_epub = gr.Audio(label="Audio"), gr.File(label="Audio File")
|
103 |
+
gr.Button("Convert").click(fn=lambda file, lang, tld: epub_to_speech(file, lang, tld),
|
104 |
+
inputs=[epub_file_input, lang_input_epub, tld_input_epub],
|
105 |
+
outputs=[audio_output_epub, file_output_epub])
|
106 |
+
|
107 |
iface.launch(enable_queue=True)
|