Spaces:
Running
Running
admin
commited on
Commit
·
c7d8a86
1
Parent(s):
ff4521e
recover
Browse files
app.py
CHANGED
@@ -28,57 +28,52 @@ def clean_cache(cache_dir=CACHE_DIR):
|
|
28 |
|
29 |
def get_audio_file_type(file_path: str):
|
30 |
try:
|
31 |
-
# 获取媒体信息
|
32 |
info = mediainfo(file_path)
|
33 |
-
# 返回文件格式
|
34 |
return "." + info["format_name"]
|
35 |
|
36 |
except Exception as e:
|
37 |
-
print(f"
|
38 |
return None
|
39 |
|
40 |
|
41 |
def download_audio(url: str, save_path: str):
|
42 |
with NamedTemporaryFile(delete=False, suffix="_temp") as tmp_file:
|
43 |
temp_file_path = tmp_file.name
|
44 |
-
# 发送HTTP GET请求并下载内容
|
45 |
response = requests.get(url, stream=True)
|
46 |
-
# 检查请求是否成功
|
47 |
if response.status_code == 200:
|
48 |
-
# 将音频内容写入临时文件
|
49 |
for chunk in response.iter_content(chunk_size=8192):
|
50 |
tmp_file.write(chunk)
|
51 |
|
52 |
else:
|
53 |
-
print(f"
|
54 |
return ""
|
55 |
|
56 |
ext = get_audio_file_type(temp_file_path)
|
57 |
full_path = f"{save_path}{ext}"
|
58 |
-
# 重命名临时文件以包含正确的扩展名
|
59 |
shutil.move(temp_file_path, full_path)
|
60 |
return full_path
|
61 |
|
62 |
|
63 |
def is_url(s: str):
|
64 |
try:
|
65 |
-
# 解析字符串
|
66 |
result = urlparse(s)
|
67 |
-
# 检查scheme(如http, https)和netloc(域名)
|
68 |
return all([result.scheme, result.netloc])
|
69 |
|
70 |
except:
|
71 |
-
# 如果解析过程中发生异常,则返回False
|
72 |
return False
|
73 |
|
74 |
|
75 |
def audio2midi(audio_path: str):
|
|
|
76 |
audio, _ = load_audio(audio_path, sr=sample_rate, mono=True)
|
|
|
77 |
transcriptor = PianoTranscription(
|
78 |
device="cuda" if torch.cuda.is_available() else "cpu",
|
79 |
checkpoint_path=WEIGHTS_PATH,
|
80 |
)
|
|
|
81 |
midi_path = f"{CACHE_DIR}/output.mid"
|
|
|
82 |
transcriptor.transcribe(audio, midi_path)
|
83 |
return midi_path, os.path.basename(audio_path).split(".")[-2].capitalize()
|
84 |
|
@@ -110,11 +105,9 @@ def music163_song_info(id: str):
|
|
110 |
detail_api = "https://music.163.com/api/v3/song/detail"
|
111 |
parm_dict = {"id": id, "c": str([{"id": id}]), "csrf_token": ""}
|
112 |
free = False
|
113 |
-
song_name = "
|
114 |
response = requests.get(detail_api, params=parm_dict)
|
115 |
-
# 检查请求是否成功
|
116 |
if response.status_code == 200:
|
117 |
-
# 处理成功响应
|
118 |
data = json.loads(response.text)
|
119 |
if data and "songs" in data and data["songs"]:
|
120 |
fee = int(data["songs"][0]["fee"])
|
@@ -122,10 +115,10 @@ def music163_song_info(id: str):
|
|
122 |
song_name = str(data["songs"][0]["name"])
|
123 |
|
124 |
else:
|
125 |
-
song_name = "
|
126 |
|
127 |
else:
|
128 |
-
raise ConnectionError(f"
|
129 |
|
130 |
return song_name, free
|
131 |
|
@@ -141,16 +134,16 @@ def url_infer(song: str):
|
|
141 |
song = f"https://music.163.com/song/media/outer/url?id={song_id}.mp3"
|
142 |
song_name, free = music163_song_info(song_id)
|
143 |
if not free:
|
144 |
-
raise AttributeError("
|
145 |
|
146 |
-
|
147 |
|
148 |
elif song.isdigit():
|
149 |
song_id = song
|
150 |
song = f"https://music.163.com/song/media/outer/url?id={song_id}.mp3"
|
151 |
song_name, free = music163_song_info(song_id)
|
152 |
if not free:
|
153 |
-
raise AttributeError("
|
154 |
|
155 |
download_path = download_audio(song, download_path)
|
156 |
|
@@ -170,42 +163,42 @@ def url_infer(song: str):
|
|
170 |
|
171 |
if __name__ == "__main__":
|
172 |
with gr.Blocks() as iface:
|
173 |
-
with gr.Tab("
|
174 |
gr.Interface(
|
175 |
fn=upl_infer,
|
176 |
inputs=gr.Audio(
|
177 |
-
label="
|
178 |
type="filepath",
|
179 |
),
|
180 |
outputs=[
|
181 |
-
gr.File(label="
|
182 |
-
gr.File(label="
|
183 |
-
gr.File(label="
|
184 |
-
gr.File(label="
|
185 |
-
gr.Textbox(label="ABC
|
186 |
-
gr.Image(label="
|
187 |
],
|
188 |
-
title="
|
189 |
flagging_mode="never",
|
190 |
)
|
191 |
|
192 |
-
with gr.Tab("
|
193 |
gr.Interface(
|
194 |
fn=url_infer,
|
195 |
inputs=gr.Textbox(
|
196 |
-
label="
|
197 |
placeholder="https://music.163.com/#/song?id=",
|
198 |
),
|
199 |
outputs=[
|
200 |
-
gr.Audio(label="
|
201 |
-
gr.File(label="
|
202 |
-
gr.File(label="
|
203 |
-
gr.File(label="
|
204 |
-
gr.File(label="
|
205 |
-
gr.Textbox(label="ABC
|
206 |
-
gr.Image(label="
|
207 |
],
|
208 |
-
title="
|
209 |
examples=["1945798894", "1945798973", "1946098771"],
|
210 |
flagging_mode="never",
|
211 |
cache_examples=False,
|
|
|
28 |
|
29 |
def get_audio_file_type(file_path: str):
|
30 |
try:
|
|
|
31 |
info = mediainfo(file_path)
|
|
|
32 |
return "." + info["format_name"]
|
33 |
|
34 |
except Exception as e:
|
35 |
+
print(f"Error occurred: {e}")
|
36 |
return None
|
37 |
|
38 |
|
39 |
def download_audio(url: str, save_path: str):
|
40 |
with NamedTemporaryFile(delete=False, suffix="_temp") as tmp_file:
|
41 |
temp_file_path = tmp_file.name
|
|
|
42 |
response = requests.get(url, stream=True)
|
|
|
43 |
if response.status_code == 200:
|
|
|
44 |
for chunk in response.iter_content(chunk_size=8192):
|
45 |
tmp_file.write(chunk)
|
46 |
|
47 |
else:
|
48 |
+
print(f"Failed to download file: HTTP {response.status_code}")
|
49 |
return ""
|
50 |
|
51 |
ext = get_audio_file_type(temp_file_path)
|
52 |
full_path = f"{save_path}{ext}"
|
|
|
53 |
shutil.move(temp_file_path, full_path)
|
54 |
return full_path
|
55 |
|
56 |
|
57 |
def is_url(s: str):
|
58 |
try:
|
|
|
59 |
result = urlparse(s)
|
|
|
60 |
return all([result.scheme, result.netloc])
|
61 |
|
62 |
except:
|
|
|
63 |
return False
|
64 |
|
65 |
|
66 |
def audio2midi(audio_path: str):
|
67 |
+
# Load audio
|
68 |
audio, _ = load_audio(audio_path, sr=sample_rate, mono=True)
|
69 |
+
# Transcriptor
|
70 |
transcriptor = PianoTranscription(
|
71 |
device="cuda" if torch.cuda.is_available() else "cpu",
|
72 |
checkpoint_path=WEIGHTS_PATH,
|
73 |
)
|
74 |
+
# device: 'cuda' | 'cpu' Transcribe and write out to MIDI file
|
75 |
midi_path = f"{CACHE_DIR}/output.mid"
|
76 |
+
# midi_path = audio_path.replace(audio_path.split(".")[-1], "mid")
|
77 |
transcriptor.transcribe(audio, midi_path)
|
78 |
return midi_path, os.path.basename(audio_path).split(".")[-2].capitalize()
|
79 |
|
|
|
105 |
detail_api = "https://music.163.com/api/v3/song/detail"
|
106 |
parm_dict = {"id": id, "c": str([{"id": id}]), "csrf_token": ""}
|
107 |
free = False
|
108 |
+
song_name = "Failed to get the song"
|
109 |
response = requests.get(detail_api, params=parm_dict)
|
|
|
110 |
if response.status_code == 200:
|
|
|
111 |
data = json.loads(response.text)
|
112 |
if data and "songs" in data and data["songs"]:
|
113 |
fee = int(data["songs"][0]["fee"])
|
|
|
115 |
song_name = str(data["songs"][0]["name"])
|
116 |
|
117 |
else:
|
118 |
+
song_name = "The song does not exist"
|
119 |
|
120 |
else:
|
121 |
+
raise ConnectionError(f"Error: {response.status_code}, {response.text}")
|
122 |
|
123 |
return song_name, free
|
124 |
|
|
|
134 |
song = f"https://music.163.com/song/media/outer/url?id={song_id}.mp3"
|
135 |
song_name, free = music163_song_info(song_id)
|
136 |
if not free:
|
137 |
+
raise AttributeError("Unable to parse VIP songs")
|
138 |
|
139 |
+
download_path = download_audio(song, download_path)
|
140 |
|
141 |
elif song.isdigit():
|
142 |
song_id = song
|
143 |
song = f"https://music.163.com/song/media/outer/url?id={song_id}.mp3"
|
144 |
song_name, free = music163_song_info(song_id)
|
145 |
if not free:
|
146 |
+
raise AttributeError("Unable to parse VIP songs")
|
147 |
|
148 |
download_path = download_audio(song, download_path)
|
149 |
|
|
|
163 |
|
164 |
if __name__ == "__main__":
|
165 |
with gr.Blocks() as iface:
|
166 |
+
with gr.Tab("Uploading Mode"):
|
167 |
gr.Interface(
|
168 |
fn=upl_infer,
|
169 |
inputs=gr.Audio(
|
170 |
+
label="Upload an audio",
|
171 |
type="filepath",
|
172 |
),
|
173 |
outputs=[
|
174 |
+
gr.File(label="Download MIDI"),
|
175 |
+
gr.File(label="Download PDF score"),
|
176 |
+
gr.File(label="Download MusicXML"),
|
177 |
+
gr.File(label="Download MXL"),
|
178 |
+
gr.Textbox(label="ABC notation", show_copy_button=True),
|
179 |
+
gr.Image(label="Staff", type="filepath"),
|
180 |
],
|
181 |
+
title="Please make sure the audio is completely uploaded before clicking Submit",
|
182 |
flagging_mode="never",
|
183 |
)
|
184 |
|
185 |
+
with gr.Tab("Direct Link Mode"):
|
186 |
gr.Interface(
|
187 |
fn=url_infer,
|
188 |
inputs=gr.Textbox(
|
189 |
+
label="Input audio direct link",
|
190 |
placeholder="https://music.163.com/#/song?id=",
|
191 |
),
|
192 |
outputs=[
|
193 |
+
gr.Audio(label="Download audio", type="filepath"),
|
194 |
+
gr.File(label="Download MIDI"),
|
195 |
+
gr.File(label="Download PDF score"),
|
196 |
+
gr.File(label="Download MusicXML"),
|
197 |
+
gr.File(label="Download MXL"),
|
198 |
+
gr.Textbox(label="ABC notation", show_copy_button=True),
|
199 |
+
gr.Image(label="Staff", type="filepath"),
|
200 |
],
|
201 |
+
title="For Netease Cloud music, you can directly input the non-VIP song page link",
|
202 |
examples=["1945798894", "1945798973", "1946098771"],
|
203 |
flagging_mode="never",
|
204 |
cache_examples=False,
|