aach456 commited on
Commit
0593593
1 Parent(s): cfb71b6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import numpy as np
4
+ from transformers import MusicgenForConditionalGeneration, AutoProcessor
5
+ import scipy.io.wavfile
6
+
7
+ def generate_music(prompt, unconditional=False):
8
+ model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
9
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
10
+ model.to(device)
11
+
12
+ # Generate music
13
+ if unconditional:
14
+ unconditional_inputs = model.get_unconditional_inputs(num_samples=1)
15
+ audio_values = model.generate(**unconditional_inputs, do_sample=True, max_new_tokens=256)
16
+ else:
17
+ processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
18
+ inputs = processor(text=prompt, padding=True, return_tensors="pt")
19
+ audio_values = model.generate(**inputs.to(device), do_sample=True, guidance_scale=3, max_new_tokens=256)
20
+
21
+ sampling_rate = model.config.audio_encoder.sampling_rate
22
+ audio_file = "musicgen_out.wav"
23
+
24
+ # Ensure audio_values is 1D and scale if necessary
25
+ audio_data = audio_values[0].cpu().numpy()
26
+
27
+ # Check if audio_data is in the correct format
28
+ if audio_data.ndim > 1:
29
+ audio_data = audio_data[0] # Take the first channel if stereo
30
+
31
+ # Scale audio data to 16-bit PCM format
32
+ audio_data = np.clip(audio_data, -1.0, 1.0) # Ensure values are in the range [-1, 1]
33
+ audio_data = (audio_data * 32767).astype(np.int16) # Scale to int16
34
+
35
+ # Save the generated audio
36
+ scipy.io.wavfile.write(audio_file, sampling_rate, audio_data)
37
+
38
+ return audio_file
39
+
40
+ def interface(prompt, unconditional):
41
+ audio_file = generate_music(prompt, unconditional)
42
+ return audio_file
43
+
44
+ with gr.Blocks() as demo:
45
+ gr.Markdown("# AI-Powered Music Generation")
46
+
47
+ with gr.Row():
48
+ prompt_input = gr.Textbox(label="Enter the Music Prompt")
49
+ unconditional_checkbox = gr.Checkbox(label="Generate Unconditional Music")
50
+
51
+ generate_button = gr.Button("Generate Music")
52
+ output_audio = gr.Audio(label="Output Music")
53
+
54
+ generate_button.click(
55
+ interface,
56
+ inputs=[prompt_input, unconditional_checkbox],
57
+ outputs=output_audio,
58
+ show_progress=True
59
+ )
60
+
61
+ demo.launch(share=True)