File size: 17,382 Bytes
4ec0ee8
3185f06
4ec0ee8
8096dcc
4ec0ee8
03572d9
8096dcc
 
95feae3
4ec0ee8
 
eb40164
3185f06
4ec0ee8
 
dec6883
4ec0ee8
 
2e03f7b
c2d8f75
 
2e03f7b
4ec0ee8
 
03572d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb40164
 
 
03572d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb40164
4fdb051
 
 
eb40164
8096dcc
239a4d6
4ec0ee8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb40164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03572d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1999671
 
 
 
 
 
 
 
 
 
 
03572d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1999671
 
 
 
 
 
 
03572d9
 
 
 
 
 
 
 
 
 
 
 
eb40164
03572d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1999671
 
03572d9
 
eb40164
 
4fdb051
 
 
 
eb40164
8096dcc
 
4fdb051
 
eb40164
239a4d6
 
4fdb051
 
 
 
 
 
eb40164
 
8096dcc
 
 
 
 
 
03572d9
8096dcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb40164
 
 
 
 
 
03572d9
eb40164
 
 
 
4fdb051
03572d9
1999671
 
eb40164
 
03572d9
 
 
 
 
 
 
eb40164
 
 
 
 
 
 
 
934f0ac
eb40164
4fdb051
 
 
 
 
 
 
 
eb40164
 
 
1999671
 
eb40164
 
 
 
03572d9
eb40164
 
 
 
 
dec6883
4ec0ee8
 
 
 
 
 
 
 
 
 
 
 
 
 
eb40164
03572d9
 
4ec0ee8
 
 
eb40164
 
 
 
 
 
 
 
 
 
03572d9
eb40164
 
 
 
 
 
 
 
4ec0ee8
 
 
 
eb40164
 
4ec0ee8
03572d9
 
4ec0ee8
 
 
eb40164
 
 
03572d9
4ec0ee8
 
 
 
 
 
03572d9
eb40164
 
 
 
4ec0ee8
 
 
 
 
c2d8f75
 
4ec0ee8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63a2f4d
 
95feae3
63a2f4d
95feae3
 
 
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
from __future__ import annotations

import json
import importlib
import math
import re
import subprocess
import sys
import time
import wave
from pathlib import Path
from typing import Any

import gradio as gr
import numpy as np
import spaces



# Buy Me a Coffee: plain static link (no third-party JS; the BMC widget script uses document.write)
BMC_LINK_HTML = '<a href="https://www.buymeacoffee.com/2btainment" target="_blank" rel="noopener noreferrer" class="bmc-link" style="position:fixed;right:16px;bottom:16px;z-index:9999;background:#FFDD00;color:#000;padding:8px 14px;border-radius:999px;font:600 14px/1.2 system-ui,-apple-system,sans-serif;text-decoration:none;box-shadow:0 2px 8px rgba(0,0,0,.25)">Buy me a coffee</a>'

OUTPUT_DIR = Path("outputs")
OUTPUT_DIR.mkdir(exist_ok=True)
DEFAULT_MODEL_ID = "facebook/musicgen-medium"
MODEL_ALIASES = {
    "small": "facebook/musicgen-small",
    "medium": "facebook/musicgen-medium",
    "large": "facebook/musicgen-large",
}
PROFILE_PRESETS: dict[str, dict[str, Any]] = {
    "profile-4-low-vram": {
        "model_id": "facebook/musicgen-small",
        "max_new_tokens": 384,
        "temperature": 1.0,
        "guidance_scale": 3.0,
        "run_n_segments": 1,
        "prompt_hint": "concise arrangement, tight rhythm section",
    },
    "profile-5-balanced": {
        "model_id": "facebook/musicgen-medium",
        "max_new_tokens": 640,
        "temperature": 0.95,
        "guidance_scale": 3.2,
        "run_n_segments": 2,
        "prompt_hint": "balanced arrangement, clear hook focus",
    },
    "profile-6-quality": {
        "model_id": "facebook/musicgen-medium",
        "max_new_tokens": 896,
        "temperature": 0.9,
        "guidance_scale": 3.6,
        "run_n_segments": 2,
        "prompt_hint": "richer arrangement layers, cinematic dynamics",
    },
}
DEFAULT_PROFILE = "profile-5-balanced"
BACKEND_DEFAULT_PROFILES = {
    "yuegp": "profile-5-balanced",
    "yue-interface": "profile-6-quality",
    "stable-audio-3": "profile-5-balanced",
    "manual": "profile-5-balanced",
}
MAX_PROMPT_CHARS = 1500
MAX_LYRIC_LINES = 12
MAX_SECTION_COUNT = 8
SECTION_PATTERN = re.compile(r"^\s*\[(?P<label>[^\]]+)\]\s*$")
MOOD_TAG_HINTS = {
    "bright",
    "dark",
    "warm",
    "cold",
    "emotional",
    "melancholic",
    "nostalgic",
    "uplifting",
    "dreamy",
    "aggressive",
    "chill",
    "energetic",
    "hopeful",
    "sad",
    "happy",
}
INSTRUMENT_TAG_HINTS = {
    "guitar",
    "piano",
    "synth",
    "strings",
    "drums",
    "bass",
    "violin",
    "trumpet",
    "sax",
    "808",
    "organ",
    "pads",
}
VOCAL_TAG_HINTS = {
    "vocal",
    "female",
    "male",
    "choir",
    "rap",
    "spoken",
    "instrumental",
}

