import gradio as gr import torch import requests import music21 import subprocess from torchvision import transforms from PIL import Image DEFAULT_FILE_NAME = 'sheet_music' # converting everything into the key of C major or A minor def convert_to_key(): # major conversions majors = dict( [("A-", 4), ("A", 3), ("B-", 2), ("B", 1), ("C", 0), ("D-", -1), ("D", -2), ("E-", -3), ("E", -4), ("F", -5), ("G-", 6), ("G", 5)]) minors = dict( [("A-", 1), ("A", 0), ("B-", -1), ("B", -2), ("C", -3), ("D-", -4), ("D", -5), ("E-", 6), ("E", 5), ("F", 4), ("G-", 3), ("G", 2)]) score = music21.converter.parse(f'{DEFAULT_FILE_NAME}.musicxml') key = score.analyze('key') file_name = 'C_file' if key.mode == "major": halfSteps = majors[key.tonic.name] file_name = 'Am_file' elif key.mode == "minor": halfSteps = minors[key.tonic.name] newscore = score.transpose(halfSteps) # key = newscore.analyze('key') # print(key.tonic.name, key.mode) newscore.write('musicxml', f'{file_name}.musicxml') # newscore.write('musicxml.pdf', fp="file.pdf") return f'{file_name}.musicxml', f'{file_name}_teaser.png' def run_oemer(): process = subprocess.Popen(['oemer', f'{DEFAULT_FILE_NAME}.png']) process.wait() def predict(inp): img = Image.fromarray(inp) img.save(f'{DEFAULT_FILE_NAME}.png') run_oemer() return convert_to_key() def run(): outputs = [gr.File(label='Download musicxml', type='file'), gr.File(label='Download detection image', type='file')] demo = gr.Interface( fn=predict, inputs='image', outputs=outputs, allow_flagging='never', ) demo.launch(server_name="0.0.0.0", server_port=7860) if __name__ == "__main__": run()