admin commited on
Commit
436c835
·
1 Parent(s): 8a8effc
Files changed (2) hide show
  1. app.py +8 -29
  2. requirements.txt +0 -1
app.py CHANGED
@@ -5,10 +5,8 @@ import torch
5
  import shutil
6
  import requests
7
  import gradio as gr
8
- import soundfile as sf
9
  from piano_transcription_inference import PianoTranscription, load_audio, sample_rate
10
  from modelscope import snapshot_download
11
- from tempfile import NamedTemporaryFile
12
  from urllib.parse import urlparse
13
  from convert import midi2xml, xml2abc, xml2mxl, xml2jpg
14
 
@@ -26,31 +24,12 @@ def clean_cache(cache_dir=CACHE_DIR):
26
  os.mkdir(cache_dir)
27
 
28
 
29
- def get_audio_file_type(file_path: str):
30
- try:
31
- with sf.SoundFile(file_path) as audio_file:
32
- return "." + str(audio_file.format).lower()
33
- except Exception as e:
34
- print(f"Error occurred: {e}")
35
- return None
36
-
37
-
38
  def download_audio(url: str, save_path: str):
39
- with NamedTemporaryFile(delete=False, suffix="_temp") as tmp_file:
40
- temp_file_path = tmp_file.name
41
- response = requests.get(url, stream=True)
42
- if response.status_code == 200:
43
- for chunk in response.iter_content(chunk_size=8192):
44
- tmp_file.write(chunk)
45
-
46
- else:
47
- print(f"Failed to download file: HTTP {response.status_code}")
48
- return ""
49
-
50
- ext = get_audio_file_type(temp_file_path)
51
- full_path = f"{save_path}{ext}"
52
- shutil.move(temp_file_path, full_path)
53
- return full_path
54
 
55
 
56
  def is_url(s: str):
@@ -125,7 +104,7 @@ def music163_song_info(id: str):
125
  def url_infer(song: str):
126
  clean_cache()
127
  song_name = ""
128
- download_path = f"{CACHE_DIR}/output"
129
  try:
130
  if is_url(song):
131
  if "163" in song and "?id=" in song:
@@ -135,7 +114,7 @@ def url_infer(song: str):
135
  if not free:
136
  raise AttributeError("Unable to parse VIP songs")
137
 
138
- download_path = download_audio(song, download_path)
139
 
140
  elif song.isdigit():
141
  song_id = song
@@ -144,7 +123,7 @@ def url_infer(song: str):
144
  if not free:
145
  raise AttributeError("Unable to parse VIP songs")
146
 
147
- download_path = download_audio(song, download_path)
148
 
149
  midi, title = audio2midi(download_path)
150
  if song_name:
 
5
  import shutil
6
  import requests
7
  import gradio as gr
 
8
  from piano_transcription_inference import PianoTranscription, load_audio, sample_rate
9
  from modelscope import snapshot_download
 
10
  from urllib.parse import urlparse
11
  from convert import midi2xml, xml2abc, xml2mxl, xml2jpg
12
 
 
24
  os.mkdir(cache_dir)
25
 
26
 
 
 
 
 
 
 
 
 
 
27
  def download_audio(url: str, save_path: str):
28
+ response = requests.get(url, stream=True)
29
+ response.raise_for_status()
30
+ with open(save_path, "wb") as file:
31
+ for chunk in response.iter_content(chunk_size=8192):
32
+ file.write(chunk)
 
 
 
 
 
 
 
 
 
 
33
 
34
 
35
  def is_url(s: str):
 
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:
 
114
  if not free:
115
  raise AttributeError("Unable to parse VIP songs")
116
 
117
+ download_audio(song, download_path)
118
 
119
  elif song.isdigit():
120
  song_id = song
 
123
  if not free:
124
  raise AttributeError("Unable to parse VIP songs")
125
 
126
+ download_audio(song, download_path)
127
 
128
  midi, title = audio2midi(download_path)
129
  if song_name:
requirements.txt CHANGED
@@ -1,7 +1,6 @@
1
  torch
2
  pymupdf
3
  music21
4
- soundfile
5
  piano_transcription_inference
6
  modelscope[framework]==1.18
7
  librosa==0.9.2
 
1
  torch
2
  pymupdf
3
  music21
 
4
  piano_transcription_inference
5
  modelscope[framework]==1.18
6
  librosa==0.9.2