next-playground commited on
Commit
46385e7
·
verified ·
1 Parent(s): 8cacf81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -28
app.py CHANGED
@@ -11,34 +11,37 @@ def hello():
11
 
12
  @app.route('/api/audio_separation', methods=['GET'])
13
  def audio_separation():
14
- # 获取MP3文件的直链地址
15
- mp3_url = request.args.get('url')
16
- if not mp3_url:
17
- return "Error: URL parameter is required", 400
18
-
19
- # 下载MP3文件到本地
20
- response = requests.get(mp3_url)
21
- mp3_filename = mp3_url.split('/')[-1] # 使用下载的文件名
22
- with open("/tmp/" + mp3_filename, 'wb') as f:
23
- f.write(response.content)
24
-
25
- # 执行音频分离操作
26
- subprocess.run(['python', 'separate.py', '/tmp/' + mp3_filename, '-o', '/tmp', '-m', './models/MDX_Net_Models/UVR-MDX-NET-Inst_HQ_3.onnx'])
27
-
28
- # 生成分离后的文件名
29
- vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_vocals.wav"
30
- no_vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_no_vocals.wav"
31
-
32
- # 提供文件的永久直链
33
- vocals_url = f"/download/{os.path.basename(vocals_filename)}"
34
- no_vocals_url = f"/download/{os.path.basename(no_vocals_filename)}"
35
-
36
- # 返回结果
37
- result = {
38
- "vocals_url": vocals_url,
39
- "no_vocals_url": no_vocals_url
40
- }
41
- return result
 
 
 
42
 
43
  @app.route('/download/<filename>', methods=['GET'])
44
  def download(filename):
 
11
 
12
  @app.route('/api/audio_separation', methods=['GET'])
13
  def audio_separation():
14
+ try:
15
+ # 获取MP3文件的直链地址
16
+ mp3_url = request.args.get('url')
17
+ if not mp3_url:
18
+ return "Error: URL parameter is required", 400
19
+
20
+ # 下载MP3文件到本地
21
+ response = requests.get(mp3_url)
22
+ mp3_filename = mp3_url.split('/')[-1] # 使用下载的文件名
23
+ with open("/tmp/" + mp3_filename, 'wb') as f:
24
+ f.write(response.content)
25
+
26
+ # 执行音频分离操作
27
+ subprocess.run(['python', 'separate.py', '/tmp/' + mp3_filename, '-o', '/tmp', '-m', './models/MDX_Net_Models/UVR-MDX-NET-Inst_HQ_3.onnx'])
28
+
29
+ # 生成分离后的文件名
30
+ vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_vocals.wav"
31
+ no_vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_no_vocals.wav"
32
+
33
+ # 提供文件的永久直链
34
+ vocals_url = f"/download/{os.path.basename(vocals_filename)}"
35
+ no_vocals_url = f"/download/{os.path.basename(no_vocals_filename)}"
36
+
37
+ # 返回结果
38
+ result = {
39
+ "vocals_url": vocals_url,
40
+ "no_vocals_url": no_vocals_url
41
+ }
42
+ return result
43
+ except Exception as e:
44
+ result = "Error: " + str(e), 500
45
 
46
  @app.route('/download/<filename>', methods=['GET'])
47
  def download(filename):