pachet commited on
Commit
dd9364e
·
1 Parent(s): 6d6adf5

Update Dockerfile and application files

Browse files
Files changed (4) hide show
  1. Dockerfile +1 -0
  2. app.py +91 -29
  3. old_app.py +37 -0
  4. requirements.txt +2 -1
Dockerfile CHANGED
@@ -16,6 +16,7 @@ RUN pip install --upgrade pip
16
  RUN pip install verovio
17
  RUN python3 -c "import verovio, sys; sys.stderr.write('✅ Verovio version: ' + verovio.toolkit().getVersion() + '\n')"
18
  RUN pip install gradio
 
19
 
20
  # Copy app and MEI file
21
  WORKDIR /app
 
16
  RUN pip install verovio
17
  RUN python3 -c "import verovio, sys; sys.stderr.write('✅ Verovio version: ' + verovio.toolkit().getVersion() + '\n')"
18
  RUN pip install gradio
19
+ RUN pip install music21
20
 
21
  # Copy app and MEI file
22
  WORKDIR /app
app.py CHANGED
@@ -1,6 +1,9 @@
1
- import verovio
2
- import gradio as gr
3
  import os
 
 
 
 
 
4
 
5
  def get_verovio_resource_path():
6
  for path in verovio.__path__:
@@ -9,34 +12,93 @@ def get_verovio_resource_path():
9
  return candidate
10
  return None
11
 
12
- def render_mei(mei_path):
13
- tk = verovio.toolkit()
14
- # Set resource path explicitly
15
- resource_path = get_verovio_resource_path()
16
- print(f"resource path: {os.listdir(resource_path)}")
17
- print("[Debug] Using resource path:", resource_path)
18
- if resource_path:
19
- try:
20
- tk.setResourcePath(resource_path)
21
- except Exception as e:
22
- print("[Error] Failed to set resourcePath:", e)
23
- # tk.setOptions({
24
- # "font": "/app/data/Bravura", # Path to the font folder
25
- # })
26
- with open(mei_path, "r", encoding="utf-8") as f:
27
- mei_data = f.read()
28
- # tk.loadFile("path-to-mei-file")
29
- # return tk.renderToSVG(1)
 
 
 
 
 
 
 
 
 
 
 
 
30
 
 
 
 
 
 
31
  try:
32
- tk.loadData(mei_data)
33
- print("before rendering")
34
- res = tk.renderToSVG(1)
35
- print("after rendering")
36
- return res
 
 
37
  except Exception as e:
38
- return f"<pre>Rendering failed:\n{e}</pre>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- gr.Interface(fn=render_mei, inputs=gr.File(type="filepath"), outputs=gr.HTML()).launch(
41
- server_name="0.0.0.0", server_port=7860
42
- )
 
 
 
1
  import os
2
+ import gradio as gr
3
+ import verovio
4
+ import music21
5
+ from music21 import converter, musicxml
6
+
7
 
8
  def get_verovio_resource_path():
9
  for path in verovio.__path__:
 
12
  return candidate
13
  return None
14
 
15
+ # Initialize the Verovio toolkit with rendering options
16
+ tk = verovio.toolkit()
17
+ resource_path = get_verovio_resource_path()
18
+ print(f"resource path: {os.listdir(resource_path)}")
19
+ print("[Debug] Using resource path:", resource_path)
20
+ if resource_path:
21
+ try:
22
+ tk.setResourcePath(resource_path)
23
+ except Exception as e:
24
+ print("[Error] Failed to set resourcePath:", e)
25
+ tk.setOptions({
26
+ "scale": 40,
27
+ "adjustPageHeight": True,
28
+ "landscape": False,
29
+ })
30
+
31
+ # Initialize Verovio toolkit
32
+ tk = verovio.toolkit()
33
+ tk.setOptions({
34
+ "scale": 40,
35
+ "adjustPageHeight": True
36
+ })
37
+
38
+ def midi_to_musicxml_string(midi_path):
39
+ # Parse MIDI file into a music21 stream
40
+ score = converter.parse(midi_path)
41
+ # Export to MusicXML string
42
+ exporter = musicxml.m21ToXml.GeneralObjectExporter(score)
43
+ musicxml_str = exporter.parse().decode('utf-8') # parse() returns bytes
44
+ return musicxml_str
45
 
