Spaces:
Paused
Paused
koby commited on
Commit ·
4a18a04
1
Parent(s): bb8ed57
exact duration: force min=max semantic tokens + end fade, slider to 180s
Browse files
app.py
CHANGED
|
@@ -68,6 +68,22 @@ def flac_to_wav(flac_path, wav_path):
|
|
| 68 |
return wav_path
|
| 69 |
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def get_duration_seconds(audio_path):
|
| 72 |
try:
|
| 73 |
import soundfile as sf
|
|
@@ -134,12 +150,14 @@ def generate(style, lyrics, cot, seed, cfg_scale, duration):
|
|
| 134 |
duration = int(duration) if duration else 120
|
| 135 |
except Exception:
|
| 136 |
duration = 120
|
| 137 |
-
duration = max(30, min(duration,
|
| 138 |
gen_kwargs = dict(style=style, lyrics=lyrics, cot=cot, seed=seed)
|
| 139 |
if cfg_scale and float(cfg_scale) != 1.0:
|
| 140 |
gen_kwargs["cfg_scale"] = float(cfg_scale)
|
| 141 |
-
#
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
song = pipeline(**gen_kwargs)
|
| 144 |
|
| 145 |
song_id = secrets.token_hex(6)
|
|
@@ -147,6 +165,7 @@ def generate(style, lyrics, cot, seed, cfg_scale, duration):
|
|
| 147 |
mp3_path = os.path.join(OUT, f"song_{song_id}.mp3")
|
| 148 |
wav_path = os.path.join(OUT, f"{song_id}.wav")
|
| 149 |
song.save(flac_path)
|
|
|
|
| 150 |
flac_to_mp3(flac_path, mp3_path)
|
| 151 |
flac_to_wav(flac_path, wav_path)
|
| 152 |
|
|
@@ -183,7 +202,7 @@ with gr.Blocks(title="YuE2-3B Music") as demo:
|
|
| 183 |
seed = gr.Number(label="Seed", value=0, precision=0)
|
| 184 |
cfg = gr.Number(label="CFG scale (1.0 = default)", value=1.0, precision=2)
|
| 185 |
with gr.Row():
|
| 186 |
-
duration = gr.Slider(30,
|
| 187 |
duration_txt = gr.Textbox(value="2:00 min", label="≈ time", scale=1, interactive=False)
|
| 188 |
btn = gr.Button("Generate song", variant="primary")
|
| 189 |
with gr.Column():
|
|
|
|
| 68 |
return wav_path
|
| 69 |
|
| 70 |
|
| 71 |
+
def _fade_out(flac_path, total_seconds, fade_dur=2.0):
|
| 72 |
+
"""Gentle fade at the very end so a forced stop never clicks. Keeps exact length."""
|
| 73 |
+
try:
|
| 74 |
+
if total_seconds <= fade_dur + 1:
|
| 75 |
+
return
|
| 76 |
+
tmp = flac_path + ".fade.flac"
|
| 77 |
+
subprocess.run(
|
| 78 |
+
["ffmpeg", "-y", "-i", flac_path, "-af",
|
| 79 |
+
f"afade=t=out:st={total_seconds - fade_dur:.2f}:d={fade_dur}", tmp],
|
| 80 |
+
capture_output=True, check=True,
|
| 81 |
+
)
|
| 82 |
+
os.replace(tmp, flac_path)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"[fade] skipped: {e}")
|
| 85 |
+
|
| 86 |
+
|
| 87 |
def get_duration_seconds(audio_path):
|
| 88 |
try:
|
| 89 |
import soundfile as sf
|
|
|
|
| 150 |
duration = int(duration) if duration else 120
|
| 151 |
except Exception:
|
| 152 |
duration = 120
|
| 153 |
+
duration = max(30, min(duration, 180))
|
| 154 |
gen_kwargs = dict(style=style, lyrics=lyrics, cot=cot, seed=seed)
|
| 155 |
if cfg_scale and float(cfg_scale) != 1.0:
|
| 156 |
gen_kwargs["cfg_scale"] = float(cfg_scale)
|
| 157 |
+
# exact duration: force the model to emit exactly this many semantic
|
| 158 |
+
# tokens (~25 tokens/sec of audio) — min == max, plus a gentle end fade
|
| 159 |
+
n_tokens = duration * 25
|
| 160 |
+
gen_kwargs["semantic_sampling"] = {"min_tokens": n_tokens, "max_tokens": n_tokens}
|
| 161 |
song = pipeline(**gen_kwargs)
|
| 162 |
|
| 163 |
song_id = secrets.token_hex(6)
|
|
|
|
| 165 |
mp3_path = os.path.join(OUT, f"song_{song_id}.mp3")
|
| 166 |
wav_path = os.path.join(OUT, f"{song_id}.wav")
|
| 167 |
song.save(flac_path)
|
| 168 |
+
_fade_out(flac_path, duration)
|
| 169 |
flac_to_mp3(flac_path, mp3_path)
|
| 170 |
flac_to_wav(flac_path, wav_path)
|
| 171 |
|
|
|
|
| 202 |
seed = gr.Number(label="Seed", value=0, precision=0)
|
| 203 |
cfg = gr.Number(label="CFG scale (1.0 = default)", value=1.0, precision=2)
|
| 204 |
with gr.Row():
|
| 205 |
+
duration = gr.Slider(30, 180, value=120, step=10, label="Duration (seconds, exact)", scale=4)
|
| 206 |
duration_txt = gr.Textbox(value="2:00 min", label="≈ time", scale=1, interactive=False)
|
| 207 |
btn = gr.Button("Generate song", variant="primary")
|
| 208 |
with gr.Column():
|