Upload app.py
Browse files
app.py
CHANGED
|
@@ -8,11 +8,11 @@ import librosa
|
|
| 8 |
import time
|
| 9 |
from tts_engine import VoiceEngine
|
| 10 |
|
| 11 |
-
# --- 1. KHỞI TẠO
|
| 12 |
os.environ['SPACES_ZERO_GPU'] = '1'
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
| 15 |
-
print(f"🔄 Đang khởi động
|
| 16 |
|
| 17 |
try:
|
| 18 |
tts = VoiceEngine(
|
|
@@ -26,7 +26,7 @@ except Exception as e:
|
|
| 26 |
print(f"❌ Lỗi nạp mô hình: {e}")
|
| 27 |
tts = None
|
| 28 |
|
| 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"},
|
|
@@ -43,16 +43,16 @@ VOICE_SAMPLES = {
|
|
| 43 |
@spaces.GPU(duration=60)
|
| 44 |
def tts_process(text, voice_choice, custom_audio, custom_text, mode_tab, pause_level, speed_value):
|
| 45 |
if tts is None:
|
| 46 |
-
return None, "
|
| 47 |
|
| 48 |
if not text or not text.strip():
|
| 49 |
-
return None, "
|
| 50 |
|
| 51 |
try:
|
| 52 |
# Chọn nguồn giọng
|
| 53 |
if mode_tab == "custom":
|
| 54 |
-
if not custom_audio: return None, "
|
| 55 |
-
if not custom_text: return None, "
|
| 56 |
ref_path, ref_txt_val = custom_audio, custom_text
|
| 57 |
else:
|
| 58 |
sample = VOICE_SAMPLES.get(voice_choice)
|
|
@@ -61,7 +61,7 @@ def tts_process(text, voice_choice, custom_audio, custom_text, mode_tab, pause_l
|
|
| 61 |
with open(sample["text"], "r", encoding="utf-8") as f:
|
| 62 |
ref_txt_val = f.read()
|
| 63 |
except:
|
| 64 |
-
return None, "
|
| 65 |
|
| 66 |
# Xử lý ngắt nghỉ
|
| 67 |
processed_text = text
|
|
@@ -76,11 +76,10 @@ def tts_process(text, voice_choice, custom_audio, custom_text, mode_tab, pause_l
|
|
| 76 |
ref_codes = tts.encode_reference(ref_path)
|
| 77 |
wav = tts.infer(processed_text[:500], ref_codes, ref_txt_val)
|
| 78 |
|
| 79 |
-
#
|
| 80 |
if speed_value != 1.0:
|
| 81 |
wav = librosa.effects.time_stretch(wav, rate=float(speed_value))
|
| 82 |
|
| 83 |
-
# Xuất file
|
| 84 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 85 |
sf.write(tmp.name, wav, 24000)
|
| 86 |
return tmp.name, f"Hoàn tất: {time.time() - start_time:.2f}s"
|
|
@@ -88,17 +87,16 @@ def tts_process(text, voice_choice, custom_audio, custom_text, mode_tab, pause_l
|
|
| 88 |
except Exception as e:
|
| 89 |
return None, f"Lỗi: {str(e)}"
|
| 90 |
|
| 91 |
-
# --- 2. GIAO DIỆN CƠ BẢN (
|
| 92 |
with gr.Blocks(title="AI Voice") as demo:
|
| 93 |
-
gr.Markdown("#
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
with gr.Column():
|
| 97 |
-
# Khu vực nhập liệu
|
| 98 |
input_text = gr.Textbox(
|
| 99 |
label="Văn bản cần đọc",
|
| 100 |
lines=5,
|
| 101 |
-
placeholder="Nhập tiếng Việt..."
|
| 102 |
)
|
| 103 |
|
| 104 |
with gr.Tabs() as tabs:
|
|
@@ -120,11 +118,9 @@ with gr.Blocks(title="AI Voice") as demo:
|
|
| 120 |
gen_btn = gr.Button("ĐỌC NGAY", variant="primary")
|
| 121 |
|
| 122 |
with gr.Column():
|
| 123 |
-
# Khu vực kết quả
|
| 124 |
output_audio = gr.Audio(label="Kết quả", interactive=False)
|
| 125 |
output_status = gr.Textbox(label="Trạng thái", interactive=False)
|
| 126 |
|
| 127 |
-
# Logic sự kiện
|
| 128 |
tabs.children[0].select(lambda: "preset", None, active_tab)
|
| 129 |
tabs.children[1].select(lambda: "custom", None, active_tab)
|
| 130 |
|
|
@@ -135,5 +131,10 @@ with gr.Blocks(title="AI Voice") as demo:
|
|
| 135 |
)
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
-
#
|
| 139 |
-
demo.queue().launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import time
|
| 9 |
from tts_engine import VoiceEngine
|
| 10 |
|
| 11 |
+
# --- 1. KHỞI TẠO (Giữ nguyên logic nạp model đã thành công) ---
|
| 12 |
os.environ['SPACES_ZERO_GPU'] = '1'
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
| 15 |
+
print(f"🔄 Đang khởi động trên: {device}")
|
| 16 |
|
| 17 |
try:
|
| 18 |
tts = VoiceEngine(
|
|
|
|
| 26 |
print(f"❌ Lỗi nạp mô hình: {e}")
|
| 27 |
tts = None
|
| 28 |
|
| 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"},
|
|
|
|
| 43 |
@spaces.GPU(duration=60)
|
| 44 |
def tts_process(text, voice_choice, custom_audio, custom_text, mode_tab, pause_level, speed_value):
|
| 45 |
if tts is None:
|
| 46 |
+
return None, "Hệ thống chưa sẵn sàng."
|
| 47 |
|
| 48 |
if not text or not text.strip():
|
| 49 |
+
return None, "Vui lòng nhập văn bản."
|
| 50 |
|
| 51 |
try:
|
| 52 |
# Chọn nguồn giọng
|
| 53 |
if mode_tab == "custom":
|
| 54 |
+
if not custom_audio: return None, "Thiếu Audio mẫu."
|
| 55 |
+
if not custom_text: return None, "Thiếu lời thoại mẫu."
|
| 56 |
ref_path, ref_txt_val = custom_audio, custom_text
|
| 57 |
else:
|
| 58 |
sample = VOICE_SAMPLES.get(voice_choice)
|
|
|
|
| 61 |
with open(sample["text"], "r", encoding="utf-8") as f:
|
| 62 |
ref_txt_val = f.read()
|
| 63 |
except:
|
| 64 |
+
return None, "Lỗi đọc file text mẫu."
|
| 65 |
|
| 66 |
# Xử lý ngắt nghỉ
|
| 67 |
processed_text = text
|
|
|
|
| 76 |
ref_codes = tts.encode_reference(ref_path)
|
| 77 |
wav = tts.infer(processed_text[:500], ref_codes, ref_txt_val)
|
| 78 |
|
| 79 |
+
# Tốc độ
|
| 80 |
if speed_value != 1.0:
|
| 81 |
wav = librosa.effects.time_stretch(wav, rate=float(speed_value))
|
| 82 |
|
|
|
|
| 83 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 84 |
sf.write(tmp.name, wav, 24000)
|
| 85 |
return tmp.name, f"Hoàn tất: {time.time() - start_time:.2f}s"
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
return None, f"Lỗi: {str(e)}"
|
| 89 |
|
| 90 |
+
# --- 2. GIAO DIỆN CƠ BẢN (Native Gradio) ---
|
| 91 |
with gr.Blocks(title="AI Voice") as demo:
|
| 92 |
+
gr.Markdown("# 🎙️ AI Voice Studio")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
with gr.Column():
|
|
|
|
| 96 |
input_text = gr.Textbox(
|
| 97 |
label="Văn bản cần đọc",
|
| 98 |
lines=5,
|
| 99 |
+
placeholder="Nhập tiếng Việt vào đây..."
|
| 100 |
)
|
| 101 |
|
| 102 |
with gr.Tabs() as tabs:
|
|
|
|
| 118 |
gen_btn = gr.Button("ĐỌC NGAY", variant="primary")
|
| 119 |
|
| 120 |
with gr.Column():
|
|
|
|
| 121 |
output_audio = gr.Audio(label="Kết quả", interactive=False)
|
| 122 |
output_status = gr.Textbox(label="Trạng thái", interactive=False)
|
| 123 |
|
|
|
|
| 124 |
tabs.children[0].select(lambda: "preset", None, active_tab)
|
| 125 |
tabs.children[1].select(lambda: "custom", None, active_tab)
|
| 126 |
|
|
|
|
| 131 |
)
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
+
# Cấu hình chuẩn để tránh mọi cảnh báo và lỗi
|
| 135 |
+
demo.queue().launch(
|
| 136 |
+
server_name="0.0.0.0",
|
| 137 |
+
server_port=7860,
|
| 138 |
+
ssr_mode=False,
|
| 139 |
+
theme=gr.themes.Default()
|
| 140 |
+
)
|