def generate_single_midi(): file_obj = request.files['midi_object'] script_dir = os.path.dirname(__file__) # Save input as input.mid in data directory rel_path = "data/input.mid" abs_path = os.path.join(script_dir, rel_path) data = file_obj.read() with open(abs_path, 'wb') as out_file: out_file.write(data) # Get notes from midi file # directory only consists of one midi file (simply to reuse code) notes = Tools.get_notes("data/*.mid") print("length of notes dictionary: " + str(len(notes))) # Get the number of pitch names n_vocab = len(set(notes)) # Load dictionaries note_to_int = Tools.load_obj(f'{data_set}_note2int') int_to_note = Tools.load_obj(f'{data_set}_int2note') # convert notes into numerical input network_input = Tools.prepare_sequences_single(notes, note_to_int, n_vocab) network_input = network_input.tolist() # Use the model to generate a new midi model = torch.load(f'lstm_pretrained_{data_set}.pt') model.eval() prediction_output = Tools.generate_notes_single(model, int_to_note, network_input, len(set(notes))) Tools.create_midi(prediction_output, f'{data_set}_midi_g') bucket = s3.Bucket(bucket_name) bucket.upload_file(f'{data_set}_midi_g.mid', f'{data_set}_midi_g.mid') print("New midi file has been generated and uploaded to database") return f'/getfile/{data_set}_midi_g.mid'
import sys import torch from tools import Tools """ Generate the music from a random midi in training set""" # Get notes from midi files dataset_name = sys.argv[1] notes = Tools.load_obj(f'{dataset_name}_notes') # Get the number of pitch names n_vocab = len(set(notes)) # Convert notes into numerical input network_input, network_output = Tools.prepare_sequences(notes, n_vocab) network_input = network_input.tolist() # Use the model to generate a midi model = torch.load(f'lstm_pretrained_{dataset_name}.pt') model.eval() prediction_output = Tools.generate_notes(model, notes, network_input, len(set(notes))) Tools.create_midi(prediction_output, f'{dataset_name}_midi_g')