import gradio as gr from musicautobot.utils.setup_musescore import play_wav from music21.midi.translate import midiFileToStream from pathlib import Path from midi2audio import FluidSynth # from musicautobot.numpy_encode import * from musicautobot.config import default_config from musicautobot.music_transformer import * from musicautobot.utils.midifile import * # from musicautobot.utils.file_processing import process_all import pickle import subprocess import os print(os.getcwd()) # Load the stored data. This is needed to generate the vocab. print('Loading data to build vocabulary.') data_dir = Path('.') data = load_data(data_dir, 'data.pkl') from huggingface_hub import hf_hub_download print('Downloading model.') model_cache_path = hf_hub_download(repo_id="psistolar/musicautobot-fine1", filename="model.pth") # Default config options config = default_config() config['encode_position'] = True print("Building model.") # Load our fine-tuned model learner = music_model_learner( data, config=config.copy(), pretrained_path=model_cache_path ) print("Ready to use.") def sonify_text(text): name = Path('C Major Scale.midi') item = MusicItem.from_file(name, data.vocab) return item def process_midi(MIDI_File, Text_to_Sonify, Randomness, Amount_of_Music_to_Add): if MIDI_File is not None: name = Path(MIDI_File.name) else: name = Path('C Major Scale.midi') sonification = False if MIDI_File is None and Text_to_Sonify is not None: sonification = True # create the model input object if sonification: item = sonify_text(Text_to_Sonify) else: item = MusicItem.from_file(name, data.vocab) # full is the prediction appended to the input temp = Randomness / 100 pred, full = learner.predict( item, n_words=Amount_of_Music_to_Add, temperatures=(temp, temp) ) # convert to stream and then MIDI file stream = full.to_stream() out = music21.midi.translate.streamToMidiFile(stream) # save MIDI file out.open('result.midi', 'wb') out.write() out.close() # use fluidsynth to convert MIDI to WAV so the user can hear the output sound_font = "/usr/share/sounds/sf2/FluidR3_GM.sf2" FluidSynth(sound_font).midi_to_audio('result.midi', 'result.wav') return 'result.wav' iface = gr.Interface( fn=process_midi, inputs=[ gr.inputs.File(optional=True, label="Upload your own MIDI file here."), "text", gr.inputs.Slider(0, 250, default=100, step=50), gr.inputs.Radio([100, 200, 500], type="value", default=100) ], outputs="audio", # examples=['C major scale.midi'] ) iface.launch()