ace-step / app.py
2Btainment's picture
fix: replace BMC widget script with static link
69ed6b1
Raw History Blame Contribute Delete
7.01 kB
"""
ACE-Step v1.5 - 2B-Studio Worker
================================
Schlanke API-Variante des ACE-Step/Ace-Step-v1.5 Spaces fuer den 2B-Studio
Worker-Verbund. ZeroGPU: @spaces.GPU auf dem Top-Level-Handler, Handler
global registriert (Fork erbt sie ohne Pickling).
Endpoint: /generate(prompt, seconds, seed, steps, instrumental) -> gr.Audio
"""
import os
import sys
import tempfile
# 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>'
# Get current directory (app.py location)
current_dir = os.path.dirname(os.path.abspath(__file__))
# Add nano-vllm to Python path (local package)
nano_vllm_path = os.path.join(current_dir, "acestep", "third_parts", "nano-vllm")
if os.path.exists(nano_vllm_path):
sys.path.insert(0, nano_vllm_path)
# Disable Gradio analytics
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
# Clear proxy settings that may affect Gradio
for proxy_var in ['http_proxy', 'https_proxy', 'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY']:
os.environ.pop(proxy_var, None)
# Import spaces for ZeroGPU support (must be imported before torch for proper interception)
try:
import spaces
HAS_SPACES = True
except ImportError:
HAS_SPACES = False
import gradio as gr
import torch
from acestep.handler import AceStepHandler
from acestep.llm_inference import LLMHandler
from acestep.inference import generate_music, GenerationParams, GenerationConfig
# Detect ZeroGPU environment
IS_HUGGINGFACE_SPACE = os.environ.get("SPACE_ID") is not None
IS_ZEROGPU = IS_HUGGINGFACE_SPACE or os.environ.get("ZEROGPU") is not None
def get_persistent_storage_path():
"""Detect and return a writable persistent storage path (/data on Spaces)."""
hf_data_path = "/data"
if os.path.exists(hf_data_path):
try:
test_file = os.path.join(hf_data_path, ".write_test")
with open(test_file, "w") as f:
f.write("test")
os.remove(test_file)
print(f"Using HuggingFace persistent storage: {hf_data_path}")
return hf_data_path
except (PermissionError, OSError) as e:
print(f"Warning: /data exists but is not writable: {e}")
fallback_path = os.path.join(current_dir, "data")
os.makedirs(fallback_path, exist_ok=True)
print(f"Using local storage (non-persistent): {fallback_path}")
return fallback_path
# ---------------------------------------------------------------------------
# Globale Handler + Modell-Initialisierung (vor launch(), ZeroGPU-Fork erbt)
# ---------------------------------------------------------------------------
persistent_storage_path = get_persistent_storage_path()
dit_handler = AceStepHandler(persistent_storage_path=persistent_storage_path)
llm_handler = LLMHandler(persistent_storage_path=persistent_storage_path)
config_path = os.environ.get("SERVICE_MODE_DIT_MODEL", "acestep-v15-xl-turbo")
lm_model_path = os.environ.get("SERVICE_MODE_LM_MODEL", "acestep-5Hz-lm-1.7B")
# ZeroGPU: PyTorch-Backend erzwingen (nano-vllm nutzt direkte CUDA-APIs)
backend = "pt" if IS_ZEROGPU else os.environ.get("SERVICE_MODE_BACKEND", "vllm")
use_flash_attention = dit_handler.is_flash_attention_available()
print(f"[ACE-Step Worker] Initializing DiT: {config_path} (flash_attn={use_flash_attention})")
init_status, enable_generate = dit_handler.initialize_service(
project_root=current_dir,
config_path=config_path,
device="auto",
use_flash_attention=use_flash_attention,
compile_model=False,
offload_to_cpu=False, # ZeroGPU: GPU wird on-demand allokiert
offload_dit_to_cpu=False,
)
print(f"[ACE-Step Worker] DiT init: {init_status}")
checkpoint_dir = dit_handler._get_checkpoint_dir()
print(f"[ACE-Step Worker] Initializing 5Hz LM: {lm_model_path} (backend={backend})")
lm_status, lm_success = llm_handler.initialize(
checkpoint_dir=checkpoint_dir,
lm_model_path=lm_model_path,
backend=backend,
device="auto",
offload_to_cpu=False,
dtype=dit_handler.dtype,
)
print(f"[ACE-Step Worker] LM init: {lm_status}")
def _generate(prompt: str, seconds: float, seed: int, steps: int, instrumental: bool):
"""Eigentliche Generierung (laeuft im ZeroGPU-Fork)."""
save_dir = tempfile.mkdtemp(prefix="ace_step_out_")
seed = int(seed)
params = GenerationParams(
task_type="text2music",
caption=prompt,
lyrics="[Instrumental]" if instrumental else "",
instrumental=instrumental,
vocal_language="unknown",
bpm=None,
keyscale="",
timesignature="",
duration=max(10.0, min(600.0, float(seconds))),
inference_steps=max(1, int(steps)),
seed=seed,
thinking=True,
use_cot_metas=True,
use_cot_caption=True,
use_cot_language=True,
use_constrained_decoding=True,
)
config = GenerationConfig(
batch_size=1,
use_random_seed=(seed < 0),
seeds=None,
audio_format="wav",
)
result = generate_music(
dit_handler=dit_handler,
llm_handler=llm_handler if lm_success else None,
params=params,
config=config,
save_dir=save_dir,
progress=None,
)
if not result.success:
raise gr.Error(f"ACE-Step generation failed: {result.error or result.status_message}")
audio_paths = [a["path"] for a in result.audios if a.get("path")]
if not audio_paths:
raise gr.Error("ACE-Step returned no audio output.")
return audio_paths[0]
# ZeroGPU: Decorator nur auf dem Top-Level-Handler
if HAS_SPACES:
generate = spaces.GPU(_generate)
else:
generate = _generate
with gr.Blocks(title="ACE-Step Worker") as demo:
gr.HTML(BMC_LINK_HTML)
gr.Markdown(
"# ACE-Step v1.5 Worker\n"
"2B-Studio Free-Modell (DiT + 5Hz-LM CoT). "
f"DiT: {config_path} | LM: {lm_model_path}"
)
with gr.Row():
prompt = gr.Textbox(
label="Prompt", lines=3,
value="An energetic electronic dance track with a driving beat and uplifting synth melody",
)
seconds = gr.Slider(10, 600, value=30, step=5, label="Dauer (s)")
seed = gr.Number(value=-1, precision=0, label="Seed (-1 = random)")
steps = gr.Slider(1, 50, value=8, step=1, label="Inference Steps")
instrumental = gr.Checkbox(value=False, label="Instrumental")
btn = gr.Button("Generate")
out = gr.Audio(label="Output")
btn.click(generate, [prompt, seconds, seed, steps, instrumental], out)
demo.queue(max_size=20)
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True)