koby commited on
Commit
4990590
·
1 Parent(s): eb50e6c

ending mode: exact vs natural with auto outro

Browse files
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import json
2
  import os
3
  import random
 
4
  import secrets
5
  import subprocess
6
  from datetime import datetime, timezone
@@ -140,7 +141,7 @@ def _upload_to_community(wav_path, meta_dict, song_id):
140
 
141
 
142
  @spaces.GPU(duration=120)
143
- def generate(style, lyrics, cot, seed, cfg_scale, duration):
144
  pipeline = get_pipeline()
145
  if seed is None or seed < 1:
146
  try:
@@ -153,13 +154,23 @@ def generate(style, lyrics, cot, seed, cfg_scale, duration):
153
  except Exception:
154
  duration = 120
155
  duration = max(30, min(duration, 360))
 
 
 
 
 
 
156
  gen_kwargs = dict(style=style, lyrics=lyrics, cot=cot, seed=seed)
157
  if cfg_scale and float(cfg_scale) != 1.0:
158
  gen_kwargs["cfg_scale"] = float(cfg_scale)
159
- # exact duration: force a hair MORE than needed (~25 tokens/sec),
160
- # then sample-trim to the number so players display it right
161
- n_tokens = duration * 25 + 25
162
- gen_kwargs["semantic_sampling"] = {"min_tokens": n_tokens, "max_tokens": n_tokens}
 
 
 
 
163
  song = pipeline(**gen_kwargs)
164
 
165
  song_id = secrets.token_hex(6)
@@ -167,7 +178,8 @@ def generate(style, lyrics, cot, seed, cfg_scale, duration):
167
  mp3_path = os.path.join(OUT, f"song_{song_id}.mp3")
168
  wav_path = os.path.join(OUT, f"{song_id}.wav")
169
  song.save(flac_path)
170
- _finish_length(flac_path, duration)
 
171
  flac_to_mp3(flac_path, mp3_path)
172
  flac_to_wav(flac_path, wav_path)
173
  ogg_path = os.path.join(OUT, f"{song_id}.ogg")
@@ -195,7 +207,7 @@ def generate(style, lyrics, cot, seed, cfg_scale, duration):
195
  "created_at": datetime.now(timezone.utc).isoformat(),
196
  }, song_id)
197
 
198
- return flac_path, flac_path, mp3_path, ogg_path, f"seed={seed} | {actual:.0f}s (asked {duration}s) | {os.path.getsize(mp3_path) // 1024}KB MP3 320kbps"
199
 
200
 
201
  with gr.Blocks(title="YuE2-3B Music") as demo:
@@ -209,7 +221,8 @@ with gr.Blocks(title="YuE2-3B Music") as demo:
209
  seed = gr.Number(label="Seed", value=0, precision=0)
210
  cfg = gr.Number(label="CFG scale (1.0 = default)", value=1.0, precision=2)
211
  with gr.Row():
212
- duration = gr.Slider(30, 360, value=60, step=10, label="Duration (seconds, exact)", scale=4)
 
213
  duration_txt = gr.Textbox(value="1:00 min", label="≈ time", scale=1, interactive=False)
214
  btn = gr.Button("Generate song", variant="primary")
215
  gr.HTML("<div style='text-align:center;margin:-12px 0 -8px;font-size:0.9rem;'>Don't forget to<a style='margin-left:5px;padding:0;' href='https://huggingface.co/m-a-p/YuE2-3B' target='_blank'>like the model ❤️</a></div>")
@@ -219,7 +232,7 @@ with gr.Blocks(title="YuE2-3B Music") as demo:
219
  mp3_file = gr.File(label="Download MP3 320kbps")
220
  ogg_file = gr.File(label="Download OGG (Opus)")
221
  info = gr.Textbox(label="Info")
222
- btn.click(fn=generate, inputs=[style, lyrics, cot, seed, cfg, duration], outputs=[audio, flac_file, mp3_file, ogg_file, info], queue=True)
223
  duration.change(fn=_dur_label, inputs=duration, outputs=duration_txt)
