ktvoice commited on
Commit
cd2ed09
·
verified ·
1 Parent(s): 17e4dc0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -8
app.py CHANGED
@@ -29,7 +29,7 @@ except Exception as e:
29
  # Danh sách giọng
30
  VOICE_SAMPLES = {
31
  "Tuyên (nam miền Bắc)": {"audio": "./sample/Tuyên (nam miền Bắc).wav", "text": "./sample/Tuyên (nam miền Bắc).txt"},
32
- "Thiện Tâm": {"audio": "./sample/thientam.mp3", "text": "./sample/thientam.txt"},
33
  "Ngọc Huyền": {"audio": "./sample/NgocHuyen.mp3", "text": "./sample/NgocHuyen.txt"},
34
  "Minh Quân": {"audio": "./sample/MinhQuan.mp3", "text": "./sample/MinhQuan.txt"},
35
  "Vĩnh (nam miền Nam)": {"audio": "./sample/Vĩnh (nam miền Nam).wav", "text": "./sample/Vĩnh (nam miền Nam).txt"},
@@ -58,12 +58,21 @@ def tts_process(text, voice_choice, custom_audio, custom_text, mode_tab, pause_l
58
  ref_path, ref_txt_val = custom_audio, custom_text
59
  else:
60
  sample = VOICE_SAMPLES.get(voice_choice)
 
 
61
  ref_path = sample["audio"]
62
  try:
63
  with open(sample["text"], "r", encoding="utf-8") as f:
64
  ref_txt_val = f.read()
65
- except:
66
- return None, "Lỗi đọc file text mẫu."
 
 
 
 
 
 
 
67
 
68
  # Xử lý ngắt nghỉ
69
  processed_text = text
@@ -74,9 +83,25 @@ def tts_process(text, voice_choice, custom_audio, custom_text, mode_tab, pause_l
74
 
75
  start_time = time.time()
76
 
77
- # Chạy AI
78
- ref_codes = tts.encode_reference(ref_path)
79
- wav = tts.infer(processed_text[:500], ref_codes, ref_txt_val)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  # Tốc độ
82
  if speed_value != 1.0:
@@ -84,10 +109,14 @@ def tts_process(text, voice_choice, custom_audio, custom_text, mode_tab, pause_l
84
 
85
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
86
  sf.write(tmp.name, wav, 24000)
87
- return tmp.name, f"Hoàn tất: {time.time() - start_time:.2f}s"
 
 
88
 
89
  except Exception as e:
90
- return None, f"Lỗi: {str(e)}"
 
 
91
 
92
  # --- 2. GIAO DIỆN CƠ BẢN (Native Gradio) ---
93
  with gr.Blocks(title="AI Voice") as demo:
 
29
  # Danh sách giọng
30
  VOICE_SAMPLES = {
31
  "Tuyên (nam miền Bắc)": {"audio": "./sample/Tuyên (nam miền Bắc).wav", "text": "./sample/Tuyên (nam miền Bắc).txt"},
32
+ "Thiện Tâm": {"audio": "./sample/ThienTam.mp3", "text": "./sample/ThienTam.txt"},
33
  "Ngọc Huyền": {"audio": "./sample/NgocHuyen.mp3", "text": "./sample/NgocHuyen.txt"},
34
  "Minh Quân": {"audio": "./sample/MinhQuan.mp3", "text": "./sample/MinhQuan.txt"},
35
  "Vĩnh (nam miền Nam)": {"audio": "./sample/Vĩnh (nam miền Nam).wav", "text": "./sample/Vĩnh (nam miền Nam).txt"},
 
58
  ref_path, ref_txt_val = custom_audio, custom_text
59
  else:
60
  sample = VOICE_SAMPLES.get(voice_choice)
61
+ if not sample:
62
+ return None, f"Lỗi: Không tìm thấy giọng '{voice_choice}'"
63
  ref_path = sample["audio"]
64
  try:
65
  with open(sample["text"], "r", encoding="utf-8") as f:
66
  ref_txt_val = f.read()
67
+ except Exception as e_txt:
68
+ return None, f"Lỗi đọc file text mẫu: {e_txt}"
69
+
70
+ # [DEBUG] Kiểm tra file audio tồn tại
71
+ if not os.path.exists(ref_path):
72
+ return None, f"Lỗi: File audio không tồn tại: {ref_path}"
73
+
74
+ file_size = os.path.getsize(ref_path)
75
+ print(f"[DEBUG] Voice: {voice_choice} | File: {ref_path} | Size: {file_size} bytes | Text mẫu: {ref_txt_val[:50]}...")
76
 
77
  # Xử lý ngắt nghỉ
78
  processed_text = text
 
83
 
84
  start_time = time.time()
85
 
86
+ # [DEBUG] Bước 1: Encode reference
87
+ print(f"[DEBUG] Bắt đầu encode_reference: {ref_path}")
88
+ try:
89
+ ref_codes = tts.encode_reference(ref_path)
90
+ print(f"[DEBUG] encode_reference thành công! Type: {type(ref_codes)}")
91
+ except Exception as e_enc:
92
+ import traceback
93
+ traceback.print_exc()
94
+ return None, f"Lỗi encode_reference: {type(e_enc).__name__}: {str(e_enc)}"
95
+
96
+ # [DEBUG] Bước 2: Infer
97
+ print(f"[DEBUG] Bắt đầu infer. Text length: {len(processed_text)}")
98
+ try:
99
+ wav = tts.infer(processed_text[:500], ref_codes, ref_txt_val)
100
+ print(f"[DEBUG] infer thành công! Wav shape: {wav.shape if hasattr(wav, 'shape') else len(wav)}")
101
+ except Exception as e_inf:
102
+ import traceback
103
+ traceback.print_exc()
104
+ return None, f"Lỗi infer: {type(e_inf).__name__}: {str(e_inf)}"
105
 
106
  # Tốc độ
107
  if speed_value != 1.0:
 
109
 
110
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
111
  sf.write(tmp.name, wav, 24000)
112
+ elapsed = time.time() - start_time
113
+ print(f"[DEBUG] Hoàn tất TTS: {elapsed:.2f}s | Output: {tmp.name}")
114
+ return tmp.name, f"Hoàn tất: {elapsed:.2f}s"
115
 
116
  except Exception as e:
117
+ import traceback
118
+ traceback.print_exc()
119
+ return None, f"Lỗi: {type(e).__name__}: {str(e)}"
120
 
121
  # --- 2. GIAO DIỆN CƠ BẢN (Native Gradio) ---
122
  with gr.Blocks(title="AI Voice") as demo: