Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,11 +6,11 @@ import numpy as np
|
|
6 |
import base64
|
7 |
from audiocraft.models import MusicGen
|
8 |
|
9 |
-
# Before
|
10 |
-
batch_size = 64
|
11 |
-
|
12 |
-
# After
|
13 |
-
batch_size = 32
|
14 |
torch.cuda.empty_cache()
|
15 |
|
16 |
genres = ["Pop", "Rock", "Jazz", "Electronic", "Hip-Hop", "Classical", "Lofi", "Chillpop"]
|
@@ -21,6 +21,8 @@ def load_model():
|
|
21 |
return model
|
22 |
|
23 |
def generate_music_tensors(description, duration: int):
|
|
|
|
|
24 |
model = load_model()
|
25 |
|
26 |
model.set_generation_params(
|
@@ -31,13 +33,13 @@ def generate_music_tensors(description, duration: int):
|
|
31 |
|
32 |
with st.spinner("Generating Music..."):
|
33 |
output = model.generate(
|
34 |
-
descriptions=description,
|
35 |
progress=True,
|
36 |
return_tokens=True
|
37 |
)
|
38 |
|
39 |
st.success("Music Generation Complete!")
|
40 |
-
return output
|
41 |
|
42 |
def save_audio(samples: torch.Tensor):
|
43 |
print("Samples (inside function): ", samples)
|
@@ -63,28 +65,30 @@ def get_binary_file_downloader_html(bin_file, file_label='File'):
|
|
63 |
|
64 |
st.set_page_config(
|
65 |
page_icon= "musical_note",
|
66 |
-
page_title= "Music
|
67 |
)
|
68 |
|
69 |
def main():
|
70 |
-
st.title("š§
|
71 |
|
72 |
-
st.subheader("Craft your perfect melody!")
|
73 |
-
bpm = st.number_input("Enter Speed in BPM", min_value=
|
74 |
|
75 |
-
text_area = st.text_area('Ex :
|
76 |
st.text('')
|
77 |
# Dropdown for genres
|
78 |
selected_genre = st.selectbox("Select Genre", genres)
|
79 |
|
80 |
-
st.subheader("2. Select time duration (In Seconds)")
|
81 |
-
|
82 |
-
mood = st.selectbox("Select Mood (Optional)", ["Happy", "Sad", "Angry", "Relaxed", "Energetic"]
|
83 |
-
instrument = st.selectbox("Select Instrument (Optional)", ["Piano", "Guitar", "Flute", "Violin", "Drums"]
|
84 |
-
tempo = st.selectbox("Select Tempo (Optional)", ["Slow", "Moderate", "Fast"]
|
85 |
-
|
|
|
86 |
|
87 |
if st.button('Let\'s Generate š¶'):
|
|
|
88 |
st.text('\n\n')
|
89 |
st.subheader("Generated Music")
|
90 |
|
@@ -93,8 +97,8 @@ def main():
|
|
93 |
if selected_genre:
|
94 |
description += f" {selected_genre}"
|
95 |
st.empty() # Hide the selected_genre selectbox after selecting one option
|
96 |
-
if bpm:
|
97 |
-
|
98 |
if mood:
|
99 |
description += f" {mood}"
|
100 |
st.empty() # Hide the mood selectbox after selecting one option
|
@@ -104,12 +108,15 @@ def main():
|
|
104 |
if tempo:
|
105 |
description += f" {tempo}"
|
106 |
st.empty() # Hide the tempo selectbox after selecting one option
|
107 |
-
if melody:
|
108 |
-
|
109 |
|
110 |
# Clear CUDA memory cache before generating music
|
111 |
torch.cuda.empty_cache()
|
112 |
-
|
|
|
|
|
|
|
113 |
music_tensors = generate_music_tensors(description, time_slider)
|
114 |
|
115 |
# Only play the full audio for index 0
|
@@ -122,7 +129,7 @@ def main():
|
|
122 |
audio_bytes = audio_file.read()
|
123 |
|
124 |
# Play the full audio
|
125 |
-
st.audio(audio_bytes
|
126 |
st.markdown(get_binary_file_downloader_html(audio_filepath, f'Audio'), unsafe_allow_html=True)
|
127 |
|
128 |
if __name__ == "__main__":
|
|
|
6 |
import base64
|
7 |
from audiocraft.models import MusicGen
|
8 |
|
9 |
+
# # Before
|
10 |
+
# batch_size = 64
|
11 |
+
#
|
12 |
+
# # After
|
13 |
+
# batch_size = 32
|
14 |
torch.cuda.empty_cache()
|
15 |
|
16 |
genres = ["Pop", "Rock", "Jazz", "Electronic", "Hip-Hop", "Classical", "Lofi", "Chillpop"]
|
|
|
21 |
return model
|
22 |
|
23 |
def generate_music_tensors(description, duration: int):
|
24 |
+
print("Description: ", description)
|
25 |
+
print("Duration: ", duration)
|
26 |
model = load_model()
|
27 |
|
28 |
model.set_generation_params(
|
|
|
33 |
|
34 |
with st.spinner("Generating Music..."):
|
35 |
output = model.generate(
|
36 |
+
descriptions=[description],
|
37 |
progress=True,
|
38 |
return_tokens=True
|
39 |
)
|
40 |
|
41 |
st.success("Music Generation Complete!")
|
42 |
+
return output[0]
|
43 |
|
44 |
def save_audio(samples: torch.Tensor):
|
45 |
print("Samples (inside function): ", samples)
|
|
|
65 |
|
66 |
st.set_page_config(
|
67 |
page_icon= "musical_note",
|
68 |
+
page_title= "AI Music Composer"
|
69 |
)
|
70 |
|
71 |
def main():
|
72 |
+
st.title("š§AI Music Composer šµ")
|
73 |
|
74 |
+
# st.subheader("Craft your perfect melody!")
|
75 |
+
# bpm = st.number_input("Enter Speed in BPM", min_value=60)
|
76 |
|
77 |
+
text_area = st.text_area('Ex : Create an epic and majestic theme for a historical documentary or period drama.')
|
78 |
st.text('')
|
79 |
# Dropdown for genres
|
80 |
selected_genre = st.selectbox("Select Genre", genres)
|
81 |
|
82 |
+
# st.subheader("2. Select time duration (In Seconds)")
|
83 |
+
|
84 |
+
mood = st.selectbox("Select Mood (Optional)", ["Happy", "Sad", "Angry", "Relaxed", "Energetic"])
|
85 |
+
instrument = st.selectbox("Select Instrument (Optional)", ["Piano", "Guitar", "Flute", "Violin", "Drums"])
|
86 |
+
tempo = st.selectbox("Select Tempo (Optional)", ["Slow", "Moderate", "Fast"])
|
87 |
+
time_slider = st.slider("Select time duration (In Seconds)", 0, 60, 10)
|
88 |
+
# melody = st.text_input("Enter Melody or Chord Progression (Optional) e.g: C D:min G:7 C, Twinkle Twinkle Little Star", " ")
|
89 |
|
90 |
if st.button('Let\'s Generate š¶'):
|
91 |
+
|
92 |
st.text('\n\n')
|
93 |
st.subheader("Generated Music")
|
94 |
|
|
|
97 |
if selected_genre:
|
98 |
description += f" {selected_genre}"
|
99 |
st.empty() # Hide the selected_genre selectbox after selecting one option
|
100 |
+
# if bpm:
|
101 |
+
# description += f" {bpm} BPM"
|
102 |
if mood:
|
103 |
description += f" {mood}"
|
104 |
st.empty() # Hide the mood selectbox after selecting one option
|
|
|
108 |
if tempo:
|
109 |
description += f" {tempo}"
|
110 |
st.empty() # Hide the tempo selectbox after selecting one option
|
111 |
+
# if melody:
|
112 |
+
# description += f" {melody}"
|
113 |
|
114 |
# Clear CUDA memory cache before generating music
|
115 |
torch.cuda.empty_cache()
|
116 |
+
st.json({
|
117 |
+
'Your Description': description,
|
118 |
+
'Selected Time Duration (in Seconds)': time_slider
|
119 |
+
})
|
120 |
music_tensors = generate_music_tensors(description, time_slider)
|
121 |
|
122 |
# Only play the full audio for index 0
|
|
|
129 |
audio_bytes = audio_file.read()
|
130 |
|
131 |
# Play the full audio
|
132 |
+
st.audio(audio_bytes)
|
133 |
st.markdown(get_binary_file_downloader_html(audio_filepath, f'Audio'), unsafe_allow_html=True)
|
134 |
|
135 |
if __name__ == "__main__":
|