224
  gr.Markdown(
225
  "---\nPowered by [YuE2](https://huggingface.co/m-a-p/YuE2-3B) · "
 
1
  import json
2
  import os
3
  import random
4
+ import re
5
  import secrets
6
  import subprocess
7
  from datetime import datetime, timezone
 
141
 
142
 
143
  @spaces.GPU(duration=120)
144
+ def generate(style, lyrics, cot, seed, cfg_scale, duration, ending):
145
  pipeline = get_pipeline()
146
  if seed is None or seed < 1:
147
  try:
 
154
  except Exception:
155
  duration = 120
156
  duration = max(30, min(duration, 360))
157
+ exact = str(ending or "Exact").lower().startswith("exact")
158
+ if not exact:
159
+ # Natural: let the model finish on its own; close with a real [Outro]
160
+ # tag (official YuE2 vocabulary) so it composes a proper ending.
161
+ if not re.search(r"\[(outro|end|ending)\]", lyrics or "", re.I):
162
+ lyrics = (lyrics or "").rstrip() + "\n[Outro]"
163
  gen_kwargs = dict(style=style, lyrics=lyrics, cot=cot, seed=seed)
164
  if cfg_scale and float(cfg_scale) != 1.0:
165
  gen_kwargs["cfg_scale"] = float(cfg_scale)
166
+ if exact:
167
+ # exact duration: force a hair MORE than needed (~25 tokens/sec),
168
+ # then sample-trim to the number so players display it right
169
+ n_tokens = duration * 25 + 25
170
+ gen_kwargs["semantic_sampling"] = {"min_tokens": n_tokens, "max_tokens": n_tokens}
171
+ else:
172
+ # natural duration: only cap the maximum, model ends when it ends
173
+ gen_kwargs["semantic_sampling"] = {"max_tokens": duration * 25}
174
  song = pipeline(**gen_kwargs)
175
 
176
  song_id = secrets.token_hex(6)
 
178
  mp3_path = os.path.join(OUT, f"song_{song_id}.mp3")
179
  wav_path = os.path.join(OUT, f"{song_id}.wav")
180
  song.save(flac_path)
181
+ if exact:
182
+ _finish_length(flac_path, duration)
183
  flac_to_mp3(flac_path, mp3_path)
184
  flac_to_wav(flac_path, wav_path)
185
  ogg_path = os.path.join(OUT, f"{song_id}.ogg")
 
207
  "created_at": datetime.now(timezone.utc).isoformat(),
208
  }, song_id)
209
 
210
+ return flac_path, flac_path, mp3_path, ogg_path, f"seed={seed} | {actual:.0f}s" + (f" (asked {duration}s)" if exact else f" (max {duration}s)") + f" | {os.path.getsize(mp3_path) // 1024}KB MP3 320kbps"
211
 
212
 
213
  with gr.Blocks(title="YuE2-3B Music") as demo:
 
221
  seed = gr.Number(label="Seed", value=0, precision=0)
222
  cfg = gr.Number(label="CFG scale (1.0 = default)", value=1.0, precision=2)
223
  with gr.Row():
224
+ duration = gr.Slider(30, 360, value=60, step=10, label="Duration (seconds)", scale=3)
225
+ ending = gr.Radio(["Exact", "Natural"], value="Exact", label="Ending", scale=2)
226
  duration_txt = gr.Textbox(value="1:00 min", label="≈ time", scale=1, interactive=False)
227
  btn = gr.Button("Generate song", variant="primary")
228
  gr.HTML("<div style='text-align:center;margin:-12px 0 -8px;font-size:0.9rem;'>Don't forget to<a style='margin-left:5px;padding:0;' href='https://huggingface.co/m-a-p/YuE2-3B' target='_blank'>like the model ❤️</a></div>")
 
232
  mp3_file = gr.File(label="Download MP3 320kbps")
233
  ogg_file = gr.File(label="Download OGG (Opus)")
234
  info = gr.Textbox(label="Info")
235
+ btn.click(fn=generate, inputs=[style, lyrics, cot, seed, cfg, duration, ending], outputs=[audio, flac_file, mp3_file, ogg_file, info], queue=True)
236
  duration.change(fn=_dur_label, inputs=duration, outputs=duration_txt)
237
  gr.Markdown(
238
  "---\nPowered by [YuE2](https://huggingface.co/m-a-p/YuE2-3B) · "