Spaces:
Running
Running
Match the PyHARP example
Browse filesCopies app.py from the midi_synthesizer example of PyHARP, which builds the synthesizer in the worker alone.
14 unmodified lines15161718192021222324252627282944 unmodified lines7475767778798081821 unmodified line848586878889904 unmodified lines9596979814 unmodified linesSAMPLE_RATE = 44100# Anything expensive to construct belongs out here, at module scope, so that it is# built once when the app starts instead of on every request. This soundfont is# downloaded on first use, and a real model would be loaded onto the GPU in the# same place. Doing either inside process_fn would repeat the cost for every user.synthesizer = Synthesizer(sf_path=BuiltInSF3.MuseScoreGeneral().path(download=True),sample_rate=SAMPLE_RATE,quality=4 # Default quality setting)# Metadata shown in HARP's model info panelmodel_card = ModelCard(44 unmodified linestype="filepath",label="Input MIDI",file_types=[".mid", ".midi"]).harp_required(True).set_info("The MIDI file to render, with each channel's program choosing its instrument."),]# Order must match the values returned by process_fn1 unmodified linegr.Audio(type="filepath",format="wav",label="Rendered Audio").set_info("The synthesized audio."),]4 unmodified linesprocess_fn=process_fn,)demo.queue().launch(show_error=True, pwa=True)14 unmodified lines15161718192021222324252627282930313244 unmodified lines777879808182831 unmodified line858687888990914 unmodified lines9697989914 unmodified linesSAMPLE_RATE = 44100# Anything expensive to construct belongs outside process_fn, so that it is built# once when the app starts instead of on every request. Only the worker that runs# process_fn needs it, and the worker imports this file under a name other than# __main__, so it is built there alone. The server would otherwise hold a second# copy, and both processes would download the soundfont at once. A real model# would be loaded onto the GPU in the same place.if __name__ != "__main__":synthesizer = Synthesizer(sf_path=BuiltInSF3.MuseScoreGeneral().path(download=True),sample_rate=SAMPLE_RATE,quality=4 # Default quality setting)# Metadata shown in HARP's model info panelmodel_card = ModelCard(44 unmodified linestype="filepath",label="Input MIDI",file_types=[".mid", ".midi"]).harp_required(True).set_info("The MIDI to render."),]# Order must match the values returned by process_fn1 unmodified linegr.Audio(type="filepath",format="wav",label="Synthesized Audio").set_info("The synthesized audio."),]4 unmodified linesprocess_fn=process_fn,)demo.queue().launch(share=True, show_error=True, pwa=True)
app.py+15-14