Spaces:
Runtime error
Runtime error
Commit
·
0133704
1
Parent(s):
4a204a9
Add @spaces.GPU decorator for GPU Zero
Browse files
app.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import soundfile as sf
|
| 4 |
-
from vieneutts import VieNeuTTS
|
| 5 |
import tempfile
|
| 6 |
-
import os
|
| 7 |
import torch
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Khởi tạo model trên CPU trước
|
| 11 |
print("📦 Đang tải model...")
|
|
@@ -29,7 +33,7 @@ VOICE_SAMPLES = {
|
|
| 29 |
}
|
| 30 |
}
|
| 31 |
|
| 32 |
-
@spaces.GPU(duration=120) # Giữ GPU trong 120 giây
|
| 33 |
def synthesize_speech(text, voice_choice, custom_audio=None, custom_text=None):
|
| 34 |
"""
|
| 35 |
Tổng hợp giọng nói từ văn bản - Chạy trên GPU
|
|
@@ -39,7 +43,7 @@ def synthesize_speech(text, voice_choice, custom_audio=None, custom_text=None):
|
|
| 39 |
if not text or text.strip() == "":
|
| 40 |
return None, "❌ Vui lòng nhập văn bản cần tổng hợp"
|
| 41 |
|
| 42 |
-
# Giới hạn độ dài text
|
| 43 |
if len(text) > 500:
|
| 44 |
return None, "❌ Văn bản quá dài! Vui lòng nhập tối đa 500 ký tự"
|
| 45 |
|
|
@@ -55,7 +59,7 @@ def synthesize_speech(text, voice_choice, custom_audio=None, custom_text=None):
|
|
| 55 |
else:
|
| 56 |
return None, "❌ Vui lòng chọn giọng hoặc tải lên audio tùy chỉnh"
|
| 57 |
|
| 58 |
-
# Di chuyển model lên GPU
|
| 59 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 60 |
if device == "cuda":
|
| 61 |
print("🚀 Đang chuyển model lên GPU...")
|
|
@@ -70,7 +74,7 @@ def synthesize_speech(text, voice_choice, custom_audio=None, custom_text=None):
|
|
| 70 |
print(f"🎵 Đang tổng hợp giọng nói trên {device.upper()}...")
|
| 71 |
wav = tts.infer(text, ref_codes, ref_text)
|
| 72 |
|
| 73 |
-
# Di chuyển model về CPU
|
| 74 |
if device == "cuda":
|
| 75 |
print("💾 Đang giải phóng GPU...")
|
| 76 |
tts.backbone = tts.backbone.to("cpu")
|
|
@@ -90,7 +94,7 @@ def synthesize_speech(text, voice_choice, custom_audio=None, custom_text=None):
|
|
| 90 |
import traceback
|
| 91 |
traceback.print_exc()
|
| 92 |
|
| 93 |
-
#
|
| 94 |
try:
|
| 95 |
if torch.cuda.is_available():
|
| 96 |
tts.backbone = tts.backbone.to("cpu")
|
|
@@ -101,7 +105,7 @@ def synthesize_speech(text, voice_choice, custom_audio=None, custom_text=None):
|
|
| 101 |
|
| 102 |
return None, f"❌ Lỗi: {str(e)}"
|
| 103 |
|
| 104 |
-
# Các ví dụ mẫu
|
| 105 |
examples = [
|
| 106 |
["Xin chào, tôi là trợ lý giọng nói tiếng Việt.", "Nam miền Nam"],
|
| 107 |
["Chúc bạn một ngày tốt lành!", "Nữ miền Nam"],
|
|
@@ -137,15 +141,6 @@ with gr.Blocks(title="VieNeu-TTS", css=custom_css, theme=gr.themes.Soft()) as de
|
|
| 137 |
Hệ thống tổng hợp tiếng nói tiếng Việt sử dụng Large Language Model và Neural Codec.
|
| 138 |
""")
|
| 139 |
|
| 140 |
-
# Thông báo GPU Zero
|
| 141 |
-
gr.Markdown("""
|
| 142 |
-
<div id="info">
|
| 143 |
-
⚡ <strong>GPU Zero Mode:</strong> Space này sử dụng GPU miễn phí với tốc độ nhanh (3-5 giây/câu).
|
| 144 |
-
<br>
|
| 145 |
-
⏳ Lần đầu sử dụng có thể phải chờ 5-30 phút trong hàng đợi GPU. Sau đó sẽ rất nhanh!
|
| 146 |
-
</div>
|
| 147 |
-
""")
|
| 148 |
-
|
| 149 |
with gr.Row():
|
| 150 |
with gr.Column():
|
| 151 |
# Input text
|
|
@@ -194,7 +189,7 @@ with gr.Blocks(title="VieNeu-TTS", css=custom_css, theme=gr.themes.Soft()) as de
|
|
| 194 |
inputs=[text_input, voice_select],
|
| 195 |
outputs=[audio_output, status_output],
|
| 196 |
fn=synthesize_speech,
|
| 197 |
-
cache_examples=False
|
| 198 |
)
|
| 199 |
|
| 200 |
# Update character count
|
|
@@ -219,18 +214,7 @@ with gr.Blocks(title="VieNeu-TTS", css=custom_css, theme=gr.themes.Soft()) as de
|
|
| 219 |
gr.Markdown("""
|
| 220 |
---
|
| 221 |
### 📌 Thông tin
|
| 222 |
-
|
| 223 |
-
**Ưu điểm GPU Zero:**
|
| 224 |
-
- ⚡ Tốc độ: 3-5 giây/câu (nhanh hơn CPU 10-20 lần)
|
| 225 |
-
- 💰 Hoàn toàn miễn phí
|
| 226 |
-
- 🎯 Chất lượng âm thanh cao
|
| 227 |
-
|
| 228 |
-
**Lưu ý:**
|
| 229 |
-
- ⏳ Lần đầu sử dụng phải chờ GPU khả dụng (5-30 phút)
|
| 230 |
-
- 🔄 Sau đó các request tiếp theo sẽ rất nhanh
|
| 231 |
-
- ⏱️ GPU timeout sau 120 giây nếu không dùng
|
| 232 |
-
- 📏 Khuyến nghị: Văn bản dưới 500 ký tự
|
| 233 |
-
|
| 234 |
**Liên kết:**
|
| 235 |
- [GitHub Repository](https://github.com/pnnbao97/VieNeu-TTS)
|
| 236 |
- [Model Card](https://huggingface.co/pnnbao-ump/VieNeu-TTS)
|
|
@@ -240,7 +224,7 @@ with gr.Blocks(title="VieNeu-TTS", css=custom_css, theme=gr.themes.Soft()) as de
|
|
| 240 |
|
| 241 |
# Launch
|
| 242 |
if __name__ == "__main__":
|
| 243 |
-
demo.queue(max_size=20)
|
| 244 |
demo.launch(
|
| 245 |
server_name="0.0.0.0",
|
| 246 |
server_port=7860,
|
|
|
|
| 1 |
+
import spaces # PHẢI import TRƯỚC mọi thứ!
|
| 2 |
+
import os
|
| 3 |
+
os.environ['SPACES_ZERO_GPU'] = '1' # Set environment variable
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
import soundfile as sf
|
|
|
|
| 8 |
import tempfile
|
|
|
|
| 9 |
import torch
|
| 10 |
+
|
| 11 |
+
# Import vieneutts SAU khi đã setup spaces
|
| 12 |
+
from vieneutts import VieNeuTTS
|
| 13 |
|
| 14 |
# Khởi tạo model trên CPU trước
|
| 15 |
print("📦 Đang tải model...")
|
|
|
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
+
@spaces.GPU(duration=120) # Giữ GPU trong 120 giây
|
| 37 |
def synthesize_speech(text, voice_choice, custom_audio=None, custom_text=None):
|
| 38 |
"""
|
| 39 |
Tổng hợp giọng nói từ văn bản - Chạy trên GPU
|
|
|
|
| 43 |
if not text or text.strip() == "":
|
| 44 |
return None, "❌ Vui lòng nhập văn bản cần tổng hợp"
|
| 45 |
|
| 46 |
+
# Giới hạn độ dài text
|
| 47 |
if len(text) > 500:
|
| 48 |
return None, "❌ Văn bản quá dài! Vui lòng nhập tối đa 500 ký tự"
|
| 49 |
|
|
|
|
| 59 |
else:
|
| 60 |
return None, "❌ Vui lòng chọn giọng hoặc tải lên audio tùy chỉnh"
|
| 61 |
|
| 62 |
+
# Di chuyển model lên GPU
|
| 63 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 64 |
if device == "cuda":
|
| 65 |
print("🚀 Đang chuyển model lên GPU...")
|
|
|
|
| 74 |
print(f"🎵 Đang tổng hợp giọng nói trên {device.upper()}...")
|
| 75 |
wav = tts.infer(text, ref_codes, ref_text)
|
| 76 |
|
| 77 |
+
# Di chuyển model về CPU
|
| 78 |
if device == "cuda":
|
| 79 |
print("💾 Đang giải phóng GPU...")
|
| 80 |
tts.backbone = tts.backbone.to("cpu")
|
|
|
|
| 94 |
import traceback
|
| 95 |
traceback.print_exc()
|
| 96 |
|
| 97 |
+
# Giải phóng GPU khi có lỗi
|
| 98 |
try:
|
| 99 |
if torch.cuda.is_available():
|
| 100 |
tts.backbone = tts.backbone.to("cpu")
|
|
|
|
| 105 |
|
| 106 |
return None, f"❌ Lỗi: {str(e)}"
|
| 107 |
|
| 108 |
+
# Các ví dụ mẫu
|
| 109 |
examples = [
|
| 110 |
["Xin chào, tôi là trợ lý giọng nói tiếng Việt.", "Nam miền Nam"],
|
| 111 |
["Chúc bạn một ngày tốt lành!", "Nữ miền Nam"],
|
|
|
|
| 141 |
Hệ thống tổng hợp tiếng nói tiếng Việt sử dụng Large Language Model và Neural Codec.
|
| 142 |
""")
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
with gr.Row():
|
| 145 |
with gr.Column():
|
| 146 |
# Input text
|
|
|
|
| 189 |
inputs=[text_input, voice_select],
|
| 190 |
outputs=[audio_output, status_output],
|
| 191 |
fn=synthesize_speech,
|
| 192 |
+
cache_examples=False
|
| 193 |
)
|
| 194 |
|
| 195 |
# Update character count
|
|
|
|
| 214 |
gr.Markdown("""
|
| 215 |
---
|
| 216 |
### 📌 Thông tin
|
| 217 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
**Liên kết:**
|
| 219 |
- [GitHub Repository](https://github.com/pnnbao97/VieNeu-TTS)
|
| 220 |
- [Model Card](https://huggingface.co/pnnbao-ump/VieNeu-TTS)
|
|
|
|
| 224 |
|
| 225 |
# Launch
|
| 226 |
if __name__ == "__main__":
|
| 227 |
+
demo.queue(max_size=20)
|
| 228 |
demo.launch(
|
| 229 |
server_name="0.0.0.0",
|
| 230 |
server_port=7860,
|