Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import spaces
|
| 3 |
from audio_separator.separator import Separator
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
separator = Separator()
|
| 6 |
|
|
@@ -94,7 +95,6 @@ demucs_models = [
|
|
| 94 |
'hdemucs_mmi.yaml',
|
| 95 |
]
|
| 96 |
|
| 97 |
-
@spaces.GPU(duration=300)
|
| 98 |
def roformer_separator(audio, checkpoint_name):
|
| 99 |
full_checkpoint_name = roformer_models[checkpoint_name]
|
| 100 |
separator.load_model(full_checkpoint_name)
|
|
@@ -103,7 +103,6 @@ def roformer_separator(audio, checkpoint_name):
|
|
| 103 |
stem2 = output_files[1]
|
| 104 |
return stem1, stem2
|
| 105 |
|
| 106 |
-
@spaces.GPU(duration=300)
|
| 107 |
def mdx_vr_separator(audio, checkpoint_name):
|
| 108 |
separator.load_model(checkpoint_name)
|
| 109 |
output_files = separator.separate(audio)
|
|
@@ -111,7 +110,6 @@ def mdx_vr_separator(audio, checkpoint_name):
|
|
| 111 |
stem2 = output_files[1]
|
| 112 |
return stem1, stem2
|
| 113 |
|
| 114 |
-
@spaces.GPU(duration=300)
|
| 115 |
def demucs_separator(audio, checkpoint_name):
|
| 116 |
separator.load_model(checkpoint_name)
|
| 117 |
output_files = separator.separate(audio)
|
|
@@ -121,56 +119,56 @@ def demucs_separator(audio, checkpoint_name):
|
|
| 121 |
stem4 = output_files[3]
|
| 122 |
return stem1, stem2, stem3, stem4
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
with
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
|
|
| 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)
|
|
|
|
| 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)
|
|
|
|
| 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)
|
|
|
|
| 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)
|
| 152 |
+
|
| 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):
|
| 171 |
+
return send_file("/tmp/" + filename, as_attachment=True)
|
| 172 |
+
|
| 173 |
+
if __name__ == '__main__':
|
| 174 |
+
app.run(debug=False)
|