next-playground commited on
Commit
d7e62a3
·
verified ·
1 Parent(s): 1be2023

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -140
app.py CHANGED
@@ -1,151 +1,32 @@
1
  from audio_separator.separator import Separator
2
- from flask import Flask, request, send_file
3
  import requests
 
 
4
  from pathlib import Path
 
 
5
 
6
  separator = Separator()
7
 
8
- roformer_models = {
9
- 'BS-Roformer-Viperx-1297.ckpt': 'model_bs_roformer_ep_317_sdr_12.9755.ckpt',
10
- 'BS-Roformer-Viperx-1296.ckpt': 'model_bs_roformer_ep_368_sdr_12.9628.ckpt',
11
- 'BS-Roformer-Viperx-1053.ckpt': 'model_bs_roformer_ep_937_sdr_10.5309.ckpt',
12
- 'Mel-Roformer-Viperx-1143.ckpt': 'model_mel_band_roformer_ep_3005_sdr_11.4360.ckpt'
13
- }
14
-
15
- mdx23c_models = [
16
- 'MDX23C_D1581.ckpt',
17
- 'MDX23C-8KFFT-InstVoc_HQ.ckpt',
18
- 'MDX23C-8KFFT-InstVoc_HQ_2.ckpt',
19
- ]
20
-
21
- mdxnet_models = [
22
- 'UVR-MDX-NET-Inst_full_292.onnx',
23
- 'UVR-MDX-NET_Inst_187_beta.onnx',
24
- 'UVR-MDX-NET_Inst_82_beta.onnx',
25
- 'UVR-MDX-NET_Inst_90_beta.onnx',
26
- 'UVR-MDX-NET_Main_340.onnx',
27
- 'UVR-MDX-NET_Main_390.onnx',
28
- 'UVR-MDX-NET_Main_406.onnx',
29
- 'UVR-MDX-NET_Main_427.onnx',
30
- 'UVR-MDX-NET_Main_438.onnx',
31
- 'UVR-MDX-NET-Inst_HQ_1.onnx',
32
- 'UVR-MDX-NET-Inst_HQ_2.onnx',
33
- 'UVR-MDX-NET-Inst_HQ_3.onnx',
34
- 'UVR-MDX-NET-Inst_HQ_4.onnx',
35
- 'UVR_MDXNET_Main.onnx',
36
- 'UVR-MDX-NET-Inst_Main.onnx',
37
- 'UVR_MDXNET_1_9703.onnx',
38
- 'UVR_MDXNET_2_9682.onnx',
39
- 'UVR_MDXNET_3_9662.onnx',
40
- 'UVR-MDX-NET-Inst_1.onnx',
41
- 'UVR-MDX-NET-Inst_2.onnx',
42
- 'UVR-MDX-NET-Inst_3.onnx',
43
- 'UVR_MDXNET_KARA.onnx',
44
- 'UVR_MDXNET_KARA_2.onnx',
45
- 'UVR_MDXNET_9482.onnx',
46
- 'UVR-MDX-NET-Voc_FT.onnx',
47
- 'Kim_Vocal_1.onnx',
48
- 'Kim_Vocal_2.onnx',
49
- 'Kim_Inst.onnx',
50
- 'Reverb_HQ_By_FoxJoy.onnx',
51
- 'UVR-MDX-NET_Crowd_HQ_1.onnx',
52
- 'kuielab_a_vocals.onnx',
53
- 'kuielab_a_other.onnx',
54
- 'kuielab_a_bass.onnx',
55
- 'kuielab_a_drums.onnx',
56
- 'kuielab_b_vocals.onnx',
57
- 'kuielab_b_other.onnx',
58
- 'kuielab_b_bass.onnx',
59
- 'kuielab_b_drums.onnx',
60
- ]
61
-
62
- vrarch_models = [
63
- '1_HP-UVR.pth',
64
- '2_HP-UVR.pth',
65
- '3_HP-Vocal-UVR.pth',
66
- '4_HP-Vocal-UVR.pth',
67
- '5_HP-Karaoke-UVR.pth',
68
- '6_HP-Karaoke-UVR.pth',
69
- '7_HP2-UVR.pth',
70
- '8_HP2-UVR.pth',
71
- '9_HP2-UVR.pth',
72
- '10_SP-UVR-2B-32000-1.pth',
73
- '11_SP-UVR-2B-32000-2.pth',
74
- '12_SP-UVR-3B-44100.pth',
75
- '13_SP-UVR-4B-44100-1.pth',
76
- '14_SP-UVR-4B-44100-2.pth',
77
- '15_SP-UVR-MID-44100-1.pth',
78
- '16_SP-UVR-MID-44100-2.pth',
79
- '17_HP-Wind_Inst-UVR.pth',
80
- 'UVR-De-Echo-Aggressive.pth',
81
- 'UVR-De-Echo-Normal.pth',
82
- 'UVR-DeEcho-DeReverb.pth',
83
- 'UVR-DeNoise-Lite.pth',
84
- 'UVR-DeNoise.pth',
85
- 'UVR-BVE-4B_SN-44100-1.pth',
86
- 'MGM_HIGHEND_v4.pth',
87
- 'MGM_LOWEND_A_v4.pth',
88
- 'MGM_LOWEND_B_v4.pth',
89
- 'MGM_MAIN_v4.pth',
90
- ]
91
-
92
- demucs_models = [
93
- 'htdemucs_ft.yaml',
94
- 'htdemucs.yaml',
95
- 'hdemucs_mmi.yaml',
96
- ]
97
-
98
- def roformer_separator(audio, checkpoint_name):
99
- full_checkpoint_name = roformer_models[checkpoint_name]
100
- separator.load_model(full_checkpoint_name)
101
- output_files = separator.separate(audio)
102
- stem1 = output_files[0]
103
- stem2 = output_files[1]
104
- return stem1, stem2
105
-
106
- def mdx_vr_separator(audio, checkpoint_name):
107
- separator.load_model(checkpoint_name)
108
- output_files = separator.separate(audio)
109
- stem1 = output_files[0]
110
- stem2 = output_files[1]
111
- return stem1, stem2
112
-
113
- def demucs_separator(audio, checkpoint_name):
114
- separator.load_model(checkpoint_name)
115
- output_files = separator.separate(audio)
116
- stem1 = output_files[0]
117
- stem2 = output_files[1]
118
- stem3 = output_files[2]
119
- stem4 = output_files[3]
120
- return stem1, stem2, stem3, stem4
121
 
