Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,54 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from torch.serialization import add_safe_globals
|
| 3 |
-
import numpy.core.multiarray
|
| 4 |
-
|
| 5 |
-
# Patch for PyTorch 2.6 to allow loading Bark's model checkpoints
|
| 6 |
-
add_safe_globals({
|
| 7 |
-
'scalar': numpy.core.multiarray.scalar
|
| 8 |
-
})
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
import os
|
| 12 |
-
import shutil
|
| 13 |
-
import tempfile
|
| 14 |
import streamlit as st
|
| 15 |
import torchaudio
|
| 16 |
-
|
| 17 |
-
from bark import generate_audio, SAMPLE_RATE as BARK_RATE
|
| 18 |
from audiocraft.models import MusicGen
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
st.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
if st.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
uploaded_voice_path = tempfile.mktemp(suffix=".wav")
|
| 68 |
-
with open(uploaded_voice_path, "wb") as f:
|
| 69 |
-
f.write(uploaded_voice.read())
|
| 70 |
-
cloned_path = tempfile.mktemp(suffix=".wav")
|
| 71 |
-
os.system(f"python infer_rvc.py --input \"{vocals_path}\" --voice \"{uploaded_voice_path}\" --output \"{cloned_path}\"")
|
| 72 |
-
vocals_path = cloned_path # use cloned vocals
|
| 73 |
-
|
| 74 |
-
# MusicGen Instrumental
|
| 75 |
-
with st.spinner("Generating instrumental with MusicGen..."):
|
| 76 |
-
musicgen.set_generation_params(duration=15)
|
| 77 |
-
music = musicgen.generate([genre_prompt])
|
| 78 |
-
instrumental_path = tempfile.mktemp(suffix=".wav")
|
| 79 |
-
torchaudio.save(instrumental_path, music[0].cpu(), 32000)
|
| 80 |
-
|
| 81 |
-
# Mix vocals + instrumental
|
| 82 |
-
with st.spinner("Mixing vocals and instrumental..."):
|
| 83 |
-
vocals_seg = AudioSegment.from_wav(vocals_path)
|
| 84 |
-
instrumental_seg = AudioSegment.from_wav(instrumental_path)
|
| 85 |
-
mixed = instrumental_seg.overlay(vocals_seg.set_frame_rate(32000).set_channels(1))
|
| 86 |
-
final_output_path = tempfile.mktemp(suffix=".wav")
|
| 87 |
-
mixed.export(final_output_path, format="wav")
|
| 88 |
-
|
| 89 |
-
st.audio(final_output_path)
|
| 90 |
-
st.success("Your song is ready!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import torchaudio
|
| 3 |
+
import torch
|
|
|
|
| 4 |
from audiocraft.models import MusicGen
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
st.set_page_config(page_title="Suno-like AI Music Generator", layout="centered")
|
| 9 |
+
st.title("Suno-like AI Music Generator")
|
| 10 |
+
|
| 11 |
+
# Load the pre-recorded male voice sample
|
| 12 |
+
st.subheader("Step 1: Pre-recorded AI Voice Sample")
|
| 13 |
+
voice_path = "sample_voice.mp3"
|
| 14 |
+
|
| 15 |
+
if os.path.exists(voice_path):
|
| 16 |
+
audio_file = open(voice_path, 'rb')
|
| 17 |
+
audio_bytes = audio_file.read()
|
| 18 |
+
st.audio(audio_bytes, format='audio/mp3')
|
| 19 |
+
else:
|
| 20 |
+
st.error("Voice sample not found. Please make sure 'sample_voice.mp3' is in the same directory.")
|
| 21 |
+
|
| 22 |
+
# Generate Instrumental with MusicGen
|
| 23 |
+
st.subheader("Step 2: Generate Instrumental with MusicGen")
|
| 24 |
+
prompt = st.text_input("Enter music description (e.g. energetic trap beat with dark mood)", "emotional trap beat with dark pads and heavy drums")
|
| 25 |
+
|
| 26 |
+
if st.button("Generate Music"):
|
| 27 |
+
with st.spinner("Generating music..."):
|
| 28 |
+
model = MusicGen.get_pretrained("facebook/musicgen-small")
|
| 29 |
+
model.set_generation_params(duration=10)
|
| 30 |
+
output = model.generate([prompt])
|
| 31 |
+
output_path = "musicgen_output.wav"
|
| 32 |
+
torchaudio.save(output_path, output[0].cpu(), 32000)
|
| 33 |
+
st.audio(output_path, format="audio/wav")
|
| 34 |
+
st.success("Music generation complete!")
|
| 35 |
+
|
| 36 |
+
# Combine voice and beat (optional step to simulate Suno-like output)
|
| 37 |
+
st.subheader("Step 3: Simulated Combination")
|
| 38 |
+
if os.path.exists("musicgen_output.wav") and os.path.exists("sample_voice.mp3"):
|
| 39 |
+
if st.button("Combine Voice + Instrumental"):
|
| 40 |
+
# Convert MP3 voice to WAV for merging
|
| 41 |
+
voice = AudioSegment.from_mp3("sample_voice.mp3")
|
| 42 |
+
beat = AudioSegment.from_wav("musicgen_output.wav")
|
| 43 |
+
|
| 44 |
+
# Adjust volumes and overlay
|
| 45 |
+
voice = voice - 4
|
| 46 |
+
beat = beat - 2
|
| 47 |
+
combined = beat.overlay(voice, loop=False)
|
| 48 |
+
combined_path = "combined_output.mp3"
|
| 49 |
+
combined.export(combined_path, format="mp3")
|
| 50 |
+
|
| 51 |
+
st.audio(combined_path, format="audio/mp3")
|
| 52 |
+
st.success("Combined output ready!")
|
| 53 |
+
else:
|
| 54 |
+
st.info("Generate the beat and make sure voice sample is available.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|