|
import torchaudio |
|
import torchaudio.transforms as T |
|
import matplotlib.pyplot as plt |
|
from transformers import AutoProcessor, MusicgenForConditionalGeneration |
|
import gradio as gr |
|
import numpy as np |
|
import torch |
|
|
|
|
|
model_options = { |
|
"Medium Model": "facebook/musicgen-medium", |
|
"Large Model": "facebook/musicgen-large" |
|
} |
|
|
|
|
|
style_tags_options = ["East Coast", "Trap", "Boom Bap", "Lo-Fi", "Experimental"] |
|
|
|
def generate_spectrogram(audio_tensor, sample_rate): |
|
spectrogram_transform = T.MelSpectrogram(sample_rate=sample_rate) |
|
spectrogram = spectrogram_transform(audio_tensor.unsqueeze(0)) |
|
spectrogram_db = T.AmplitudeToDB()(spectrogram) |
|
|
|
plt.figure(figsize=(10, 4)) |
|
plt.imshow(spectrogram_db.log2()[0,:,:].detach().numpy(), cmap='viridis', aspect='auto') |
|
plt.colorbar(format='%+2.0f dB') |
|
plt.title('Mel Spectrogram') |
|
plt.tight_layout() |
|
plt.ylabel('Mel bins') |
|
plt.xlabel('Time') |
|
spectrogram_path = "generated_spectrogram.png" |
|
plt.savefig(spectrogram_path) |
|
plt.close() |
|
return spectrogram_path |
|
|
|
def generate_music(description, model_choice, style_tags, tempo, intensity, duration): |
|
try: |
|
processor = AutoProcessor.from_pretrained(model_options[model_choice]) |
|
model = MusicgenForConditionalGeneration.from_pretrained(model_options[model_choice]) |
|
inputs = processor(text=[description + " " + " ".join(style_tags)], return_tensors="pt", padding=True) |
|
|
|
audio_output = model.generate(**inputs, max_new_tokens=256) |
|
|
|
sampling_rate = 16000 |
|
output_file = "generated_music.wav" |
|
torchaudio.save(output_file, audio_output[0].cpu(), sampling_rate) |
|
spectrogram_path = generate_spectrogram(audio_output[0].squeeze(), sampling_rate) |
|
|
|
return output_file, spectrogram_path, None |
|
except Exception as e: |
|
error_message = f"An error occurred: {str(e)}" |
|
return None, None, error_message |
|
|
|
iface = gr.Interface( |
|
fn=generate_music, |
|
inputs=[ |
|
gr.Textbox(label="Enter a description for the music"), |
|
gr.Dropdown(label="Select Model", choices=list(model_options.keys())), |
|
gr.CheckboxGroup(label="Style Tags", choices=style_tags_options), |
|
gr.Slider(label="Tempo", minimum=60, maximum=240, step=1, value=120), |
|
gr.Slider(label="Intensity", minimum=1, maximum=10, step=1, value=5), |
|
gr.Slider(label="Duration (Seconds)", minimum=15, maximum=300, step=1, value=60) |
|
], |
|
outputs=[ |
|
gr.Audio(label="Generated Music"), |
|
gr.Image(label="Spectrogram"), |
|
gr.Textbox(label="Error Message", visible=True) |
|
], |
|
title="Hip Hop Music Generation with MusicGen", |
|
description="Type in a description, select a model, choose style tags, and adjust generation parameters to generate music. Spectrogram and any errors will be displayed." |
|
) |
|
|
|
iface.launch() |
|
|