Mynameisju commited on
Commit
08ec91c
·
verified ·
1 Parent(s): 989d165

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -29
app.py CHANGED
@@ -1,29 +1,48 @@
1
- import os
2
- import subprocess
3
- import gradio as gr
4
-
5
- def ipa_to_speech(ipa_input):
6
- ipa_input = ipa_input.strip()
7
- if not ipa_input.startswith("/"):
8
- ipa_input = "/" + ipa_input
9
- if not ipa_input.endswith("/"):
10
- ipa_input = ipa_input + "/"
11
-
12
- output_path = "output.wav"
13
-
14
- result = subprocess.run([
15
- "espeak-ng", "-v", "en", "-q", ipa_input, "-w", output_path
16
- ], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
17
-
18
- if result.returncode != 0 or not os.path.exists(output_path) or os.path.getsize(output_path) < 500:
19
- return f"❌ Lỗi tạo âm thanh: {result.stderr.decode()}"
20
-
21
- return output_path
22
-
23
- gr.Interface(
24
- fn=ipa_to_speech,
25
- inputs=gr.Textbox(label="IPA input (ví dụ: /tɕao1/)"),
26
- outputs=gr.Audio(type="filepath", label="Giọng đọc"),
27
- title="TTS IPA bằng eSpeak NG",
28
- description="Nhập chuỗi IPA để tạo giọng nói bằng eSpeak NG"
29
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import os
4
+
5
+ def ipa_to_speech(ipa_input):
6
+ text = ipa_input.strip().replace("/", "") # Loại dấu / ở đầu và cuối
7
+
8
+ if not text:
9
+ return "Lỗi: Nhập IPA rỗng", None
10
+
11
+ output_path = "output.wav"
12
+
13
+ try:
14
+ # Dùng voice test để đọc IPA như là text
15
+ result = subprocess.run(
16
+ ["espeak-ng", "-v", "test", "-q", "-w", output_path, text],
17
+ check=True,
18
+ stdout=subprocess.PIPE,
19
+ stderr=subprocess.PIPE
20
+ )
21
+
22
+ if not os.path.exists(output_path):
23
+ return "Lỗi: Không tạo được file âm thanh", None
24
+
25
+ if os.path.getsize(output_path) == 0:
26
+ return "Lỗi: File âm thanh rỗng", None
27
+
28
+ return " Thành công!", output_path
29
+
30
+ except subprocess.CalledProcessError as e:
31
+ return f"Lỗi khi chạy espeak-ng:\n{e.stderr.decode()}", None
32
+ except Exception as e:
33
+ return f"Lỗi không xác định: {str(e)}", None
34
+
35
+
36
+ iface = gr.Interface(
37
+ fn=ipa_to_speech,
38
+ inputs=gr.Textbox(label="IPA input (ví dụ: /qazaʃa/)"),
39
+ outputs=[
40
+ gr.Textbox(label="Thông báo"),
41
+ gr.Audio(label="Kết quả")
42
+ ],
43
+ title="TTS IPA bằng eSpeak NG",
44
+ description="Nhập chuỗi IPA để tạo giọng nói bằng eSpeak NG"
45
+ )
46
+
47
+ if __name__ == "__main__":
48
+ iface.launch()