Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,29 @@
|
|
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","
|
11 |
-
"EDM", "Soil", "Funk","Reggae", "Disco", "Punk Rock", "House",
|
12 |
-
"Techno","Indie Rock", "Grunge", "Ambient","Gospel" ]
|
13 |
|
14 |
@st.cache_resource()
|
15 |
def load_model():
|
16 |
model = MusicGen.get_pretrained('facebook/musicgen-medium')
|
17 |
return model
|
|
|
18 |
|
19 |
-
def generate_music_tensors(description, duration: int):
|
20 |
model = load_model()
|
21 |
|
22 |
model.set_generation_params(
|
@@ -26,14 +33,25 @@ def generate_music_tensors(description, duration: int):
|
|
26 |
)
|
27 |
|
28 |
with st.spinner("Generating Music..."):
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
st.success("Music Generation Complete!")
|
36 |
return output
|
|
|
37 |
|
38 |
def save_audio(samples: torch.Tensor, filename):
|
39 |
sample_rate = 30000
|
@@ -71,8 +89,7 @@ def main():
|
|
71 |
text_area = st.text_area('Example: 80s rock song with guitar and drums')
|
72 |
selected_genre = st.selectbox("Select Genre", genres)
|
73 |
time_slider = st.slider("Select time duration (In Seconds)", 0, 30, 10)
|
74 |
-
|
75 |
-
st.write("Additional options")
|
76 |
mood = st.selectbox("Select Mood", ["Happy", "Sad", "Angry", "Relaxed", "Energetic"])
|
77 |
instrument = st.selectbox("Select Instrument", ["Piano", "Guitar", "Flute", "Violin", "Drums"])
|
78 |
tempo = st.selectbox("Select Tempo", ["Slow", "Moderate", "Fast"])
|
@@ -83,7 +100,8 @@ def main():
|
|
83 |
st.subheader("Generated Music")
|
84 |
|
85 |
description = f"{text_area} {selected_genre} {bpm} BPM {mood} {instrument} {tempo} {melody}"
|
86 |
-
music_tensors = generate_music_tensors(description, time_slider)
|
|
|
87 |
|
88 |
idx = 0
|
89 |
audio_path = save_audio(music_tensors[idx], "audio_output")
|
@@ -95,4 +113,3 @@ def main():
|
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
main()
|
98 |
-
|
|
|
1 |
import streamlit as st
|
2 |
import torch
|
3 |
import torchaudio
|
|
|
4 |
import os
|
5 |
import numpy as np
|
6 |
import base64
|
7 |
+
import math
|
8 |
+
|
9 |
+
from audiocraft.data.audio_utils import convert_audio
|
10 |
+
from audiocraft.data.audio import audio_write
|
11 |
+
from audiocraft.models.encodec import InterleaveStereoCompressionModel
|
12 |
+
from audiocraft.models import MusicGen, MultiBandDiffusion
|
13 |
+
from audiocraft.utils.notebook import display_audio
|
14 |
+
from audiocraft.models import MusicGen
|
15 |
+
# from audiocraft.models import audiogen
|
16 |
|
17 |
genres = ["Pop", "Rock", "Jazz", "Electronic", "Hip-Hop", "Classical",
|
18 |
+
"Lofi", "Chillpop","Country","R&G", "Folk","EDM", "Disco", "House", "Techno",]
|
|
|
|
|
19 |
|
20 |
@st.cache_resource()
|
21 |
def load_model():
|
22 |
model = MusicGen.get_pretrained('facebook/musicgen-medium')
|
23 |
return model
|
24 |
+
|
25 |
|
26 |
+
def generate_music_tensors(description, duration: int, batch_size=1):
|
27 |
model = load_model()
|
28 |
|
29 |
model.set_generation_params(
|
|
|
33 |
)
|
34 |
|
35 |
with st.spinner("Generating Music..."):
|
36 |
+
output = []
|
37 |
+
for i in range(0, len(description), batch_size):
|
38 |
+
batch_descriptions = description[i:i+batch_size]
|
39 |
+
batch_output = model.generate(
|
40 |
+
descriptions=batch_descriptions,
|
41 |
+
progress=True,
|
42 |
+
return_tokens=True
|
43 |
+
)
|
44 |
+
output.extend(batch_output)
|
45 |
+
|
46 |
+
# output = model.generate(
|
47 |
+
# descriptions=description,
|
48 |
+
# progress=True,
|
49 |
+
# return_tokens=True
|
50 |
+
# )
|
51 |
|
52 |
st.success("Music Generation Complete!")
|
53 |
return output
|
54 |
+
|
55 |
|
56 |
def save_audio(samples: torch.Tensor, filename):
|
57 |
sample_rate = 30000
|
|
|
89 |
text_area = st.text_area('Example: 80s rock song with guitar and drums')
|
90 |
selected_genre = st.selectbox("Select Genre", genres)
|
91 |
time_slider = st.slider("Select time duration (In Seconds)", 0, 30, 10)
|
92 |
+
|
|
|
93 |
mood = st.selectbox("Select Mood", ["Happy", "Sad", "Angry", "Relaxed", "Energetic"])
|
94 |
instrument = st.selectbox("Select Instrument", ["Piano", "Guitar", "Flute", "Violin", "Drums"])
|
95 |
tempo = st.selectbox("Select Tempo", ["Slow", "Moderate", "Fast"])
|
|
|
100 |
st.subheader("Generated Music")
|
101 |
|
102 |
description = f"{text_area} {selected_genre} {bpm} BPM {mood} {instrument} {tempo} {melody}"
|
103 |
+
music_tensors = generate_music_tensors(description, time_slider, batch_size=2)
|
104 |
+
|
105 |
|
106 |
idx = 0
|
107 |
audio_path = save_audio(music_tensors[idx], "audio_output")
|
|
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
main()
|
|