_MUSICGEN_MODEL: Any | None = None
_MUSICGEN_PROCESSOR: Any | None = None
_LOADED_MODEL_ID: str | None = None
_PIPELINE_DEVICE: str = "cpu"
_DEPENDENCIES_READY = False
FORCE_CPU = True


def write_dummy_wav(path: Path, seed: int, duration: float = 2.5, sample_rate: int = 44100) -> None:
    rng = np.random.default_rng(seed)
    t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
    base = 220 + (seed % 220)
    tone = 0.25 * np.sin(2 * math.pi * base * t)
    overtone = 0.12 * np.sin(2 * math.pi * base * 1.5 * t)
    noise = 0.015 * rng.standard_normal(t.shape)
    audio = np.clip(tone + overtone + noise, -1.0, 1.0)
    pcm = (audio * 32767).astype(np.int16)
    stereo = np.column_stack([pcm, pcm]).ravel()
    with wave.open(str(path), "wb") as handle:
        handle.setnchannels(2)
        handle.setsampwidth(2)
        handle.setframerate(sample_rate)
        handle.writeframes(stereo.tobytes())


def write_audio_wav(path: Path, audio: np.ndarray, sample_rate: int) -> None:
    data = np.asarray(audio, dtype=np.float32)
    if data.ndim == 1:
        data = data[np.newaxis, :]
    if data.ndim != 2:
        raise ValueError(f"Expected audio shape (channels, samples), got {data.shape}")
    data = np.clip(data, -1.0, 1.0)
    if data.shape[0] == 1:
        data = np.vstack([data, data])
    pcm = (data.T * 32767.0).astype(np.int16)
    with wave.open(str(path), "wb") as handle:
        handle.setnchannels(pcm.shape[1])
        handle.setsampwidth(2)
        handle.setframerate(sample_rate)
        handle.writeframes(pcm.tobytes())


def normalize_key(value: str) -> str:
    return value.strip().lower()


def parse_bool(value: Any, default: bool) -> bool:
    if value is None:
        return default
    if isinstance(value, bool):
        return value
    return normalize_key(str(value)) in {"1", "true", "yes", "y", "on"}


def unique_keep_order(values: list[str]) -> list[str]:
    seen: set[str] = set()
    result: list[str] = []
    for value in values:
        key = normalize_key(value)
        if not key or key in seen:
            continue
        seen.add(key)
        result.append(value.strip())
    return result


def parse_tag_list(genre_tags: str) -> list[str]:
    tags: list[str] = []
    for raw in (genre_tags or "").replace(";", ",").split(","):
        cleaned = " ".join(raw.split())
        if cleaned:
            tags.append(cleaned)
    return unique_keep_order(tags)


def parse_negative_terms(value: Any) -> list[str]:
    if value is None:
        return []
    if isinstance(value, list):
        raw_values = [str(item) for item in value]
    else:
        raw_values = str(value).replace(";", ",").replace("|", ",").split(",")
    cleaned = [" ".join(item.split()) for item in raw_values if " ".join(item.split())]
    return unique_keep_order(cleaned)


