Reaper200 commited on
Commit
7d0a451
·
verified ·
1 Parent(s): 55d4db2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -49
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
- # Function to generate voice with Resemble AI
9
- def generate_resemble_audio(api_key, project_uuid, text, voice_uuid):
10
- url = f"https://app.resemble.ai/api/v2/projects/{project_uuid}/clips"
11
- headers = {"Authorization": f"Token {Gpn4u6ObHlmLWc5aQvs8cQtt}"}
12
- data = {"voice_uuid": voice_uuid, "text": text, "is_active": True}
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="Suno-like Music App with Resemble AI", layout="centered")
23
- st.title("Suno-like AI Music Generator with Resemble AI")
 
 
 
 
 
 
 
 
 
24
 
25
- # Step 1: Input lyrics and music prompt
26
- st.subheader("Step 1: Input Lyrics and Music Prompt")
27
- lyrics = st.text_area("Enter lyrics:", "Life is good, the sky is blue, moving fast in brand new shoes")
28
- prompt = st.text_input("Enter music style:", "moody trap instrumental with 808 bass")
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- # Step 2: Resemble AI credentials
31
- st.subheader("Step 2: Resemble AI Settings")
32
- api_key = st.text_input("Gpn4u6ObHlmLWc5aQvs8cQtt", type="password")
33
- project_id = st.text_input("Resemble Project UUID")
34
- voice_id = st.text_input("Resemble Voice UUID")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Step 3: Generate Instrumental
37
- output_path = "musicgen_output.wav"
38
- if st.button("Generate Instrumental"):
39
- with st.spinner("Generating music with MusicGen..."):
 
 
40
  model = MusicGen.get_pretrained("facebook/musicgen-small")
41
- model.set_generation_params(duration=10)
42
  output = model.generate([prompt])
43
- torchaudio.save(output_path, output[0].cpu(), 32000)
44
- st.audio(output_path, format="audio/wav")
45
- st.success("Music generated successfully!")
46
 
47
- # Step 4: Generate Voice with Resemble and Combine
48
- if st.button("Generate Voice and Combine"):
 
49
  try:
50
- # Generate voice using Resemble
51
- audio_url = generate_resemble_audio(api_key, project_id, lyrics, voice_id)
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, loop=False)
62
-
63
- combined_path = "final_mix.mp3"
64
- combined.export(combined_path, format="mp3")
65
- st.audio(combined_path, format="audio/mp3")
66
- st.success("Final mix is ready!")
67
  except Exception as e:
68
- st.error(str(e))
 
 
 
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.")