Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,46 @@
|
|
1 |
-
import tempfile
|
2 |
-
from audiocraft.models import MusicGen
|
3 |
-
from audiocraft.data.audio import audio_write
|
4 |
-
import gradio as gr
|
5 |
-
import torch
|
6 |
-
import uuid
|
7 |
-
import os
|
8 |
-
from scipy.io.wavfile import write
|
9 |
-
|
10 |
-
|
11 |
-
model
|
12 |
-
model.set_generation_params(duration=5)
|
13 |
|
14 |
def generate_music(description):
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
18 |
|
|
|
|
|
19 |
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
f'{file_id}.wav'
|
25 |
-
)
|
26 |
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
return file_path
|
33 |
|
|
|
34 |
iface = gr.Interface(
|
35 |
-
fn=generate_music,
|
36 |
-
inputs="text",
|
37 |
-
outputs=gr.components.Audio(type="filepath", label="Audio"),
|
38 |
-
title="Text to Audio Generation",
|
39 |
-
description="Generate audio based on text descriptions.",
|
40 |
-
live=False
|
41 |
)
|
42 |
|
43 |
-
|
44 |
-
iface.launch(debug=True)
|
|
|
1 |
+
import tempfile
|
2 |
+
from audiocraft.models import MusicGen
|
3 |
+
from audiocraft.data.audio import audio_write
|
4 |
+
import gradio as gr
|
5 |
+
import torch
|
6 |
+
import uuid
|
7 |
+
import os
|
8 |
+
from scipy.io.wavfile import write
|
9 |
+
|
10 |
+
model = MusicGen.get_pretrained("facebook/musicgen-small")
|
11 |
+
model.set_generation_params(duration=5)
|
|
|
12 |
|
13 |
def generate_music(description):
|
14 |
+
# This line was not indented properly, fixed by adding indentation
|
15 |
+
wav = model.generate([description])
|
16 |
+
audio_array = wav.cpu().numpy().squeeze()
|
17 |
+
sample_rate = model.sample_rate
|
18 |
|
19 |
+
# Generate a unique file path
|
20 |
+
file_id = uuid.uuid1()
|
21 |
|
22 |
+
file_path = os.path.join(
|
23 |
+
tempfile.gettempdir(),
|
24 |
+
f'{file_id}.wav'
|
25 |
+
)
|
26 |
|
27 |
+
print(f"Temporary directory: {tempfile.gettempdir()}")
|
28 |
+
print(f"File path: {file_path}")
|
|
|
|
|
29 |
|
30 |
+
# Write the audio file to the temporary path
|
31 |
+
write(file_path, rate=sample_rate, data=audio_array)
|
32 |
|
33 |
+
return file_path
|
|
|
|
|
34 |
|
35 |
+
# Create the Gradio interface
|
36 |
iface = gr.Interface(
|
37 |
+
fn=generate_music,
|
38 |
+
inputs="text",
|
39 |
+
outputs=gr.components.Audio(type="filepath", label="Audio"),
|
40 |
+
title="Text to Audio Generation",
|
41 |
+
description="Generate audio based on text descriptions.",
|
42 |
+
live=False
|
43 |
)
|
44 |
|
45 |
+
# Launch the Gradio interface
|
46 |
+
iface.launch(debug=True)
|