File size: 1,958 Bytes
a931541
 
ff647b0
 
1de73a8
789718b
ff647b0
84a8d96
2dcf4b6
e49932c
 
84a8d96
e49932c
 
 
3f41985
a973402
3f41985
a973402
f19aeec
e49932c
d02a40d
84a8d96
e49932c
776a79b
c90d42e
 
d02a40d
c90d42e
 
 
 
e49932c
 
 
50efdde
c90d42e
d02a40d
e49932c
 
 
c90d42e
 
e49932c
a1f6dfc
d02a40d
 
a1f6dfc
a9764fc
249bbc0
 
e49932c
 
a099098
e49932c
 
d02a40d
e49932c
 
 
 
 
 
 
 
 
 
 
 
 
 
c7cb9a5
 
 
a099098
 
249bbc0
c7cb9a5
a931541
 
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
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 process_midi(MIDI_File):
    name = Path(MIDI_File.name)
    
    # create the model input object
    item = MusicItem.from_file(name, data.vocab)

    # full is the prediction appended to the input
    pred, full = learner.predict(item, n_words=100)
   
    # 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=["file"], 
    outputs="audio",
    examples=['C major scale.midi']
)

iface.launch()