46
+ def xml_to_mei_string(xmlstring):
47
+ tk.loadData(xmlstring)
48
+ return tk.getMEI()
49
+
50
+ def midi_to_svg(midi_path):
51
  try:
52
+ # Convert MIDI to MEI using music21
53
+ musicxml = midi_to_musicxml_string(midi_path)
54
+ mei_str = xml_to_mei_string(musicxml)
55
+ # Load MEI and render SVG
56
+ tk.loadData(mei_str)
57
+ svg = tk.renderToSVG(1)
58
+ return svg
59
  except Exception as e:
60
+ return f"<p style='color:red'>Error: {e}</p>"
61
+
62
+ # Gradio UI
63
+ with gr.Blocks() as demo:
64
+ gr.Markdown("## 🎼 MIDI to Score Viewer (via MEI)")
65
+
66
+ midi_input = gr.File(label="Upload a MIDI file", file_types=[".mid", ".midi"])
67
+ render_btn = gr.Button("Convert & Render")
68
+ score_display = gr.HTML(label="Score Output")
69
+
70
+ render_btn.click(fn=midi_to_svg, inputs=midi_input, outputs=score_display)
71
+
72
+ import subprocess
73
+ import tempfile
74
+
75
+ def midi_to_mei_string(midi_path):
76
+ try:
77
+ with tempfile.NamedTemporaryFile(suffix=".mei", delete=False) as tmp:
78
+ mei_path = tmp.name
79
+
80
+ # Call Verovio CLI
81
+ subprocess.run(
82
+ ["verovio", "-f", "midi", midi_path, "-o", mei_path],
83
+ check=True
84
+ )
85
+
86
+ # Read the resulting MEI file
87
+ with open(mei_path, "r", encoding="utf-8") as f:
88
+ mei_str = f.read()
89
+
90
+ return mei_str
91
+
92
+ except subprocess.CalledProcessError as e:
93
+ return f"<p style='color:red'>Verovio CLI failed: {e}</p>"
94
+ except Exception as e:
95
+ return f"<p style='color:red'>Error: {e}</p>"
96
+
97
+
98
+ # Smart launch settings
99
+ if os.getenv("SPACE_ID"):
100
+ demo.launch()
101
+ else:
102
+ # demo.launch(share=False, inbrowser=True)
103
+ demo.launch(server_name="0.0.0.0", server_port=7860)
104
 
 
 
 
old_app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import verovio
2
+ import os
3
+
4
+ def get_verovio_resource_path():
5
+ for path in verovio.__path__:
6
+ candidate = os.path.join(path, "data")
7
+ if os.path.exists(candidate):
8
+ return candidate
9
+ return None
10
+
11
+ def render_mei(mei_path):
12
+ tk = verovio.toolkit()
13
+ # Set resource path explicitly
14
+ resource_path = get_verovio_resource_path()
15
+ print("[Debug] Using resource path:", resource_path)
16
+ if resource_path:
17
+ try:
18
+ tk.setResourcePath(resource_path)
19
+ except Exception as e:
20
+ print("[Error] Failed to set resourcePath:", e)
21
+ # tk.setOptions({
22
+ # "font": "/app/data/Bravura", # Path to the font folder
23
+ # })
24
+ with open(mei_path, "r", encoding="utf-8") as f:
25
+ mei_data = f.read()
26
+ # tk.loadFile("path-to-mei-file")
27
+ # return tk.renderToSVG(1)
28
+
29
+ try:
30
+ tk.loadData(mei_data)
31
+ return tk.renderToSVG(1)
32
+ except Exception as e:
33
+ return f"<pre>Rendering failed:\n{e}</pre>"
34
+
35
+
36
+ string = render_mei("output.mei")
37
+ print(string)
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- verovio
 
 
1
+ verovio
2
+ music21