Reaper200 commited on
Commit
0dee12f
·
verified ·
1 Parent(s): 32e2270

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -86
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
- from pydub import AudioSegment
17
- from bark import generate_audio, SAMPLE_RATE as BARK_RATE
18
  from audiocraft.models import MusicGen
 
 
19
 
20
- # Use smaller Bark models for better compatibility
21
- os.environ["SUNO_USE_SMALL_MODELS"] = "1"
22
- BARK_CACHE = os.path.expanduser("~/.cache/suno/")
23
-
24
- # Load MusicGen model once
25
- musicgen = MusicGen.get_pretrained('facebook/musicgen-small')
26
-
27
- # Streamlit layout
28
- st.set_page_config(page_title="Suno-Like Generator", layout="centered")
29
- st.title("Suno-Like Music Generator")
30
- st.markdown("Powered by **Bark + RVC + MusicGen**")
31
-
32
- # Inputs
33
- lyrics = st.text_area("Enter your lyrics:", height=150)
34
- genre_prompt = st.text_input("Describe the music style (e.g., 'afrobeats with guitar')", value="trap beat with piano")
35
- uploaded_voice = st.file_uploader("Upload your voice sample for cloning (WAV)", type=["wav"])
36
-
37
- # Optional dev button
38
- if st.sidebar.button("Clear Bark Cache"):
39
- shutil.rmtree(BARK_CACHE, ignore_errors=True)
40
- st.success("Bark model cache cleared. Reload app to redownload models.")
41
-
42
- # Bark fallback-safe function
43
- def safe_bark_generate(text, speaker="en_speaker_6"):
44
- try:
45
- return generate_audio(text, history_prompt=speaker)
46
- except Exception as e:
47
- st.warning(f"Speaker {speaker} failed, retrying with default. Error: {e}")
48
- try:
49
- return generate_audio(text)
50
- except Exception as e2:
51
- st.error(f"Bark generation failed. Error: {e2}")
52
- return None
53
-
54
- # Main generation block
55
- if st.button("Generate Song"):
56
- # Bark TTS
57
- with st.spinner("Generating vocals with Bark..."):
58
- vocals_tensor = safe_bark_generate(lyrics)
59
- if vocals_tensor is None:
60
- st.stop()
61
- vocals_path = tempfile.mktemp(suffix=".wav")
62
- torchaudio.save(vocals_path, vocals_tensor.squeeze(0).cpu(), BARK_RATE)
63
-
64
- # RVC Voice Cloning
65
- if uploaded_voice:
66
- with st.spinner("Cloning voice with RVC..."):
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.")