Spaces:
Sleeping
Sleeping
File size: 2,951 Bytes
aa34f54 0a0256f aa34f54 0a0256f aa34f54 07c6778 aa34f54 c15ea16 07c6778 aa34f54 07c6778 aa34f54 | 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 | import os
import subprocess
import tempfile
import soundfile as sf
import numpy as np
import gradio as gr
from pyharp import ModelCard, build_endpoint
synthesis_generator = None
expression_generator = None
INSTRUMENTS = [
"violin", "viola", "cello", "double bass", "flute", "oboe",
"clarinet", "saxophone", "bassoon", "trumpet", "horn", "trombone", "tuba"
]
def get_model():
global synthesis_generator, expression_generator
if synthesis_generator is None:
print("Downloading MIDI-DDSP model weights...", flush=True)
subprocess.run(["midi_ddsp_download_model_weights"], check=True)
print("Loading MIDI-DDSP model...", flush=True)
from midi_ddsp import load_pretrained_model
synthesis_generator, expression_generator = load_pretrained_model()
print("Model loaded.", flush=True)
return synthesis_generator, expression_generator
model_card = ModelCard(
name="MIDI-DDSP",
description="Synthesize MIDI files into expressive audio using DDSP. Supports 13 orchestral instruments with realistic performance rendering.",
author="Yusong Wu, Ethan Manilow, Yi Deng, et al. (Google Magenta)",
tags=["midi", "synthesis", "ddsp", "performance-rendering", "orchestral"],
)
def process_fn(input_midi_path: str, instrument: str) -> str:
print(f"Synthesizing {instrument}...", flush=True)
syn_gen, exp_gen = get_model()
from midi_ddsp.utils.midi_synthesis_utils import synthesize_mono_midi
from midi_ddsp.data_handling.instrument_name_utils import INST_NAME_TO_ID_DICT
instrument_id = INST_NAME_TO_ID_DICT[instrument]
print(f"instrument_id: {instrument_id}", flush=True)
output_dir = tempfile.mkdtemp()
midi_audio, midi_control_params, midi_synth_params, conditioning_df = synthesize_mono_midi(
syn_gen, exp_gen, input_midi_path, instrument_id, output_dir=None
)
audio = midi_audio.numpy()
if audio.ndim > 1:
audio = audio.squeeze()
output_path = tempfile.mktemp(suffix=".wav")
sf.write(output_path, audio, 16000)
print("Done.", flush=True)
return output_path
with gr.Blocks() as demo:
input_components = [
gr.File(
type="filepath",
label="Input MIDI File",
file_types=[".mid", ".midi"],
).harp_required(True),
gr.Dropdown(
choices=INSTRUMENTS,
value="violin",
label="Instrument",
),
]
output_components = [
gr.Audio(
type="filepath",
label="Synthesized Audio",
).set_info("Expressive audio rendered from MIDI using DDSP."),
]
app = build_endpoint(
model_card=model_card,
input_components=input_components,
output_components=output_components,
process_fn=process_fn,
)
print("Launching Gradio...", flush=True)
demo.queue().launch(server_name="0.0.0.0", server_port=7860, show_error=True, pwa=True)
|