Spaces:
Runtime error
Runtime error
Commit
·
1486598
1
Parent(s):
0671817
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,28 @@
|
|
1 |
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
|
|
2 |
|
3 |
-
|
4 |
processor = AutoProcessor.from_pretrained("facebook/musicgen-large")
|
5 |
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-large")
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
audio_values = model.generate(**inputs, max_new_tokens=256)
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
Audio(
|
|
|
1 |
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
2 |
+
from IPython.display import Audio
|
3 |
|
4 |
+
# Load the processor and model
|
5 |
processor = AutoProcessor.from_pretrained("facebook/musicgen-large")
|
6 |
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-large")
|
7 |
|
8 |
+
# Function to generate music from text
|
9 |
+
def generate_music_from_text(text):
|
10 |
+
inputs = processor(
|
11 |
+
text=[text],
|
12 |
+
padding=True,
|
13 |
+
return_tensors="pt",
|
14 |
+
)
|
15 |
|
16 |
+
audio_values = model.generate(**inputs, max_new_tokens=256)
|
17 |
|
18 |
+
sampling_rate = model.config.audio_encoder.sampling_rate
|
19 |
+
return audio_values[0].numpy(), sampling_rate
|
20 |
+
|
21 |
+
# Input text
|
22 |
+
text_input = input("Enter the text you want to transform into music: ")
|
23 |
+
|
24 |
+
# Generate music from the input text
|
25 |
+
audio_data, sampling_rate = generate_music_from_text(text_input)
|
26 |
|
27 |
+
# Display the generated music as audio
|
28 |
+
Audio(audio_data, rate=sampling_rate)
|