Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,25 @@
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
3 |
-
import edge_tts
|
4 |
import io
|
5 |
import os
|
6 |
import time
|
7 |
-
import asyncio
|
8 |
from gtts.lang import _main_langs
|
9 |
|
10 |
AUDIO_DIR = 'audio_files'
|
11 |
MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
voices = await edge_tts.list_voices() # Lấy danh sách giọng nói
|
16 |
-
print(voices) # In ra danh sách giọng nói để kiểm tra cấu trúc
|
17 |
-
return voices # Trả về toàn bộ danh sách giọng nói
|
18 |
-
|
19 |
-
async def get_filtered_voices(lang):
|
20 |
-
all_voices = await get_edge_voices()
|
21 |
-
# Lọc giọng nói dựa trên ngôn ngữ đã chọn
|
22 |
-
# Thay đổi điều kiện lọc dựa trên cấu trúc của voice
|
23 |
-
filtered_voices = [voice['name'] for voice in all_voices if lang in voice['locale']] # Thay 'locale' bằng khóa phù hợp
|
24 |
-
return filtered_voices
|
25 |
-
|
26 |
-
async def text_to_speech(text, lang, tld, method, voice):
|
27 |
lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()}
|
28 |
lang_code = lang_codes[lang]
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
fp.seek(0)
|
35 |
-
|
36 |
-
elif method == "Edge TTS":
|
37 |
-
# Convert text to speech using Edge TTS
|
38 |
-
output = io.BytesIO()
|
39 |
-
# Create an Edge TTS object and get audio
|
40 |
-
communicate = edge_tts.Communicate(text, voice)
|
41 |
-
await communicate.save(output)
|
42 |
-
|
43 |
-
output.seek(0)
|
44 |
-
fp = output # Use the in-memory output
|
45 |
|
46 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
47 |
-
file_name = str(time.time()) + '.
|
48 |
file_path = os.path.join(AUDIO_DIR, file_name)
|
49 |
|
50 |
with open(file_path, 'wb') as f:
|
@@ -60,48 +35,17 @@ def delete_old_audio_files():
|
|
60 |
if now - os.path.getmtime(file_path) > MAX_FILE_AGE:
|
61 |
os.remove(file_path)
|
62 |
|
63 |
-
# Hàm chuyển đổi file .txt thành giọng nói
|
64 |
-
async def txt_to_speech(file, lang, tld, method, voice):
|
65 |
-
with open(file.name, 'r') as f:
|
66 |
-
text = f.read()
|
67 |
-
return await text_to_speech(text, lang, tld, method, voice)
|
68 |
-
|
69 |
# Tạo giao diện Gradio với tab
|
70 |
with gr.Blocks() as iface:
|
71 |
-
# Dropdown cho ngôn ngữ
|
72 |
-
lang_input = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:", interactive=True)
|
73 |
-
|
74 |
-
# Dropdown cho giọng nói, sẽ được cập nhật sau
|
75 |
-
voice_input = gr.Dropdown(label="Select voice:", interactive=True)
|
76 |
-
|
77 |
-
# Tạo các phần giao diện cho Text to Speech
|
78 |
with gr.Tab("Text to Speech"):
|
79 |
gr.Markdown("### Convert text to speech")
|
80 |
text_input = gr.Textbox(lines=10, label="Enter your text here:")
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
|
85 |
-
gr.Button("Convert").click(fn=lambda text, lang, tld
|
86 |
-
inputs=[text_input, lang_input, tld_input
|
87 |
outputs=[audio_output, file_output])
|
88 |
-
|
89 |
-
# Cập nhật giọng nói khi ngôn ngữ thay đổi
|
90 |
-
def update_voices(selected_lang):
|
91 |
-
voices = asyncio.run(get_filtered_voices(selected_lang))
|
92 |
-
return voices
|
93 |
-
|
94 |
-
lang_input.change(fn=update_voices, inputs=lang_input, outputs=voice_input)
|
95 |
-
|
96 |
-
# Tạo phần cho TXT to Speech
|
97 |
-
with gr.Tab("TXT to Speech"):
|
98 |
-
gr.Markdown("### Convert .txt file to speech")
|
99 |
-
file_input = gr.File(label="Upload your .txt file")
|
100 |
-
method_input_file = gr.Radio(choices=["gTTS", "Edge TTS"], label="Select method:", value="gTTS")
|
101 |
-
|
102 |
-
audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
|
103 |
-
gr.Button("Convert").click(fn=lambda file, lang, tld, method, voice: asyncio.run(txt_to_speech(file, lang, tld, method, voice)),
|
104 |
-
inputs=[file_input, lang_input, tld_input, method_input_file, voice_input],
|
105 |
-
outputs=[audio_output_file, file_output_file])
|
106 |
|
107 |
iface.launch(enable_queue=True)
|
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
|
|
3 |
import io
|
4 |
import os
|
5 |
import time
|
|
|
6 |
from gtts.lang import _main_langs
|
7 |
|
8 |
AUDIO_DIR = 'audio_files'
|
9 |
MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
|
10 |
|
11 |
+
# Hàm chuyển đổi văn bản thành giọng nói sử dụng gTTS
|
12 |
+
def text_to_speech(text, lang, tld):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()}
|
14 |
lang_code = lang_codes[lang]
|
15 |
|
16 |
+
tts = gTTS(text, lang=lang_code, tld=tld)
|
17 |
+
fp = io.BytesIO()
|
18 |
+
tts.write_to_fp(fp)
|
19 |
+
fp.seek(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
22 |
+
file_name = str(time.time()) + '.mp3' # Đổi định dạng thành mp3
|
23 |
file_path = os.path.join(AUDIO_DIR, file_name)
|
24 |
|
25 |
with open(file_path, 'wb') as f:
|
|
|
35 |
if now - os.path.getmtime(file_path) > MAX_FILE_AGE:
|
36 |
os.remove(file_path)
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# Tạo giao diện Gradio với tab
|
39 |
with gr.Blocks() as iface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
with gr.Tab("Text to Speech"):
|
41 |
gr.Markdown("### Convert text to speech")
|
42 |
text_input = gr.Textbox(lines=10, label="Enter your text here:")
|
43 |
+
lang_input = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
|
44 |
+
tld_input = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD
|
45 |
+
|
46 |
audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
|
47 |
+
gr.Button("Convert").click(fn=lambda text, lang, tld: text_to_speech(text, lang, tld),
|
48 |
+
inputs=[text_input, lang_input, tld_input],
|
49 |
outputs=[audio_output, file_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
iface.launch(enable_queue=True)
|