Spaces:
Running
Running
import gradio as gr | |
from gtts import gTTS | |
import io | |
import os | |
import time | |
from gtts.lang import _main_langs | |
from docx import Document # Thư viện để làm việc với tệp .docx | |
from ebooklib import epub # Thư viện để làm việc với tệp .epub | |
AUDIO_DIR = 'audio_files' | |
MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours) | |
# Hàm chuyển đổi văn bản thành giọng nói sử dụng gTTS | |
def text_to_speech(text, lang, tld): | |
lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()} | |
lang_code = lang_codes[lang] | |
tts = gTTS(text, lang=lang_code, tld=tld) | |
fp = io.BytesIO() | |
tts.write_to_fp(fp) | |
fp.seek(0) | |
os.makedirs(AUDIO_DIR, exist_ok=True) | |
file_name = str(time.time()) + '.mp3' # Đổi định dạng thành mp3 | |
file_path = os.path.join(AUDIO_DIR, file_name) | |
with open(file_path, 'wb') as f: | |
f.write(fp.read()) | |
delete_old_audio_files() | |
return file_path, file_path | |
def delete_old_audio_files(): | |
now = time.time() | |
for file_name in os.listdir(AUDIO_DIR): | |
file_path = os.path.join(AUDIO_DIR, file_name) | |
if now - os.path.getmtime(file_path) > MAX_FILE_AGE: | |
os.remove(file_path) | |
# Hàm chuyển đổi file .txt thành giọng nói | |
def txt_to_speech(file, lang, tld): | |
with open(file.name, 'r') as f: | |
text = f.read() | |
return text_to_speech(text, lang, tld) | |
# Hàm chuyển đổi file .docx thành giọng nói | |
def docx_to_speech(file, lang, tld): | |
doc = Document(file.name) | |
text = "\n".join([para.text for para in doc.paragraphs]) # Lấy tất cả văn bản từ các đoạn | |
return text_to_speech(text, lang, tld) | |
# Hàm chuyển đổi file .epub thành giọng nói | |
def epub_to_speech(file, lang, tld): | |
book = epub.read_epub(file.name) | |
text = "" | |
for item in book.get_items_of_type(epub.EpubHtml): | |
text += item.get_body_content_str().decode('utf-8') # Lấy nội dung của mỗi trang | |
return text_to_speech(text, lang, tld) | |
# Tạo giao diện Gradio với tab | |
with gr.Blocks() as iface: | |
with gr.Tab("Text to Speech"): | |
gr.Markdown("### Convert text to speech") | |
text_input = gr.Textbox(lines=10, label="Enter your text here:") | |
lang_input = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:") | |
tld_input = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD | |
audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File") | |
gr.Button("Convert").click(fn=lambda text, lang, tld: text_to_speech(text, lang, tld), | |
inputs=[text_input, lang_input, tld_input], | |
outputs=[audio_output, file_output]) | |
with gr.Tab("TXT to Speech"): | |
gr.Markdown("### Convert .txt file to speech") | |
file_input = gr.File(label="Upload your .txt file") | |
lang_input_file = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:") | |
tld_input_file = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD | |
audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File") | |
gr.Button("Convert").click(fn=lambda file, lang, tld: txt_to_speech(file, lang, tld), | |
inputs=[file_input, lang_input_file, tld_input_file], | |
outputs=[audio_output_file, file_output_file]) | |
with gr.Tab("DOCX to Speech"): | |
gr.Markdown("### Convert .docx file to speech") | |
docx_file_input = gr.File(label="Upload your .docx file") | |
lang_input_docx = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:") | |
tld_input_docx = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD | |
audio_output_docx, file_output_docx = gr.Audio(label="Audio"), gr.File(label="Audio File") | |
gr.Button("Convert").click(fn=lambda file, lang, tld: docx_to_speech(file, lang, tld), | |
inputs=[docx_file_input, lang_input_docx, tld_input_docx], | |
outputs=[audio_output_docx, file_output_docx]) | |
with gr.Tab("EPUB to Speech"): | |
gr.Markdown("### Convert .epub file to speech") | |
epub_file_input = gr.File(label="Upload your .epub file") | |
lang_input_epub = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:") | |
tld_input_epub = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD | |
audio_output_epub, file_output_epub = gr.Audio(label="Audio"), gr.File(label="Audio File") | |
gr.Button("Convert").click(fn=lambda file, lang, tld: epub_to_speech(file, lang, tld), | |
inputs=[epub_file_input, lang_input_epub, tld_input_epub], | |
outputs=[audio_output_epub, file_output_epub]) | |
iface.launch(enable_queue=True) | |