import gradio as gr import os import re import subprocess import time from symusic import Score, Synthesizer, BuiltInSF3, dump_wav import torchaudio import torch import music21 def convert_midi_to_svg(midi_file_path, output_svg_path): # Load MIDI file midi_stream = music21.converter.parse(midi_file_path) # Convert MIDI to MusicXML musicxml_string = midi_stream.write('musicxml') # Load MusicXML string score = music21.converter.parseData(musicxml_string) # Convert MusicXML to SVG svg_string = score.write('musicxml.png', fmt='musicxml', subformats=['svg']) # Write SVG to file with open(output_svg_path, 'wb') as f: f.write(svg_string) return output_svg_path def parse_abc_notation(text="", conversation_id='1'): os.makedirs(f"tmp/{conversation_id}", exist_ok=True) ts = time.time() abc_pattern = r'(X:\d+\n(?:[^\n]*\n)+)' abc_notation = re.findall(abc_pattern, text+'\n') print(f'extract abc block: {abc_notation}') if abc_notation: # Convert ABC to midi s = Score.from_abc(abc_notation[0]) wav_file = f'tmp/{conversation_id}/{ts}.mp3' audio = Synthesizer().render(s, stereo=True) torchaudio.save(wav_file, torch.FloatTensor(audio), 44100) # Convert abc notation to SVG tmp_midi = f'tmp/{conversation_id}/{ts}.mid' midi_file = s.dump_midi(tmp_midi) svg_file = f'tmp/{conversation_id}/{ts}.svg' convert_midi_to_svg(tmp_midi, svg_file) return svg_file, wav_file else: return None, None gradio_app = gr.Interface( parse_abc_notation, inputs=["text"], outputs=[gr.Image(label="Processed Image"), gr.Audio(label="Result")], title="Hot Dog? Or Not?", ) def greet(name, intensity): return "Hello, " + name + "!" * int(intensity) demo = gr.Interface( fn=greet, inputs=["text", "slider"], outputs=["text"], ) demo.launch() if __name__ == "__main__": gradio_app.launch()