admin commited on
Commit
486d10a
·
1 Parent(s): 436c835

seperate mode 1 2 cache dir

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -10,14 +10,13 @@ from modelscope import snapshot_download
10
  from urllib.parse import urlparse
11
  from convert import midi2xml, xml2abc, xml2mxl, xml2jpg
12
 
13
- CACHE_DIR = "./flagged"
14
  WEIGHTS_PATH = (
15
  snapshot_download("Genius-Society/piano_trans", cache_dir="./__pycache__")
16
  + "/CRNN_note_F1=0.9677_pedal_F1=0.9186.pth"
17
  )
18
 
19
 
20
- def clean_cache(cache_dir=CACHE_DIR):
21
  if os.path.exists(cache_dir):
22
  shutil.rmtree(cache_dir)
23
 
@@ -41,7 +40,7 @@ def is_url(s: str):
41
  return False
42
 
43
 
44
- def audio2midi(audio_path: str):
45
  # Load audio
46
  audio, _ = load_audio(audio_path, sr=sample_rate, mono=True)
47
  # Transcriptor
@@ -50,16 +49,16 @@ def audio2midi(audio_path: str):
50
  checkpoint_path=WEIGHTS_PATH,
51
  )
52
  # device: 'cuda' | 'cpu' Transcribe and write out to MIDI file
53
- midi_path = f"{CACHE_DIR}/output.mid"
54
  # midi_path = audio_path.replace(audio_path.split(".")[-1], "mid")
55
  transcriptor.transcribe(audio, midi_path)
56
  return midi_path, os.path.basename(audio_path).split(".")[-2].capitalize()
57
 
58
 
59
- def upl_infer(audio_path: str):
60
- clean_cache()
61
  try:
62
- midi, title = audio2midi(audio_path)
63
  xml = midi2xml(midi, title)
64
  abc = xml2abc(xml)
65
  mxl = xml2mxl(xml)
@@ -101,10 +100,10 @@ def music163_song_info(id: str):
101
  return song_name, free
102
 
103
 
104
- def url_infer(song: str):
105
- clean_cache()
106
  song_name = ""
107
- download_path = f"{CACHE_DIR}/output.mp3"
 
108
  try:
109
  if is_url(song):
110
  if "163" in song and "?id=" in song:
@@ -125,7 +124,7 @@ def url_infer(song: str):
125
 
126
  download_audio(song, download_path)
127
 
128
- midi, title = audio2midi(download_path)
129
  if song_name:
130
  title = song_name
131
 
 
10
  from urllib.parse import urlparse
11
  from convert import midi2xml, xml2abc, xml2mxl, xml2jpg
12
 
 
13
  WEIGHTS_PATH = (
14
  snapshot_download("Genius-Society/piano_trans", cache_dir="./__pycache__")
15
  + "/CRNN_note_F1=0.9677_pedal_F1=0.9186.pth"
16
  )
17
 
18
 
19
+ def clean_cache(cache_dir):
20
  if os.path.exists(cache_dir):
21
  shutil.rmtree(cache_dir)
22
 
 
40
  return False
41
 
42
 
43
+ def audio2midi(audio_path: str, cache_dir: str):
44
  # Load audio
45
  audio, _ = load_audio(audio_path, sr=sample_rate, mono=True)
46
  # Transcriptor
 
49
  checkpoint_path=WEIGHTS_PATH,
50
  )
51
  # device: 'cuda' | 'cpu' Transcribe and write out to MIDI file
52
+ midi_path = f"{cache_dir}/output.mid"
53
  # midi_path = audio_path.replace(audio_path.split(".")[-1], "mid")
54
  transcriptor.transcribe(audio, midi_path)
55
  return midi_path, os.path.basename(audio_path).split(".")[-2].capitalize()
56
 
57
 
58
+ def upl_infer(audio_path: str, cache_dir="./__pycache__/mode1"):
59
+ clean_cache(cache_dir)
60
  try:
61
+ midi, title = audio2midi(audio_path, cache_dir)
62
  xml = midi2xml(midi, title)
63
  abc = xml2abc(xml)
64
  mxl = xml2mxl(xml)
 
100
  return song_name, free
101
 
102
 
103
+ def url_infer(song: str, cache_dir="./__pycache__/mode2"):
 
104
  song_name = ""
105
+ clean_cache(cache_dir)
106
+ download_path = f"{cache_dir}/output.mp3"
107
  try:
108
  if is_url(song):
109
  if "163" in song and "?id=" in song:
 
124
 
125
  download_audio(song, download_path)
126
 
127
+ midi, title = audio2midi(download_path, cache_dir)
128
  if song_name:
129
  title = song_name
130