tts-vie-applio / app.py
Nông Văn Thắng
main
71a8039
import time
import os
import gradio as gr
from gradio.themes.utils.theme_dropdown import create_theme_dropdown
from convert_to_audio import read_text_from_file, run_tts_script
def text_to_speech(text, voice):
# Lưu văn bản vào file text.txt
text_file_path = "text.txt"
with open(text_file_path, "w", encoding="utf-8") as file:
file.write(text)
# Đọc nội dung từ file và chuyển đổi thành giọng nói
text_content = read_text_from_file(text_file_path)
output_path = "./exports/output.mp3"
if voice == None:
voice = "vi-VN-HoaiMyNeural"
print(f"HELLO {text_content, voice, 0, output_path}")
run_tts_script(text_content, voice, 0, output_path)
return output_path if os.path.exists(output_path) else None
dropdown, js = create_theme_dropdown()
darkModeActive = """
() => {
document.body.classList.toggle('dark');
}
"""
with gr.Blocks(theme='upsatwal/mlsc_tiet', js=darkModeActive) as demo:
with gr.Row():
with gr.Column(scale=10):
text_input = gr.Textbox(label="Nhập văn bản cần chuyển đổi", placeholder="Nhập văn bản ở đây...")
voice_select = gr.Dropdown(["vi-VN-HoaiMyNeural", "vi-VN-NamMinhNeural"], label="Chọn giọng nói")
go_btn = gr.Button("Chuyển đổi", value="Chuyển văn bản thành giọng nói", variant="primary")
audio_output = gr.Audio(label="Âm thanh đầu ra")
go_btn.click(
fn=text_to_speech,
inputs=[text_input, voice_select],
outputs=audio_output
)
if __name__ == "__main__":
demo.queue().launch()