122
- app = Flask(__name__)
123
-
124
- @app.route('/', methods=['GET'])
125
- def hello():
126
- return "Hello! This is an api server, and it is running successfully. For usage, please contact the person who hosted this api server."
127
-
128
- @app.route('/api/audio_separation', methods=['GET'])
129
- def audio_separation():
130
  try:
131
- # 获取MP3文件的直链地址
132
- mp3_url = request.args.get('url')
133
- if not mp3_url:
134
- return "Error: URL parameter is required", 400
135
-
136
- # 下载MP3文件到本地
137
  response = requests.get(mp3_url)
138
- mp3_filename = mp3_url.split('/')[-1] # 使用下载的文件名
139
  with open("/tmp/" + mp3_filename, 'wb') as f:
140
- f.write(response.content)
141
 
142
  # 执行音频分离操作
143
- mdxnet_stem1, mdxnet_stem2 = mdx_vr_separator([response.content, UVR-MDX-NET-Inst_HQ_3.onnx])
144
 
145
  # 生成分离后的文件名
146
  vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_vocals.wav"
147
  no_vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_no_vocals.wav"
148
-
149
  # 保存结果
150
  with open(vocals_filename, 'wb') as file:
151
  file.write(mdxnet_stem1)
@@ -153,18 +34,49 @@ def audio_separation():
153
  with open(no_vocals_filename, 'wb') as file:
