Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,20 +6,17 @@ 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-small')
|
| 17 |
return model
|
|
|
|
| 18 |
|
| 19 |
-
def generate_music_tensors(
|
| 20 |
model = load_model()
|
| 21 |
-
# model = load_model().to('cpu')
|
| 22 |
-
|
| 23 |
|
| 24 |
model.set_generation_params(
|
| 25 |
use_sampling=True,
|
|
@@ -27,20 +24,30 @@ def generate_music_tensors(descriptions, duration: int):
|
|
| 27 |
duration=duration
|
| 28 |
)
|
| 29 |
|
| 30 |
-
with st.spinner("Generating Music
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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()
|
|
@@ -48,8 +55,9 @@ def save_audio(samples: torch.Tensor):
|
|
| 48 |
samples = samples[None, ...]
|
| 49 |
|
| 50 |
for idx, audio in enumerate(samples):
|
| 51 |
-
audio_path = os.path.join(save_path, f"
|
| 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:
|
|
@@ -64,56 +72,36 @@ st.set_page_config(
|
|
| 64 |
)
|
| 65 |
|
| 66 |
def main():
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 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, 60, 10)
|
| 80 |
-
# time_slider = st.slider("Select time duration (In Minutes)", 0,300,10, step=1)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
st.title("""🎵 AI Composer Small-Model 🎵""")
|
| 84 |
-
st.text('')
|
| 85 |
-
left_co,right_co = st.columns(2)
|
| 86 |
-
left_co.write("""Music Generation with Prompts""")
|
| 87 |
-
left_co.write(("""First generation may take some time ......."""))
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 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()
|
|
|
|
| 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","EDM", "Disco", "House", "Techno",]
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@st.cache_resource()
|
| 13 |
def load_model():
|
| 14 |
model = MusicGen.get_pretrained('facebook/musicgen-small')
|
| 15 |
return model
|
| 16 |
+
|
| 17 |
|
| 18 |
+
def generate_music_tensors(description, duration: int, batch_size=1):
|
| 19 |
model = load_model()
|
|
|
|
|
|
|
| 20 |
|
| 21 |
model.set_generation_params(
|
| 22 |
use_sampling=True,
|
|
|
|
| 24 |
duration=duration
|
| 25 |
)
|
| 26 |
|
| 27 |
+
with st.spinner("Generating Music..."):
|
| 28 |
+
output = []
|
| 29 |
+
for i in range(0, len(description), batch_size):
|
| 30 |
+
batch_descriptions = description[i:i+batch_size]
|
| 31 |
+
batch_output = model.generate(
|
| 32 |
+
descriptions=batch_descriptions,
|
| 33 |
+
progress=True,
|
| 34 |
+
return_tokens=True
|
| 35 |
+
)
|
| 36 |
+
output.extend(batch_output)
|
| 37 |
+
|
| 38 |
+
# output = model.generate(
|
| 39 |
+
# descriptions=description,
|
| 40 |
+
# progress=True,
|
| 41 |
+
# return_tokens=True
|
| 42 |
+
# )
|
| 43 |
|
| 44 |
st.success("Music Generation Complete!")
|
| 45 |
return output
|
| 46 |
+
|
| 47 |
|
| 48 |
+
def save_audio(samples: torch.Tensor, filename):
|
|
|
|
| 49 |
sample_rate = 30000
|
| 50 |
+
save_path = "/content/drive/MyDrive/Colab Notebooks/audio_output"
|
| 51 |
assert samples.dim() == 2 or samples.dim() == 3
|
| 52 |
|
| 53 |
samples = samples.detach().cpu()
|
|
|
|
| 55 |
samples = samples[None, ...]
|
| 56 |
|
| 57 |
for idx, audio in enumerate(samples):
|
| 58 |
+
audio_path = os.path.join(save_path, f"{filename}_{idx}.wav")
|
| 59 |
torchaudio.save(audio_path, audio, sample_rate)
|
| 60 |
+
return audio_path
|
| 61 |
|
| 62 |
def get_binary_file_downloader_html(bin_file, file_label='File'):
|
| 63 |
with open(bin_file, 'rb') as f:
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
def main():
|
| 75 |
+
st.title("🎧AI Composer Medium-Model 🎧")
|
| 76 |
+
|
| 77 |
+
st.subheader("Generate Music")
|
| 78 |
+
st.write("Craft your perfect melody! Fill in the blanks below to create your music masterpiece:")
|
| 79 |
+
|
| 80 |
+
bpm = st.number_input("Enter Speed in BPM", min_value=60)
|
| 81 |
+
text_area = st.text_area('Example: 80s rock song with guitar and drums')
|
| 82 |
+
selected_genre = st.selectbox("Select Genre", genres)
|
| 83 |
+
time_slider = st.slider("Select time duration (In Seconds)", 0, 30, 10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
mood = st.selectbox("Select Mood", ["Happy", "Sad", "Angry", "Relaxed", "Energetic"])
|
| 86 |
+
instrument = st.selectbox("Select Instrument", ["Piano", "Guitar", "Flute", "Violin", "Drums"])
|
| 87 |
+
tempo = st.selectbox("Select Tempo", ["Slow", "Moderate", "Fast"])
|
| 88 |
+
melody = st.text_input("Enter Melody or Chord Progression", "e.g., C D:min G:7 C, Twinkle Twinkle Little Star")
|
| 89 |
+
|
| 90 |
+
if st.button('Let\'s Generate 🎶'):
|
| 91 |
+
st.text('\n\n')
|
| 92 |
+
st.subheader("Generated Music")
|
| 93 |
+
|
| 94 |
+
description = f"{text_area} {selected_genre} {bpm} BPM {mood} {instrument} {tempo} {melody}"
|
| 95 |
+
music_tensors = generate_music_tensors(description, time_slider, batch_size=2)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
idx = 0
|
| 99 |
+
audio_path = save_audio(music_tensors[idx], "audio_output")
|
| 100 |
+
audio_file = open(audio_path, 'rb')
|
| 101 |
+
audio_bytes = audio_file.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
st.audio(audio_bytes, format='audio/wav')
|
| 104 |
+
st.markdown(get_binary_file_downloader_html(audio_path, f'Audio_{idx}'), unsafe_allow_html=True)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
main()
|