-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
283 lines (204 loc) · 10.1 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os
import streamlit as st
import pandas as pd
import numpy as np
import cv2
from PIL import Image
import plotly.graph_objects as go
# from midi2audio import FluidSynth
import time
from app_utils import *
from midi2audio import FluidSynth
import warnings
warnings.filterwarnings('ignore')
# '''
# TODO :
# 0) Format the code
# 1) Download the checkpoints for genre based remixing : 'lakh_genre_model.pth'
# 2) In the genre continuation UI, add slider for Instrument Temperature & Output BPM and dropdown for Genre
# 3) Complete the note remixing section and embedd the required model
# 4) What to do about s2s ?
# 5) Update the application to a good framework
# '''
if __name__ == '__main__':
# Basic Page Configurations
st.set_page_config(
page_title="Deep Music Generation",
page_icon="🎙️",
layout="centered",
menu_items={
'About': "##### This is an SER based Emotion prediction app which predicts the emotion by analyzing the input audio of human voice."
}
)
# markdown for customization
st.markdown("""
<style>
div.stButton > button:first-child {
border-radius: 0%;
height: 3em;
width: 44em;
}
.stProgress > div > div > div > div {
background-color: #FF4B4B;
}
</style>""", unsafe_allow_html=True)
# load all the models
print(f'Loading the models......')
# 1) Music generation model with genre conditioning
genre_model = createGenreContinuationModel()
# 2) S2S model
# 3) Remix model (MASK one I guess)
remix_model = createRemixModel()
print(f'Models loaded')
###################################################################################################################################
###################################################################################################################################
###################################################################################################################################
# Main Body
# Title of the page
st.title("Deep Music Generation")
# Cover of the page
cover = Image.open('./images/cover2.png')
st.image(cover)
# give indent
st.write(" ")
# Show metrics of the project
with st.container():
col1, col2, col3 = st.columns(3)
col1.metric("Models Embedded", "3", "+3")
col2.metric("Instruments", "6", "+6")
col3.metric("Genre Classes", "6", "+6")
# give indent
st.write(" ")
st.write(" ")
###################################################################################################################################
###################################################################################################################################
###################################################################################################################################
# side navigation bar
with st.sidebar:
nav = Image.open('./images/nav.jpg')
st.image(nav)
st.write(" ")
st.title('Select the task type:')
st.write(" ")
option = st.selectbox(
'Models',
('Music Generation', 'Instrument Interconversion', 'Music remixing'))
st.info(f'Currently performing : {option}')
# Main content of the page
if(option == 'Music Generation'):
st.subheader("Input")
uploaded_file = st.file_uploader("Upload an audio file",
type=['mid'],
accept_multiple_files = False,)
# fs = FluidSynth()
# fs.midi_to_audio(uploaded_file, 'output.wav')
# print(type(uploaded_file), uploaded_file.name)
if uploaded_file:
with open("tempDir/uploadedMidi.mid","wb") as f:
f.write(uploaded_file.getbuffer())
data_vocab = deep_music_genre.MusicVocab.create()
# deep_music_genre.MusicItem.from_file("tempDir/uploadedMidi.mid", data_vocab).to_stream().write('mp3', fp= "tempDir/uploadedFile.wav")
# os.system('fluidsynth wt_183k_G.sf2 -F tempDir/uploadedMidi.mid tempDir/uploadedFile.wav')
# fs.midi_to_audio("./tempDir/uploadedMidi.mid", "./tempDir/uploadedFile.wav")
st.audio('tempDir/uploadedMidi.mid', format='audio/wav', start_time=0)
st.write(" ")
st.write(" ")
st.subheader("Parameters")
# temperature
temperature_notes = st.slider('Temperature (Notes)', 0.9, 2.5, 1.8)
temperature_duration = st.slider('Temperature (Duration)', 0.9, 2.5, 1.8)
temperature_instrument = st.slider('Temperature (Instrument)', 0.9, 2.5, 1.0)
top_p = st.slider('Top p', 0.0, 1.0, 0.3)
output_bpm = st.slider('Output BPM', 1, 240, 120)
with st.container():
col1, col2, col3 = st.columns(3)
with col1:
num_tokens = st.number_input('Maximum Length', min_value = 0, max_value = 1024)
with col2:
bars = st.number_input('cutoff beat', min_value = 4, max_value = 128)
with col3:
mem_len = st.number_input('Memory Length', min_value = 512, max_value = 2048)
ins_list = st.multiselect(
'What are your favorite instruments',
['Piano', 'Guitar', 'Bass', 'Violin', 'Flute', 'Brass', 'Misc'],
[])
genre = st.selectbox('What genre do you like?',
('Auto', 'Pop', 'Folk', 'Jazz', 'Rock', 'Electronic'))
st.write(" ")
st.write(" ")
st.subheader("Predict")
if st.button('Run Prediction'):
st.write(" ")
# Generates the prediction and automatically saves the file
full = predictNwGenreModel(genre_model, 'tempDir/uploadedMidi.mid', top_p= top_p,
genre = genre, temperature_notes = temperature_notes, temperature_duration = temperature_duration,
temperature_ins = temperature_instrument, mem_len = num_tokens, allowed_ins= ins_list,
cutoff_beat = bars, output_bpm = output_bpm)
st.write("Generating Output File")
my_bar = st.progress(0)
for percent_complete in range(100):
time.sleep(0.1)
my_bar.progress(percent_complete + 1)
full.to_stream(bpm = output_bpm).write('midi', fp= './outputs/genre_output.mid')
st.write(" ")
st.write(" ")
st.subheader("Output")
if(percent_complete == 99):
st.success(f'The output is saved as genre_output.mid in outputs folder')
st.write(" ")
elif(option == 'Music remixing'):
st.subheader("Input")
uploaded_file = st.file_uploader("Upload an audio file",
type=['mid'],
accept_multiple_files = False)
if uploaded_file:
with open("tempDir/uploadedMidi.mid","wb") as f:
f.write(uploaded_file.getbuffer())
data_vocab = deep_music_genre.MusicVocab.create()
# deep_music_genre.MusicItem.from_file("tempDir/uploadedMidi.mid", data_vocab).to_stream().write('mp3', fp= "tempDir/uploadedFile.wav")
# os.system('fluidsynth wt_183k_G.sf2 -F tempDir/uploadedMidi.mid tempDir/uploadedFile.wav')
# fs.midi_to_audio("./tempDir/uploadedMidi.mid", "./tempDir/uploadedFile.wav")
st.audio('tempDir/uploadedMidi.mid', format='audio/wav', start_time=0)
st.write(" ")
st.write(" ")
# parameters to be tweaked
st.subheader("Parameters")
# temperature
temperature_notes = st.slider('Temperature (Notes)', 0.9, 2.5, 1.0)
temperature_duration = st.slider('Temperature (Duration)', 0.9, 2.5, 1.0)
# top-p
top_p = st.slider('Top p', 0.0, 1.0, 0.3)
# percentage of notes/duration mask
mask_percentage = st.slider('Mask Percentage', 10, 100, 60)
output_bpm = st.slider('Output BPM', 1, 240, 120)
with st.container():
col1, col2, col3 = st.columns(3)
with col1:
genre = st.selectbox('What genre do you like?',
('Auto', 'Pop', 'Folk', 'Jazz', 'Rock', 'Electronic'))
with col2:
bars = st.number_input('cutoff beat', min_value = 4, max_value = 128)
with col3:
# Remix type
remix_type = st.selectbox(
'What do you want to remix?',
('Notes', 'Duration'))
st.write(" ")
st.write(" ")
st.subheader("Predict")
if st.button('Run Prediction'):
st.write(" ")
full = predictMaskModel(remix_model, 'tempDir/uploadedMidi.mid', top_p = top_p,
genre = genre, temperature_notes = temperature_notes, temperature_duration = temperature_duration,
cutoff_beat = bars, output_bpm = output_bpm, pred_type = remix_type.lower(), mask_proportion = mask_percentage/100)
st.write("Generating Output File")
my_bar = st.progress(0)
for percent_complete in range(100):
time.sleep(0.1)
my_bar.progress(percent_complete + 1)
full.to_stream(bpm = output_bpm).write('midi', fp= f'./outputs/remix_{remix_type}_output.mid')
st.write(" ")
st.write(" ")
st.subheader("Output")
if(percent_complete == 99):
st.success(f'The output is saved as remix_{remix_type}_output.mid in outputs folder')