Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -6,16 +6,72 @@ import os
|
|
| 6 |
import time
|
| 7 |
import sys
|
| 8 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
from muscriptor.transcription_model import TranscriptionModel
|
| 11 |
-
from muscriptor.events import NoteStartEvent, NoteEndEvent, ProgressEvent
|
| 12 |
-
from muscriptor.tokenizer.mt3 import MT3_FULL_PLUS_GROUP_NAMES
|
| 13 |
|
| 14 |
-
# Load the model at module scope — ZeroGPU intercepts .to("cuda") and packs
|
| 15 |
-
# weights to disk, streaming them into VRAM on the first @spaces.GPU call.
|
| 16 |
print("[muscriptor-space] Loading model...", file=sys.stderr, flush=True)
|
| 17 |
t0 = time.perf_counter()
|
| 18 |
-
model =
|
| 19 |
print(f"[muscriptor-space] Model loaded in {time.perf_counter() - t0:.1f}s", file=sys.stderr, flush=True)
|
| 20 |
|
| 21 |
# Build the instrument choices list (sorted by group ID for stable ordering)
|
|
|
|
| 6 |
import time
|
| 7 |
import sys
|
| 8 |
import io
|
| 9 |
+
import json
|
| 10 |
+
import re
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
from safetensors.torch import load_file
|
| 14 |
+
from huggingface_hub import hf_hub_download
|
| 15 |
+
|
| 16 |
+
from muscriptor.models.lm import LMModel, TorchAutocast
|
| 17 |
+
from muscriptor.modules.conditioners import (
|
| 18 |
+
MelSpectrogramConditioner,
|
| 19 |
+
ClassConditioner,
|
| 20 |
+
ConditioningProvider,
|
| 21 |
+
)
|
| 22 |
+
from muscriptor.tokenizer.mt3 import MT3Tokenizer, MT3_FULL_PLUS_GROUP_NAMES
|
| 23 |
+
from muscriptor.transcription_model import (
|
| 24 |
+
TranscriptionModel,
|
| 25 |
+
_resolve_source,
|
| 26 |
+
_resolve_config,
|
| 27 |
+
_remap_single_codebook_keys,
|
| 28 |
+
_build_model,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
_SAMPLE_RATE = 16000
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def load_model_zerogpu():
|
| 35 |
+
"""Load the MuScriptor model in a ZeroGPU-compatible way.
|
| 36 |
+
|
| 37 |
+
On ZeroGPU, safetensors.load_file(device="cuda") fails because there's no
|
| 38 |
+
real GPU at module scope. We load weights on CPU, build the model on CPU,
|
| 39 |
+
then call .to("cuda") — which ZeroGPU intercepts to pack weights to disk
|
| 40 |
+
and stream into VRAM on the first @spaces.GPU call.
|
| 41 |
+
"""
|
| 42 |
+
device = torch.device("cuda") # ZeroGPU intercepts this
|
| 43 |
+
|
| 44 |
+
source = _resolve_source("medium")
|
| 45 |
+
weights_path = Path(hf_hub_download(
|
| 46 |
+
repo_id="MuScriptor/muscriptor-medium",
|
| 47 |
+
filename="model.safetensors",
|
| 48 |
+
))
|
| 49 |
+
cfg = _resolve_config(source, weights_path)
|
| 50 |
+
|
| 51 |
+
# Build the model on CPU first
|
| 52 |
+
cpu_device = torch.device("cpu")
|
| 53 |
+
model = _build_model(cpu_device, cfg)
|
| 54 |
+
model.eval()
|
| 55 |
+
|
| 56 |
+
# Load weights on CPU
|
| 57 |
+
state_dict = load_file(str(weights_path), device="cpu")
|
| 58 |
+
state_dict = _remap_single_codebook_keys(state_dict)
|
| 59 |
+
model.load_state_dict(state_dict)
|
| 60 |
+
|
| 61 |
+
# Now move to "cuda" — ZeroGPU intercepts this and packs to disk
|
| 62 |
+
model.to("cuda")
|
| 63 |
+
|
| 64 |
+
tokenizer = MT3Tokenizer(
|
| 65 |
+
instrument_vocabulary="MT3_FULL_PLUS",
|
| 66 |
+
max_shift_steps=1001,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
return TranscriptionModel(model=model, tokenizer=tokenizer, device=device)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
|
|
|
|
|
|
|
| 72 |
print("[muscriptor-space] Loading model...", file=sys.stderr, flush=True)
|
| 73 |
t0 = time.perf_counter()
|
| 74 |
+
model = load_model_zerogpu()
|
| 75 |
print(f"[muscriptor-space] Model loaded in {time.perf_counter() - t0:.1f}s", file=sys.stderr, flush=True)
|
| 76 |
|
| 77 |
# Build the instrument choices list (sorted by group ID for stable ordering)
|