Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import tempfile
|
| 3 |
import os
|
| 4 |
-
import requests
|
| 5 |
-
|
| 6 |
from basic_pitch.inference import predict
|
| 7 |
-
from basic_pitch import ICASSP_2022_MODEL_PATH
|
| 8 |
-
import pretty_midi
|
| 9 |
|
| 10 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
model_or_model_path=ICASSP_2022_MODEL_PATH
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
notes = []
|
| 28 |
-
for inst in midi_data.instruments:
|
| 29 |
-
for n in inst.notes:
|
| 30 |
-
notes.append({
|
| 31 |
-
"pitch": int(n.pitch),
|
| 32 |
-
"startTime": round(n.start, 4),
|
| 33 |
-
"duration": round(n.end - n.start, 4),
|
| 34 |
-
"velocity": int(n.velocity)
|
| 35 |
-
})
|
| 36 |
-
|
| 37 |
-
return {
|
| 38 |
-
"instrument": instrument,
|
| 39 |
-
"notes": notes
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
except Exception as e:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
| 57 |
if __name__ == "__main__":
|
| 58 |
-
demo.launch(
|
| 59 |
-
ssr_mode=False,
|
| 60 |
-
theme=gr.themes.Soft(
|
| 61 |
-
primary_hue="indigo",
|
| 62 |
-
secondary_hue="blue",
|
| 63 |
-
),
|
| 64 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
from basic_pitch.inference import predict
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
def convert_audio_to_midi(audio_filepath):
|
| 6 |
+
"""
|
| 7 |
+
Takes an audio file path, runs Basic Pitch, and saves/returns a MIDI file.
|
| 8 |
+
"""
|
| 9 |
+
if audio_filepath is None:
|
| 10 |
+
return None
|
| 11 |
+
|
| 12 |
try:
|
| 13 |
+
# Run the basic pitch model on the audio file
|
| 14 |
+
# predict() returns model_output, midi_data, and note_events
|
| 15 |
+
_, midi_data, _ = predict(audio_filepath)
|
| 16 |
+
|
| 17 |
+
# Define a temporary output path for the generated MIDI file
|
| 18 |
+
output_midi_path = "output.mid"
|
| 19 |
+
|
| 20 |
+
# Save the MIDI data to the file
|
| 21 |
+
midi_data.write(output_midi_path)
|
| 22 |
+
|
| 23 |
+
return output_midi_path
|
| 24 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
+
print(f"Error during conversion: {e}")
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
# Build the Gradio Interface
|
| 30 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 31 |
+
gr.Markdown(
|
| 32 |
+
"""
|
| 33 |
+
# 🎵 Basic Pitch: Audio to MIDI Converter
|
| 34 |
+
Upload an audio file (vocals, guitar, piano, etc.) and convert it into a MIDI file using Spotify's open-source **Basic Pitch** machine learning model.
|
| 35 |
+
"""
|
| 36 |
+
)
|
| 37 |
|
| 38 |
+
with gr.Row():
|
| 39 |
+
with gr.Column():
|
| 40 |
+
# User uploads audio; Gradio passes the file path to our function
|
| 41 |
+
audio_input = gr.Audio(type="filepath", label="Upload Audio File (WAV, MP3, etc.)")
|
| 42 |
+
convert_btn = gr.Button("Convert to MIDI", variant="primary")
|
| 43 |
+
|
| 44 |
+
with gr.Column():
|
| 45 |
+
# The output will be a downloadable file
|
| 46 |
+
midi_output = gr.File(label="Download Generated MIDI")
|
| 47 |
+
|
| 48 |
+
# Link the button click to the conversion function
|
| 49 |
+
convert_btn.click(
|
| 50 |
+
fn=convert_audio_to_midi,
|
| 51 |
+
inputs=audio_input,
|
| 52 |
+
outputs=midi_output
|
| 53 |
+
)
|
| 54 |
|
| 55 |
+
# Launch the app
|
| 56 |
if __name__ == "__main__":
|
| 57 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|