Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,56 +3,40 @@ import torch
|
|
| 3 |
from transformers import MusicgenForConditionalGeneration, AutoProcessor
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
# Load the model
|
| 7 |
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
| 8 |
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
| 9 |
|
| 10 |
def generate_music(prompt, duration):
|
| 11 |
if not prompt:
|
| 12 |
-
return None
|
| 13 |
-
|
| 14 |
-
# Prepare the input
|
| 15 |
inputs = processor(text=[prompt], padding=True, return_tensors="pt")
|
| 16 |
-
|
| 17 |
-
# Generate - 1 second is roughly 50 tokens
|
| 18 |
max_tokens = int(duration * 50)
|
| 19 |
audio_values = model.generate(**inputs, max_new_tokens=max_tokens)
|
| 20 |
|
| 21 |
-
# Get sampling rate and clean up audio for Gradio
|
| 22 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
| 23 |
audio_data = audio_values[0, 0].cpu().numpy()
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 29 |
-
gr.
|
| 30 |
-
*Describe a vibe, and our neural engine will compose a unique track for your blog.*""")
|
| 31 |
|
| 32 |
with gr.Row():
|
| 33 |
with gr.Column():
|
| 34 |
-
text_input = gr.Textbox(
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
duration_slider = gr.Slider(
|
| 39 |
-
minimum=2, maximum=15, value=8, step=1,
|
| 40 |
-
label="Duration (Seconds)"
|
| 41 |
-
)
|
| 42 |
-
generate_btn = gr.Button("Compose Music", variant="primary")
|
| 43 |
-
|
| 44 |
with gr.Column():
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
generate_btn.click(
|
| 48 |
-
fn=generate_music,
|
| 49 |
-
inputs=[text_input, duration_slider],
|
| 50 |
-
outputs=audio_output
|
| 51 |
-
)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
examples=[["Upbeat jazz with a fast trumpet solo", 10], ["Ambient space music with deep bass", 12]],
|
| 55 |
-
inputs=[text_input, duration_slider]
|
| 56 |
-
)
|
| 57 |
|
| 58 |
demo.launch()
|
|
|
|
| 3 |
from transformers import MusicgenForConditionalGeneration, AutoProcessor
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
# Load the model
|
| 7 |
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
| 8 |
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
| 9 |
|
| 10 |
def generate_music(prompt, duration):
|
| 11 |
if not prompt:
|
| 12 |
+
return None, "Please enter a description."
|
| 13 |
+
|
|
|
|
| 14 |
inputs = processor(text=[prompt], padding=True, return_tensors="pt")
|
|
|
|
|
|
|
| 15 |
max_tokens = int(duration * 50)
|
| 16 |
audio_values = model.generate(**inputs, max_new_tokens=max_tokens)
|
| 17 |
|
|
|
|
| 18 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
| 19 |
audio_data = audio_values[0, 0].cpu().numpy()
|
| 20 |
|
| 21 |
+
# Legal Certificate generated with the track
|
| 22 |
+
license_cert = f"✅ COMMERCIAL LICENSE ACTIVATED\nPrompt: {prompt}\nStatus: Royalty-Free for Commercial Use"
|
| 23 |
+
|
| 24 |
+
return (sampling_rate, audio_data), license_cert
|
| 25 |
|
| 26 |
+
# UI Layout
|
| 27 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 28 |
+
gr.HTML("<div style='text-align:center'><h1>🎹 NEURAL MUSIC STUDIO</h1><p>Generate & Own Your Tracks</p></div>")
|
|
|
|
| 29 |
|
| 30 |
with gr.Row():
|
| 31 |
with gr.Column():
|
| 32 |
+
text_input = gr.Textbox(label="Music Style", placeholder="e.g., Chill lo-fi hip hop")
|
| 33 |
+
duration_slider = gr.Slider(minimum=5, maximum=20, value=10, label="Length (Sec)")
|
| 34 |
+
generate_btn = gr.Button("Compose Now", variant="primary")
|
| 35 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
with gr.Column():
|
| 37 |
+
audio_out = gr.Audio(label="Studio Master Output", type="numpy")
|
| 38 |
+
license_out = gr.Textbox(label="Legal Rights", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
generate_btn.click(fn=generate_music, inputs=[text_input, duration_slider], outputs=[audio_out, license_out])
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
demo.launch()
|