154
  file.write(mdxnet_stem2)
155
 
156
- # 提供文件的永久直链
157
  vocals_url = f"/download/{os.path.basename(vocals_filename)}"
158
  no_vocals_url = f"/download/{os.path.basename(no_vocals_filename)}"
159
 
160
- # 返回结果
161
- result = {
162
- "vocals_url": vocals_url,
163
- "no_vocals_url": no_vocals_url
164
- }
165
- return result
166
  except Exception as e:
167
- return "Error: " + str(e), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
  @app.route('/download/<filename>', methods=['GET'])
170
  def download(filename):
 
1
  from audio_separator.separator import Separator
2
+ from flask import Flask, request, jsonify, send_file
3
  import requests
4
+ import os
5
+ import uuid
6
  from pathlib import Path
7
+ import threading
8
+ import time
9
 
10
  separator = Separator()
11
 
12
+ # 任务队列和状态存储
13
+ tasks = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ def process_audio_separation(task_id, mp3_url):
 
 
 
 
 
 
 
16
  try:
17
+ # 下载MP3文件到本地
 
 
 
 
 
18
  response = requests.get(mp3_url)
19
+ mp3_filename = mp3_url.split('/')[-1]
20
  with open("/tmp/" + mp3_filename, 'wb') as f:
21
+ f.write(response.content)
22
 
23
  # 执行音频分离操作
24
+ mdxnet_stem1, mdxnet_stem2 = mdx_vr_separator([response.content, 'UVR-MDX-NET-Inst_HQ_3.onnx'])
25
 
26
  # 生成分离后的文件名
27
  vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_vocals.wav"
28
  no_vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_no_vocals.wav"
29
+
30
  # 保存结果
31
  with open(vocals_filename, 'wb') as file:
32
  file.write(mdxnet_stem1)
 
34
  with open(no_vocals_filename, 'wb') as file:
35
  file.write(mdxnet_stem2)
36
 
37
+ # 提供文件的永久直链
38
  vocals_url = f"/download/{os.path.basename(vocals_filename)}"
39
  no_vocals_url = f"/download/{os.path.basename(no_vocals_filename)}"
40
 
41
+ # 更新任务状态
42
+ tasks[task_id] = {
43
+ "status": "completed",
44
+ "vocals_url": vocals_url,
45
+ "no_vocals_url": no_vocals_url
46
+ }
47
  except Exception as e:
48
+ tasks[task_id] = {
49
+ "status": "error",
50
+ "message": str(e)
51
+ }
52
+
53
+ app = Flask(__name__)
54
+
55
+ @app.route('/', methods=['GET'])
56
+ def hello():
57
+ return "Hello! This is an api server, and it is running successfully. For usage, please contact the person who hosted this api server."
58
+
59
+ @app.route('/api/audio_separation', methods=['GET'])
60
+ def audio_separation():
61
+ mp3_url = request.args.get('url')
62
+ if not mp3_url:
63
+ return jsonify({"error": "URL parameter is required"}), 400
64
+
65
+ task_id = str(uuid.uuid4())
66
+ tasks[task_id] = {"status": "processing"}
67
+
68
+ # 异步执行任务
69
+ threading.Thread(target=process_audio_separation, args=(task_id, mp3_url)).start()
70
+
71
+ return jsonify({"task_id": task_id}), 202
72
+
73
+ @app.route('/api/tasks/<task_id>', methods=['GET'])
74
+ def get_task_status(task_id):
75
+ task = tasks.get(task_id)
76
+ if task:
77
+ return jsonify(task)
78
+ else:
79
+ return jsonify({"error": "Task not found"}), 404
80
 
81
  @app.route('/download/<filename>', methods=['GET'])
82
  def download(filename):