Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,27 @@
|
|
| 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
|
| 12 |
st.subheader("Step 1: Pre-recorded AI Voice Sample")
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
st.
|
| 19 |
-
|
| 20 |
-
|
| 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)",
|
|
|
|
| 25 |
|
| 26 |
if st.button("Generate Music"):
|
| 27 |
with st.spinner("Generating music..."):
|
|
@@ -33,22 +33,29 @@ if st.button("Generate Music"):
|
|
| 33 |
st.audio(output_path, format="audio/wav")
|
| 34 |
st.success("Music generation complete!")
|
| 35 |
|
| 36 |
-
# Combine
|
| 37 |
-
st.subheader("Step 3: Simulated Combination")
|
| 38 |
-
|
|
|
|
| 39 |
if st.button("Combine Voice + Instrumental"):
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
else:
|
| 54 |
-
st.info("Generate the
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import torchaudio
|
|
|
|
| 3 |
from audiocraft.models import MusicGen
|
| 4 |
+
import requests
|
| 5 |
from pydub import AudioSegment
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# Set Streamlit page configuration
|
| 9 |
st.set_page_config(page_title="Suno-like AI Music Generator", layout="centered")
|
| 10 |
st.title("Suno-like AI Music Generator")
|
| 11 |
|
| 12 |
+
# Step 1: Load Pre-recorded AI Voice Sample
|
| 13 |
st.subheader("Step 1: Pre-recorded AI Voice Sample")
|
| 14 |
+
voice_url = "https://www2.cs.uic.edu/~i101/SoundFiles/StarWars60.wav" # Example URL for voice sample
|
| 15 |
+
try:
|
| 16 |
+
audio_data = requests.get(voice_url).content
|
| 17 |
+
st.audio(audio_data, format="audio/wav")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
st.error(f"Failed to load audio: {e}")
|
| 20 |
+
|
| 21 |
+
# Step 2: Generate Instrumental with MusicGen
|
|
|
|
|
|
|
| 22 |
st.subheader("Step 2: Generate Instrumental with MusicGen")
|
| 23 |
+
prompt = st.text_input("Enter music description (e.g. energetic trap beat with dark mood)",
|
| 24 |
+
"emotional trap beat with dark pads and heavy drums")
|
| 25 |
|
| 26 |
if st.button("Generate Music"):
|
| 27 |
with st.spinner("Generating music..."):
|
|
|
|
| 33 |
st.audio(output_path, format="audio/wav")
|
| 34 |
st.success("Music generation complete!")
|
| 35 |
|
| 36 |
+
# Step 3: Combine Voice and Instrumental (Simulated)
|
| 37 |
+
st.subheader("Step 3: Simulated Combination of Voice + Instrumental")
|
| 38 |
+
|
| 39 |
+
if os.path.exists("musicgen_output.wav"):
|
| 40 |
if st.button("Combine Voice + Instrumental"):
|
| 41 |
+
try:
|
| 42 |
+
# Load the generated instrumental and the pre-recorded voice
|
| 43 |
+
beat = AudioSegment.from_wav("musicgen_output.wav")
|
| 44 |
+
voice = AudioSegment.from_wav("StarWars60.wav") # Use the public voice sample
|
| 45 |
+
|
| 46 |
+
# Adjust volumes and overlay (to avoid distortion)
|
| 47 |
+
voice = voice - 4 # Reduce the voice volume
|
| 48 |
+
beat = beat - 2 # Reduce the beat volume
|
| 49 |
+
|
| 50 |
+
# Combine voice and instrumental
|
| 51 |
+
combined = beat.overlay(voice, loop=False)
|
| 52 |
+
combined_path = "combined_output.mp3"
|
| 53 |
+
combined.export(combined_path, format="mp3")
|
| 54 |
+
|
| 55 |
+
# Play the combined audio
|
| 56 |
+
st.audio(combined_path, format="audio/mp3")
|
| 57 |
+
st.success("Combined output ready!")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
st.error(f"Error while combining: {e}")
|
| 60 |
else:
|
| 61 |
+
st.info("Generate the music first to combine it with the voice.")
|