Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,44 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
from transformers import AutoModel, AutoProcessor
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
model = AutoModel.from_pretrained(model_name)
|
| 11 |
-
processor = AutoProcessor.from_pretrained(model_name)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
# Generate audio (samples)
|
| 19 |
-
with torch.no_grad():
|
| 20 |
-
generated_audio = model.generate(**inputs)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
audio_path = "/tmp/generated_audio.wav"
|
| 24 |
-
audio_data = generated_audio[0].cpu().numpy() # Access the first sample
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
|
| 31 |
-
# Set up the Gradio interface
|
| 32 |
iface = gr.Interface(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
-
iface.launch()
|
| 41 |
|
|
|
|
|
|
| 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 = MusicGen.get_pretrained("facebook/musicgen-small")
|
| 12 |
+
model.set_generation_params(duration=5)
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def generate_music(description):
|
| 15 |
+
wav = model.generate([description])
|
| 16 |
+
audio_array = wav.cpu().numpy().squeeze()
|
| 17 |
+
sample_rate = model.sample_rate
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 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(file_path, rate=sample_rate, data=audio_array)
|
| 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)
|