|
import gradio as gr
|
|
import librosa
|
|
import os
|
|
import spaces
|
|
from pathlib import Path
|
|
from pytorch.inference import PianoTranscription
|
|
from utils import config
|
|
|
|
from midi2audio import FluidSynth
|
|
|
|
RESULTS_DIR='results'
|
|
|
|
|
|
transcriptor = PianoTranscription("Note_pedal")
|
|
|
|
|
|
soundfont_path = "soundfont/MuseScore_General.sf3"
|
|
fs = FluidSynth(soundfont_path)
|
|
|
|
@spaces.GPU
|
|
def transcribe_and_visualize(audio_file):
|
|
|
|
|
|
base_name = os.path.splitext(os.path.basename(audio_file))[0]
|
|
midi_filename = f"{base_name}_transcription.mid"
|
|
|
|
flac_filename = f"{base_name}_transcription.flac"
|
|
|
|
|
|
audio, _ = librosa.core.load(audio_file, sr=config.sample_rate)
|
|
transcriptor.transcribe(audio, midi_filename)
|
|
|
|
|
|
|
|
|
|
|
|
fs.midi_to_audio(midi_filename, flac_filename)
|
|
|
|
|
|
return flac_filename, midi_filename
|
|
|
|
|
|
iface = gr.Interface(
|
|
fn=transcribe_and_visualize,
|
|
inputs=gr.Audio(type="filepath", label="Upload Piano Audio"),
|
|
|
|
outputs=[gr.Audio(label="MIDI transcription"), gr.File(label="MIDI file")],
|
|
title="MOZART - AI Piano Transcriber",
|
|
description="Gradio-based piano transcriber, using Bytedance's Piano Transcription AI model. Upload a piano audio file to transcribe it into a MIDI file. Open in a piano roll app like Synthesia to see the magic.",
|
|
)
|
|
|
|
|
|
iface.launch() |