Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import MusicGenForConditionalGeneration, MusicGenProcessor
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
# Load the MusicGen model and processor
|
| 6 |
model_name = "facebook/musicgen-small"
|
|
@@ -14,11 +15,14 @@ def generate_audio(text):
|
|
| 14 |
|
| 15 |
# Generate audio (samples)
|
| 16 |
with torch.no_grad():
|
| 17 |
-
generated_audio = model.
|
| 18 |
|
| 19 |
-
#
|
| 20 |
audio_path = "/tmp/generated_audio.wav"
|
| 21 |
-
generated_audio.
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
return audio_path
|
| 24 |
|
|
@@ -32,3 +36,4 @@ iface = gr.Interface(
|
|
| 32 |
)
|
| 33 |
|
| 34 |
iface.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import MusicGenForConditionalGeneration, MusicGenProcessor
|
| 3 |
import torch
|
| 4 |
+
import soundfile as sf # For saving audio as WAV
|
| 5 |
|
| 6 |
# Load the MusicGen model and processor
|
| 7 |
model_name = "facebook/musicgen-small"
|
|
|
|
| 15 |
|
| 16 |
# Generate audio (samples)
|
| 17 |
with torch.no_grad():
|
| 18 |
+
generated_audio = model.generate(**inputs)
|
| 19 |
|
| 20 |
+
# Convert tensor to numpy and save as a WAV file
|
| 21 |
audio_path = "/tmp/generated_audio.wav"
|
| 22 |
+
audio_data = generated_audio[0].cpu().numpy() # Access the first sample
|
| 23 |
+
|
| 24 |
+
# Save the generated audio
|
| 25 |
+
sf.write(audio_path, audio_data, 16000) # Assuming a sample rate of 16kHz
|
| 26 |
|
| 27 |
return audio_path
|
| 28 |
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
iface.launch()
|
| 39 |
+
|