asigalov61's picture
Update app.py
c1fc4e4 verified
raw
history blame
7.21 kB
# =================================================================================================
# https://huggingface.co/spaces/asigalov61/Chords-Progressions-Generator
# =================================================================================================
import os
import time as reqtime
import datetime
from pytz import timezone
import gradio as gr
import numpy as np
import os
import random
from collections import Counter
import TMIDIX
from midi_to_colab_audio import midi_to_colab_audio
# =================================================================================================
def Generate_Chords_Progression(input_midi, input_sampling_resolution):
print('=' * 70)
print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
start_time = reqtime.time()
print('=' * 70)
fn = os.path.basename(input_midi.name)
fn1 = fn.split('.')[0]
print('=' * 70)
print('Ultimate MIDI Classifier')
print('=' * 70)
print('Input MIDI file name:', fn)
print('=' * 70)
print('Done!')
print('=' * 70)
classification_summary_string = '=' * 70
classification_summary_string += '\n'
samples_overlap = 340 - chunk_size // input_sampling_resolution // 3
print('Composition has', notes_counter, 'notes')
print('=' * 70)
print('Composition was split into' , len(input_data), 'samples', 'of 340 notes each with', samples_overlap, 'notes overlap')
print('=' * 70)
print('Number of notes in all composition samples:', len(input_data) * 340)
print('=' * 70)
classification_summary_string += 'Composition has ' + str(notes_counter) + ' notes\n'
classification_summary_string += '=' * 70
classification_summary_string += '\n'
classification_summary_string += 'Composition was split into ' + 'samples of 340 notes each with ' + str(samples_overlap) + ' notes overlap\n'
classification_summary_string += 'Number of notes in all composition samples: ' + str(len(input_data) * 340) + '\n'
classification_summary_string += '=' * 70
classification_summary_string += '\n'
#==================================================================
print('=' * 70)
print('Ultimate MIDI Classifier')
print('=' * 70)
print('=' * 70)
print('Done!')
print('=' * 70)
result_toks = [final_result[0]-512, final_result[1]-512]
mc_song_artist = song_artist_tokens_to_song_artist(result_toks)
gidx = genre_labels_fnames.index(mc_song_artist)
mc_genre = genre_labels[gidx][1]
print('Most common classification genre label:', mc_genre)
print('Most common classification song-artist label:', mc_song_artist)
print('Most common song-artist classification label ratio:' , results.count(final_result) / len(results))
print('=' * 70)
classification_summary_string += 'Most common classification genre label: ' + str(mc_genre) + '\n'
classification_summary_string += 'Most common classification song-artist label: ' + str(mc_song_artist) + '\n'
classification_summary_string += 'Most common song-artist classification label ratio: '+ str(results.count(final_result) / len(results)) + '\n'
classification_summary_string += '=' * 70
classification_summary_string += '\n'
print('All classification labels summary:')
print('=' * 70)
all_artists_labels = []
for i, res in enumerate(results):
result_toks = [res[0]-512, res[1]-512]
song_artist = song_artist_tokens_to_song_artist(result_toks)
gidx = genre_labels_fnames.index(song_artist)
genre = genre_labels[gidx][1]
print('Notes', i*(340-samples_overlap), '-', (i*(340-samples_overlap))+340, '===', genre, '---', song_artist)
classification_summary_string += 'Notes ' + str(i*samples_overlap) + ' - ' + str((i*samples_overlap)+340) + ' === ' + str(genre) + ' --- ' + str(song_artist) + '\n'
artist_label = str_strip_artist(song_artist.split(' --- ')[1])
all_artists_labels.append(artist_label)
classification_summary_string += '=' * 70
classification_summary_string += '\n'
print('=' * 70)
mode_artist_label = mode(all_artists_labels)
mode_artist_label_count = all_artists_labels.count(mode_artist_label)
print('Aggregated artist classification label:', mode_artist_label)
print('Aggregated artist classification label ratio:', mode_artist_label_count / len(all_artists_labels))
classification_summary_string += 'Aggregated artist classification label: ' + str(mode_artist_label) + '\n'
classification_summary_string += 'Aggregated artist classification label ratio: ' + str(mode_artist_label_count / len(all_artists_labels)) + '\n'
classification_summary_string += '=' * 70
classification_summary_string += '\n'
print('=' * 70)
print('Done!')
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 classification_summary_string
# =================================================================================================
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)
print('=' * 70)
print('Loading Ultimate MIDI Classifier labels...')
print('=' * 70)
good_chords_chunks = TMIDIX.Tegridy_Any_Pickle_File_Reader('pitches_chords_progressions_5_3_15')
print('=' * 70)
print('Done!')
print('=' * 70)
app = gr.Blocks()
with app:
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Chords Progressions Generator</h1>")
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Generate unique chords progressions</h1>")
gr.Markdown(
"![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.Chords-Progressions-Generator&style=flat)\n\n"
"This is a demo for Tegridy MIDI Dataset\n\n"
"Check out [Tegridy MIDI Dataset](https://github.com/asigalov61/Tegridy-MIDI-Dataset) on GitHub!\n\n"
"[Open In Colab]"
"(https://colab.research.google.com/github/asigalov61/Tegridy-MIDI-Dataset/blob/master/Chords-Progressions/Pitches_Chords_Progressions_Generator.ipynb)"
" for all options, faster execution and endless generation"
)
gr.Markdown("## Select generation options")
input_sampling_resolution = gr.Slider(1, 5, value=2, step=1, label="Classification sampling resolution")
run_btn = gr.Button("classify", variant="primary")
gr.Markdown("## Generation results")
output_midi_cls_summary = gr.Textbox(label="MIDI classification results")
run_event = run_btn.click(ClassifyMIDI, [input_midi, input_sampling_resolution],
[output_midi_cls_summary])
app.queue().launch()