Reaper200 commited on
Commit
19be27e
·
verified ·
1 Parent(s): 0dee12f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -30
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 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..."):
@@ -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 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.")
 
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.")