Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -132,74 +132,58 @@ def main():
|
|
132 |
np.random.seed(seed)
|
133 |
|
134 |
# Rutas de archivos
|
135 |
-
sample_file = 'Preludes 2 Through Major keys 39.mid'
|
136 |
out_file = 'output.mid'
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
# # To convert to a string based IO:
|
146 |
-
# stringio = StringIO(sample_file.getvalue().decode("utf-8"))
|
147 |
-
# st.write(stringio)
|
148 |
-
|
149 |
-
# # To read file as string:
|
150 |
-
# string_data = stringio.read()
|
151 |
-
# st.write(string_data)
|
152 |
-
|
153 |
-
# # Can be used wherever a "file-like" object is accepted:
|
154 |
-
# dataframe = pd.read_csv(uploaded_file)
|
155 |
-
# st.write(dataframe)
|
156 |
-
|
157 |
-
# Cargar modelo y pesos
|
158 |
-
with custom_object_scope({'mse_with_positive_pressure': mse_with_positive_pressure}):
|
159 |
-
model = keras.models.load_model("mi_modelo_music.h5")
|
160 |
-
|
161 |
-
model.load_weights("mi_pesos_music.h5", skip_mismatch=False, by_name=False, options=None)
|
162 |
-
|
163 |
-
# Convertir MIDI generado por el modelo a archivo WAV
|
164 |
-
pm = pretty_midi.PrettyMIDI(sample_file)
|
165 |
-
instrument_name = pretty_midi.program_to_instrument_name(pm.instruments[0].program)
|
166 |
-
raw_notes = midi_to_notes(sample_file)
|
167 |
-
key_order = ['pitch', 'step', 'duration']
|
168 |
-
seq_length = 25
|
169 |
-
vocab_size = 128
|
170 |
-
temperature = 2.0
|
171 |
-
num_predictions = 120
|
172 |
-
sample_notes = np.stack([raw_notes[key] for key in key_order], axis=1)
|
173 |
-
input_notes = (sample_notes[:seq_length] / np.array([vocab_size, 1, 1]))
|
174 |
-
generated_notes = []
|
175 |
-
prev_start = 0
|
176 |
-
for _ in range(num_predictions):
|
177 |
-
pitch, step, duration = predict_next_note(input_notes, model, temperature)
|
178 |
-
start = prev_start + step
|
179 |
-
end = start + duration
|
180 |
-
input_note = (pitch, step, duration)
|
181 |
-
generated_notes.append((*input_note, start, end))
|
182 |
-
input_notes = np.delete(input_notes, 0, axis=0)
|
183 |
-
input_notes = np.append(input_notes, np.expand_dims(input_note, 0), axis=0)
|
184 |
-
prev_start = start
|
185 |
-
|
186 |
-
generated_notes = pd.DataFrame(
|
187 |
-
generated_notes, columns=(*key_order, 'start', 'end'))
|
188 |
-
|
189 |
-
notes_to_midi(
|
190 |
-
generated_notes, out_file=out_file, instrument_name=instrument_name)
|
191 |
-
|
192 |
-
# Interfaz de Streamlit
|
193 |
-
st.title("Generador de notas musicales")
|
194 |
-
|
195 |
-
archivo_midi = open(out_file, 'rb').read()
|
196 |
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
if __name__ == "__main__":
|
205 |
main()
|
|
|
132 |
np.random.seed(seed)
|
133 |
|
134 |
# Rutas de archivos
|
135 |
+
#sample_file = 'Preludes 2 Through Major keys 39.mid'
|
136 |
out_file = 'output.mid'
|
137 |
+
sample_file = st.file_uploader("Sube un archivo MIDI")
|
138 |
+
|
139 |
+
if sample_file is not None:
|
140 |
+
|
141 |
+
# Cargar modelo y pesos
|
142 |
+
with custom_object_scope({'mse_with_positive_pressure': mse_with_positive_pressure}):
|
143 |
+
model = keras.models.load_model("mi_modelo_music.h5")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
+
model.load_weights("mi_pesos_music.h5", skip_mismatch=False, by_name=False, options=None)
|
146 |
+
|
147 |
+
# Convertir MIDI generado por el modelo a archivo WAV
|
148 |
+
pm = pretty_midi.PrettyMIDI(sample_file)
|
149 |
+
instrument_name = pretty_midi.program_to_instrument_name(pm.instruments[0].program)
|
150 |
+
raw_notes = midi_to_notes(sample_file)
|
151 |
+
key_order = ['pitch', 'step', 'duration']
|
152 |
+
seq_length = 25
|
153 |
+
vocab_size = 128
|
154 |
+
temperature = 2.0
|
155 |
+
num_predictions = 120
|
156 |
+
sample_notes = np.stack([raw_notes[key] for key in key_order], axis=1)
|
157 |
+
input_notes = (sample_notes[:seq_length] / np.array([vocab_size, 1, 1]))
|
158 |
+
generated_notes = []
|
159 |
+
prev_start = 0
|
160 |
+
for _ in range(num_predictions):
|
161 |
+
pitch, step, duration = predict_next_note(input_notes, model, temperature)
|
162 |
+
start = prev_start + step
|
163 |
+
end = start + duration
|
164 |
+
input_note = (pitch, step, duration)
|
165 |
+
generated_notes.append((*input_note, start, end))
|
166 |
+
input_notes = np.delete(input_notes, 0, axis=0)
|
167 |
+
input_notes = np.append(input_notes, np.expand_dims(input_note, 0), axis=0)
|
168 |
+
prev_start = start
|
169 |
+
|
170 |
+
generated_notes = pd.DataFrame(
|
171 |
+
generated_notes, columns=(*key_order, 'start', 'end'))
|
172 |
+
|
173 |
+
notes_to_midi(
|
174 |
+
generated_notes, out_file=out_file, instrument_name=instrument_name)
|
175 |
+
|
176 |
+
# Interfaz de Streamlit
|
177 |
+
st.title("Generador de notas musicales")
|
178 |
+
|
179 |
+
archivo_midi = open(out_file, 'rb').read()
|
180 |
+
|
181 |
+
st.download_button(
|
182 |
+
label="Descargar MIDI",
|
183 |
+
data=archivo_midi,
|
184 |
+
file_name=out_file, # Nombre del archivo que se descargará
|
185 |
+
mime='audio/midi'
|
186 |
+
)
|
187 |
|
188 |
if __name__ == "__main__":
|
189 |
main()
|