agent404's picture
Update app.py
ec2c213 verified
raw
history blame
1.55 kB
import gradio as gr
import os
import re
import subprocess
import time
from symusic import Score, Synthesizer
import torchaudio
import torch
# for rendering abc notation
os.environ['QT_QPA_PLATFORM']='offscreen'
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/", 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'{ts}.mp3'
audio = Synthesizer().render(s, stereo=True)
torchaudio.save(wav_file, torch.FloatTensor(audio), 44100)
# Convert abc notation to SVG
tmp_midi = f'{ts}.mid'
s.dump_midi(tmp_midi)
svg_file = f'{ts}.svg'
subprocess.run(["./MuseScore-4.1.1.232071203-x86_64.AppImage", "-f", "-o", svg_file, tmp_midi])
return svg_file, wav_file
else:
return None, None
if __name__ == "__main__":
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()