next-playground commited on
Commit
af76885
·
verified ·
1 Parent(s): b09b5b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -58
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
- with gr.Blocks(title="🎵 UVR5 UI 🎵") as demo:
125
- gr.Markdown("<h1> 🎵 UVR5 UI 🎵 </h1>")
126
- with gr.Tab("Vocal Separator (UVR)"):
127
- gr.Markdown("Separate vocals and instruments from an audio file using UVR models.")
128
- with gr.Tab("Mel/BS Roformer"):
129
- roformer_audio_file = gr.Audio(label="Audio File", type="filepath")
130
- with gr.Row():
131
- roformer_model = gr.Dropdown(label="Model", choices=list(roformer_models.keys()))
132
- roformer_button = gr.Button("Separate", variant="primary")
133
- roformer_stem1 = gr.Audio(type="filepath", label="Stem 1")
134
- roformer_stem2 = gr.Audio(type="filepath", label="Stem 2")
135
-
136
- roformer_button.click(roformer_separator, [roformer_audio_file, roformer_model], [roformer_stem1, roformer_stem2])
137
- with gr.Tab("MDX23C"):
138
- mdx23c_audio_file = gr.Audio(label="Audio File", type="filepath")
139
- with gr.Row():
140
- mdx23c_model = gr.Dropdown(label="Model", choices=mdx23c_models)
141
- mdx23c_button = gr.Button("Separate", variant="primary")
142
- mdx23c_stem1 = gr.Audio(type="filepath", label="Stem 1")
143
- mdx23c_stem2 = gr.Audio(type="filepath", label="Stem 2")
144
-
145
- mdx23c_button.click(mdx_vr_separator, [mdx23c_audio_file, mdx23c_model], [mdx23c_stem1, mdx23c_stem2])
146
- with gr.Tab("MDX-NET"):
147
- mdxnet_audio_file = gr.Audio(label="Audio File", type="filepath")
148
- with gr.Row():
149
- mdxnet_model = gr.Dropdown(label="Model", choices=mdxnet_models)
150
- mdxnet_button = gr.Button("Separate", variant="primary")
151
- mdxnet_stem1 = gr.Audio(type="filepath", label="Stem 1")
152
- mdxnet_stem2 = gr.Audio(type="filepath", label="Stem 2")
153
-
154
- mdxnet_button.click(mdx_vr_separator, [mdxnet_audio_file, mdxnet_model], [mdxnet_stem1, mdxnet_stem2])
155
- with gr.Tab("VR-ARCH"):
156
- vr_audio_file = gr.Audio(label="Audio File", type="filepath")
157
- with gr.Row():
158
- vr_model = gr.Dropdown(label="Model", choices=vrarch_models)
159
- vr_button = gr.Button("Separate", variant="primary")
160
- vr_stem1 = gr.Audio(type="filepath", label="Stem 1")
161
- vr_stem2 = gr.Audio(type="filepath", label="Stem 2")
162
-
163
- vr_button.click(mdx_vr_separator, [vr_audio_file, vr_model], [vr_stem1, vr_stem2])
164
- with gr.Tab("Demucs"):
165
- demucs_audio_file = gr.Audio(label="Audio File", type="filepath")
166
- with gr.Row():
167
- demucs_model = gr.Dropdown(label="Model", choices=demucs_models)
168
- demucs_button = gr.Button("Separate", variant="primary")
169
- demucs_stem1 = gr.Audio(type="filepath", label="Stem 1")
170
- demucs_stem2 = gr.Audio(type="filepath", label="Stem 2")
171
- demucs_stem3 = gr.Audio(type="filepath", label="Stem 3")
172
- demucs_stem4 = gr.Audio(type="filepath", label="Stem 4")
173
-
174
- demucs_button.click(demucs_separator, [demucs_audio_file, demucs_model], [demucs_stem1, demucs_stem2, demucs_stem3, demucs_stem4])
175
-
176
- demo.launch()
 
 
 
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)