MusicGenDemucs / app.py
nakas's picture
Update app.py
d598853
raw
history blame
2.44 kB
import os
import gradio as gr
from scipy.io.wavfile import write
import subprocess
import torch
import typing as tp
from audiocraft.data.audio_utils import convert_audio
# Import the necessary MusicGen code here
def load_model():
# Load the MusicGen model here
def music_gen_and_separation(text, audio):
# Perform music generation with the loaded MusicGen model
texts = [text] # Use the provided text for music generation
melodies = [(audio[1], audio[0])] # Convert audio to melody format for MusicGen
# Perform music generation using the loaded MusicGen model
generated_music = predict_full(model, texts, melodies, duration, topk, topp, temperature, cfg_coef)
# Perform source separation using Demucs
# Save the generated music to a temporary file
temp_file = "generated_music.wav"
write(temp_file, generated_music, 32000)
# Run Demucs for source separation
command = "python3 -m demucs.separate -n mdx_extra_q -d cpu " + temp_file + " -o out"
process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("Demucs script output:", process.stdout.decode())
# Check if files exist before returning
files = ["./out/mdx_extra_q/test/vocals.wav",
"./out/mdx_extra_q/test/bass.wav",
"./out/mdx_extra_q/test/drums.wav",
"./out/mdx_extra_q/test/other.wav"]
for file in files:
if not os.path.isfile(file):
print(f"File not found: {file}")
else:
print(f"File exists: {file}")
# Convert the separated audio files to numpy arrays
separated_audio = []
for file in files:
_, audio = read(file)
separated_audio.append(audio)
return separated_audio
title = "MusicGen with Demucs"
description = "Combine MusicGen with Demucs for music generation and source separation."
article = "<p>Article content goes here.</p>"
input_text = gr.inputs.Textbox(label="Input Text")
input_audio = gr.inputs.Audio(label="Input Audio")
output_vocals = gr.outputs.Audio(label="Vocals")
output_bass = gr.outputs.Audio(label="Bass")
output_drums = gr.outputs.Audio(label="Drums")
output_other = gr.outputs.Audio(label="Other")
gr.Interface(
music_gen_and_separation,
[input_text, input_audio],
[output_vocals, output_bass, output_drums, output_other],
title=title,
description=description,
article=article
).launch()