Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -6,52 +6,51 @@ import logging
|
|
6 |
import tempfile
|
7 |
import numpy as np
|
8 |
|
9 |
-
#
|
10 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
11 |
|
12 |
@spaces.GPU(duration=120)
|
13 |
def generate_music(description, melody_audio):
|
14 |
-
logging.info("
|
15 |
|
16 |
-
#
|
17 |
-
logging.info("
|
18 |
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
|
19 |
model.set_generation_params(duration=8)
|
20 |
|
21 |
if description:
|
22 |
description = [description]
|
23 |
if melody_audio:
|
24 |
-
logging.info(f"
|
25 |
melody, sr = torchaudio.load(melody_audio)
|
26 |
-
logging.info("
|
27 |
wav = model.generate_with_chroma(description, melody[None], sr)
|
28 |
else:
|
29 |
-
logging.info("
|
30 |
wav = model.generate(description)
|
31 |
else:
|
32 |
-
logging.info("
|
33 |
wav = model.generate_unconditional(1)
|
34 |
|
35 |
-
#
|
36 |
-
logging.info(f"
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
gr.
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
).launch()
|
|
|
6 |
import tempfile
|
7 |
import numpy as np
|
8 |
|
9 |
+
# Configure logging
|
10 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
11 |
|
12 |
@spaces.GPU(duration=120)
|
13 |
def generate_music(description, melody_audio):
|
14 |
+
logging.info("Starting music generation.")
|
15 |
|
16 |
+
# Load the pre-trained model
|
17 |
+
logging.info("Loading pre-trained model.")
|
18 |
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
|
19 |
model.set_generation_params(duration=8)
|
20 |
|
21 |
if description:
|
22 |
description = [description]
|
23 |
if melody_audio:
|
24 |
+
logging.info(f"Loading audio melody from: {melody_audio}")
|
25 |
melody, sr = torchaudio.load(melody_audio)
|
26 |
+
logging.info("Generating music with description and melody.")
|
27 |
wav = model.generate_with_chroma(description, melody[None], sr)
|
28 |
else:
|
29 |
+
logging.info("Generating music with description only.")
|
30 |
wav = model.generate(description)
|
31 |
else:
|
32 |
+
logging.info("Generating music unconditionally.")
|
33 |
wav = model.generate_unconditional(1)
|
34 |
|
35 |
+
# Check the shape of the generated audio tensor
|
36 |
+
logging.info(f"The shape of the generated audio tensor: {wav[0].shape}")
|
37 |
+
|
38 |
+
# Convert the generated audio to the format expected by gr.Audio
|
39 |
+
audio_data = (model.sample_rate, (wav[0].cpu().numpy() * 32767).astype(np.int16))
|
40 |
+
logging.info("Music generated successfully.")
|
41 |
+
|
42 |
+
return audio_data
|
43 |
+
|
44 |
+
# Create the Gradio interface
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
with gr.Row():
|
47 |
+
with gr.Column():
|
48 |
+
description = gr.Textbox(label="Music Description")
|
49 |
+
melody_audio = gr.Audio(label="Melody Audio")
|
50 |
+
with gr.Column():
|
51 |
+
output_audio = gr.Audio(label="Generated Music", interactive=False)
|
52 |
+
|
53 |
+
generate_btn = gr.Button("Generate Music")
|
54 |
+
generate_btn.click(generate_music, inputs=[description, melody_audio], outputs=[output_audio])
|
55 |
+
|
56 |
+
demo.launch()
|
|