File size: 8,925 Bytes
bd71b6f 718967a 5d3e0a8 718967a 77c722a 718967a bd71b6f 718967a bd71b6f 718967a bd71b6f 718967a 6c0dab8 718967a 0e6d052 718967a 0e6d052 718967a 0e6d052 718967a 8bdfa67 718967a 77c722a 0e6d052 718967a 0e6d052 718967a 0e6d052 2508606 0e6d052 707c61d 718967a 2508606 718967a 707c61d 718967a 2508606 718967a bbc9116 718967a aff06f3 bd71b6f 718967a 23de607 718967a 2ef01c1 718967a d3192fa 707c61d 718967a d3192fa 718967a 77c722a 718967a 77c722a 718967a 77c722a 718967a 2508606 707c61d 8bdfa67 718967a 77c722a 2508606 718967a |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# https://huggingface.co/spaces/asigalov61/LAKH-MIDI-Dataset-Search
import os
import time as reqtime
import datetime
from pytz import timezone
from sentence_transformers import SentenceTransformer
from sentence_transformers import util
import numpy as np
from datasets import load_dataset
import gradio as gr
import copy
import random
import pickle
import zlib
from midi_to_colab_audio import midi_to_colab_audio
import TMIDIX
import matplotlib.pyplot as plt
#==========================================================================================================
def find_midi(input_search_string):
print('=' * 70)
print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
start_time = reqtime.time()
print('-' * 70)
print('Req search str:', input_search_string)
print('-' * 70)
print('Searching...')
query_embedding = model.encode([input_search_string])
# Compute cosine similarity between query and each sentence in the corpus
similarities = util.cos_sim(query_embedding, corpus_embeddings)
top_ten_matches_idxs = np.argsort(-similarities)[0][:10].tolist()
# Find the index of the most similar sentence
closest_index = np.argmax(similarities)
closest_index_match_ratio = max(similarities[0].tolist())
best_corpus_match = mc_dataset['train'][closest_index.tolist()]
print('Done!')
print('=' * 70)
print('Match corpus index', closest_index)
print('Match corpus ratio', closest_index_match_ratio)
print('=' * 70)
print('Done!')
print('=' * 70)
LAKH_hash = best_corpus_match['location'].split('/')[-1].split('.mid')[0]
LAKH_caption = str(best_corpus_match['caption'])
zlib_file_name = all_MIDI_files_names[MIDI_files_names.index(LAKH_hash)][1]
print('Fetching MIDI score...')
with open(zlib_file_name, 'rb') as f:
compressed_data = f.read()
# Decompress the data
decompressed_data = zlib.decompress(compressed_data)
# Convert the bytes back to a list using pickle
scores_data = pickle.loads(decompressed_data)
fnames = [f[0] for f in scores_data]
fnameidx = fnames.index(LAKH_hash)
MIDI_score_metadata = scores_data[fnameidx][1]
MIDI_score_data = scores_data[fnameidx][2]
print('Rendering results...')
print('=' * 70)
print('MIDi Title:', LAKH_hash)
print('Sample INTs', MIDI_score_data[:12])
print('=' * 70)
if len(MIDI_score_data) != 0:
song = MIDI_score_data
song_f = []
time = 0
dur = 0
vel = 90
pitch = 0
channel = 0
patches = [-1] * 16
channels = [0] * 16
channels[9] = 1
for ss in song:
if 0 <= ss < 256:
time += ss * 16
if 256 <= ss < 512:
dur = (ss-256) * 16
if 512 <= ss <= 640:
patch = (ss-512)
if patch < 128:
if patch not in patches:
if 0 in channels:
cha = channels.index(0)
channels[cha] = 1
else:
cha = 15
patches[cha] = patch
channel = patches.index(patch)
else:
channel = patches.index(patch)
if patch == 128:
channel = 9
if 640 < ss < 768:
ptc = (ss-640)
if 768 < ss < 896:
vel = (ss - 768)
song_f.append(['note', time, dur, channel, ptc, vel, patch ])
patches = [0 if x==-1 else x for x in patches]
song_f = song_f[:3000]
print('=' * 70)
#===============================================================================
output_score, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(song_f)
detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(output_score,
output_signature = 'LAKH MIDI Dataset Search',
output_file_name = LAKH_hash,
track_name='Project Los Angeles',
list_of_MIDI_patches=patches
)
new_fn = LAKH_hash + '.mid'
audio = midi_to_colab_audio(new_fn,
soundfont_path=soundfont,
sample_rate=16000,
volume_scale=10,
output_for_gradio=True
)
print('Done!')
print('=' * 70)
#========================================================
output_midi_title = str(LAKH_hash)
output_midi_caption = str(best_corpus_match['caption'])
output_midi_summary = str(MIDI_score_metadata)
output_midi_caps = str(best_corpus_match)
output_midi = str(new_fn)
output_audio = (16000, audio)
output_plot = TMIDIX.plot_ms_SONG(output_score, plot_title=output_midi_title, return_plt=True)
print('Output MIDI file name:', output_midi)
print('Output MIDI caption string:', output_midi_caption)
print('Output MIDI title:', output_midi_title)
print('Output MIDI summary:', output_midi_summary)
print('Output MidiCaps information:', output_midi_caps)
print('=' * 70)
#========================================================
print('-' * 70)
print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
print('-' * 70)
print('Req execution time:', (reqtime.time() - start_time), 'sec')
return output_midi_title, output_midi_caption, output_midi_summary, output_midi_caps, output_midi, output_audio, output_plot
#==========================================================================================================
if __name__ == "__main__":
PDT = timezone('US/Pacific')
print('=' * 70)
print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
print('=' * 70)
soundfont = "SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2"
print('Loading MidiCaps dataset...')
mc_dataset = load_dataset("amaai-lab/MidiCaps")
print('=' * 70)
print('Loading files list...')
all_MIDI_files_names = TMIDIX.Tegridy_Any_Pickle_File_Reader('LAKH_all_files_names')
MIDI_files_names = [f[0] for f in all_MIDI_files_names]
print('=' * 70)
print('Loading MIDI corpus embeddings...')
corpus_embeddings = np.load('MIDI_corpus_embeddings_all-mpnet-base-v2.npz.gz.npz')['data']
print('Done!')
print('=' * 70)
print('Loading Sentence Transformer model...')
model = SentenceTransformer('all-mpnet-base-v2')
print('Done!')
print('=' * 70)
app = gr.Blocks()
with app:
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>LAKH MIDI Dataset Search</h1>")
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Search and explore LAKH MIDI dataset with MidiCaps dataset and sentence transformer</h1>")
gr.Markdown("![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.LAKH-MIDI-Dataset-Search&style=flat)\n\n"
"This is a demo for MidiCaps dataset\n\n"
"Check out [MidiCaps Dataset](https://huggingface.co/datasets/amaai-lab/MidiCaps) on Hugging Face!\n\n"
)
gr.Markdown("# Enter any desired song description\n\n")
input_search_string = gr.Textbox(label="Search string", value="Cheery pop song about love and happiness")
submit = gr.Button(value='Search')
gr.ClearButton(components=[input_search_string])
gr.Markdown("# Search results")
output_midi_title = gr.Textbox(label="Output MIDI title")
output_midi_caption = gr.Textbox(label="MIDI caption string")
output_midi_summary = gr.Textbox(label="Aggregated MIDI file text metadata")
output_midi_caps = gr.Textbox(label="MidiCaps dataset information")
output_audio = gr.Audio(label="Output MIDI audio", format="mp3", elem_id="midi_audio")
output_plot = gr.Plot(label="Output MIDI score plot")
output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
run_event = submit.click(find_midi, [input_search_string],
[output_midi_title, output_midi_caption, output_midi_summary, output_midi_caps, output_midi, output_audio, output_plot ])
app.launch() |