2baudio / app.py
2Btainment's picture
fix: replace BMC widget script with static link
c2d8f75
Raw History Blame Contribute Delete
17.4 kB
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)