hf-lin commited on
Commit
2158a6f
1 Parent(s): fab4518

add app.py

Browse files
Files changed (2) hide show
  1. app.py +72 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import re
4
+ import subprocess
5
+ import time
6
+ from symusic import Score, Synthesizer, BuiltInSF3, dump_wav
7
+ import torchaudio
8
+ import torch
9
+ import music21
10
+
11
+ def convert_midi_to_svg(midi_file_path, output_svg_path):
12
+ # Load MIDI file
13
+ midi_stream = music21.converter.parse(midi_file_path)
14
+
15
+ # Convert MIDI to MusicXML
16
+ musicxml_string = midi_stream.write('musicxml')
17
+
18
+ # Load MusicXML string
19
+ score = music21.converter.parseData(musicxml_string)
20
+
21
+ # Convert MusicXML to SVG
22
+ svg_string = score.write('musicxml.png', fmt='musicxml', subformats=['svg'])
23
+
24
+ # Write SVG to file
25
+ with open(output_svg_path, 'wb') as f:
26
+ f.write(svg_string)
27
+ return output_svg_path
28
+
29
+
30
+ def parse_abc_notation(text="", conversation_id='1'):
31
+ os.makedirs(f"tmp/{conversation_id}", exist_ok=True)
32
+ ts = time.time()
33
+ abc_pattern = r'(X:\d+\n(?:[^\n]*\n)+)'
34
+ abc_notation = re.findall(abc_pattern, text+'\n')
35
+ print(f'extract abc block: {abc_notation}')
36
+ if abc_notation:
37
+ # Convert ABC to midi
38
+ s = Score.from_abc(abc_notation[0])
39
+ wav_file = f'tmp/{conversation_id}/{ts}.mp3'
40
+ audio = Synthesizer().render(s, stereo=True)
41
+ torchaudio.save(wav_file, torch.FloatTensor(audio), 44100)
42
+
43
+ # Convert abc notation to SVG
44
+ tmp_midi = f'tmp/{conversation_id}/{ts}.mid'
45
+ midi_file = s.dump_midi(tmp_midi)
46
+ svg_file = f'tmp/{conversation_id}/{ts}.svg'
47
+ convert_midi_to_svg(tmp_midi, svg_file)
48
+ return svg_file, wav_file
49
+ else:
50
+ return None, None
51
+
52
+ gradio_app = gr.Interface(
53
+ parse_abc_notation,
54
+ inputs=["text"],
55
+ outputs=[gr.Image(label="Processed Image"), gr.Audio(label="Result")],
56
+ title="Hot Dog? Or Not?",
57
+ )
58
+
59
+ def greet(name, intensity):
60
+ return "Hello, " + name + "!" * int(intensity)
61
+
62
+ demo = gr.Interface(
63
+ fn=greet,
64
+ inputs=["text", "slider"],
65
+ outputs=["text"],
66
+ )
67
+
68
+ demo.launch()
69
+
70
+
71
+ if __name__ == "__main__":
72
+ gradio_app.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio==4.19.2
2
+ music21==9.1.0
3
+ symusic==0.4.2
4
+ torch==2.2.1
5
+ torchaudio==2.2.1
6
+ transformers==4.32.0