MuGeminorum commited on
Commit
33ea879
1 Parent(s): 2588105

sync modelscope

Browse files
Files changed (3) hide show
  1. app.py +13 -16
  2. render.py → convert.py +32 -6
  3. utils.py +2 -0
app.py CHANGED
@@ -7,22 +7,12 @@ import argparse
7
  import gradio as gr
8
  from utils import *
9
  from config import *
10
- from render import *
11
- from music21 import converter
12
  from transformers import GPT2Config
13
  import warnings
14
  warnings.filterwarnings('ignore')
15
 
16
 
17
- def abc_to_midi(abc_content, output_midi_path):
18
- # 解析 ABC 格式的乐谱
19
- score = converter.parse(abc_content)
20
-
21
- # 将乐谱保存为 MIDI 文件
22
- score.write('midi', fp=output_midi_path)
23
- return output_midi_path
24
-
25
-
26
  def get_args(parser):
27
  parser.add_argument('-num_tunes', type=int, default=1,
28
  help='the number of independently computed returned tunes')
@@ -172,10 +162,12 @@ def generate_abc(args, region):
172
  create_dir('./tmp')
173
  timestamp = time.strftime("%a_%d_%b_%Y_%H_%M_%S", time.localtime())
174
  out_midi = abc_to_midi(tunes, f'./tmp/[{region}]{timestamp}.mid')
175
- pdf_file, jpg_file = midi2jpg(out_midi)
 
 
176
  wav_file = midi2wav(out_midi)
177
 
178
- return tunes, out_midi, pdf_file, jpg_file, wav_file
179
 
180
 
181
  def inference(region):
@@ -194,8 +186,8 @@ with gr.Blocks() as demo:
194
  choices=[
195
  'Mondstadt', 'Liyue', 'Inazuma', 'Sumeru', 'Fontaine'
196
  ],
197
- value='Liyue',
198
- label='Region'
199
  )
200
  gen_btn = gr.Button("Generate")
201
 
@@ -203,13 +195,18 @@ with gr.Blocks() as demo:
203
  wav_output = gr.Audio(label='Audio', type='filepath')
204
  dld_midi = gr.components.File(label="Download MIDI")
205
  pdf_score = gr.components.File(label="Download PDF score")
 
 
206
  abc_output = gr.TextArea(label='abc score')
207
  img_score = gr.Image(label='Staff', type='filepath')
208
 
209
  gen_btn.click(
210
  inference,
211
  inputs=region_opt,
212
- outputs=[abc_output, dld_midi, pdf_score, img_score, wav_output]
 
 
 
213
  )
214
 
215
  demo.launch(share=True)
 
7
  import gradio as gr
8
  from utils import *
9
  from config import *
10
+ from convert import *
 
11
  from transformers import GPT2Config
12
  import warnings
13
  warnings.filterwarnings('ignore')
14
 
15
 
 
 
 
 
 
 
 
 
 
16
  def get_args(parser):
17
  parser.add_argument('-num_tunes', type=int, default=1,
18
  help='the number of independently computed returned tunes')
 
162
  create_dir('./tmp')
163
  timestamp = time.strftime("%a_%d_%b_%Y_%H_%M_%S", time.localtime())
164
  out_midi = abc_to_midi(tunes, f'./tmp/[{region}]{timestamp}.mid')
165
+ out_xml = abc_to_musicxml(tunes, f'./tmp/[{region}]{timestamp}.musicxml')
166
+ out_mxl = musicxml_to_mxl(f'./tmp/[{region}]{timestamp}.musicxml')
167
+ pdf_file, jpg_file = mxl2jpg(out_mxl)
168
  wav_file = midi2wav(out_midi)
169
 
170
+ return tunes, out_midi, pdf_file, out_xml, out_mxl, jpg_file, wav_file
171
 
172
 
173
  def inference(region):
 
186
  choices=[
187
  'Mondstadt', 'Liyue', 'Inazuma', 'Sumeru', 'Fontaine'
188
  ],
189
+ value='Mondstadt',
190
+ label='Region genre'
191
  )
192
  gen_btn = gr.Button("Generate")
193
 
 
195
  wav_output = gr.Audio(label='Audio', type='filepath')
196
  dld_midi = gr.components.File(label="Download MIDI")
197
  pdf_score = gr.components.File(label="Download PDF score")
198
+ dld_xml = gr.components.File(label="Download MusicXML")
199
+ dld_mxl = gr.components.File(label="Download MXL")
200
  abc_output = gr.TextArea(label='abc score')