def pick_profile_name(profile: str, backend: str) -> str:
    profile_key = normalize_key(profile)
    if profile_key in PROFILE_PRESETS:
        return profile_key
    backend_key = normalize_key(backend)
    backend_profile = BACKEND_DEFAULT_PROFILES.get(backend_key, DEFAULT_PROFILE)
    if backend_profile in PROFILE_PRESETS:
        return backend_profile
    return DEFAULT_PROFILE


def normalize_model_hint(value: str) -> str | None:
    cleaned = value.strip()
    if not cleaned:
        return None
    if "/" in cleaned:
        return cleaned
    return MODEL_ALIASES.get(normalize_key(cleaned))


def pick_model_id(backend: str, profile: str, model: str, parameters: dict[str, Any]) -> str:
    explicit_param = normalize_model_hint(str(parameters.get("hf_model_id", "")))
    if explicit_param:
        return explicit_param

    explicit_model = normalize_model_hint(model)
    if explicit_model:
        return explicit_model

    profile_name = pick_profile_name(profile, backend)
    preset_model = str(PROFILE_PRESETS[profile_name].get("model_id", "")).strip()
    if preset_model:
        return preset_model

    return DEFAULT_MODEL_ID


def extract_lyric_structure(lyrics: str) -> tuple[list[str], list[str]]:
    sections: list[str] = []
    key_lines: list[str] = []
    current_section = ""
    for raw_line in (lyrics or "").splitlines():
        line = " ".join(raw_line.split())
        if not line:
            continue
        match = SECTION_PATTERN.match(line)
        if match:
            section = normalize_key(match.group("label")).replace("_", " ").replace("-", " ")
            section = " ".join(section.split())
            if section and len(sections) < MAX_SECTION_COUNT:
                sections.append(section)
                current_section = section
            continue
        if len(key_lines) < MAX_LYRIC_LINES:
            if current_section:
                key_lines.append(f"{current_section}: {line}")
            else:
                key_lines.append(line)
    return unique_keep_order(sections), key_lines


def build_prompt(
    project_name: str,
    genre_tags: str,
    lyrics: str,
    profile_name: str,
    negative_terms: list[str] | None = None,
) -> str:
    tags = parse_tag_list(genre_tags)
    sections, lyric_lines = extract_lyric_structure(lyrics)

    lowered_tags = [normalize_key(tag) for tag in tags]
    mood_tags = [tag for tag, lowered in zip(tags, lowered_tags) if lowered in MOOD_TAG_HINTS]
    instrument_tags = [tag for tag, lowered in zip(tags, lowered_tags) if lowered in INSTRUMENT_TAG_HINTS]
    vocal_tags = [tag for tag, lowered in zip(tags, lowered_tags) if lowered in VOCAL_TAG_HINTS]
    style_tags = [tag for tag in tags if tag not in mood_tags and tag not in instrument_tags and tag not in vocal_tags]
    if not style_tags:
        style_tags = tags[:4]

    prompt_parts = ["Polished studio music production with a strong melodic identity."]
    if project_name.strip():
        prompt_parts.append(f"Project title cue: {project_name.strip()}.")
    if style_tags:
        prompt_parts.append(f"Core styles: {', '.join(style_tags[:6])}.")
    if mood_tags:
        prompt_parts.append(f"Mood and emotion: {', '.join(mood_tags[:5])}.")
    if instrument_tags:
        prompt_parts.append(f"Instrumentation focus: {', '.join(instrument_tags[:6])}.")
    if vocal_tags:
        if "instrumental" in [normalize_key(tag) for tag in vocal_tags]:
            prompt_parts.append("Vocal direction: instrumental, no lead vocals.")
        else:
            prompt_parts.append(f"Vocal direction: {', '.join(vocal_tags[:4])}.")
    if sections:
        prompt_parts.append(f"Song structure cues: {' -> '.join(sections[:6])}.")
    if lyric_lines:
        prompt_parts.append(f"Lyrical cues: {' | '.join(lyric_lines[:8])}.")

    profile_hint = PROFILE_PRESETS.get(profile_name, {}).get("prompt_hint", "")
    if profile_hint:
        prompt_parts.append(f"Production target: {profile_hint}.")
    if negative_terms:
        prompt_parts.append(f"Avoid: {', '.join(negative_terms[:16])}.")
    prompt_parts.append("Clean low-end, defined drums, clear lead elements, modern stereo image.")
    return " ".join(prompt_parts)[:MAX_PROMPT_CHARS]


