Upload 6 files
Browse files- app.py +27 -14
- requirements.txt +4 -3
app.py
CHANGED
|
@@ -1,15 +1,17 @@
|
|
| 1 |
import time
|
| 2 |
import os
|
|
|
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import snapshot_download
|
| 6 |
-
from mira.model import MiraTTS
|
| 7 |
-
from mira.utils import split_text
|
| 8 |
|
| 9 |
# Model config
|
| 10 |
HF_MODEL_ID = "dolly-vn/Vira-TTS"
|
| 11 |
MODEL_PATH = "model_pretrained"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
def download_model_if_needed():
|
| 14 |
"""Download model from HuggingFace if not exists locally."""
|
| 15 |
if not os.path.exists(MODEL_PATH) or not os.listdir(MODEL_PATH):
|
|
@@ -23,18 +25,28 @@ def download_model_if_needed():
|
|
| 23 |
else:
|
| 24 |
print(f"✅ Model found at: {MODEL_PATH}")
|
| 25 |
|
| 26 |
-
# Download model
|
| 27 |
download_model_if_needed()
|
| 28 |
|
| 29 |
-
print("🔄 Loading Vira-TTS...")
|
| 30 |
-
mira_tts = MiraTTS(MODEL_PATH)
|
| 31 |
-
print("✅ Model loaded!")
|
| 32 |
-
|
| 33 |
SAMPLE_RATE = 48000
|
| 34 |
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def generate_speech(text: str, reference_audio: str):
|
| 37 |
"""Generate speech from text using reference audio for voice cloning."""
|
|
|
|
| 38 |
|
| 39 |
if not text.strip():
|
| 40 |
return None, "Vui lòng nhập văn bản."
|
|
@@ -43,8 +55,11 @@ def generate_speech(text: str, reference_audio: str):
|
|
| 43 |
return None, "Vui lòng upload file audio tham chiếu."
|
| 44 |
|
| 45 |
try:
|
|
|
|
|
|
|
|
|
|
| 46 |
# Encode reference audio
|
| 47 |
-
context_tokens =
|
| 48 |
|
| 49 |
# Split text into sentences
|
| 50 |
sentences = split_text(text)
|
|
@@ -53,11 +68,9 @@ def generate_speech(text: str, reference_audio: str):
|
|
| 53 |
start_time = time.time()
|
| 54 |
|
| 55 |
if len(sentences) == 1:
|
| 56 |
-
|
| 57 |
-
audio = mira_tts.generate(sentences[0], context_tokens)
|
| 58 |
else:
|
| 59 |
-
|
| 60 |
-
audio = mira_tts.batch_generate(sentences, [context_tokens])
|
| 61 |
|
| 62 |
inference_time = time.time() - start_time
|
| 63 |
|
|
@@ -66,13 +79,13 @@ def generate_speech(text: str, reference_audio: str):
|
|
| 66 |
audio_duration = len(audio_np) / SAMPLE_RATE
|
| 67 |
rtf = inference_time / audio_duration
|
| 68 |
|
| 69 |
-
# Create stats message
|
| 70 |
stats = f"📝 Số câu: {len(sentences)} | ⏱️ Inference: {inference_time:.2f}s | 🎵 Audio: {audio_duration:.2f}s | 📊 RTF: {rtf:.4f}"
|
| 71 |
|
| 72 |
return (SAMPLE_RATE, audio_np), stats
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
# Create Gradio interface
|
|
|
|
| 1 |
import time
|
| 2 |
import os
|
| 3 |
+
import spaces
|
| 4 |
import torch
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import snapshot_download
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Model config
|
| 9 |
HF_MODEL_ID = "dolly-vn/Vira-TTS"
|
| 10 |
MODEL_PATH = "model_pretrained"
|
| 11 |
|
| 12 |
+
# Global model variable
|
| 13 |
+
mira_tts = None
|
| 14 |
+
|
| 15 |
def download_model_if_needed():
|
| 16 |
"""Download model from HuggingFace if not exists locally."""
|
| 17 |
if not os.path.exists(MODEL_PATH) or not os.listdir(MODEL_PATH):
|
|
|
|
| 25 |
else:
|
| 26 |
print(f"✅ Model found at: {MODEL_PATH}")
|
| 27 |
|
| 28 |
+
# Download model at startup (no GPU needed)
|
| 29 |
download_model_if_needed()
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
SAMPLE_RATE = 48000
|
| 32 |
|
| 33 |
|
| 34 |
+
def get_model():
|
| 35 |
+
"""Lazy load model when GPU is available."""
|
| 36 |
+
global mira_tts
|
| 37 |
+
if mira_tts is None:
|
| 38 |
+
from mira.model import MiraTTS
|
| 39 |
+
from mira.utils import split_text
|
| 40 |
+
print("🔄 Loading Vira-TTS...")
|
| 41 |
+
mira_tts = MiraTTS(MODEL_PATH)
|
| 42 |
+
print("✅ Model loaded!")
|
| 43 |
+
return mira_tts
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@spaces.GPU
|
| 47 |
def generate_speech(text: str, reference_audio: str):
|
| 48 |
"""Generate speech from text using reference audio for voice cloning."""
|
| 49 |
+
from mira.utils import split_text
|
| 50 |
|
| 51 |
if not text.strip():
|
| 52 |
return None, "Vui lòng nhập văn bản."
|
|
|
|
| 55 |
return None, "Vui lòng upload file audio tham chiếu."
|
| 56 |
|
| 57 |
try:
|
| 58 |
+
# Get model (lazy load with GPU)
|
| 59 |
+
model = get_model()
|
| 60 |
+
|
| 61 |
# Encode reference audio
|
| 62 |
+
context_tokens = model.encode_audio(reference_audio)
|
| 63 |
|
| 64 |
# Split text into sentences
|
| 65 |
sentences = split_text(text)
|
|
|
|
| 68 |
start_time = time.time()
|
| 69 |
|
| 70 |
if len(sentences) == 1:
|
| 71 |
+
audio = model.generate(sentences[0], context_tokens)
|
|
|
|
| 72 |
else:
|
| 73 |
+
audio = model.batch_generate(sentences, [context_tokens])
|
|
|
|
| 74 |
|
| 75 |
inference_time = time.time() - start_time
|
| 76 |
|
|
|
|
| 79 |
audio_duration = len(audio_np) / SAMPLE_RATE
|
| 80 |
rtf = inference_time / audio_duration
|
| 81 |
|
|
|
|
| 82 |
stats = f"📝 Số câu: {len(sentences)} | ⏱️ Inference: {inference_time:.2f}s | 🎵 Audio: {audio_duration:.2f}s | 📊 RTF: {rtf:.4f}"
|
| 83 |
|
| 84 |
return (SAMPLE_RATE, audio_np), stats
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
+
import traceback
|
| 88 |
+
return None, f"Lỗi: {str(e)}\n{traceback.format_exc()}"
|
| 89 |
|
| 90 |
|
| 91 |
# Create Gradio interface
|
requirements.txt
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
--extra-index-url https://huggingface.github.io/lmdeploy-wheel-index/
|
| 2 |
|
|
|
|
| 3 |
lmdeploy
|
| 4 |
librosa
|
| 5 |
einops
|
|
@@ -8,8 +9,8 @@ gradio
|
|
| 8 |
soundfile
|
| 9 |
soe-vinorm
|
| 10 |
huggingface_hub
|
| 11 |
-
fastaudiosr @ https://github.com/ysharma3501/FlashSR/archive/refs/heads/master.zip
|
| 12 |
-
ncodec @ https://github.com/ysharma3501/FastBiCodec/archive/refs/heads/master.zip
|
| 13 |
torch
|
| 14 |
torchaudio
|
| 15 |
-
omegaconf
|
|
|
|
|
|
|
|
|
| 1 |
--extra-index-url https://huggingface.github.io/lmdeploy-wheel-index/
|
| 2 |
|
| 3 |
+
spaces
|
| 4 |
lmdeploy
|
| 5 |
librosa
|
| 6 |
einops
|
|
|
|
| 9 |
soundfile
|
| 10 |
soe-vinorm
|
| 11 |
huggingface_hub
|
|
|
|
|
|
|
| 12 |
torch
|
| 13 |
torchaudio
|
| 14 |
+
omegaconf
|
| 15 |
+
fastaudiosr @ https://github.com/ysharma3501/FlashSR/archive/refs/heads/master.zip
|
| 16 |
+
ncodec @ https://github.com/ysharma3501/FastBiCodec/archive/refs/heads/master.zip
|