File size: 11,386 Bytes
77a45c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c7ad29
 
77a45c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d2b17a
10d5a4b
77a45c9
 
 
 
e540626
 
 
 
 
 
77a45c9
 
 
 
18a2a2e
77a45c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18a2a2e
77a45c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e540626
 
 
 
77a45c9
 
 
 
 
 
 
 
 
 
 
 
e540626
 
 
 
 
 
77a45c9
 
 
 
18a2a2e
 
 
 
 
 
 
 
 
 
 
 
 
77a45c9
 
 
 
 
 
 
 
 
 
 
 
 
 
00b55ef
77a45c9
 
18a2a2e
77a45c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18a2a2e
 
 
 
 
 
 
77a45c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10d5a4b
77a45c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10d5a4b
77a45c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from __future__ import annotations

import os
import sys
import tempfile
import threading
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Tuple

try:
    import spaces
except ImportError:
    class _SpacesShim:
        @staticmethod
        def GPU(*_args, **_kwargs):
            def decorator(fn):
                return fn

            return decorator

    spaces = _SpacesShim()

import gradio as gr
import torch
from huggingface_hub import hf_hub_download, snapshot_download


APP_ROOT = Path(__file__).resolve().parent
HEARTLIB_SRC = APP_ROOT / "heartlib" / "src"
HEARTLIB_PACKAGE_INIT = HEARTLIB_SRC / "heartlib" / "__init__.py"
if HEARTLIB_PACKAGE_INIT.is_file():
    sys.path.insert(0, str(HEARTLIB_SRC))

from heartlib import HeartMuLaGenPipeline


@dataclass(frozen=True)
class ModelConfig:
    version: str
    generator_repo: str
    mula_repo: str
    codec_repo: str
    mula_dirname: str
    codec_dirname: str


MODEL_CONFIG = ModelConfig(
    version=os.getenv("HEARTMULA_VERSION", "3B"),
    generator_repo=os.getenv("HEARTMULA_GENERATOR_REPO", "HeartMuLa/HeartMuLaGen"),
    mula_repo=os.getenv(
        "HEARTMULA_MULA_REPO", "HeartMuLa/HeartMuLa-oss-3B-happy-new-year"
    ),
    codec_repo=os.getenv(
        "HEARTMULA_CODEC_REPO", "HeartMuLa/HeartCodec-oss-20260123"
    ),
    mula_dirname=os.getenv("HEARTMULA_MULA_DIRNAME", "HeartMuLa-oss-3B"),
    codec_dirname=os.getenv("HEARTMULA_CODEC_DIRNAME", "HeartCodec-oss"),
)

GPU_DURATION_SECONDS = int(os.getenv("HEARTMULA_GPU_DURATION_SECONDS", "300"))
COMPILE_DURATION_SECONDS = int(os.getenv("HEARTMULA_COMPILE_DURATION_SECONDS", "600"))
MAX_DURATION_SECONDS = int(os.getenv("HEARTMULA_MAX_DURATION_SECONDS", "180"))
DEFAULT_DURATION_SECONDS = min(
    int(os.getenv("HEARTMULA_DEFAULT_DURATION_SECONDS", "60")),
    MAX_DURATION_SECONDS,
)
ENABLE_FLASH_ATTN = os.getenv("HEARTMULA_ENABLE_FLASH_ATTN", "1") != "0"
ENABLE_AOTI = os.getenv("HEARTMULA_ENABLE_AOTI", "1") != "0"
AOTI_MAX_BATCH = int(os.getenv("HEARTMULA_AOTI_MAX_BATCH", "2"))
AOTI_MAX_SEQ_LEN = int(os.getenv("HEARTMULA_AOTI_MAX_SEQ_LEN", "4096"))
KEEP_MULA_LOADED = os.getenv("HEARTMULA_KEEP_MULA_LOADED", "1") != "0"
KEEP_CODEC_LOADED = os.getenv("HEARTMULA_KEEP_CODEC_LOADED", "0") != "0"

MODEL_LOCK = threading.Lock()
PIPELINE_LOCK = threading.Lock()
PIPELINE_CACHE: Dict[Tuple[str, str], HeartMuLaGenPipeline] = {}
_RUNTIME_PREPARED = False


