Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,68 +9,71 @@ AUDIO_DIR = 'audio_files'
|
|
9 |
MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
|
10 |
|
11 |
def text_to_speech(text, lang, tld):
|
12 |
-
# map the language name to its corresponding code
|
13 |
lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()}
|
14 |
lang_code = lang_codes[lang]
|
15 |
|
16 |
-
# create the text-to-speech audio
|
17 |
tts = gTTS(text, lang=lang_code, tld=tld)
|
18 |
fp = io.BytesIO()
|
19 |
tts.write_to_fp(fp)
|
20 |
fp.seek(0)
|
21 |
|
22 |
-
# create the audio directory if it does not exist
|
23 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
24 |
-
|
25 |
-
# generate a unique file name for the audio file
|
26 |
file_name = str(time.time()) + '.wav'
|
27 |
file_path = os.path.join(AUDIO_DIR, file_name)
|
28 |
|
29 |
-
# save the audio stream to a file
|
30 |
with open(file_path, 'wb') as f:
|
31 |
f.write(fp.read())
|
32 |
|
33 |
-
# delete old audio files
|
34 |
delete_old_audio_files()
|
35 |
-
|
36 |
-
# return the file path
|
37 |
-
return file_path, f.name
|
38 |
-
|
39 |
-
def txt_file_to_speech(txt_file, lang, tld):
|
40 |
-
# read text from the uploaded .txt file
|
41 |
-
text = txt_file.read().decode('utf-8')
|
42 |
-
return text_to_speech(text, lang, tld)
|
43 |
|
44 |
def delete_old_audio_files():
|
45 |
-
# delete audio files older than MAX_FILE_AGE
|
46 |
now = time.time()
|
47 |
for file_name in os.listdir(AUDIO_DIR):
|
48 |
file_path = os.path.join(AUDIO_DIR, file_name)
|
49 |
if now - os.path.getmtime(file_path) > MAX_FILE_AGE:
|
50 |
os.remove(file_path)
|
51 |
|
52 |
-
#
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
]
|
55 |
|
56 |
-
#
|
57 |
with gr.Blocks() as iface:
|
58 |
with gr.Tab("Text to Speech"):
|
59 |
gr.Markdown("### Convert text to speech")
|
60 |
text_input = gr.Textbox(lines=10, label="Enter your text here:")
|
61 |
-
|
62 |
-
|
63 |
-
audio_output = gr.Audio(label="Audio")
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
gr.
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
file_output_txt = gr.File(label="Audio File")
|
74 |
-
gr.Button("Convert").click(txt_file_to_speech, inputs=[txt_input, lang_dropdown_txt, tld_dropdown_txt], outputs=[audio_output_txt, file_output_txt])
|
75 |
|
76 |
iface.launch(enable_queue=True)
|
|
|
9 |
MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
|
10 |
|
11 |
def text_to_speech(text, lang, tld):
|
|
|
12 |
lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()}
|
13 |
lang_code = lang_codes[lang]
|
14 |
|
|
|
15 |
tts = gTTS(text, lang=lang_code, tld=tld)
|
16 |
fp = io.BytesIO()
|
17 |
tts.write_to_fp(fp)
|
18 |
fp.seek(0)
|
19 |
|
|
|
20 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
|
|
|
|
21 |
file_name = str(time.time()) + '.wav'
|
22 |
file_path = os.path.join(AUDIO_DIR, file_name)
|
23 |
|
|
|
24 |
with open(file_path, 'wb') as f:
|
25 |
f.write(fp.read())
|
26 |
|
|
|
27 |
delete_old_audio_files()
|
28 |
+
return file_path, file_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
def delete_old_audio_files():
|
|
|
31 |
now = time.time()
|
32 |
for file_name in os.listdir(AUDIO_DIR):
|
33 |
file_path = os.path.join(AUDIO_DIR, file_name)
|
34 |
if now - os.path.getmtime(file_path) > MAX_FILE_AGE:
|
35 |
os.remove(file_path)
|
36 |
|
37 |
+
# Hàm chuyển đổi file .txt thành giọng nói
|
38 |
+
def txt_to_speech(file, lang, tld):
|
39 |
+
with open(file.name, 'r') as f:
|
40 |
+
text = f.read()
|
41 |
+
return text_to_speech(text, lang, tld)
|
42 |
+
|
43 |
+
tlds = [
|
44 |
+
"com", "ad", "ae", "com.af", "com.ag", "com.ai", "com.ar", "as", "at", "com.au", "az",
|
45 |
+
"ba", "com.bd", "be", "bf", "bg", "bj", "br", "bs", "bt", "co.bw", "by", "com.bz", "ca",
|
46 |
+
"cd", "ch", "ci", "co.ck", "cl", "cm", "cn", "com.co", "co.cr", "cv", "dj", "dm", "com.do",
|
47 |
+
"dz", "com.ec", "ee", "com.eg", "es", "et", "fi", "com.fj", "fm", "fr", "ga", "ge", "gg",
|
48 |
+
"com.gh", "com.gi", "gl", "gm", "gr", "com.gt", "gy", "com.hk", "hn", "ht", "hr", "hu",
|
49 |
+
"co.id", "ie", "co.il", "im", "co.in", "iq", "is", "it", "iw", "je", "com.je", "jo",
|
50 |
+
"co.jp", "co.ke", "com.kh", "ki", "kg", "co.kr", "com.kw", "kz", "la", "com.lb", "li",
|
51 |
+
"lk", "co.ls", "lt", "lu", "lv", "com.ly", "com.ma", "md", "me", "mg", "mk", "ml", "mm",
|
52 |
+
"mn", "ms", "com.mt", "mu", "mv", "mw", "com.mx", "com.my", "co.mz", "na", "ng", "ni",
|
53 |
+
"ne", "nl", "no", "com.np", "nr", "nu", "co.nz", "com.om", "pa", "pe", "pg", "ph", "pk",
|
54 |
+
"pl", "pn", "com.pr", "ps", "pt", "com.py", "com.qa", "ro", "ru", "rw", "com.sa", "com.sb",
|
55 |
+
"sc", "se", "com.sg", "sh", "si", "sk", "com.sl", "sn", "so", "sm", "sr", "st", "com.sv",
|
56 |
+
"td", "tg", "co.th", "com.tj", "tl", "tm", "tn", "to", "com.tr", "tt", "com.tw", "co.tz",
|
57 |
+
"com.ua", "co.ug", "co.uk", "com.uy", "co.uz", "com.vc", "co.ve", "vg", "co.vi", "com.vn",
|
58 |
+
"vu", "ws", "rs", "co.za", "co.zm", "co.zw", "cat", "com.ng"
|
59 |
]
|
60 |
|
61 |
+
# Tạo giao diện Gradio với tab
|
62 |
with gr.Blocks() as iface:
|
63 |
with gr.Tab("Text to Speech"):
|
64 |
gr.Markdown("### Convert text to speech")
|
65 |
text_input = gr.Textbox(lines=10, label="Enter your text here:")
|
66 |
+
lang_input = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
|
67 |
+
tld_input = gr.Dropdown(choices=[tld for tld in tlds], label="Select TLD:", value="com")
|
68 |
+
audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
|
69 |
+
gr.Button("Convert").click(fn=text_to_speech, inputs=[text_input, lang_input, tld_input], outputs=[audio_output, file_output])
|
70 |
+
|
71 |
+
with gr.Tab("TXT to Speech"):
|
72 |
+
gr.Markdown("### Convert .txt file to speech")
|
73 |
+
file_input = gr.File(label="Upload your .txt file")
|
74 |
+
lang_input_file = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
|
75 |
+
tld_input_file = gr.Dropdown(choices=[tld for tld in tlds], label="Select TLD:", value="com")
|
76 |
+
audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
|
77 |
+
gr.Button("Convert").click(fn=txt_to_speech, inputs=[file_input, lang_input_file, tld_input_file], outputs=[audio_output_file, file_output_file])
|
|
|
|
|
78 |
|
79 |
iface.launch(enable_queue=True)
|