Kabatubare commited on
Commit
4199a1a
·
verified ·
1 Parent(s): e3e20c4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torchaudio
2
+ import torchaudio.transforms as T
3
+ import matplotlib.pyplot as plt
4
+ from transformers import AutoProcessor, MusicgenForConditionalGeneration
5
+ import gradio as gr
6
+ import numpy as np
7
+ import torch
8
+
9
+ # Define available model options for dropdown
10
+ model_options = {
11
+ "Medium Model": "facebook/musicgen-medium",
12
+ "Large Model": "facebook/musicgen-large"
13
+ }
14
+
15
+ # Define style tags options
16
+ style_tags_options = ["East Coast", "Trap", "Boom Bap", "Lo-Fi", "Experimental"]
17
+
18
+ def generate_spectrogram(audio_tensor, sample_rate):
19
+ spectrogram_transform = T.MelSpectrogram(sample_rate=sample_rate)
20
+ spectrogram = spectrogram_transform(audio_tensor.unsqueeze(0))
21
+ spectrogram_db = T.AmplitudeToDB()(spectrogram)
22
+
23
+ plt.figure(figsize=(10, 4))
24
+ plt.imshow(spectrogram_db.log2()[0,:,:].detach().numpy(), cmap='viridis', aspect='auto')
25
+ plt.colorbar(format='%+2.0f dB')
26
+ plt.title('Mel Spectrogram')
27
+ plt.tight_layout()
28
+ plt.ylabel('Mel bins')
29
+ plt.xlabel('Time')
30
+ spectrogram_path = "generated_spectrogram.png"
31
+ plt.savefig(spectrogram_path)
32
+ plt.close()
33
+ return spectrogram_path
34
+
35
+ def generate_music(description, model_choice, style_tags, tempo, intensity, duration):
36
+ try:
37
+ processor = AutoProcessor.from_pretrained(model_options[model_choice])
38
+ model = MusicgenForConditionalGeneration.from_pretrained(model_options[model_choice])
39
+ inputs = processor(text=[description + " " + " ".join(style_tags)], return_tensors="pt", padding=True)
40
+
41
+ audio_output = model.generate(**inputs, max_new_tokens=256)
42
+
43
+ sampling_rate = 16000
44
+ output_file = "generated_music.wav"
45
+ torchaudio.save(output_file, audio_output[0].cpu(), sampling_rate)
46
+ spectrogram_path = generate_spectrogram(audio_output[0].squeeze(), sampling_rate)
47
+
48
+ return output_file, spectrogram_path, None
49
+ except Exception as e:
50
+ error_message = f"An error occurred: {str(e)}"
51
+ return None, None, error_message
52
+
53
+ iface = gr.Interface(
54
+ fn=generate_music,
55
+ inputs=[
56
+ gr.Textbox(label="Enter a description for the music"),
57
+ gr.Dropdown(label="Select Model", choices=list(model_options.keys())),
58
+ gr.CheckboxGroup(label="Style Tags", choices=style_tags_options),
59
+ gr.Slider(label="Tempo", minimum=60, maximum=240, step=1, value=120),
60
+ gr.Slider(label="Intensity", minimum=1, maximum=10, step=1, value=5),
61
+ gr.Slider(label="Duration (Seconds)", minimum=15, maximum=300, step=1, value=60)
62
+ ],
63
+ outputs=[
64
+ gr.Audio(label="Generated Music"),
65
+ gr.Image(label="Spectrogram"),
66
+ gr.Textbox(label="Error Message", visible=True)
67
+ ],
68
+ title="Hip Hop Music Generation with MusicGen",
69
+ 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."
70
+ )
71
+
72
+ iface.launch()