def _default_cache_root() -> Path:
    env_home = os.getenv("HF_HOME")
    if env_home:
        return Path(env_home)

    data_home = Path("/data/.huggingface")
    if data_home.parent.exists():
        return data_home

    return Path("/tmp/huggingface")


def _model_root() -> Path:
    return Path(
        os.getenv(
            "HEARTMULA_MODEL_DIR",
            str(_default_cache_root() / "heartmula_models"),
        )
    )


def _read_text(path: Path, fallback: str) -> str:
    if path.is_file():
        return path.read_text(encoding="utf-8").strip()
    return fallback


def _cached_model_exists(model_dir: Path) -> bool:
    required_paths = [
        model_dir / "tokenizer.json",
        model_dir / "gen_config.json",
        model_dir / MODEL_CONFIG.mula_dirname,
        model_dir / MODEL_CONFIG.codec_dirname,
    ]
    return all(path.exists() for path in required_paths)


def ensure_model_artifacts(progress: gr.Progress | None = None) -> Path:
    model_dir = _model_root()
    model_dir.mkdir(parents=True, exist_ok=True)

    if _cached_model_exists(model_dir):
        if progress is not None:
            progress(0.05, desc="Using cached model artifacts")
        return model_dir

    with MODEL_LOCK:
        if _cached_model_exists(model_dir):
            if progress is not None:
                progress(0.05, desc="Using cached model artifacts")
            return model_dir

        if progress is not None:
            progress(0.05, desc="Downloading tokenizer and generation config")
        for filename in ("tokenizer.json", "gen_config.json"):
            hf_hub_download(
                repo_id=MODEL_CONFIG.generator_repo,
                filename=filename,
                local_dir=str(model_dir),
            )

        if progress is not None:
            progress(0.25, desc="Downloading HeartMuLa checkpoint")
        snapshot_download(
            repo_id=MODEL_CONFIG.mula_repo,
            local_dir=str(model_dir / MODEL_CONFIG.mula_dirname),
        )

        if progress is not None:
            progress(0.6, desc="Downloading HeartCodec checkpoint")
        snapshot_download(
            repo_id=MODEL_CONFIG.codec_repo,
            local_dir=str(model_dir / MODEL_CONFIG.codec_dirname),
        )

    if progress is not None:
        progress(0.95, desc="Model artifacts ready")
    return model_dir


def _runtime_key() -> Tuple[str, str]:
    runtime = "cuda" if torch.cuda.is_available() else "cpu"
    return runtime, str(_model_root())


def get_pipeline(model_dir: Path) -> HeartMuLaGenPipeline:
    """Create pipeline and store acceleration config. Does NOT trigger compilation."""
    runtime = "cuda" if torch.cuda.is_available() else "cpu"
    cache_key = (runtime, str(model_dir))

    with PIPELINE_LOCK:
        if cache_key in PIPELINE_CACHE:
            return PIPELINE_CACHE[cache_key]

        if runtime == "cuda":
            device = {
                "mula": torch.device("cuda"),
                "codec": torch.device("cuda"),
            }
            dtype = {
                "mula": torch.bfloat16,
                "codec": torch.float32,
            }
            lazy_load = {
                "mula": not KEEP_MULA_LOADED,
                "codec": not KEEP_CODEC_LOADED,
            }
        else:
            device = torch.device("cpu")
            dtype = torch.float32
            lazy_load = False

        pipeline = HeartMuLaGenPipeline.from_pretrained(
            str(model_dir),
            device=device,
            dtype=dtype,
            version=MODEL_CONFIG.version,
            lazy_load=lazy_load,
        )
        pipeline.configure_runtime_acceleration(
            enable_flash_attn=runtime == "cuda" and ENABLE_FLASH_ATTN,
            enable_aoti=runtime == "cuda" and ENABLE_AOTI,
            max_batch_size=AOTI_MAX_BATCH,
            max_compile_seq_len=AOTI_MAX_SEQ_LEN,
        )
        PIPELINE_CACHE[cache_key] = pipeline
        return pipeline


@spaces.GPU(duration=COMPILE_DURATION_SECONDS)
def _compile_runtime(model_dir: str):
    """AoTI compilation + FA3 injection on a real GPU.

    Uses a separate, long time budget (default 1500 s) so that first-time
    compilation does not compete with inference for GPU seconds.  On
    subsequent calls the ``spaces.aoti_compile`` filesystem cache makes
    this effectively a no-op.
    """
    pipeline = get_pipeline(Path(model_dir))
    pipeline.prepare_runtime()


