vuxuanhoan commited on
Commit
03a52a4
·
verified ·
1 Parent(s): 690b063

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from gtts import gTTS
 
3
  import io
4
  import os
5
  import time
@@ -8,14 +9,27 @@ from gtts.lang import _main_langs
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
  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'
@@ -35,27 +49,15 @@ def delete_old_audio_files():
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
@@ -65,15 +67,17 @@ with gr.Blocks() as iface:
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)
 
1
  import gradio as gr
2
  from gtts import gTTS
3
+ import edge_tts
4
  import io
5
  import os
6
  import time
 
9
  AUDIO_DIR = 'audio_files'
10
  MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
11
 
12
+ def text_to_speech(text, lang, tld, method):
13
  lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()}
14
  lang_code = lang_codes[lang]
15
 
16
+ if method == "gTTS":
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
+ elif method == "Edge TTS":
23
+ # Convert text to speech using Edge TTS
24
+ output = io.BytesIO()
25
+ voice = f"{lang_code}-{tld}".replace('_', '-')
26
+ # Create an Edge TTS object and get audio
27
+ await edge_tts.init()
28
+ communicate = edge_tts.Communicate(text, voice)
29
+ await communicate.save(output)
30
+
31
+ output.seek(0)
32
+ fp = output # Use the in-memory output
33
 
34
  os.makedirs(AUDIO_DIR, exist_ok=True)
35
  file_name = str(time.time()) + '.wav'
 
49
  os.remove(file_path)
50
 
51
  # Hàm chuyển đổi file .txt thành giọng nói
52
+ def txt_to_speech(file, lang, tld, method):
53
  with open(file.name, 'r') as f:
54
  text = f.read()
55
+ return 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
 
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=[tld for tld in tlds], label="Select TLD:", value="com")
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=text_to_speech, inputs=[text_input, lang_input, tld_input, method_input], outputs=[audio_output, file_output])
73
 
74
  with gr.Tab("TXT to Speech"):
75
  gr.Markdown("### Convert .txt file to speech")
76
  file_input = gr.File(label="Upload your .txt file")
77
  lang_input_file = gr.Dropdown(choices=list(_main_langs().values()), label="Select language:")
78
  tld_input_file = gr.Dropdown(choices=[tld for tld in tlds], label="Select TLD:", value="com")
79
+ method_input_file = gr.Radio(choices=["gTTS", "Edge TTS"], label="Select method:", value="gTTS")
80
  audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
81
+ gr.Button("Convert").click(fn=txt_to_speech, inputs=[file_input, lang_input_file, tld_input_file, method_input_file], outputs=[audio_output_file, file_output_file])
82
 
83
  iface.launch(enable_queue=True)