def get_musicgen_components(model_id: str):
    global _MUSICGEN_MODEL, _MUSICGEN_PROCESSOR, _LOADED_MODEL_ID, _PIPELINE_DEVICE
    if _MUSICGEN_MODEL is not None and _MUSICGEN_PROCESSOR is not None and _LOADED_MODEL_ID == model_id:
        return _MUSICGEN_MODEL, _MUSICGEN_PROCESSOR

    ensure_runtime_dependencies()

    import torch
    from transformers import AutoProcessor, MusicgenForConditionalGeneration

    # ZeroGPU environments can report CUDA availability while blocking low-level CUDA init.
    # CPU mode is slower but stable and avoids intermittent emulation/runtime crashes.
    device = "cpu" if FORCE_CPU else ("cuda:0" if torch.cuda.is_available() else "cpu")
    _PIPELINE_DEVICE = device
    _MUSICGEN_PROCESSOR = AutoProcessor.from_pretrained(model_id)
    _MUSICGEN_MODEL = MusicgenForConditionalGeneration.from_pretrained(model_id).to(device)
    _LOADED_MODEL_ID = model_id
    return _MUSICGEN_MODEL, _MUSICGEN_PROCESSOR


def ensure_runtime_dependencies() -> None:
    global _DEPENDENCIES_READY
    if _DEPENDENCIES_READY:
        return

    required = [
        ("transformers", "transformers==4.44.2"),
        ("sentencepiece", "sentencepiece>=0.2"),
    ]
    for module_name, package_spec in required:
        try:
            importlib.import_module(module_name)
        except Exception:
            subprocess.check_call(
                [sys.executable, "-m", "pip", "install", "--no-cache-dir", package_spec],
                stdout=sys.stderr,
                stderr=sys.stderr,
            )
            importlib.import_module(module_name)
    _DEPENDENCIES_READY = True


def generate_music(
    *,
    model_id: str,
    project_name: str,
    genre_tags: str,
    lyrics: str,
    profile_name: str,
    parameters: dict[str, Any],
) -> tuple[np.ndarray, int, dict[str, Any]]:
    import torch

    model, processor = get_musicgen_components(model_id)
    preset = PROFILE_PRESETS.get(profile_name, PROFILE_PRESETS[DEFAULT_PROFILE])
    negative_terms = parse_negative_terms(parameters.get("negative_prompt", ""))
    prompt = build_prompt(project_name, genre_tags, lyrics, profile_name, negative_terms=negative_terms)

    seed = int(parameters.get("seed", 12345))
    run_n_segments = int(parameters.get("run_n_segments", int(preset.get("run_n_segments", 1))))
    default_max_tokens = int(preset.get("max_new_tokens", 512))
    max_new_tokens = int(parameters.get("max_new_tokens", max(default_max_tokens, 192 * max(1, run_n_segments))))
    max_new_tokens = max(128, min(max_new_tokens, 1503))
    temperature = float(parameters.get("temperature", float(preset.get("temperature", 1.0))))
    guidance_scale = float(parameters.get("guidance_scale", float(preset.get("guidance_scale", 3.0))))
    do_sample = parse_bool(parameters.get("do_sample", True), default=True)

    generate_kwargs = {
        "do_sample": do_sample,
        "temperature": temperature,
        "guidance_scale": guidance_scale,
        "max_new_tokens": max_new_tokens,
    }
    # Keep deterministic output across calls with the same seed.
    torch.manual_seed(seed)

    inputs = processor(text=[prompt], padding=True, return_tensors="pt")
    inputs = {key: value.to(_PIPELINE_DEVICE) for key, value in inputs.items()}

    with torch.inference_mode():
        audio_values = model.generate(**inputs, **generate_kwargs)

    audio = audio_values[0].detach().cpu().numpy()
    sampling_rate = int(model.config.audio_encoder.sampling_rate)
    debug = {
        "prompt": prompt,
        "prompt_chars": len(prompt),
        "negative_terms": negative_terms,
        "prompt_filter_level": str(parameters.get("prompt_filter_level", "balanced")),
        "seed": seed,
        "max_new_tokens": max_new_tokens,
        "temperature": temperature,
        "guidance_scale": guidance_scale,
        "profile": profile_name,
        "device": _PIPELINE_DEVICE,
    }
    return audio, sampling_rate, debug


