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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 |
+
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 delete_old_audio_files():
|
40 |
+
# delete audio files older than MAX_FILE_AGE
|
41 |
+
now = time.time()
|
42 |
+
for file_name in os.listdir(AUDIO_DIR):
|
43 |
+
file_path = os.path.join(AUDIO_DIR, file_name)
|
44 |
+
if now - os.path.getmtime(file_path) > MAX_FILE_AGE:
|
45 |
+
os.remove(file_path)
|
46 |
+
|
47 |
+
# list of supported TLDs
|
48 |
+
tlds = [
|
49 |
+
"com",
|
50 |
+
"ad",
|
51 |
+
"ae",
|
52 |
+
"com.af",
|
53 |
+
"com.ag",
|
54 |
+
"com.ai",
|
55 |
+
"com.ar",
|
56 |
+
"as",
|
57 |
+
"at",
|
58 |
+
"com.au",
|
59 |
+
"az",
|
60 |
+
"ba",
|
61 |
+
"com.bd",
|
62 |
+
"be",
|
63 |
+
"bf",
|
64 |
+
"bg",
|
65 |
+
"bj",
|
66 |
+
"br",
|
67 |
+
"bs",
|
68 |
+
"bt",
|
69 |
+
"co.bw",
|
70 |
+
"by",
|
71 |
+
"com.bz",
|
72 |
+
"ca",
|
73 |
+
"cd",
|
74 |
+
"ch",
|
75 |
+
"ci",
|
76 |
+
"co.ck",
|
77 |
+
"cl",
|
78 |
+
"cm",
|
79 |
+
"cn",
|
80 |
+
"com.co",
|
81 |
+
"co.cr",
|
82 |
+
"cv",
|
83 |
+
"dj",
|
84 |
+
"dm",
|
85 |
+
"com.do",
|
86 |
+
"dz",
|
87 |
+
"com.ec",
|
88 |
+
"ee",
|
89 |
+
"com.eg",
|
90 |
+
"es",
|
91 |
+
"et",
|
92 |
+
"fi",
|
93 |
+
"com.fj",
|
94 |
+
"fm",
|
95 |
+
"fr",
|
96 |
+
"ga",
|
97 |
+
"ge",
|
98 |
+
"gg",
|
99 |
+
"com.gh",
|
100 |
+
"com.gi",
|
101 |
+
"gl",
|
102 |
+
"gm",
|
103 |
+
"gr",
|
104 |
+
"com.gt",
|
105 |
+
"gy",
|
106 |
+
"com.hk",
|
107 |
+
"hn",
|
108 |
+
"ht",
|
109 |
+
"hr",
|
110 |
+
"hu",
|
111 |
+
"co.id",
|
112 |
+
"ie",
|
113 |
+
"co.il",
|
114 |
+
"im",
|
115 |
+
"co.in",
|
116 |
+
"iq",
|
117 |
+
"is",
|
118 |
+
"it",
|
119 |
+
"iw",
|
120 |
+
"je",
|
121 |
+
"com.je",
|
122 |
+
"jo",
|
123 |
+
"co.jp",
|
124 |
+
"co.ke",
|
125 |
+
"com.kh",
|
126 |
+
"ki",
|
127 |
+
"kg",
|
128 |
+
"co.kr",
|
129 |
+
"com.kw",
|
130 |
+
"kz",
|
131 |
+
"la",
|
132 |
+
"com.lb",
|
133 |
+
"li",
|
134 |
+
"lk",
|
135 |
+
"co.ls",
|
136 |
+
"lt",
|
137 |
+
"lu",
|
138 |
+
"lv",
|
139 |
+
"com.ly",
|
140 |
+
"com.ma",
|
141 |
+
"md",
|
142 |
+
"me",
|
143 |
+
"mg",
|
144 |
+
"mk",
|
145 |
+
"ml",
|
146 |
+
"mm",
|
147 |
+
"mn",
|
148 |
+
"ms",
|
149 |
+
"com.mt",
|
150 |
+
"mu",
|
151 |
+
"mv",
|
152 |
+
"mw",
|
153 |
+
"com.mx",
|
154 |
+
"com.my",
|
155 |
+
"co.mz",
|
156 |
+
"na",
|
157 |
+
"ng",
|
158 |
+
"ni",
|
159 |
+
"ne",
|
160 |
+
"nl",
|
161 |
+
"no",
|
162 |
+
"com.np",
|
163 |
+
"nr",
|
164 |
+
"nu",
|
165 |
+
"co.nz",
|
166 |
+
"com.om",
|
167 |
+
"pa",
|
168 |
+
"pe",
|
169 |
+
"pg",
|
170 |
+
"ph",
|
171 |
+
"pk",
|
172 |
+
"pl",
|
173 |
+
"pn",
|
174 |
+
"com.pr",
|
175 |
+
"ps",
|
176 |
+
"pt",
|
177 |
+
"com.py",
|
178 |
+
"com.qa",
|
179 |
+
"ro",
|
180 |
+
"ru",
|
181 |
+
"rw",
|
182 |
+
"com.sa",
|
183 |
+
"com.sb",
|
184 |
+
"sc",
|
185 |
+
"se",
|
186 |
+
"com.sg",
|
187 |
+
"sh",
|
188 |
+
"si",
|
189 |
+
"sk",
|
190 |
+
"com.sl",
|
191 |
+
"sn",
|
192 |
+
"so",
|
193 |
+
"sm",
|
194 |
+
"sr",
|
195 |
+
"st",
|
196 |
+
"com.sv",
|
197 |
+
"td",
|
198 |
+
"tg",
|
199 |
+
"co.th",
|
200 |
+
"com.tj",
|
201 |
+
"tl",
|
202 |
+
"tm",
|
203 |
+
"tn",
|
204 |
+
"to",
|
205 |
+
"com.tr",
|
206 |
+
"tt",
|
207 |
+
"com.tw",
|
208 |
+
"co.tz",
|
209 |
+
"com.ua",
|
210 |
+
"co.ug",
|
211 |
+
"co.uk",
|
212 |
+
"com,uy",
|
213 |
+
"co.uz",
|
214 |
+
"com.vc",
|
215 |
+
"co.ve",
|
216 |
+
"vg",
|
217 |
+
"co.vi",
|
218 |
+
"com.vn",
|
219 |
+
"vu",
|
220 |
+
"ws",
|
221 |
+
"rs",
|
222 |
+
"co.za",
|
223 |
+
"co.zm",
|
224 |
+
"co.zw",
|
225 |
+
"cat",
|
226 |
+
]
|
227 |
+
|
228 |
+
# create the Gradio interface
|
229 |
+
iface = gr.Interface(fn=text_to_speech,
|
230 |
+
inputs=[gr.inputs.Textbox(lines=10, label="Enter your text here:"),
|
231 |
+
gr.inputs.Dropdown(choices=list(_main_langs().values()), label="Select language:"),
|
232 |
+
gr.inputs.Dropdown(choices=[tld for tld in tlds], label="Select TLD:", default="com")],
|
233 |
+
outputs=[gr.Audio(label="Audio"), gr.File(label="Audio File")],
|
234 |
+
allow_flagging="never")
|
235 |
+
|
236 |
+
iface.launch(enable_queue=True)
|