Datasets:
Tasks:
Audio Classification
Formats:
parquet
Size:
1K - 10K
ArXiv:
Tags:
arxiv:2606.01686
music
ai-generated-music
ai-generated-music-detection
plagiarism-detection
ace-step
License:
| #!/usr/bin/env python3 | |
| import argparse | |
| import shutil | |
| import subprocess | |
| import tempfile | |
| from pathlib import Path | |
| from uuid import uuid4 | |
| import numpy as np | |
| import torch | |
| from scipy.io import wavfile | |
| from tqdm import tqdm | |
| from utils import * | |
| MODEL_MAP = { | |
| "base": "ACE-Step/acestep-v15-base", | |
| "turbo": "ACE-Step/Ace-Step1.5", | |
| } | |
| def parse_args(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--variant", choices=["base", "turbo"], default="turbo") | |
| parser.add_argument("--target", type=int, default=2000) | |
| parser.add_argument( | |
| "--output-dir", | |
| type=Path, | |
| default=None, | |
| help="Default: FAKE_DIR/A_opensource/acestep_1.5_{variant}", | |
| ) | |
| parser.add_argument( | |
| "--batch-size", | |
| type=int, | |
| default=2, | |
| help="Recommended 1-2 on 24GB VRAM; use smaller values for base model", | |
| ) | |
| parser.add_argument("--device", choices=["cuda", "cpu"], default="cuda") | |
| return parser.parse_args() | |
| def to_int16(audio: np.ndarray): | |
| audio = np.nan_to_num(audio.astype(np.float32), nan=0.0, posinf=0.0, neginf=0.0) | |
| peak = np.max(np.abs(audio)) if audio.size else 0.0 | |
| if peak > 1.0: | |
| audio = audio / peak | |
| return np.int16(np.clip(audio, -1.0, 1.0) * 32767) | |
| def try_build_pipeline(model_id: str, device: str): | |
| try: | |
| from transformers import pipeline | |
| use_cuda = device == "cuda" and torch.cuda.is_available() | |
| device_index = 0 if use_cuda else -1 | |
| return pipeline("text-to-audio", model=model_id, device=device_index, trust_remote_code=True) | |
| except Exception: | |
| return None | |
| def extract_audio_from_pipeline_output(output): | |
| if isinstance(output, list): | |
| output = output[0] | |
| if isinstance(output, dict): | |
| audio = output.get("audio") | |
| sr = int(output.get("sampling_rate", 32000)) | |
| if audio is None: | |
| return None, None | |
| return np.array(audio), sr | |
| return None, None | |
| def ensure_repo(models_dir: Path) -> Path: | |
| repo_dir = models_dir / "ACE-Step-1.5" | |
| if repo_dir.exists(): | |
| return repo_dir | |
| models_dir.mkdir(parents=True, exist_ok=True) | |
| cmd = ["git", "clone", "https://github.com/ace-step/ACE-Step-1.5", str(repo_dir)] | |
| subprocess.run(cmd, check=True) | |
| return repo_dir | |
| def run_cli_fallback( | |
| repo_dir: Path, prompt: str, output_path: Path, model_id: str, device: str | |
| ) -> bool: | |
| with tempfile.TemporaryDirectory() as tmp_dir: | |
| tmp_dir_path = Path(tmp_dir) | |
| candidates = [ | |
| [ | |
| "python", | |
| "infer.py", | |
| "--prompt", | |
| prompt, | |
| "--output", | |
| str(tmp_dir_path), | |
| "--model", | |
| model_id, | |
| "--device", | |
| device, | |
| ], | |
| [ | |
| "python", | |
| "inference.py", | |
| "--prompt", | |
| prompt, | |
| "--output_dir", | |
| str(tmp_dir_path), | |
| "--model_id", | |
| model_id, | |
| "--device", | |
| device, | |
| ], | |
| ] | |
| for cmd in candidates: | |
| proc = subprocess.run(cmd, cwd=repo_dir, capture_output=True, text=True) | |
| if proc.returncode != 0: | |
| continue | |
| generated = sorted( | |
| list(tmp_dir_path.rglob("*.wav")) + list(tmp_dir_path.rglob("*.mp3")), | |
| key=lambda p: p.stat().st_mtime, | |
| ) | |
| if generated: | |
| shutil.move(str(generated[-1]), str(output_path)) | |
| return True | |
| return False | |
| def main(): | |
| args = parse_args() | |
| device = "cuda" if args.device == "cuda" and torch.cuda.is_available() else "cpu" | |
| model_id = MODEL_MAP[args.variant] | |
| output_dir = args.output_dir or ( | |
| FAKE_DIR / "A_opensource" / f"acestep_1.5_{args.variant}" | |
| ) | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| if not ensure_disk_space(): | |
| raise RuntimeError("Insufficient disk space before generation start.") | |
| meta_mgr = MetadataManager(output_dir) | |
| existing = meta_mgr.get_count() | |
| if existing >= args.target: | |
| print(f"Target already reached: {existing}/{args.target}") | |
| return | |
| prompts = get_diverse_prompts(args.target) | |
| pipe = try_build_pipeline(model_id, device) | |
| repo_dir = None | |
| if pipe is None: | |
| repo_dir = ensure_repo(BASE_DIR / "models") | |
| progress = tqdm( | |
| total=args.target, initial=existing, desc=f"ACE-Step-{args.variant}" | |
| ) | |
| generated_this_run = 0 | |
| for idx in range(existing, args.target): | |
| prompt = prompts[idx] | |
| track_id = str(uuid4()) | |
| filename = f"{track_id}.wav" | |
| file_path = output_dir / filename | |
| ok = False | |
| if pipe is not None: | |
| try: | |
| output = pipe(prompt) | |
| audio, sr = extract_audio_from_pipeline_output(output) | |
| if audio is not None and sr is not None and audio.size > 0: | |
| if audio.ndim > 1: | |
| audio = np.mean(audio, axis=0) | |
| wavfile.write(file_path, int(sr), to_int16(audio)) | |
| ok = True | |
| except Exception: | |
| ok = False | |
| if not ok: | |
| if repo_dir is None: | |
| repo_dir = ensure_repo(BASE_DIR / "models") | |
| ok = run_cli_fallback(repo_dir, prompt, file_path, model_id, device) | |
| if not ok or not file_path.exists(): | |
| continue | |
| info = get_audio_info(file_path) | |
| if ( | |
| not info | |
| or info.get("duration_sec", 0.0) <= 0.5 | |
| or info.get("file_size_bytes", 0) <= 1024 | |
| ): | |
| file_path.unlink(missing_ok=True) | |
| continue | |
| md5_hash = compute_md5(file_path) | |
| meta = TrackMetadata( | |
| track_id=track_id, | |
| filename=filename, | |
| category="A_opensource", | |
| subcategory=f"acestep_1.5_{args.variant}", | |
| source_platform="acestep", | |
| source_type="open-source", | |
| model_name="ACE-Step", | |
| model_version=f"1.5-{args.variant}", | |
| collection_method="generate", | |
| audio_format=file_path.suffix.lstrip(".") or "wav", | |
| prompt=prompt, | |
| md5_hash=md5_hash, | |
| duration_sec=info.get("duration_sec"), | |
| sample_rate=info.get("sample_rate"), | |
| channels=info.get("channels"), | |
| bitrate_kbps=info.get("bitrate_kbps"), | |
| file_size_bytes=info.get("file_size_bytes"), | |
| ) | |
| meta_mgr.add_track(meta) | |
| generated_this_run += 1 | |
| progress.update(1) | |
| if meta_mgr.get_count() % 50 == 0: | |
| if not ensure_disk_space(): | |
| meta_mgr.update_summary() | |
| raise RuntimeError("Low disk space, stopping generation.") | |
| meta_mgr.update_summary() | |
| if meta_mgr.get_count() >= args.target: | |
| break | |
| meta_mgr.update_summary() | |
| progress.close() | |
| print( | |
| f"Generated {generated_this_run} tracks. Total: {meta_mgr.get_count()}/{args.target}" | |
| ) | |
| if __name__ == "__main__": | |
| main() | |