Spaces:
Running on Zero
Running on Zero
deploy: switch to chatterbox requirements @ 035108d
Browse files- steps/s1b_separate.py +10 -11
steps/s1b_separate.py
CHANGED
|
@@ -16,7 +16,6 @@ import spaces
|
|
| 16 |
|
| 17 |
|
| 18 |
_MODEL = None
|
| 19 |
-
_MODEL_DEVICE = None
|
| 20 |
|
| 21 |
|
| 22 |
def _select_device() -> str:
|
|
@@ -30,19 +29,15 @@ def _select_device() -> str:
|
|
| 30 |
def _get_model():
|
| 31 |
"""Lazy-load htdemucs once per process. Module-level semantics; we load
|
| 32 |
on first call so the import itself stays cheap on non-GPU envs."""
|
| 33 |
-
global _MODEL
|
| 34 |
if _MODEL is None:
|
| 35 |
from demucs.pretrained import get_model
|
| 36 |
-
|
| 37 |
-
print(f"[s1b] Loading htdemucs on {device}...")
|
| 38 |
model = get_model("htdemucs")
|
| 39 |
model.eval()
|
| 40 |
-
|
| 41 |
-
# outside a @spaces.GPU scope. On Mac, this is MPS or CPU.
|
| 42 |
-
model.to(device)
|
| 43 |
_MODEL = model
|
| 44 |
-
|
| 45 |
-
return _MODEL, _MODEL_DEVICE
|
| 46 |
|
| 47 |
|
| 48 |
@spaces.GPU(duration=120)
|
|
@@ -50,7 +45,10 @@ def _apply_demucs(mix: torch.Tensor, device: str) -> torch.Tensor:
|
|
| 50 |
"""GPU-bound inference call. `mix` shape: [1, channels, time]."""
|
| 51 |
from demucs.apply import apply_model
|
| 52 |
|
| 53 |
-
model
|
|
|
|
|
|
|
|
|
|
| 54 |
with torch.no_grad():
|
| 55 |
# apply_model returns [batch, sources, channels, time]
|
| 56 |
sources = apply_model(
|
|
@@ -99,7 +97,8 @@ def separate_audio(
|
|
| 99 |
out = Path(output_dir)
|
| 100 |
out.mkdir(parents=True, exist_ok=True)
|
| 101 |
|
| 102 |
-
model
|
|
|
|
| 103 |
target_sr = model.samplerate
|
| 104 |
target_ch = model.audio_channels
|
| 105 |
source_names = list(model.sources)
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
_MODEL = None
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
def _select_device() -> str:
|
|
|
|
| 29 |
def _get_model():
|
| 30 |
"""Lazy-load htdemucs once per process. Module-level semantics; we load
|
| 31 |
on first call so the import itself stays cheap on non-GPU envs."""
|
| 32 |
+
global _MODEL
|
| 33 |
if _MODEL is None:
|
| 34 |
from demucs.pretrained import get_model
|
| 35 |
+
print("[s1b] Loading htdemucs on cpu...")
|
|
|
|
| 36 |
model = get_model("htdemucs")
|
| 37 |
model.eval()
|
| 38 |
+
model.to("cpu")
|
|
|
|
|
|
|
| 39 |
_MODEL = model
|
| 40 |
+
return _MODEL
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
@spaces.GPU(duration=120)
|
|
|
|
| 45 |
"""GPU-bound inference call. `mix` shape: [1, channels, time]."""
|
| 46 |
from demucs.apply import apply_model
|
| 47 |
|
| 48 |
+
model = _get_model()
|
| 49 |
+
if next(model.parameters()).device.type != device:
|
| 50 |
+
print(f"[s1b] Moving htdemucs to {device} inside GPU scope...")
|
| 51 |
+
model = model.to(device)
|
| 52 |
with torch.no_grad():
|
| 53 |
# apply_model returns [batch, sources, channels, time]
|
| 54 |
sources = apply_model(
|
|
|
|
| 97 |
out = Path(output_dir)
|
| 98 |
out.mkdir(parents=True, exist_ok=True)
|
| 99 |
|
| 100 |
+
model = _get_model()
|
| 101 |
+
device = _select_device()
|
| 102 |
target_sr = model.samplerate
|
| 103 |
target_ch = model.audio_channels
|
| 104 |
source_names = list(model.sources)
|