Spaces:
Paused
Paused
File size: 7,010 Bytes
bdf51d6 53b6d8c 69ed6b1 53b6d8c bdf51d6 69ed6b1 bdf51d6 | 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 | """
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) |