Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import random
|
| 4 |
+
import subprocess
|
| 5 |
+
import spaces
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import torch
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
# 谞转讬讘 砖诪讬专讛 转讜讗诐 诇住讘讬讘转 Hugging Face Spaces
|
| 11 |
+
OUT = "/tmp/yue2_out"
|
| 12 |
+
os.makedirs(OUT, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
REPO = "m-a-p/YuE2-3B"
|
| 15 |
+
pipe = None
|
| 16 |
+
|
| 17 |
+
def get_pipeline():
|
| 18 |
+
global pipe
|
| 19 |
+
if pipe is None:
|
| 20 |
+
from yue2 import YuE2Pipeline
|
| 21 |
+
print("Loading YuE2-3B pipeline on GPU...")
|
| 22 |
+
pipe = YuE2Pipeline.from_pretrained(REPO, device="cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
+
print("Pipeline loaded.")
|
| 24 |
+
return pipe
|
| 25 |
+
|
| 26 |
+
def flac_to_mp3(flac_path, mp3_path, bitrate="320k"):
|
| 27 |
+
subprocess.run(
|
| 28 |
+
["ffmpeg", "-y", "-i", flac_path, "-b:a", bitrate, mp3_path],
|
| 29 |
+
capture_output=True, check=True,
|
| 30 |
+
)
|
| 31 |
+
return mp3_path
|
| 32 |
+
|
| 33 |
+
# 讛-Decorator 讛拽专讬讟讬 砖诪驻注讬诇 讗转 讛-ZeroGPU 讘讝诪谉 讬爪讬专转 讛砖讬专
|
| 34 |
+
@spaces.GPU(duration=120)
|
| 35 |
+
def generate(style, lyrics, cot, seed, cfg_scale):
|
| 36 |
+
pipeline = get_pipeline()
|
| 37 |
+
|
| 38 |
+
if seed is None or seed < 1:
|
| 39 |
+
seed = random.randint(1, 10**6)
|
| 40 |
+
|
| 41 |
+
gen_kwargs = dict(style=style, lyrics=lyrics, cot=cot, seed=int(seed))
|
| 42 |
+
if cfg_scale and float(cfg_scale) != 1.0:
|
| 43 |
+
gen_kwargs["cfg_scale"] = float(cfg_scale)
|
| 44 |
+
|
| 45 |
+
song = pipeline(**gen_kwargs)
|
| 46 |
+
|
| 47 |
+
idx = random.randint(1, 10**9)
|
| 48 |
+
flac_path = os.path.join(OUT, f"song_{idx}.flac")
|
| 49 |
+
mp3_path = os.path.join(OUT, f"song_{idx}.mp3")
|
| 50 |
+
|
| 51 |
+
song.save(flac_path)
|
| 52 |
+
flac_to_mp3(flac_path, mp3_path)
|
| 53 |
+
|
| 54 |
+
return flac_path, mp3_path, f"seed={seed} | {os.path.getsize(mp3_path) // 1024}KB MP3 320kbps"
|
| 55 |
+
|
| 56 |
+
with gr.Blocks(title="YuE2-3B Music") as demo:
|
| 57 |
+
gr.Markdown("# YuE2-3B Music Generator\nFull song (vocals + accompaniment) from a style prompt and lyrics.")
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
style = gr.Textbox(label="Style prompt", value="Cyber metal, English, aggressive male vocals, distorted guitars, double-kick drums", lines=3)
|
| 61 |
+
lyrics = gr.Textbox(label="Lyrics ([verse]/[chorus] etc.)", value="[verse]\nnothing here but static skies\nwe ride the current till it dies", lines=8)
|
| 62 |
+
with gr.Row():
|
| 63 |
+
cot = gr.Radio(["full", "melody", "off"], value="full", label="CoT mode")
|
| 64 |
+
seed = gr.Number(label="Seed", value=0, precision=0)
|
| 65 |
+
cfg = gr.Number(label="CFG scale (1.0 = default)", value=1.0, precision=2)
|
| 66 |
+
btn = gr.Button("Generate song", variant="primary")
|
| 67 |
+
with gr.Column():
|
| 68 |
+
audio = gr.Audio(label="Play song", type="filepath")
|
| 69 |
+
mp3_file = gr.File(label="Download MP3 320kbps")
|
| 70 |
+
info = gr.Textbox(label="Info")
|
| 71 |
+
|
| 72 |
+
btn.click(fn=generate, inputs=[style, lyrics, cot, seed, cfg], outputs=[audio, mp3_file, info], queue=True)
|
| 73 |
+
|
| 74 |
+
# 讛专爪讛 转讜讗诪转 Hugging Face Spaces
|
| 75 |
+
demo.queue().launch()
|