Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,98 @@
|
|
| 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 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
response = requests.post(url, headers=headers, json=data)
|
| 14 |
-
|
| 15 |
-
if response.status_code == 200:
|
| 16 |
-
clip = response.json()
|
| 17 |
-
return clip["audio_src"]
|
| 18 |
-
else:
|
| 19 |
-
raise Exception(f"Failed to generate audio: {response.text}")
|
| 20 |
|
| 21 |
# Streamlit UI
|
| 22 |
-
st.set_page_config(page_title="
|
| 23 |
-
st.title("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
st.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Step 3: Generate Instrumental
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
model = MusicGen.get_pretrained("facebook/musicgen-small")
|
| 41 |
-
model.set_generation_params(duration=
|
| 42 |
output = model.generate([prompt])
|
| 43 |
-
torchaudio.save(
|
| 44 |
-
st.audio(
|
| 45 |
-
st.success("
|
| 46 |
|
| 47 |
-
# Step 4:
|
| 48 |
-
|
|
|
|
| 49 |
try:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
voice_file = "resemble_voice.mp3"
|
| 53 |
-
with open(voice_file, "wb") as f:
|
| 54 |
-
f.write(requests.get(audio_url).content)
|
| 55 |
-
|
| 56 |
-
# Combine voice with music
|
| 57 |
-
beat = AudioSegment.from_wav(output_path)
|
| 58 |
-
voice = AudioSegment.from_mp3(voice_file)
|
| 59 |
voice = voice - 4
|
| 60 |
beat = beat - 2
|
| 61 |
-
combined = beat.overlay(voice
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
st.audio(combined_path, format="audio/mp3")
|
| 66 |
-
st.success("Final mix is ready!")
|
| 67 |
except Exception as e:
|
| 68 |
-
st.error(
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
import torchaudio
|
| 4 |
from audiocraft.models import MusicGen
|
|
|
|
| 5 |
from pydub import AudioSegment
|
| 6 |
import os
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
+
# Constants
|
| 10 |
+
AUDIO_OUTPUT_PATH = "voice_output.mp3"
|
| 11 |
+
COMBINED_OUTPUT_PATH = "combined_output.mp3"
|
| 12 |
+
MUSICGEN_OUTPUT_PATH = "musicgen_output.wav"
|
| 13 |
+
RESEMBLE_API_TOKEN = "your_resemble_api_token_here"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Streamlit UI
|
| 16 |
+
st.set_page_config(page_title="AI Music Generator", layout="centered")
|
| 17 |
+
st.title("AI Music Generator like Suno")
|
| 18 |
+
|
| 19 |
+
# Step 1: User Lyrics Input
|
| 20 |
+
st.subheader("Step 1: Enter Your Lyrics")
|
| 21 |
+
lyrics = st.text_area("Lyrics (max 300 characters)", max_chars=300)
|
| 22 |
+
|
| 23 |
+
# Step 2: Generate Voice with Resemble AI
|
| 24 |
+
st.subheader("Step 2: Generate Voice")
|
| 25 |
+
project_uuid = st.text_input("Enter your Resemble Project UUID", "96e7c9b4-voice")
|
| 26 |
+
voice_clip_uuid = st.text_input("Enter your Voice Clip UUID (or leave blank to generate new)", "")
|
| 27 |
|
| 28 |
+
# Function to call Resemble API
|
| 29 |
+
def generate_voice_with_resemble(text, project_uuid):
|
| 30 |
+
headers = {
|
| 31 |
+
"Authorization": f"Token {RESEMBLE_API_TOKEN}",
|
| 32 |
+
"Accept": "application/json",
|
| 33 |
+
"Content-Type": "application/json"
|
| 34 |
+
}
|
| 35 |
+
data = {
|
| 36 |
+
"project_uuid": project_uuid,
|
| 37 |
+
"title": "streamlit_clip",
|
| 38 |
+
"body": text,
|
| 39 |
+
"voice": "your_custom_voice_id_if_needed"
|
| 40 |
+
}
|
| 41 |
+
res = requests.post("https://app.resemble.ai/api/v2/clips", headers=headers, json=data)
|
| 42 |
+
res_data = res.json()
|
| 43 |
+
clip_id = res_data.get("item", {}).get("uuid")
|
| 44 |
|
| 45 |
+
if not clip_id:
|
| 46 |
+
st.error("Failed to create clip on Resemble.")
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
# Polling for completion
|
| 50 |
+
for _ in range(30):
|
| 51 |
+
clip_res = requests.get(f"https://app.resemble.ai/api/v2/clips/{clip_id}", headers=headers)
|
| 52 |
+
clip_info = clip_res.json()
|
| 53 |
+
if clip_info.get("is_complete"):
|
| 54 |
+
audio_url = clip_info.get("audio_src")
|
| 55 |
+
audio_data = requests.get(audio_url).content
|
| 56 |
+
with open(AUDIO_OUTPUT_PATH, "wb") as f:
|
| 57 |
+
f.write(audio_data)
|
| 58 |
+
return AUDIO_OUTPUT_PATH
|
| 59 |
+
time.sleep(3)
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
if st.button("Generate Voice") and lyrics:
|
| 63 |
+
st.info("Requesting Resemble to generate voice...")
|
| 64 |
+
path = generate_voice_with_resemble(lyrics, project_uuid)
|
| 65 |
+
if path and os.path.exists(path):
|
| 66 |
+
st.audio(path, format="audio/mp3")
|
| 67 |
+
else:
|
| 68 |
+
st.error("Voice generation failed.")
|
| 69 |
|
| 70 |
# Step 3: Generate Instrumental
|
| 71 |
+
st.subheader("Step 3: Generate Instrumental")
|
| 72 |
+
prompt = st.text_input("Describe the music (e.g. energetic trap beat)", "emotional trap beat")
|
| 73 |
+
|
| 74 |
+
if st.button("Generate Music"):
|
| 75 |
+
with st.spinner("Generating music..."):
|
| 76 |
model = MusicGen.get_pretrained("facebook/musicgen-small")
|
| 77 |
+
model.set_generation_params(duration=5)
|
| 78 |
output = model.generate([prompt])
|
| 79 |
+
torchaudio.save(MUSICGEN_OUTPUT_PATH, output[0].cpu(), 32000)
|
| 80 |
+
st.audio(MUSICGEN_OUTPUT_PATH, format="audio/wav")
|
| 81 |
+
st.success("Instrumental generated!")
|
| 82 |
|
| 83 |
+
# Step 4: Combine
|
| 84 |
+
st.subheader("Step 4: Combine Voice and Beat")
|
| 85 |
+
if os.path.exists(MUSICGEN_OUTPUT_PATH) and os.path.exists(AUDIO_OUTPUT_PATH):
|
| 86 |
try:
|
| 87 |
+
beat = AudioSegment.from_wav(MUSICGEN_OUTPUT_PATH)
|
| 88 |
+
voice = AudioSegment.from_mp3(AUDIO_OUTPUT_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
voice = voice - 4
|
| 90 |
beat = beat - 2
|
| 91 |
+
combined = beat.overlay(voice)
|
| 92 |
+
combined.export(COMBINED_OUTPUT_PATH, format="mp3")
|
| 93 |
+
st.audio(COMBINED_OUTPUT_PATH, format="audio/mp3")
|
| 94 |
+
st.success("Combined track ready!")
|
|
|
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
+
st.error(f"Error combining files: {e}")
|
| 97 |
+
else:
|
| 98 |
+
st.info("Generate both voice and music to combine.")
|