@spaces.GPU(duration=GPU_DURATION_SECONDS)
def _run_generation(
    model_dir: str,
    lyrics: str,
    tags: str,
    max_duration_seconds: int,
    temperature: float,
    topk: int,
    cfg_scale: float,
    progress=gr.Progress(track_tqdm=True),
):
    pipeline = get_pipeline(Path(model_dir))
    max_audio_length_ms = max_duration_seconds * 1000

    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
        output_path = fp.name

    progress(0.05, desc="Generating audio")
    with torch.no_grad():
        pipeline(
            {
                "lyrics": lyrics,
                "tags": tags,
            },
            max_audio_length_ms=max_audio_length_ms,
            save_path=output_path,
            topk=topk,
            temperature=temperature,
            cfg_scale=cfg_scale,
        )

    return output_path


def generate_music(
    lyrics: str,
    tags: str,
    max_duration_seconds: int,
    temperature: float,
    topk: int,
    cfg_scale: float,
    progress=gr.Progress(track_tqdm=True),
):
    if not lyrics.strip():
        raise gr.Error("Please enter lyrics before generating.")
    if not tags.strip():
        raise gr.Error("Please enter at least one style tag.")

    model_dir = ensure_model_artifacts(progress)

    global _RUNTIME_PREPARED
    if not _RUNTIME_PREPARED:
        progress(0.02, desc="Compiling runtime (first request, please wait)")
        _compile_runtime(str(model_dir))
        _RUNTIME_PREPARED = True

    return _run_generation(
        str(model_dir),
        lyrics,
        tags,
        max_duration_seconds,
        temperature,
        topk,
        cfg_scale,
    )


DEFAULT_LYRICS = _read_text(
    APP_ROOT / "heartlib" / "assets" / "lyrics.txt",
    """[Verse]
The city wakes before the sun
We keep moving one by one

[Chorus]
Hold the light and sing it through
Every road comes back to you""",
)

DEFAULT_TAGS = _read_text(
    APP_ROOT / "heartlib" / "assets" / "tags.txt",
    "female,indie pop,piano,emotional,night,silky,memories",
)


with gr.Blocks(title="HeartMuLa ZeroGPU Demo") as demo:
    gr.Markdown(
        """
        # HeartMuLa ZeroGPU Demo

        Generate music from lyrics and style tags with **HeartMuLa** on Hugging Face Spaces.

        First use may take longer because model files need to be cached.
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            lyrics_input = gr.Textbox(
                label="Lyrics",
                lines=18,
                value=DEFAULT_LYRICS,
                placeholder="Use structured sections such as [Verse], [Chorus], [Bridge].",
            )
            tags_input = gr.Textbox(
                label="Tags",
                value=DEFAULT_TAGS,
                placeholder="female,indie pop,piano,emotional,night,silky,memories",
                info="Comma-separated tags without spaces for best compatibility.",
            )
            with gr.Accordion("Generation Settings", open=False):
                max_duration_input = gr.Slider(
                    minimum=30,
                    maximum=MAX_DURATION_SECONDS,
                    value=DEFAULT_DURATION_SECONDS,
                    step=10,
                    label="Max Duration (seconds)",
                )
                temperature_input = gr.Slider(
                    minimum=0.1,
                    maximum=2.0,
                    value=1.0,
                    step=0.1,
                    label="Temperature",
                )
                topk_input = gr.Slider(
                    minimum=1,
                    maximum=100,
                    value=50,
                    step=1,
                    label="Top-K",
                )
                cfg_scale_input = gr.Slider(
                    minimum=1.0,
                    maximum=3.0,
                    value=1.5,
                    step=0.1,
                    label="CFG Scale",
                )

            generate_button = gr.Button("Generate Music", variant="primary")

        with gr.Column(scale=1):
            audio_output = gr.Audio(label="Generated Audio", type="filepath")

    generate_button.click(
        fn=generate_music,
        inputs=[
            lyrics_input,
            tags_input,
            max_duration_input,
            temperature_input,
            topk_input,
            cfg_scale_input,
        ],
        outputs=audio_output,
    )

demo.queue(default_concurrency_limit=1)


if __name__ == "__main__":
    demo.launch()