201
  img_score = gr.Image(label='Staff', type='filepath')
202
 
203
  gen_btn.click(
204
  inference,
205
  inputs=region_opt,
206
+ outputs=[
207
+ abc_output, dld_midi, pdf_score,
208
+ dld_xml, dld_mxl, img_score, wav_output
209
+ ]
210
  )
211
 
212
  demo.launch(share=True)
render.py → convert.py RENAMED
@@ -3,6 +3,7 @@ import sys
3
  import fitz
4
  import subprocess
5
  from PIL import Image
 
6
  from utils import download
7
 
8
  if sys.platform.startswith('linux'):
@@ -23,6 +24,26 @@ else:
23
  mscore = "D:/Program Files/MuseScore 3/bin/MuseScore3.exe"
24
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def midi2wav(mid_file: str):
27
  wav_file = mid_file.replace('.mid', '.wav')
28
  command = [mscore, "-o", wav_file, mid_file]
@@ -41,12 +62,17 @@ def pdf2img(pdf_path: str):
41
  # 将页面渲染为图像
42
  image = page.get_pixmap()
43
  # 将图像添加到列表
44
- images.append(Image.frombytes(
45
- "RGB", [image.width, image.height], image.samples)
 
 
 
 
46
  )
47
  # 竖向合并图像
48
  merged_image = Image.new(
49
- "RGB", (images[0].width, sum(image.height for image in images))
 
50
  )
51
  y_offset = 0
52
  for image in images:
@@ -59,9 +85,9 @@ def pdf2img(pdf_path: str):
59
  return output_path
60
 
61
 
62
- def midi2jpg(mid_file: str):
63
- pdf_score = mid_file.replace('.mid', '.pdf')
64
- command = [mscore, "-o", pdf_score, mid_file]
65
  result = subprocess.run(command)
66
  print(result)
67
  return pdf_score, pdf2img(pdf_score)
 
3
  import fitz
4
  import subprocess
5
  from PIL import Image
6
+ from music21 import converter
7
  from utils import download
8
 
9
  if sys.platform.startswith('linux'):
 
24
  mscore = "D:/Program Files/MuseScore 3/bin/MuseScore3.exe"
25
 
26
 
27
+ def abc_to_midi(abc_content, output_midi_path):
28
+ score = converter.parse(abc_content, format='abc')
29
+ score.write('midi', fp=output_midi_path)
30
+ return output_midi_path
31
+
32
+
33
+ def abc_to_musicxml(abc_content, output_xml_path):
34
+ score = converter.parse(abc_content, format='abc')
35
+ score.write('musicxml', fp=output_xml_path)
36
+ return output_xml_path
37
+
38
+
39
+ def musicxml_to_mxl(xml_path):
40
+ mxl_file = xml_path.replace('.musicxml', '.mxl')
41
+ command = [mscore, "-o", mxl_file, xml_path]
42
+ result = subprocess.run(command)
43
+ print(result)
44
+ return mxl_file
45
+
46
+
47
  def midi2wav(mid_file: str):
48
  wav_file = mid_file.replace('.mid', '.wav')
49
  command = [mscore, "-o", wav_file, mid_file]
 
62
  # 将页面渲染为图像
63
  image = page.get_pixmap()
64
  # 将图像添加到列表
65
+ images.append(
66
+ Image.frombytes(
67
+ "RGB",
68
+ [image.width, image.height],
69
+ image.samples
70
+ )
71
  )
72
  # 竖向合并图像
73
  merged_image = Image.new(
74
+ "RGB",
75
+ (images[0].width, sum(image.height for image in images))
76
  )
77
  y_offset = 0
78
  for image in images:
 
85
  return output_path
86
 
87
 
88
+ def mxl2jpg(mxl_file: str):
89
+ pdf_score = mxl_file.replace('.mxl', '.pdf')
90
+ command = [mscore, "-o", pdf_score, mxl_file]
91
  result = subprocess.run(command)
92
  print(result)
93
  return pdf_score, pdf2img(pdf_score)
utils.py CHANGED
@@ -21,8 +21,10 @@ S:2
21
  B:9
22
  E:4
23
  B:9
 
24
  L:1/8
25
  M:3/4
 
26
  K:D
27
  de |"D"'''
28
 
 
21
  B:9
22
  E:4
23
  B:9
24
+ T:{region} Fragment
25
  L:1/8
26
  M:3/4
27
+ C:Generated by AI
28
  K:D
29
  de |"D"'''
30