Spaces:
Running
on
Zero
Running
on
Zero
刘鑫
commited on
Commit
·
126f04d
1
Parent(s):
479ea03
set zero gpu inference
Browse files
app.py
CHANGED
|
@@ -8,23 +8,31 @@ from pathlib import Path
|
|
| 8 |
import tempfile
|
| 9 |
import soundfile as sf
|
| 10 |
|
| 11 |
-
# ========== Cache Configuration ==========
|
| 12 |
-
# Set cache directories BEFORE importing any model libraries
|
| 13 |
-
_cache_home = os.path.join(os.path.expanduser("~"), ".cache")
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Torch Hub cache (for some audio models like ZipEnhancer)
|
| 23 |
-
os.environ["TORCH_HOME"] = os.path.join(_cache_home, "torch")
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
os.makedirs(d, exist_ok=True)
|
| 28 |
|
| 29 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 30 |
if os.environ.get("HF_REPO_ID", "").strip() == "":
|
|
@@ -34,6 +42,56 @@ if os.environ.get("HF_REPO_ID", "").strip() == "":
|
|
| 34 |
_asr_model = None
|
| 35 |
_voxcpm_model = None
|
| 36 |
_default_local_model_dir = "./models/VoxCPM1.5"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
def _resolve_model_dir() -> str:
|
|
@@ -66,8 +124,12 @@ def get_asr_model():
|
|
| 66 |
"""Lazy load ASR model."""
|
| 67 |
global _asr_model
|
| 68 |
if _asr_model is None:
|
|
|
|
|
|
|
|
|
|
| 69 |
from funasr import AutoModel
|
| 70 |
print("Loading ASR model...")
|
|
|
|
| 71 |
_asr_model = AutoModel(
|
| 72 |
model="iic/SenseVoiceSmall", # ModelScope model ID
|
| 73 |
hub="ms", # Use ModelScope Hub
|
|
@@ -79,15 +141,48 @@ def get_asr_model():
|
|
| 79 |
return _asr_model
|
| 80 |
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
def get_voxcpm_model():
|
| 83 |
"""Lazy load VoxCPM model."""
|
| 84 |
global _voxcpm_model
|
| 85 |
if _voxcpm_model is None:
|
|
|
|
|
|
|
|
|
|
| 86 |
import voxcpm
|
| 87 |
print("Loading VoxCPM model...")
|
| 88 |
model_dir = _resolve_model_dir()
|
| 89 |
print(f"Using model dir: {model_dir}")
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
print("VoxCPM model loaded.")
|
| 92 |
return _voxcpm_model
|
| 93 |
|
|
|
|
| 8 |
import tempfile
|
| 9 |
import soundfile as sf
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def setup_cache_env():
|
| 13 |
+
"""
|
| 14 |
+
Setup cache environment variables.
|
| 15 |
+
Must be called in GPU worker context as well.
|
| 16 |
+
"""
|
| 17 |
+
_cache_home = os.path.join(os.path.expanduser("~"), ".cache")
|
| 18 |
+
|
| 19 |
+
# HuggingFace cache
|
| 20 |
+
os.environ["HF_HOME"] = os.path.join(_cache_home, "huggingface")
|
| 21 |
+
os.environ["HUGGINGFACE_HUB_CACHE"] = os.path.join(_cache_home, "huggingface", "hub")
|
| 22 |
+
|
| 23 |
+
# ModelScope cache (for FunASR SenseVoice)
|
| 24 |
+
os.environ["MODELSCOPE_CACHE"] = os.path.join(_cache_home, "modelscope")
|
| 25 |
+
|
| 26 |
+
# Torch Hub cache (for some audio models like ZipEnhancer)
|
| 27 |
+
os.environ["TORCH_HOME"] = os.path.join(_cache_home, "torch")
|
| 28 |
+
|
| 29 |
+
# Create cache directories
|
| 30 |
+
for d in [os.environ["HF_HOME"], os.environ["MODELSCOPE_CACHE"], os.environ["TORCH_HOME"]]:
|
| 31 |
+
os.makedirs(d, exist_ok=True)
|
| 32 |
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Setup cache in main process BEFORE any imports
|
| 35 |
+
setup_cache_env()
|
|
|
|
| 36 |
|
| 37 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 38 |
if os.environ.get("HF_REPO_ID", "").strip() == "":
|
|
|
|
| 42 |
_asr_model = None
|
| 43 |
_voxcpm_model = None
|
| 44 |
_default_local_model_dir = "./models/VoxCPM1.5"
|
| 45 |
+
_zipenhancer_local_path = None # Will be set after pre-download
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def predownload_models():
|
| 49 |
+
"""
|
| 50 |
+
Pre-download models at startup (runs in main process, not GPU worker).
|
| 51 |
+
This ensures models are cached before GPU functions are called.
|
| 52 |
+
"""
|
| 53 |
+
global _zipenhancer_local_path
|
| 54 |
+
|
| 55 |
+
print("=" * 50)
|
| 56 |
+
print("Pre-downloading models to cache...")
|
| 57 |
+
print(f"MODELSCOPE_CACHE={os.environ.get('MODELSCOPE_CACHE')}")
|
| 58 |
+
print(f"HF_HOME={os.environ.get('HF_HOME')}")
|
| 59 |
+
print("=" * 50)
|
| 60 |
+
|
| 61 |
+
# Pre-download ZipEnhancer from ModelScope
|
| 62 |
+
try:
|
| 63 |
+
from modelscope.hub.snapshot_download import snapshot_download as ms_snapshot_download
|
| 64 |
+
zipenhancer_model_id = "iic/speech_zipenhancer_ans_multiloss_16k_base"
|
| 65 |
+
print(f"Pre-downloading ZipEnhancer: {zipenhancer_model_id}")
|
| 66 |
+
_zipenhancer_local_path = ms_snapshot_download(
|
| 67 |
+
zipenhancer_model_id,
|
| 68 |
+
cache_dir=os.environ.get("MODELSCOPE_CACHE"),
|
| 69 |
+
)
|
| 70 |
+
print(f"ZipEnhancer downloaded to: {_zipenhancer_local_path}")
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Warning: Failed to pre-download ZipEnhancer: {e}")
|
| 73 |
+
_zipenhancer_local_path = None
|
| 74 |
+
|
| 75 |
+
# Pre-download ASR model (SenseVoice) from ModelScope
|
| 76 |
+
try:
|
| 77 |
+
from modelscope.hub.snapshot_download import snapshot_download as ms_snapshot_download
|
| 78 |
+
asr_model_id = "iic/SenseVoiceSmall"
|
| 79 |
+
print(f"Pre-downloading ASR model: {asr_model_id}")
|
| 80 |
+
asr_local_path = ms_snapshot_download(
|
| 81 |
+
asr_model_id,
|
| 82 |
+
cache_dir=os.environ.get("MODELSCOPE_CACHE"),
|
| 83 |
+
)
|
| 84 |
+
print(f"ASR model downloaded to: {asr_local_path}")
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Warning: Failed to pre-download ASR model: {e}")
|
| 87 |
+
|
| 88 |
+
print("=" * 50)
|
| 89 |
+
print("Model pre-download complete!")
|
| 90 |
+
print("=" * 50)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# Run pre-download at startup
|
| 94 |
+
predownload_models()
|
| 95 |
|
| 96 |
|
| 97 |
def _resolve_model_dir() -> str:
|
|
|
|
| 124 |
"""Lazy load ASR model."""
|
| 125 |
global _asr_model
|
| 126 |
if _asr_model is None:
|
| 127 |
+
# Setup cache env in GPU worker context
|
| 128 |
+
setup_cache_env()
|
| 129 |
+
|
| 130 |
from funasr import AutoModel
|
| 131 |
print("Loading ASR model...")
|
| 132 |
+
print(f" MODELSCOPE_CACHE={os.environ.get('MODELSCOPE_CACHE')}")
|
| 133 |
_asr_model = AutoModel(
|
| 134 |
model="iic/SenseVoiceSmall", # ModelScope model ID
|
| 135 |
hub="ms", # Use ModelScope Hub
|
|
|
|
| 141 |
return _asr_model
|
| 142 |
|
| 143 |
|
| 144 |
+
def _get_zipenhancer_local_path():
|
| 145 |
+
"""
|
| 146 |
+
Get ZipEnhancer local path from ModelScope cache.
|
| 147 |
+
This works in both main process and GPU worker.
|
| 148 |
+
"""
|
| 149 |
+
setup_cache_env()
|
| 150 |
+
try:
|
| 151 |
+
from modelscope.hub.snapshot_download import snapshot_download as ms_snapshot_download
|
| 152 |
+
zipenhancer_model_id = "iic/speech_zipenhancer_ans_multiloss_16k_base"
|
| 153 |
+
# This will use cache if already downloaded
|
| 154 |
+
local_path = ms_snapshot_download(
|
| 155 |
+
zipenhancer_model_id,
|
| 156 |
+
cache_dir=os.environ.get("MODELSCOPE_CACHE"),
|
| 157 |
+
)
|
| 158 |
+
return local_path
|
| 159 |
+
except Exception as e:
|
| 160 |
+
print(f"Warning: Failed to get ZipEnhancer path: {e}")
|
| 161 |
+
return "iic/speech_zipenhancer_ans_multiloss_16k_base"
|
| 162 |
+
|
| 163 |
+
|
| 164 |
def get_voxcpm_model():
|
| 165 |
"""Lazy load VoxCPM model."""
|
| 166 |
global _voxcpm_model
|
| 167 |
if _voxcpm_model is None:
|
| 168 |
+
# Setup cache env in GPU worker context
|
| 169 |
+
setup_cache_env()
|
| 170 |
+
|
| 171 |
import voxcpm
|
| 172 |
print("Loading VoxCPM model...")
|
| 173 |
model_dir = _resolve_model_dir()
|
| 174 |
print(f"Using model dir: {model_dir}")
|
| 175 |
+
|
| 176 |
+
# Get ZipEnhancer local path (uses cache if pre-downloaded)
|
| 177 |
+
zipenhancer_path = _get_zipenhancer_local_path()
|
| 178 |
+
print(f"ZipEnhancer path: {zipenhancer_path}")
|
| 179 |
+
|
| 180 |
+
_voxcpm_model = voxcpm.VoxCPM(
|
| 181 |
+
voxcpm_model_path=model_dir,
|
| 182 |
+
optimize=False,
|
| 183 |
+
enable_denoiser=True,
|
| 184 |
+
zipenhancer_model_path=zipenhancer_path,
|
| 185 |
+
)
|
| 186 |
print("VoxCPM model loaded.")
|
| 187 |
return _voxcpm_model
|
| 188 |
|