Spaces:
Configuration error
Configuration error
Commit ·
672a4ea
1
Parent(s): df65fb0
not sure what is going wrong now ugh
Browse files
app.py
CHANGED
|
@@ -11,29 +11,18 @@ from pydub import AudioSegment
|
|
| 11 |
import io
|
| 12 |
import random
|
| 13 |
|
| 14 |
-
|
| 15 |
-
#logging.basicConfig(level=logging.DEBUG)
|
| 16 |
-
|
| 17 |
# Check if CUDA is available
|
| 18 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 19 |
|
| 20 |
-
# Placeholder Utility Functions
|
| 21 |
-
#def peak_normalize(y, target_peak=0.97):
|
| 22 |
-
# return target_peak * (y / np.max(np.abs(y)))
|
| 23 |
-
#
|
| 24 |
-
#def rms_normalize(y, target_rms=0.05):
|
| 25 |
-
# return y * (target_rms / np.sqrt(np.mean(y**2)))
|
| 26 |
-
|
| 27 |
def preprocess_audio(waveform):
|
| 28 |
-
waveform_np = waveform.cpu().squeeze().numpy()
|
| 29 |
-
# processed_waveform_np = rms_normalize(peak_normalize(waveform_np))
|
| 30 |
return torch.from_numpy(waveform_np).unsqueeze(0).to(device)
|
| 31 |
|
| 32 |
@spaces.GPU(duration=10)
|
| 33 |
def generate_drum_sample():
|
| 34 |
model = MusicGen.get_pretrained('pharoAIsanders420/micro-musicgen-jungle')
|
| 35 |
model.set_generation_params(duration=10)
|
| 36 |
-
wav = model.generate_unconditional(1).squeeze(0)
|
| 37 |
|
| 38 |
filename_without_extension = f'jungle'
|
| 39 |
filename_with_extension = f'{filename_without_extension}.wav'
|
|
@@ -44,15 +33,15 @@ def generate_drum_sample():
|
|
| 44 |
|
| 45 |
@spaces.GPU(duration=10)
|
| 46 |
def continue_drum_sample(existing_audio_path):
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
existing_audio, sr = torchaudio.load(existing_audio_path)
|
| 49 |
-
existing_audio = existing_audio.to(device)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
output_duration = 10 # seconds
|
| 54 |
|
| 55 |
-
# Calculate the slice from the end of the current audio based on prompt_duration
|
| 56 |
num_samples = int(prompt_duration * sr)
|
| 57 |
if existing_audio.shape[1] < num_samples:
|
| 58 |
raise ValueError("The existing audio is too short for the specified prompt duration.")
|
|
@@ -60,43 +49,37 @@ def continue_drum_sample(existing_audio_path):
|
|
| 60 |
start_sample = existing_audio.shape[1] - num_samples
|
| 61 |
prompt_waveform = existing_audio[..., start_sample:]
|
| 62 |
|
| 63 |
-
# Assume model is already loaded and configured to generate drum samples
|
| 64 |
model = MusicGen.get_pretrained('pharoAIsanders420/micro-musicgen-jungle')
|
| 65 |
model.set_generation_params(duration=output_duration)
|
| 66 |
|
| 67 |
-
# Generate continuation
|
| 68 |
output = model.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
|
| 69 |
-
output = output.to(device)
|
| 70 |
|
| 71 |
-
if output.dim() == 3:
|
| 72 |
-
output = output.squeeze(0)
|
| 73 |
|
| 74 |
if output.dim() == 1:
|
| 75 |
-
output = output.unsqueeze(0)
|
| 76 |
|
| 77 |
-
# Combine the new output with the existing audio
|
| 78 |
combined_audio = torch.cat((existing_audio, output), dim=1)
|
| 79 |
-
|
| 80 |
-
# Move combined audio to CPU for saving
|
| 81 |
combined_audio = combined_audio.cpu()
|
| 82 |
|
| 83 |
-
# Save combined audio to a new file
|
| 84 |
combined_file_path = f'./continued_jungle_{random.randint(1000, 9999)}.wav'
|
| 85 |
torchaudio.save(combined_file_path, combined_audio, sr)
|
| 86 |
|
| 87 |
return combined_file_path
|
| 88 |
|
| 89 |
-
@spaces.GPU(
|
| 90 |
def generate_music(wav_filename, prompt_duration, musicgen_model, output_duration):
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
song, sr = torchaudio.load(wav_filename)
|
| 93 |
song = song.to(device)
|
| 94 |
|
| 95 |
-
# Load the model
|
| 96 |
model_name = musicgen_model.split(" ")[0]
|
| 97 |
model_continue = MusicGen.get_pretrained(model_name)
|
| 98 |
|
| 99 |
-
# Setting generation parameters
|
| 100 |
model_continue.set_generation_params(
|
| 101 |
use_sampling=True,
|
| 102 |
top_k=250,
|
|
@@ -110,9 +93,8 @@ def generate_music(wav_filename, prompt_duration, musicgen_model, output_duratio
|
|
| 110 |
prompt_waveform = preprocess_audio(prompt_waveform)
|
| 111 |
|
| 112 |
output = model_continue.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
|
| 113 |
-
output = output.cpu()
|
| 114 |
|
| 115 |
-
# Ensure the output tensor has at most 2 dimensions
|
| 116 |
if len(output.size()) > 2:
|
| 117 |
output = output.squeeze()
|
| 118 |
|
|
@@ -124,11 +106,12 @@ def generate_music(wav_filename, prompt_duration, musicgen_model, output_duratio
|
|
| 124 |
|
| 125 |
@spaces.GPU(duration=120)
|
| 126 |
def continue_music(input_audio_path, prompt_duration, musicgen_model, output_duration):
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
song, sr = torchaudio.load(input_audio_path)
|
| 129 |
song = song.to(device)
|
| 130 |
|
| 131 |
-
# Load the model and set generation parameters
|
| 132 |
model_continue = MusicGen.get_pretrained(musicgen_model.split(" ")[0])
|
| 133 |
model_continue.set_generation_params(
|
| 134 |
use_sampling=True,
|
|
@@ -142,10 +125,9 @@ def continue_music(input_audio_path, prompt_duration, musicgen_model, output_dur
|
|
| 142 |
original_audio = AudioSegment.from_mp3(input_audio_path)
|
| 143 |
current_audio = original_audio
|
| 144 |
|
| 145 |
-
file_paths_for_cleanup = []
|
| 146 |
|
| 147 |
for i in range(1):
|
| 148 |
-
# Calculate the slice from the end of the current audio based on prompt_duration
|
| 149 |
num_samples = int(prompt_duration * sr)
|
| 150 |
if current_audio.duration_seconds * 1000 < prompt_duration * 1000:
|
| 151 |
raise ValueError("The prompt_duration is longer than the current audio length.")
|
|
@@ -153,42 +135,38 @@ def continue_music(input_audio_path, prompt_duration, musicgen_model, output_dur
|
|
| 153 |
start_time = current_audio.duration_seconds * 1000 - prompt_duration * 1000
|
| 154 |
prompt_audio = current_audio[start_time:]
|
| 155 |
|
| 156 |
-
# Convert the prompt audio to a PyTorch tensor
|
| 157 |
prompt_bytes = prompt_audio.export(format="wav").read()
|
| 158 |
prompt_waveform, _ = torchaudio.load(io.BytesIO(prompt_bytes))
|
| 159 |
prompt_waveform = prompt_waveform.to(device)
|
| 160 |
|
| 161 |
-
# Prepare the audio slice for generation
|
| 162 |
prompt_waveform = preprocess_audio(prompt_waveform)
|
| 163 |
|
| 164 |
output = model_continue.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
|
| 165 |
-
output = output.cpu()
|
| 166 |
|
| 167 |
if len(output.size()) > 2:
|
| 168 |
output = output.squeeze()
|
| 169 |
|
| 170 |
filename_without_extension = f'continue_{i}'
|
| 171 |
filename_with_extension = f'{filename_without_extension}.wav'
|
| 172 |
-
correct_filename_extension = f'{filename_without_extension}.wav.wav'
|
| 173 |
|
| 174 |
audio_write(filename_with_extension, output, model_continue.sample_rate, strategy="loudness", loudness_compressor=True)
|
| 175 |
generated_audio_segment = AudioSegment.from_wav(correct_filename_extension)
|
| 176 |
|
| 177 |
-
# Replace the prompt portion with the generated audio
|
| 178 |
current_audio = current_audio[:start_time] + generated_audio_segment
|
| 179 |
|
| 180 |
-
file_paths_for_cleanup.append(correct_filename_extension)
|
| 181 |
|
| 182 |
combined_audio_filename = f"combined_audio_{random.randint(1, 10000)}.mp3"
|
| 183 |
current_audio.export(combined_audio_filename, format="mp3")
|
| 184 |
|
| 185 |
-
# Clean up temporary files using the list of file paths
|
| 186 |
for file_path in file_paths_for_cleanup:
|
| 187 |
os.remove(file_path)
|
| 188 |
|
| 189 |
return combined_audio_filename
|
| 190 |
|
| 191 |
-
|
| 192 |
musicgen_micro_blurb = """
|
| 193 |
## musicgen_micro
|
| 194 |
musicgen micro is an experimental series of models by aaron abebe. they are incredibly fast, and extra insane. this one does goated jungle drums. we're very excited about these.
|
|
@@ -211,7 +189,6 @@ the fine-tunes hosted on the huggingface hub are provided collectively by the mu
|
|
| 211 |
[<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" style="vertical-align:middle"> fine-tuning colab notebook by lyra](https://colab.research.google.com/drive/13tbcC3A42KlaUZ21qvUXd25SFLu8WIvb)
|
| 212 |
"""
|
| 213 |
|
| 214 |
-
# Define the fine-tunes blurb for each model
|
| 215 |
fine_tunes_info = """
|
| 216 |
## thepatch/vanya_ai_dnb_0.1
|
| 217 |
thepatch/vanya_ai_dnb_0.1 was trained by vanya. [vanya's Twitter](https://twitter.com/@veryVANYA) 🔗 - it treats almost all input audio as the beginning of a buildup to a dnb drop (can do downtempo well)
|
|
@@ -232,7 +209,7 @@ thepatch/PhonkV2 was trained by MJ BERSABEph. there are multiple versions in the
|
|
| 232 |
foureyednymph/musicgen-sza-sos-small was just trained by foureyednymph. We're all about to find out if it does continuations well.
|
| 233 |
"""
|
| 234 |
|
| 235 |
-
# Create the Gradio interface
|
| 236 |
with gr.Blocks() as iface:
|
| 237 |
gr.Markdown("# the-micro-slot-machine")
|
| 238 |
gr.Markdown("two ai's jamming. warning: outputs will be very strange, likely stupid, and possibly rad.")
|
|
@@ -249,21 +226,39 @@ with gr.Blocks() as iface:
|
|
| 249 |
with gr.Row():
|
| 250 |
with gr.Column():
|
| 251 |
generate_button = gr.Button("Generate Drum Sample")
|
| 252 |
-
drum_audio = gr.Audio(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
continue_drum_sample_button = gr.Button("Continue Drum Sample")
|
| 254 |
-
|
| 255 |
|
| 256 |
with gr.Column():
|
| 257 |
-
prompt_duration = gr.Dropdown(
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
"
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
generate_music_button = gr.Button("Generate Music")
|
| 268 |
output_audio = gr.Audio(label="Generated Music", type="filepath")
|
| 269 |
continue_button = gr.Button("Continue Generating Music")
|
|
@@ -275,4 +270,5 @@ with gr.Blocks() as iface:
|
|
| 275 |
generate_music_button.click(generate_music, inputs=[drum_audio, prompt_duration, musicgen_model, output_duration], outputs=[output_audio])
|
| 276 |
continue_button.click(continue_music, inputs=[output_audio, prompt_duration, musicgen_model, output_duration], outputs=continue_output_audio)
|
| 277 |
|
| 278 |
-
|
|
|
|
|
|
| 11 |
import io
|
| 12 |
import random
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
# Check if CUDA is available
|
| 15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def preprocess_audio(waveform):
|
| 18 |
+
waveform_np = waveform.cpu().squeeze().numpy()
|
|
|
|
| 19 |
return torch.from_numpy(waveform_np).unsqueeze(0).to(device)
|
| 20 |
|
| 21 |
@spaces.GPU(duration=10)
|
| 22 |
def generate_drum_sample():
|
| 23 |
model = MusicGen.get_pretrained('pharoAIsanders420/micro-musicgen-jungle')
|
| 24 |
model.set_generation_params(duration=10)
|
| 25 |
+
wav = model.generate_unconditional(1).squeeze(0)
|
| 26 |
|
| 27 |
filename_without_extension = f'jungle'
|
| 28 |
filename_with_extension = f'{filename_without_extension}.wav'
|
|
|
|
| 33 |
|
| 34 |
@spaces.GPU(duration=10)
|
| 35 |
def continue_drum_sample(existing_audio_path):
|
| 36 |
+
if existing_audio_path is None:
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
existing_audio, sr = torchaudio.load(existing_audio_path)
|
| 40 |
+
existing_audio = existing_audio.to(device)
|
| 41 |
|
| 42 |
+
prompt_duration = 2
|
| 43 |
+
output_duration = 10
|
|
|
|
| 44 |
|
|
|
|
| 45 |
num_samples = int(prompt_duration * sr)
|
| 46 |
if existing_audio.shape[1] < num_samples:
|
| 47 |
raise ValueError("The existing audio is too short for the specified prompt duration.")
|
|
|
|
| 49 |
start_sample = existing_audio.shape[1] - num_samples
|
| 50 |
prompt_waveform = existing_audio[..., start_sample:]
|
| 51 |
|
|
|
|
| 52 |
model = MusicGen.get_pretrained('pharoAIsanders420/micro-musicgen-jungle')
|
| 53 |
model.set_generation_params(duration=output_duration)
|
| 54 |
|
|
|
|
| 55 |
output = model.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
|
| 56 |
+
output = output.to(device)
|
| 57 |
|
| 58 |
+
if output.dim() == 3:
|
| 59 |
+
output = output.squeeze(0)
|
| 60 |
|
| 61 |
if output.dim() == 1:
|
| 62 |
+
output = output.unsqueeze(0)
|
| 63 |
|
|
|
|
| 64 |
combined_audio = torch.cat((existing_audio, output), dim=1)
|
|
|
|
|
|
|
| 65 |
combined_audio = combined_audio.cpu()
|
| 66 |
|
|
|
|
| 67 |
combined_file_path = f'./continued_jungle_{random.randint(1000, 9999)}.wav'
|
| 68 |
torchaudio.save(combined_file_path, combined_audio, sr)
|
| 69 |
|
| 70 |
return combined_file_path
|
| 71 |
|
| 72 |
+
@spaces.GPU(duration=120)
|
| 73 |
def generate_music(wav_filename, prompt_duration, musicgen_model, output_duration):
|
| 74 |
+
if wav_filename is None:
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
song, sr = torchaudio.load(wav_filename)
|
| 78 |
song = song.to(device)
|
| 79 |
|
|
|
|
| 80 |
model_name = musicgen_model.split(" ")[0]
|
| 81 |
model_continue = MusicGen.get_pretrained(model_name)
|
| 82 |
|
|
|
|
| 83 |
model_continue.set_generation_params(
|
| 84 |
use_sampling=True,
|
| 85 |
top_k=250,
|
|
|
|
| 93 |
prompt_waveform = preprocess_audio(prompt_waveform)
|
| 94 |
|
| 95 |
output = model_continue.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
|
| 96 |
+
output = output.cpu()
|
| 97 |
|
|
|
|
| 98 |
if len(output.size()) > 2:
|
| 99 |
output = output.squeeze()
|
| 100 |
|
|
|
|
| 106 |
|
| 107 |
@spaces.GPU(duration=120)
|
| 108 |
def continue_music(input_audio_path, prompt_duration, musicgen_model, output_duration):
|
| 109 |
+
if input_audio_path is None:
|
| 110 |
+
return None
|
| 111 |
+
|
| 112 |
song, sr = torchaudio.load(input_audio_path)
|
| 113 |
song = song.to(device)
|
| 114 |
|
|
|
|
| 115 |
model_continue = MusicGen.get_pretrained(musicgen_model.split(" ")[0])
|
| 116 |
model_continue.set_generation_params(
|
| 117 |
use_sampling=True,
|
|
|
|
| 125 |
original_audio = AudioSegment.from_mp3(input_audio_path)
|
| 126 |
current_audio = original_audio
|
| 127 |
|
| 128 |
+
file_paths_for_cleanup = []
|
| 129 |
|
| 130 |
for i in range(1):
|
|
|
|
| 131 |
num_samples = int(prompt_duration * sr)
|
| 132 |
if current_audio.duration_seconds * 1000 < prompt_duration * 1000:
|
| 133 |
raise ValueError("The prompt_duration is longer than the current audio length.")
|
|
|
|
| 135 |
start_time = current_audio.duration_seconds * 1000 - prompt_duration * 1000
|
| 136 |
prompt_audio = current_audio[start_time:]
|
| 137 |
|
|
|
|
| 138 |
prompt_bytes = prompt_audio.export(format="wav").read()
|
| 139 |
prompt_waveform, _ = torchaudio.load(io.BytesIO(prompt_bytes))
|
| 140 |
prompt_waveform = prompt_waveform.to(device)
|
| 141 |
|
|
|
|
| 142 |
prompt_waveform = preprocess_audio(prompt_waveform)
|
| 143 |
|
| 144 |
output = model_continue.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
|
| 145 |
+
output = output.cpu()
|
| 146 |
|
| 147 |
if len(output.size()) > 2:
|
| 148 |
output = output.squeeze()
|
| 149 |
|
| 150 |
filename_without_extension = f'continue_{i}'
|
| 151 |
filename_with_extension = f'{filename_without_extension}.wav'
|
| 152 |
+
correct_filename_extension = f'{filename_without_extension}.wav.wav'
|
| 153 |
|
| 154 |
audio_write(filename_with_extension, output, model_continue.sample_rate, strategy="loudness", loudness_compressor=True)
|
| 155 |
generated_audio_segment = AudioSegment.from_wav(correct_filename_extension)
|
| 156 |
|
|
|
|
| 157 |
current_audio = current_audio[:start_time] + generated_audio_segment
|
| 158 |
|
| 159 |
+
file_paths_for_cleanup.append(correct_filename_extension)
|
| 160 |
|
| 161 |
combined_audio_filename = f"combined_audio_{random.randint(1, 10000)}.mp3"
|
| 162 |
current_audio.export(combined_audio_filename, format="mp3")
|
| 163 |
|
|
|
|
| 164 |
for file_path in file_paths_for_cleanup:
|
| 165 |
os.remove(file_path)
|
| 166 |
|
| 167 |
return combined_audio_filename
|
| 168 |
|
| 169 |
+
# Define the expandable sections (keeping your existing content)
|
| 170 |
musicgen_micro_blurb = """
|
| 171 |
## musicgen_micro
|
| 172 |
musicgen micro is an experimental series of models by aaron abebe. they are incredibly fast, and extra insane. this one does goated jungle drums. we're very excited about these.
|
|
|
|
| 189 |
[<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" style="vertical-align:middle"> fine-tuning colab notebook by lyra](https://colab.research.google.com/drive/13tbcC3A42KlaUZ21qvUXd25SFLu8WIvb)
|
| 190 |
"""
|
| 191 |
|
|
|
|
| 192 |
fine_tunes_info = """
|
| 193 |
## thepatch/vanya_ai_dnb_0.1
|
| 194 |
thepatch/vanya_ai_dnb_0.1 was trained by vanya. [vanya's Twitter](https://twitter.com/@veryVANYA) 🔗 - it treats almost all input audio as the beginning of a buildup to a dnb drop (can do downtempo well)
|
|
|
|
| 209 |
foureyednymph/musicgen-sza-sos-small was just trained by foureyednymph. We're all about to find out if it does continuations well.
|
| 210 |
"""
|
| 211 |
|
| 212 |
+
# Create the Gradio interface with explicit types
|
| 213 |
with gr.Blocks() as iface:
|
| 214 |
gr.Markdown("# the-micro-slot-machine")
|
| 215 |
gr.Markdown("two ai's jamming. warning: outputs will be very strange, likely stupid, and possibly rad.")
|
|
|
|
| 226 |
with gr.Row():
|
| 227 |
with gr.Column():
|
| 228 |
generate_button = gr.Button("Generate Drum Sample")
|
| 229 |
+
drum_audio = gr.Audio(
|
| 230 |
+
label="Generated Drum Sample",
|
| 231 |
+
type="filepath",
|
| 232 |
+
interactive=True,
|
| 233 |
+
show_download_button=True
|
| 234 |
+
)
|
| 235 |
continue_drum_sample_button = gr.Button("Continue Drum Sample")
|
|
|
|
| 236 |
|
| 237 |
with gr.Column():
|
| 238 |
+
prompt_duration = gr.Dropdown(
|
| 239 |
+
label="Prompt Duration (seconds)",
|
| 240 |
+
choices=list(range(1, 11)),
|
| 241 |
+
value=5
|
| 242 |
+
)
|
| 243 |
+
output_duration = gr.Slider(
|
| 244 |
+
label="Output Duration (seconds)",
|
| 245 |
+
minimum=10,
|
| 246 |
+
maximum=30,
|
| 247 |
+
step=1,
|
| 248 |
+
value=20
|
| 249 |
+
)
|
| 250 |
+
musicgen_model = gr.Dropdown(
|
| 251 |
+
label="MusicGen Model",
|
| 252 |
+
choices=[
|
| 253 |
+
"thepatch/vanya_ai_dnb_0.1 (small)",
|
| 254 |
+
"thepatch/budots_remix (small)",
|
| 255 |
+
"thepatch/PhonkV2 (small)",
|
| 256 |
+
"thepatch/bleeps-medium (medium)",
|
| 257 |
+
"thepatch/hoenn_lofi (large)",
|
| 258 |
+
"foureyednymph/musicgen-sza-sos-small (small)"
|
| 259 |
+
],
|
| 260 |
+
value="thepatch/vanya_ai_dnb_0.1 (small)"
|
| 261 |
+
)
|
| 262 |
generate_music_button = gr.Button("Generate Music")
|
| 263 |
output_audio = gr.Audio(label="Generated Music", type="filepath")
|
| 264 |
continue_button = gr.Button("Continue Generating Music")
|
|
|
|
| 270 |
generate_music_button.click(generate_music, inputs=[drum_audio, prompt_duration, musicgen_model, output_duration], outputs=[output_audio])
|
| 271 |
continue_button.click(continue_music, inputs=[output_audio, prompt_duration, musicgen_model, output_duration], outputs=continue_output_audio)
|
| 272 |
|
| 273 |
+
if __name__ == "__main__":
|
| 274 |
+
iface.launch()
|