Spaces:
Sleeping
Sleeping
File size: 1,718 Bytes
d8d9ed1 635465d 0bfbeab d8d9ed1 |
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 |
import os
import re
import time
import subprocess
import gradio as gr
os.environ['QT_QPA_PLATFORM']='offscreen'
# os.environ['MPLCONFIGDIR'] = os.getcwd() + "/configs/"
# for rendering abc notation
default_abc = """
X:1
L:1/8
M:2/4
K:G
|:"G" G>A Bc | dB dB |"C" ce ce |"D7" dB A2 |"G" G>A Bc | dB dB |"Am" cA"D7" FA |"G" AG G2 ::
"Em" g2"D" f>e | de Bd |"C" ce ce |"D7" dB A2 |"G" g2"D" f>e | de Bd |"Am" cA"D7" FA |"G" AG G2 :|
"""
def parse_abc_notation(text='', conversation_id='debug'):
os.makedirs(f"tmp/{conversation_id}", exist_ok=True)
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:
ts = time.time()
# Write the ABC text to a temporary file
tmp_abc = f"tmp/{conversation_id}/{ts}.abc"
with open(tmp_abc, "w") as abc_file:
abc_file.write(abc_notation[0])
# Convert abc notation to midi
tmp_midi = f'tmp/{conversation_id}/{ts}.mid'
subprocess.run(["abc2midi", str(tmp_abc), "-o", tmp_midi])
# Convert abc notation to SVG
svg_file = f'tmp/{conversation_id}/{ts}.svg'
audio_file = f'tmp/{conversation_id}/{ts}.mp3'
subprocess.run(["musescore", "-o", svg_file, tmp_midi], capture_output=True, text=True)
subprocess.run(["musescore","-o", audio_file, tmp_midi])
return svg_file.replace(".svg", "-1.svg"), audio_file
else:
return None, None
gradio_app = gr.Interface(
parse_abc_notation,
inputs=["text"],
outputs=[gr.Image(label="svg"), gr.Audio(label="audio")],
title="ABC notation parse",
examples=[default_abc]
)
gradio_app.launch() |