dx2102 commited on
Commit
219120c
·
verified ·
1 Parent(s): 82fa5ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -23,7 +23,7 @@ pipe = transformers.pipeline(
23
  )
24
  print('Done')
25
 
26
- default_prefix = '''pitch duration wait
27
 
28
  71 1310 0
29
  48 330 350
@@ -47,35 +47,50 @@ default_prefix = '''pitch duration wait
47
  default_prefix_len = default_prefix.count('\n') - 2
48
 
49
 
 
50
  def postprocess(txt, path):
51
- # saves the text representation to a midi file
52
  txt = txt.split('\n\n')[-1]
53
 
54
- notes = []
 
 
55
  now = 0
56
  # we need to ignore the invalid output by the model
57
  try:
58
  for line in txt.split('\n'):
59
- pitch, duration, wait = [int(x) for x in line.split()]
 
 
 
 
 
 
 
60
  # Eg. Note(time=7.47, duration=5.25, pitch=43, velocity=64, ttype='Quarter')
61
- notes.append(symusic.core.NoteSecond(
62
  time=now/1000,
63
  duration=duration/1000,
64
  pitch=int(pitch),
65
- velocity=80,
66
  ))
67
  now += wait
68
  except Exception as e:
69
- print('Ignored error:', e)
 
 
70
 
71
  try:
72
- track = symusic.core.TrackSecond()
73
- track.notes = symusic.core.NoteSecondList(notes)
74
  score = symusic.Score(ttype='Second')
75
- score.tracks.append(track)
 
76
  score.dump_midi(path)
77
  except Exception as e:
78
- print('Ignored error:', e)
 
 
79
 
80
  with gr.Blocks() as demo:
81
  chatbot_box = gr.Chatbot(type="messages", render_markdown=False, sanitize_html=False)
 
23
  )
24
  print('Done')
25
 
26
+ default_prefix = '''pitch duration wait velocity instrument
27
 
28
  71 1310 0
29
  48 330 350
 
47
  default_prefix_len = default_prefix.count('\n') - 2
48
 
49
 
50
+
51
  def postprocess(txt, path):
52
+ # assert txt.startswith(prompt)
53
  txt = txt.split('\n\n')[-1]
54
 
55
+ # track = symusic.core.TrackSecond()
56
+ tracks = {}
57
+
58
  now = 0
59
  # we need to ignore the invalid output by the model
60
  try:
61
  for line in txt.split('\n'):
62
+ pitch, duration, wait, velocity, instrument = line.split()
63
+ pitch, duration, wait, velocity = [int(x) for x in [pitch, duration, wait, velocity]]
64
+ if instrument not in tracks:
65
+ tracks[instrument] = symusic.core.TrackSecond()
66
+ if instrument != 'drum':
67
+ tracks[instrument].program = int(instrument)
68
+ else:
69
+ tracks[instrument].is_drum = True
70
  # Eg. Note(time=7.47, duration=5.25, pitch=43, velocity=64, ttype='Quarter')
71
+ tracks[instrument].notes.append(symusic.core.NoteSecond(
72
  time=now/1000,
73
  duration=duration/1000,
74
  pitch=int(pitch),
75
+ velocity=int(velocity * 4),
76
  ))
77
  now += wait
78
  except Exception as e:
79
+ print('Postprocess: Ignored error:', e)
80
+
81
+ print(f'Postprocess: Got {sum(len(track.notes) for track in tracks.values())} notes')
82
 
83
  try:
84
+ # track = symusic.core.TrackSecond()
85
+ # track.notes = symusic.core.NoteSecondList(notes)
86
  score = symusic.Score(ttype='Second')
87
+ # score.tracks.append(track)
88
+ score.tracks.extend(tracks.values())
89
  score.dump_midi(path)
90
  except Exception as e:
91
+ print('Postprocess: Ignored postprocessing error:', e)
92
+
93
+
94
 
95
  with gr.Blocks() as demo:
96
  chatbot_box = gr.Chatbot(type="messages", render_markdown=False, sanitize_html=False)