@spaces.GPU
def generate(
    job_id: str,
    project_name: str,
    backend: str,
    model: str,
    profile: str,
    genre_tags: str,
    lyrics: str,
    parameters_json: str,
):
    try:
        parameters = json.loads(parameters_json or "{}")
    except json.JSONDecodeError:
        parameters = {}

    profile_name = pick_profile_name(profile, backend)
    model_id = pick_model_id(backend, profile_name, model, parameters)
    seed = int(parameters.get("seed", 12345))
    safe_job = "".join(ch if ch.isalnum() or ch in "-_" else "_" for ch in job_id or "amap-job")
    audio_path = OUTPUT_DIR / f"{safe_job}.wav"
    generation_mode = "model"
    generation_error = None
    generation_debug: dict[str, Any] = {}

    try:
        audio_values, sampling_rate, generation_debug = generate_music(
            model_id=model_id,
            project_name=project_name,
            genre_tags=genre_tags,
            lyrics=lyrics,
            profile_name=profile_name,
            parameters=parameters,
        )
        write_audio_wav(audio_path, audio_values, sampling_rate)
    except Exception as exc:  # pragma: no cover - fallback for runtime-only failures
        generation_mode = "dummy_fallback"
        generation_error = str(exc)
        write_dummy_wav(audio_path, seed)

    settings = {
        "job_id": job_id,
        "project_name": project_name,
        "backend": backend,
        "worker_backend": "text-to-audio",
        "model_id": model_id,
        "model": model,
        "profile": profile_name,
        "profile_input": profile,
        "genre_tags": genre_tags,
        "lyrics_chars": len(lyrics or ""),
        "parameters": parameters,
        "generation_mode": generation_mode,
        "generation_error": generation_error,
        "generation_debug": generation_debug,
        "worker": "amap-zerogpu-musicgen-v4",
    }
    log = "\n".join(
        [
            f"AMAP ZeroGPU worker received job {job_id}",
            f"Project: {project_name}",
            f"Backend requested: {backend}",
            f"Resolved profile: {profile_name}",
            f"Selected model: {model_id}",
            f"Generation mode: {generation_mode}",
            f"Generated WAV: {audio_path}",
            f"Generation error: {generation_error or 'none'}",
        ]
    )
    return str(audio_path), log, settings


with gr.Blocks(title="AMAP ZeroGPU Worker") as demo:
    gr.HTML(BMC_LINK_HTML)
    gr.Markdown("# AMAP ZeroGPU Worker")
    with gr.Row():
        job_id = gr.Textbox(label="Job ID")
        project_name = gr.Textbox(label="Project")
    with gr.Row():
        backend = gr.Textbox(label="Backend")
        model = gr.Textbox(label="Model")
        profile = gr.Textbox(label="Profile")
    genre_tags = gr.Textbox(label="Genre Tags")
    lyrics = gr.Textbox(label="Lyrics", lines=8)
    parameters_json = gr.Textbox(label="Parameters JSON", lines=5, value='{"seed": 12345}')
    button = gr.Button("Generate")
    audio = gr.Audio(label="Generated Audio", type="filepath")
    log = gr.Textbox(label="Log", lines=6)
    settings = gr.JSON(label="Settings")
    button.click(
        generate,
        inputs=[job_id, project_name, backend, model, profile, genre_tags, lyrics, parameters_json],
        outputs=[audio, log, settings],
        api_name="generate",
    )


if __name__ == "__main__":
    demo.queue()
    try:
        demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False, prevent_thread_lock=True)
    except TypeError:
        demo.launch(server_name="0.0.0.0", server_port=7860, prevent_thread_lock=True)
    while True:
        time.sleep(3600)