Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,12 @@ from gtts.lang import _main_langs
|
|
10 |
AUDIO_DIR = 'audio_files'
|
11 |
MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()}
|
15 |
lang_code = lang_codes[lang]
|
16 |
|
@@ -23,7 +28,6 @@ async def text_to_speech(text, lang, tld, method):
|
|
23 |
elif method == "Edge TTS":
|
24 |
# Convert text to speech using Edge TTS
|
25 |
output = io.BytesIO()
|
26 |
-
voice = f"{lang_code}-{tld}".replace('_', '-')
|
27 |
# Create an Edge TTS object and get audio
|
28 |
communicate = edge_tts.Communicate(text, voice)
|
29 |
await communicate.save(output)
|
@@ -49,37 +53,43 @@ def delete_old_audio_files():
|
|
49 |
os.remove(file_path)
|
50 |
|
51 |
# Hàm chuyển đổi file .txt thành giọng nói
|
52 |
-
async def txt_to_speech(file, lang, tld, method):
|
53 |
with open(file.name, 'r') as f:
|
54 |
text = f.read()
|
55 |
-
return await text_to_speech(text, lang, tld, method)
|
56 |
-
|
57 |
-
tlds = [
|
58 |
-
"com", "ad", "ae", "com.af", "com.ag", "com.ai", "com.ar", "as", "at", "com.au", "az",
|
59 |
-
# (remaining TLDs omitted for brevity)
|
60 |
-
"com.vn"
|
61 |
-
]
|
62 |
|
63 |
# Tạo giao diện Gradio với tab
|
64 |
with gr.Blocks() as iface:
|
|
|
|
|
65 |
with gr.Tab("Text to Speech"):
|
66 |
gr.Markdown("### Convert text to speech")
|
67 |
text_input = gr.Textbox(lines=10, label="Enter your text here:")
|
68 |
lang_input = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
|
69 |
-
tld_input = gr.Dropdown(choices=
|
70 |
method_input = gr.Radio(choices=["gTTS", "Edge TTS"], label="Select method:", value="gTTS")
|
|
|
|
|
|
|
|
|
71 |
audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
|
72 |
-
gr.Button("Convert").click(fn=lambda text, lang, tld, method: asyncio.run(text_to_speech(text, lang, tld, method)),
|
73 |
-
inputs=[text_input, lang_input, tld_input, method_input
|
|
|
74 |
|
75 |
with gr.Tab("TXT to Speech"):
|
76 |
gr.Markdown("### Convert .txt file to speech")
|
77 |
file_input = gr.File(label="Upload your .txt file")
|
78 |
lang_input_file = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
|
79 |
-
tld_input_file = gr.Dropdown(choices=
|
80 |
method_input_file = gr.Radio(choices=["gTTS", "Edge TTS"], label="Select method:", value="gTTS")
|
|
|
|
|
|
|
|
|
81 |
audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
|
82 |
-
gr.Button("Convert").click(fn=lambda file, lang, tld, method: asyncio.run(txt_to_speech(file, lang, tld, method)),
|
83 |
-
inputs=[file_input, lang_input_file, tld_input_file, method_input_file
|
|
|
84 |
|
85 |
iface.launch(enable_queue=True)
|
|
|
10 |
AUDIO_DIR = 'audio_files'
|
11 |
MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
|
12 |
|
13 |
+
# Lấy danh sách giọng nói cho Edge TTS
|
14 |
+
async def get_edge_voices():
|
15 |
+
voices = await edge_tts.get_voices()
|
16 |
+
return [voice['name'] for voice in voices]
|
17 |
+
|
18 |
+
async def text_to_speech(text, lang, tld, method, voice):
|
19 |
lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()}
|
20 |
lang_code = lang_codes[lang]
|
21 |
|
|
|
28 |
elif method == "Edge TTS":
|
29 |
# Convert text to speech using Edge TTS
|
30 |
output = io.BytesIO()
|
|
|
31 |
# Create an Edge TTS object and get audio
|
32 |
communicate = edge_tts.Communicate(text, voice)
|
33 |
await communicate.save(output)
|
|
|
53 |
os.remove(file_path)
|
54 |
|
55 |
# Hàm chuyển đổi file .txt thành giọng nói
|
56 |
+
async def txt_to_speech(file, lang, tld, method, voice):
|
57 |
with open(file.name, 'r') as f:
|
58 |
text = f.read()
|
59 |
+
return await text_to_speech(text, lang, tld, method, voice)
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# Tạo giao diện Gradio với tab
|
62 |
with gr.Blocks() as iface:
|
63 |
+
voices = asyncio.run(get_edge_voices())
|
64 |
+
|
65 |
with gr.Tab("Text to Speech"):
|
66 |
gr.Markdown("### Convert text to speech")
|
67 |
text_input = gr.Textbox(lines=10, label="Enter your text here:")
|
68 |
lang_input = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
|
69 |
+
tld_input = gr.Dropdown(choices=tlds, label="Select TLD:", value="com")
|
70 |
method_input = gr.Radio(choices=["gTTS", "Edge TTS"], label="Select method:", value="gTTS")
|
71 |
+
|
72 |
+
# Dropdown cho giọng nói
|
73 |
+
voice_input = gr.Dropdown(choices=voices, label="Select voice:", value=voices[0])
|
74 |
+
|
75 |
audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
|
76 |
+
gr.Button("Convert").click(fn=lambda text, lang, tld, method, voice: asyncio.run(text_to_speech(text, lang, tld, method, voice)),
|
77 |
+
inputs=[text_input, lang_input, tld_input, method_input, voice_input],
|
78 |
+
outputs=[audio_output, file_output])
|
79 |
|
80 |
with gr.Tab("TXT to Speech"):
|
81 |
gr.Markdown("### Convert .txt file to speech")
|
82 |
file_input = gr.File(label="Upload your .txt file")
|
83 |
lang_input_file = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
|
84 |
+
tld_input_file = gr.Dropdown(choices=tlds, label="Select TLD:", value="com")
|
85 |
method_input_file = gr.Radio(choices=["gTTS", "Edge TTS"], label="Select method:", value="gTTS")
|
86 |
+
|
87 |
+
# Dropdown cho giọng nói
|
88 |
+
voice_input_file = gr.Dropdown(choices=voices, label="Select voice:", value=voices[0])
|
89 |
+
|
90 |
audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
|
91 |
+
gr.Button("Convert").click(fn=lambda file, lang, tld, method, voice: asyncio.run(txt_to_speech(file, lang, tld, method, voice)),
|
92 |
+
inputs=[file_input, lang_input_file, tld_input_file, method_input_file, voice_input_file],
|
93 |
+
outputs=[audio_output_file, file_output_file])
|
94 |
|
95 |
iface.launch(enable_queue=True)
|