Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,62 +3,83 @@ from audiocraft.models import MusicGen
|
|
| 3 |
from audiocraft.data.audio import audio_write
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Load the model
|
| 8 |
-
# This will download the first time you run the app
|
| 9 |
model = MusicGen.get_pretrained('facebook/musicgen-melody')
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
model.set_generation_params(duration=duration)
|
| 17 |
|
| 18 |
-
# Get the sampling rate and the audio data from your upload
|
| 19 |
sr, data = audio_input
|
| 20 |
-
|
| 21 |
-
# Convert to the format the AI needs
|
| 22 |
audio_tensor = torch.from_numpy(data).float().t().unsqueeze(0)
|
| 23 |
-
if audio_tensor.shape[1] > 1:
|
| 24 |
audio_tensor = audio_tensor.mean(dim=1, keepdim=True)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
wav = model.generate_with_chroma(
|
| 28 |
-
descriptions=[text_description],
|
| 29 |
-
melody_wavs=audio_tensor,
|
| 30 |
-
sr=sr
|
| 31 |
-
)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
| 35 |
audio_write(output_path, wav[0].cpu(), model.sample_rate, strategy="loudness")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
# Create a sleek Dark Mode interface
|
| 41 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
-
with gr.Column():
|
| 47 |
-
audio_in = gr.Audio(label="
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
submit_btn.click(
|
| 59 |
fn=finish_my_song,
|
| 60 |
-
inputs=[audio_in,
|
| 61 |
-
outputs=[
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
demo.launch()
|
|
|
|
| 3 |
from audiocraft.data.audio import audio_write
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
+
import random
|
| 7 |
+
import uuid # For unique filenames
|
| 8 |
|
| 9 |
+
# Load the model
|
|
|
|
| 10 |
model = MusicGen.get_pretrained('facebook/musicgen-melody')
|
| 11 |
|
| 12 |
+
STYLE_MAP = {
|
| 13 |
+
"Phonk": "Add aggressive high-pitched cowbell melodies, distorted 808 percussion, and a dark, lo-fi Memphis rap atmosphere. Heavy sidechain compression, 120 BPM, gritty and phonk-style.",
|
| 14 |
+
"Hyperpop": "Add high-energy metallic percussion, glitchy synth arpeggios, and bright bubblegum-pop textures. Use sharp, digital drums and a very polished, futuristic sound. 160 BPM.",
|
| 15 |
+
"Neo-Soul": "Add a warm Rhodes electric piano layer, subtle jazzy guitar licks, and a laid-back, swinging drum kit with soft rimshots. Soulful, organic, 90 BPM, deep groove.",
|
| 16 |
+
"Dark Techno": "Add a heavy, driving industrial kick drum, pulsing white noise textures, and a rhythmic hypnotic synth sequence. Minimalist, dark warehouse vibe, 130 BPM.",
|
| 17 |
+
"Lofi Hip-Hop": "Add a dusty, bit-crushed boom-bap drum loop, mellow vinyl crackle, and a soft flute melody in the background. Nostalgic, rainy day vibe, 85 BPM.",
|
| 18 |
+
"Custom (Type Below)": ""
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
def get_random_style():
|
| 22 |
+
choices = [s for s in STYLE_MAP.keys() if s != "Custom (Type Below)"]
|
| 23 |
+
return random.choice(choices)
|
| 24 |
|
| 25 |
+
def finish_my_song(audio_input, style_choice, custom_prompt, duration, current_history):
|
| 26 |
+
if audio_input is None:
|
| 27 |
+
return None, current_history
|
| 28 |
+
|
| 29 |
+
final_prompt = custom_prompt if style_choice == "Custom (Type Below)" else STYLE_MAP[style_choice]
|
| 30 |
model.set_generation_params(duration=duration)
|
| 31 |
|
|
|
|
| 32 |
sr, data = audio_input
|
|
|
|
|
|
|
| 33 |
audio_tensor = torch.from_numpy(data).float().t().unsqueeze(0)
|
| 34 |
+
if audio_tensor.shape[1] > 1:
|
| 35 |
audio_tensor = audio_tensor.mean(dim=1, keepdim=True)
|
| 36 |
|
| 37 |
+
wav = model.generate_with_chroma(descriptions=[final_prompt], melody_wavs=audio_tensor, sr=sr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Generate a unique filename so history doesn't overwrite itself
|
| 40 |
+
unique_id = str(uuid.uuid4())[:8]
|
| 41 |
+
output_path = f"finished_beat_{unique_id}"
|
| 42 |
audio_write(output_path, wav[0].cpu(), model.sample_rate, strategy="loudness")
|
| 43 |
|
| 44 |
+
full_file_path = f"{output_path}.wav"
|
| 45 |
+
|
| 46 |
+
# Add new file to the top of history
|
| 47 |
+
new_history = [full_file_path] + current_history
|
| 48 |
+
return full_file_path, new_history
|
| 49 |
|
|
|
|
| 50 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 51 |
+
# State to hold the list of file paths
|
| 52 |
+
history_state = gr.State([])
|
| 53 |
+
|
| 54 |
+
gr.Markdown("# 🎹 The Song Finisher Pro + History")
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
+
with gr.Column(scale=2):
|
| 58 |
+
audio_in = gr.Audio(label="Upload Chords/Bass")
|
| 59 |
+
with gr.Row():
|
| 60 |
+
style_dropdown = gr.Dropdown(choices=list(STYLE_MAP.keys()), value="Phonk", label="Style")
|
| 61 |
+
random_btn = gr.Button("🎲 Random")
|
| 62 |
+
|
| 63 |
+
custom_text = gr.Textbox(label="Custom Prompt", placeholder="Type here...")
|
| 64 |
+
length = gr.Slider(minimum=5, maximum=30, value=15, step=5, label="Seconds")
|
| 65 |
+
submit_btn = gr.Button("🔥 Generate Arrangement", variant="primary")
|
| 66 |
+
|
| 67 |
+
latest_output = gr.Audio(label="Latest Generation", type="filepath")
|
| 68 |
|
| 69 |
+
with gr.Column(scale=1):
|
| 70 |
+
gr.Markdown("### 📜 Session History")
|
| 71 |
+
# This gallery shows all previous versions
|
| 72 |
+
history_gallery = gr.Files(label="Download Past Versions", interactive=False)
|
| 73 |
+
|
| 74 |
+
# Logic
|
| 75 |
+
random_btn.click(fn=get_random_style, outputs=style_dropdown)
|
| 76 |
+
|
| 77 |
submit_btn.click(
|
| 78 |
fn=finish_my_song,
|
| 79 |
+
inputs=[audio_in, style_dropdown, custom_text, length, history_state],
|
| 80 |
+
outputs=[latest_output, history_state]
|
| 81 |
+
).then(
|
| 82 |
+
fn=lambda x: x, inputs=history_state, outputs=history_gallery
|
| 83 |
)
|
| 84 |
|
| 85 |
demo.launch()
|