Spaces:
Sleeping
Sleeping
File size: 4,038 Bytes
112539c 49317a0 112539c 586604c 112539c 586604c 112539c 586604c 112539c 690b063 909da50 112539c f3b5211 5130a91 690b063 909da50 586604c 49317a0 586604c 690b063 586604c 9a9dd6e 112539c f3b5211 49317a0 f3b5211 5130a91 49317a0 5130a91 49317a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
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êm thư viện này để làm việc với tệp docx
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)
# 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])
iface.launch(enable_queue=True)
|