Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,13 +19,9 @@ def generate_music(prompt, duration):
|
|
| 19 |
# Prepare inputs
|
| 20 |
inputs = processor(text=[prompt], return_tensors="pt").to(device)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
max_new_tokens = int(256 * (duration / 8))
|
| 24 |
-
max_new_tokens = min(max_new_tokens, 2048) # Safety cap
|
| 25 |
-
|
| 26 |
-
# Generate audio
|
| 27 |
with torch.no_grad():
|
| 28 |
-
audio = model.generate(**inputs,
|
| 29 |
|
| 30 |
sr = model.config.audio_encoder.sampling_rate
|
| 31 |
audio_arr = audio[0, 0].cpu().numpy()
|
|
@@ -37,7 +33,7 @@ def generate_music(prompt, duration):
|
|
| 37 |
return tmp.name, f"β
Generated {duration}s of audio!"
|
| 38 |
|
| 39 |
with gr.Blocks(title="MusicGen πΆ") as demo:
|
| 40 |
-
gr.Markdown("# π΅ MusicGen β Text-to-Music Generator (
|
| 41 |
with gr.Row():
|
| 42 |
prompt = gr.Textbox(label="πΌ Describe your music", placeholder="e.g. dreamy lo-fi with soft piano")
|
| 43 |
duration = gr.Slider(4, 40, value=15, step=1, label="Duration (seconds)")
|
|
@@ -46,4 +42,4 @@ with gr.Blocks(title="MusicGen πΆ") as demo:
|
|
| 46 |
msg = gr.Textbox(label="Status", interactive=False)
|
| 47 |
btn.click(generate_music, inputs=[prompt, duration], outputs=[audio_out, msg])
|
| 48 |
|
| 49 |
-
demo.launch(share=True)
|
|
|
|
| 19 |
# Prepare inputs
|
| 20 |
inputs = processor(text=[prompt], return_tensors="pt").to(device)
|
| 21 |
|
| 22 |
+
# β
Use exact audio length parameter instead of token approximation
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
with torch.no_grad():
|
| 24 |
+
audio = model.generate(**inputs, do_sample=True, guidance_scale=3, audio_length_in_s=int(duration))
|
| 25 |
|
| 26 |
sr = model.config.audio_encoder.sampling_rate
|
| 27 |
audio_arr = audio[0, 0].cpu().numpy()
|
|
|
|
| 33 |
return tmp.name, f"β
Generated {duration}s of audio!"
|
| 34 |
|
| 35 |
with gr.Blocks(title="MusicGen πΆ") as demo:
|
| 36 |
+
gr.Markdown("# π΅ MusicGen β Text-to-Music Generator (Accurate Duration 40s Version)")
|
| 37 |
with gr.Row():
|
| 38 |
prompt = gr.Textbox(label="πΌ Describe your music", placeholder="e.g. dreamy lo-fi with soft piano")
|
| 39 |
duration = gr.Slider(4, 40, value=15, step=1, label="Duration (seconds)")
|
|
|
|
| 42 |
msg = gr.Textbox(label="Status", interactive=False)
|
| 43 |
btn.click(generate_music, inputs=[prompt, duration], outputs=[audio_out, msg])
|
| 44 |
|
| 45 |
+
demo.launch(share=True)
|