Reaper200 commited on
Commit
1ca7632
·
verified ·
1 Parent(s): 1e17d2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -12
app.py CHANGED
@@ -1,44 +1,74 @@
 
 
 
1
  import streamlit as st
2
  import torchaudio
3
- import tempfile
4
- import os
5
  from pydub import AudioSegment
6
  from bark import generate_audio, SAMPLE_RATE as BARK_RATE
7
  from audiocraft.models import MusicGen
8
 
9
- # Load MusicGen model
 
 
 
 
10
  musicgen = MusicGen.get_pretrained('facebook/musicgen-small')
11
 
12
- st.set_page_config(page_title="Suno Clone", layout="centered")
13
- st.title("Suno-like Music Generator")
14
- st.markdown("Combining **Bark TTS**, **RVC Voice Cloning**, and **MusicLM** for full track creation.")
 
15
 
 
16
  lyrics = st.text_area("Enter your lyrics:", height=150)
17
- genre_prompt = st.text_input("Enter Music Style (e.g., 'trap beat with piano')", value="trap beat with piano")
18
- uploaded_voice = st.file_uploader("Upload your voice sample (for cloning - WAV only)", type=["wav"])
 
 
 
 
 
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  if st.button("Generate Song"):
 
21
  with st.spinner("Generating vocals with Bark..."):
22
- vocals_tensor = generate_audio(lyrics, history_prompt="v2/en_speaker_6")
 
 
23
  vocals_path = tempfile.mktemp(suffix=".wav")
24
  torchaudio.save(vocals_path, vocals_tensor.squeeze(0).cpu(), BARK_RATE)
25
 
 
26
  if uploaded_voice:
27
  with st.spinner("Cloning voice with RVC..."):
28
  uploaded_voice_path = tempfile.mktemp(suffix=".wav")
29
  with open(uploaded_voice_path, "wb") as f:
30
  f.write(uploaded_voice.read())
31
-
32
  cloned_path = tempfile.mktemp(suffix=".wav")
33
  os.system(f"python infer_rvc.py --input \"{vocals_path}\" --voice \"{uploaded_voice_path}\" --output \"{cloned_path}\"")
34
- vocals_path = cloned_path
35
 
36
- with st.spinner("Generating instrumental with MusicLM..."):
 
37
  musicgen.set_generation_params(duration=15)
38
  music = musicgen.generate([genre_prompt])
39
  instrumental_path = tempfile.mktemp(suffix=".wav")
40
  torchaudio.save(instrumental_path, music[0].cpu(), 32000)
41
 
 
42
  with st.spinner("Mixing vocals and instrumental..."):
43
  vocals_seg = AudioSegment.from_wav(vocals_path)
44
  instrumental_seg = AudioSegment.from_wav(instrumental_path)
 
1
+ import os
2
+ import shutil
3
+ import tempfile
4
  import streamlit as st
5
  import torchaudio
 
 
6
  from pydub import AudioSegment
7
  from bark import generate_audio, SAMPLE_RATE as BARK_RATE
8
  from audiocraft.models import MusicGen
9
 
10
+ # Use smaller Bark models for better compatibility
11
+ os.environ["SUNO_USE_SMALL_MODELS"] = "1"
12
+ BARK_CACHE = os.path.expanduser("~/.cache/suno/")
13
+
14
+ # Load MusicGen model once
15
  musicgen = MusicGen.get_pretrained('facebook/musicgen-small')
16
 
17
+ # Streamlit layout
18
+ st.set_page_config(page_title="Suno-Like Generator", layout="centered")
19
+ st.title("Suno-Like Music Generator")
20
+ st.markdown("Powered by **Bark + RVC + MusicGen**")
21
 
22
+ # Inputs
23
  lyrics = st.text_area("Enter your lyrics:", height=150)
24
+ genre_prompt = st.text_input("Describe the music style (e.g., 'afrobeats with guitar')", value="trap beat with piano")
25
+ uploaded_voice = st.file_uploader("Upload your voice sample for cloning (WAV)", type=["wav"])
26
+
27
+ # Optional dev button
28
+ if st.sidebar.button("Clear Bark Cache"):
29
+ shutil.rmtree(BARK_CACHE, ignore_errors=True)
30
+ st.success("Bark model cache cleared. Reload app to redownload models.")
31
 
32
+ # Bark fallback-safe function
33
+ def safe_bark_generate(text, speaker="en_speaker_6"):
34
+ try:
35
+ return generate_audio(text, history_prompt=speaker)
36
+ except Exception as e:
37
+ st.warning(f"Speaker {speaker} failed, retrying with default. Error: {e}")
38
+ try:
39
+ return generate_audio(text)
40
+ except Exception as e2:
41
+ st.error(f"Bark generation failed. Error: {e2}")
42
+ return None
43
+
44
+ # Main generation block
45
  if st.button("Generate Song"):
46
+ # Bark TTS
47
  with st.spinner("Generating vocals with Bark..."):
48
+ vocals_tensor = safe_bark_generate(lyrics)
49
+ if vocals_tensor is None:
50
+ st.stop()
51
  vocals_path = tempfile.mktemp(suffix=".wav")
52
  torchaudio.save(vocals_path, vocals_tensor.squeeze(0).cpu(), BARK_RATE)
53
 
54
+ # RVC Voice Cloning
55
  if uploaded_voice:
56
  with st.spinner("Cloning voice with RVC..."):
57
  uploaded_voice_path = tempfile.mktemp(suffix=".wav")
58
  with open(uploaded_voice_path, "wb") as f:
59
  f.write(uploaded_voice.read())
 
60
  cloned_path = tempfile.mktemp(suffix=".wav")
61
  os.system(f"python infer_rvc.py --input \"{vocals_path}\" --voice \"{uploaded_voice_path}\" --output \"{cloned_path}\"")
62
+ vocals_path = cloned_path # use cloned vocals
63
 
64
+ # MusicGen Instrumental
65
+ with st.spinner("Generating instrumental with MusicGen..."):
66
  musicgen.set_generation_params(duration=15)
67
  music = musicgen.generate([genre_prompt])
68
  instrumental_path = tempfile.mktemp(suffix=".wav")
69
  torchaudio.save(instrumental_path, music[0].cpu(), 32000)
70
 
71
+ # Mix vocals + instrumental
72
  with st.spinner("Mixing vocals and instrumental..."):
73
  vocals_seg = AudioSegment.from_wav(vocals_path)
74
  instrumental_seg = AudioSegment.from_wav(instrumental_path)