Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import torchaudio
|
4 |
+
from audiocraft.models import MusicGen
|
5 |
+
import os
|
6 |
+
import numpy as np
|
7 |
+
import base64
|
8 |
+
|
9 |
+
genres = ["Pop", "Rock", "Jazz", "Electronic", "Hip-Hop", "Classical",
|
10 |
+
"Lofi", "Chillpop","Country","R&G", "Folk","Heavy Metal",
|
11 |
+
"EDM", "Soil", "Funk","Reggae", "Disco", "Punk Rock", "House",
|
12 |
+
"Techno","Indie Rock", "Grunge", "Ambient","Gospel", "Latin Music","Grime" ,"Trap", "Psychedelic Rock" ]
|
13 |
+
|
14 |
+
@st.cache_resource()
|
15 |
+
def load_model():
|
16 |
+
model = MusicGen.get_pretrained('facebook/musicgen-melody')
|
17 |
+
return model
|
18 |
+
|
19 |
+
def generate_music_tensors(descriptions, duration: int):
|
20 |
+
model = load_model()
|
21 |
+
# model = load_model().to('cpu')
|
22 |
+
|
23 |
+
|
24 |
+
model.set_generation_params(
|
25 |
+
use_sampling=True,
|
26 |
+
top_k=250,
|
27 |
+
duration=duration
|
28 |
+
)
|
29 |
+
|
30 |
+
with st.spinner("Generating Music..."):
|
31 |
+
output = model.generate(
|
32 |
+
descriptions=descriptions,
|
33 |
+
progress=True,
|
34 |
+
return_tokens=True
|
35 |
+
)
|
36 |
+
|
37 |
+
st.success("Music Generation Complete!")
|
38 |
+
return output
|
39 |
+
|
40 |
+
|
41 |
+
def save_audio(samples: torch.Tensor):
|
42 |
+
sample_rate = 30000
|
43 |
+
save_path = "audio_output"
|
44 |
+
assert samples.dim() == 2 or samples.dim() == 3
|
45 |
+
|
46 |
+
samples = samples.detach().cpu()
|
47 |
+
if samples.dim() == 2:
|
48 |
+
samples = samples[None, ...]
|
49 |
+
|
50 |
+
for idx, audio in enumerate(samples):
|
51 |
+
audio_path = os.path.join(save_path, f"audio_{idx}.wav")
|
52 |
+
torchaudio.save(audio_path, audio, sample_rate)
|
53 |
+
|
54 |
+
def get_binary_file_downloader_html(bin_file, file_label='File'):
|
55 |
+
with open(bin_file, 'rb') as f:
|
56 |
+
data = f.read()
|
57 |
+
bin_str = base64.b64encode(data).decode()
|
58 |
+
href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">Download {file_label}</a>'
|
59 |
+
return href
|
60 |
+
|
61 |
+
st.set_page_config(
|
62 |
+
page_icon= "musical_note",
|
63 |
+
page_title= "Music Gen"
|
64 |
+
)
|
65 |
+
|
66 |
+
def main():
|
67 |
+
with st.sidebar:
|
68 |
+
st.header("""⚙️Generate Music ⚙️""",divider="rainbow")
|
69 |
+
st.text("")
|
70 |
+
st.subheader("1. Enter your music description.......")
|
71 |
+
bpm = st.number_input("Enter Speed in BPM", min_value=60)
|
72 |
+
|
73 |
+
text_area = st.text_area('Ex : 80s rock song with guitar and drums')
|
74 |
+
st.text('')
|
75 |
+
# Dropdown for genres
|
76 |
+
selected_genre = st.selectbox("Select Genre", genres)
|
77 |
+
|
78 |
+
st.subheader("2. Select time duration (In Seconds)")
|
79 |
+
time_slider = st.slider("Select time duration (In Seconds)", 0, 10, 10)
|
80 |
+
# time_slider = st.slider("Select time duration (In Minutes)", 0,300,10, step=1)
|
81 |
+
|
82 |
+
|
83 |
+
st.title("""🎵 Song Lab AI Melody-Model 🎵""")
|
84 |
+
st.text('')
|
85 |
+
left_co,right_co = st.columns(2)
|
86 |
+
left_co.write("""Music Generation through a prompt""")
|
87 |
+
left_co.write(("""PS : First generation may take some time ......."""))
|
88 |
+
|
89 |
+
if st.sidebar.button('Generate !'):
|
90 |
+
with left_co:
|
91 |
+
st.text('')
|
92 |
+
st.text('')
|
93 |
+
st.text('')
|
94 |
+
st.text('')
|
95 |
+
st.text('')
|
96 |
+
st.text('')
|
97 |
+
st.text('\n\n')
|
98 |
+
st.subheader("Generated Music")
|
99 |
+
|
100 |
+
# Generate audio
|
101 |
+
# descriptions = [f"{text_area} {selected_genre} {bpm} BPM" for _ in range(5)]
|
102 |
+
descriptions = [f"{text_area} {selected_genre} {bpm} BPM" for _ in range(1)] # Change the batch size to 1
|
103 |
+
music_tensors = generate_music_tensors(descriptions, time_slider)
|
104 |
+
|
105 |
+
# Only play the full audio for index 0
|
106 |
+
idx = 0
|
107 |
+
music_tensor = music_tensors[idx]
|
108 |
+
save_music_file = save_audio(music_tensor)
|
109 |
+
audio_filepath = f'/audio_output/audio_{idx}.wav'
|
110 |
+
audio_file = open(audio_filepath, 'rb')
|
111 |
+
audio_bytes = audio_file.read()
|
112 |
+
|
113 |
+
# Play the full audio
|
114 |
+
st.audio(audio_bytes, format='audio/wav')
|
115 |
+
st.markdown(get_binary_file_downloader_html(audio_filepath, f'Audio_{idx}'), unsafe_allow_html=True)
|
116 |
+
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
119 |
+
main()
|
120 |
+
|