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