Spaces:
Running
Running
Upload myinfer_latest.py
Browse files- myinfer_latest.py +61 -44
myinfer_latest.py
CHANGED
@@ -21,6 +21,9 @@ import os
|
|
21 |
import werkzeug
|
22 |
from pydub import AudioSegment
|
23 |
import uuid
|
|
|
|
|
|
|
24 |
|
25 |
|
26 |
app = Flask(__name__)
|
@@ -31,6 +34,14 @@ shutil.rmtree(tmp, ignore_errors=True)
|
|
31 |
os.makedirs(tmp, exist_ok=True)
|
32 |
os.environ["TEMP"] = tmp
|
33 |
split_model="htdemucs"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
from lib.infer_pack.models import (
|
35 |
SynthesizerTrnMs256NSFsid,
|
36 |
SynthesizerTrnMs256NSFsid_nono,
|
@@ -116,51 +127,57 @@ def cleanup_files(file_paths):
|
|
116 |
processed_audio_storage = {}
|
117 |
@app.route('/convert_voice', methods=['POST'])
|
118 |
def api_convert_voice():
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
# Set the flag indicating the form has been submitted
|
124 |
-
session['submitted'] = True
|
125 |
-
print(request.form)
|
126 |
-
print(request.files)
|
127 |
-
spk_id = request.form['spk_id']+'.pth'
|
128 |
-
voice_transform = request.form['voice_transform']
|
129 |
-
|
130 |
-
# The file part
|
131 |
-
if 'file' not in request.files:
|
132 |
-
return jsonify({"error": "No file part"}), 400
|
133 |
-
file = request.files['file']
|
134 |
-
if file.filename == '':
|
135 |
-
return jsonify({"error": "No selected file"}), 400
|
136 |
-
created_files = []
|
137 |
-
# Save the file to a temporary path
|
138 |
-
unique_id = str(uuid.uuid4())
|
139 |
-
print(unique_id)
|
140 |
-
|
141 |
-
filename = werkzeug.utils.secure_filename(file.filename)
|
142 |
-
input_audio_path = os.path.join(tmp, f"{spk_id}_input_audio_{unique_id}.{filename.split('.')[-1]}")
|
143 |
-
file.save(input_audio_path)
|
144 |
-
|
145 |
-
created_files.append(input_audio_path)
|
146 |
-
|
147 |
-
#split audio
|
148 |
-
cut_vocal_and_inst(input_audio_path,spk_id)
|
149 |
-
print("audio splitting performed")
|
150 |
-
vocal_path = f"output/{split_model}/{spk_id}_input_audio_{unique_id}/vocals.wav"
|
151 |
-
inst = f"output/{split_model}/{spk_id}_input_audio_{unique_id}/no_vocals.wav"
|
152 |
-
|
153 |
-
output_path = convert_voice(spk_id, vocal_path, voice_transform,unique_id)
|
154 |
-
output_path1= combine_vocal_and_inst(output_path,inst,unique_id)
|
155 |
-
|
156 |
-
processed_audio_storage[unique_id] = output_path1
|
157 |
-
session['processed_audio_id'] = unique_id
|
158 |
-
|
159 |
-
print(output_path1)
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
#if os.path.exists(output_path1):
|
165 |
|
166 |
# return send_file(output_path1, as_attachment=True)
|
|
|
21 |
import werkzeug
|
22 |
from pydub import AudioSegment
|
23 |
import uuid
|
24 |
+
from threading import Semaphore
|
25 |
+
|
26 |
+
|
27 |
|
28 |
|
29 |
app = Flask(__name__)
|
|
|
34 |
os.makedirs(tmp, exist_ok=True)
|
35 |
os.environ["TEMP"] = tmp
|
36 |
split_model="htdemucs"
|
37 |
+
|
38 |
+
# Define the maximum number of concurrent requests
|
39 |
+
MAX_CONCURRENT_REQUESTS = 1 # Adjust this number as needed
|
40 |
+
|
41 |
+
# Initialize the semaphore with the maximum number of concurrent requests
|
42 |
+
request_semaphore = Semaphore(MAX_CONCURRENT_REQUESTS)
|
43 |
+
|
44 |
+
|
45 |
from lib.infer_pack.models import (
|
46 |
SynthesizerTrnMs256NSFsid,
|
47 |
SynthesizerTrnMs256NSFsid_nono,
|
|
|
127 |
processed_audio_storage = {}
|
128 |
@app.route('/convert_voice', methods=['POST'])
|
129 |
def api_convert_voice():
|
130 |
+
acquired = request_semaphore.acquire(blocking=False)
|
131 |
+
if not acquired:
|
132 |
+
return jsonify({"error": "Too many requests, please try again later"}), 429
|
133 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
+
if session.get('submitted'):
|
136 |
+
return jsonify({"error": "Form already submitted"}), 400
|
137 |
+
|
138 |
+
# Process the form here...
|
139 |
+
# Set the flag indicating the form has been submitted
|
140 |
+
session['submitted'] = True
|
141 |
+
print(request.form)
|
142 |
+
print(request.files)
|
143 |
+
spk_id = request.form['spk_id']+'.pth'
|
144 |
+
voice_transform = request.form['voice_transform']
|
145 |
+
|
146 |
+
# The file part
|
147 |
+
if 'file' not in request.files:
|
148 |
+
return jsonify({"error": "No file part"}), 400
|
149 |
+
file = request.files['file']
|
150 |
+
if file.filename == '':
|
151 |
+
return jsonify({"error": "No selected file"}), 400
|
152 |
+
created_files = []
|
153 |
+
# Save the file to a temporary path
|
154 |
+
unique_id = str(uuid.uuid4())
|
155 |
+
print(unique_id)
|
156 |
+
|
157 |
+
filename = werkzeug.utils.secure_filename(file.filename)
|
158 |
+
input_audio_path = os.path.join(tmp, f"{spk_id}_input_audio_{unique_id}.{filename.split('.')[-1]}")
|
159 |
+
file.save(input_audio_path)
|
160 |
+
|
161 |
+
created_files.append(input_audio_path)
|
162 |
+
|
163 |
+
#split audio
|
164 |
+
cut_vocal_and_inst(input_audio_path,spk_id)
|
165 |
+
print("audio splitting performed")
|
166 |
+
vocal_path = f"output/{split_model}/{spk_id}_input_audio_{unique_id}/vocals.wav"
|
167 |
+
inst = f"output/{split_model}/{spk_id}_input_audio_{unique_id}/no_vocals.wav"
|
168 |
+
|
169 |
+
output_path = convert_voice(spk_id, vocal_path, voice_transform,unique_id)
|
170 |
+
output_path1= combine_vocal_and_inst(output_path,inst,unique_id)
|
171 |
+
|
172 |
+
processed_audio_storage[unique_id] = output_path1
|
173 |
+
session['processed_audio_id'] = unique_id
|
174 |
+
|
175 |
+
print(output_path1)
|
176 |
+
|
177 |
+
created_files.extend([vocal_path, inst, output_path])
|
178 |
+
return jsonify({"message": "File processed successfully", "audio_id": unique_id}), 200
|
179 |
+
finally:
|
180 |
+
semaphore.release()
|
181 |
#if os.path.exists(output_path1):
|
182 |
|
183 |
# return send_file(output_path1, as_attachment=True)
|