Reaper200 commited on
Commit
a100b38
·
verified ·
1 Parent(s): 38a7104

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torchaudio
3
+ import tempfile
4
+ from audiocraft.models import MusicGen
5
+ from bark import SAMPLE_RATE as BARK_SAMPLE_RATE, generate_audio as bark_generate_audio
6
+ from pydub import AudioSegment
7
+
8
+ # Load MusicGen model
9
+ musicgen = MusicGen.get_pretrained('facebook/musicgen-small')
10
+
11
+ # Function to generate vocals and music
12
+ def generate_song(lyrics, genre_prompt):
13
+ # Step 1: Generate vocals from Bark
14
+ vocals = bark_generate_audio(lyrics, history_prompt="v2/en_speaker_6")
15
+
16
+ # Save Bark vocals temporarily
17
+ vocals_path = tempfile.mktemp(suffix=".wav")
18
+ torchaudio.save(vocals_path, vocals.squeeze(0).cpu(), BARK_SAMPLE_RATE)
19
+
20
+ # Step 2: Generate music from MusicGen
21
+ musicgen.set_generation_params(duration=15) # Set song length (in seconds)
22
+ music = musicgen.generate([genre_prompt]) # Generate instrumental based on genre
23
+ music_path = tempfile.mktemp(suffix=".wav")
24
+ torchaudio.save(music_path, music[0].cpu(), 32000)
25
+
26
+ # Step 3: Mix the two audio files using pydub
27
+ vocals_seg = AudioSegment.from_wav(vocals_path)
28
+ music_seg = AudioSegment.from_wav(music_path)
29
+
30
+ # Overlay vocals over the music
31
+ mixed = music_seg.overlay(vocals_seg.set_frame_rate(32000).set_channels(1))
32
+ output_path = tempfile.mktemp(suffix=".wav")
33
+ mixed.export(output_path, format="wav")
34
+
35
+ return output_path
36
+
37
+ # Gradio interface
38
+ iface = gr.Interface(
39
+ fn=generate_song,
40
+ inputs=[
41
+ gr.Textbox(label="Enter Lyrics", lines=4),
42
+ gr.Textbox(label="Enter Genre (e.g., 'hip-hop with 808s')")
43
+ ],
44
+ outputs=gr.Audio(label="Generated Song")
45
+ )
46
+
47
+ # Launch the Gradio interface
48
+ iface.
49
+ launch()