-
Notifications
You must be signed in to change notification settings - Fork 82
/
server.py
89 lines (79 loc) · 2.96 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import io
import json
import os
import traceback
from typing import *
import soundfile as sf
from flask import Flask, make_response, request, send_file
from scipy.io.wavfile import write
from modules.server.model import VoiceServerModel
model: Optional[VoiceServerModel] = None
app = Flask(__name__)
@app.route('/ping')
def ping():
return make_response("server is alive", 200)
@app.route('/upload_model', methods=['POST'])
def upload_model():
"""
input:
json:
rvc_model_file: str
specify rvc model's absolute path (.pt, .pth)
faiss_index_file: Optional[str]
specify faiss index'S absolute path (.index)
"""
global model
if request.method == "POST":
rvc_model_file = request.json["rvc_model_file"]
faiss_index_file =request.json["faiss_index_file"] if "faiss_index_file" in request.json else ""
try:
model = VoiceServerModel(rvc_model_file, faiss_index_file)
return make_response("model is load", 200)
except:
traceback.print_exc()
return make_response("model load error", 400)
else:
return make_response("use post method", 400)
@app.route('/convert_sound', methods=['POST'])
def convert_sound():
"""
input:
params: json
speaker_id: int
default: 0
transpose: int
default: 0
pitch_extraction_algo: str
default: dio
value: ["dio", "harvest", "mangio-crepe", "crepe"]
retrieval_feature_ratio: float
default: 0
value: 0. ~ 1.
input_wav: wav file
output:
wavfile
"""
global model
if model is None:
return make_response("please upload model", 400)
print("start")
if request.method == "POST":
input_buffer = io.BytesIO(request.files["input_wav"].stream.read())
audio, sr = sf.read(input_buffer)
req_json = json.load(io.BytesIO(request.files["params"].stream.read()))
sid = int(req_json.get("speaker_id", 0))
transpose = int(req_json.get("transpose", 0))
pitch_extraction_algo = req_json.get("pitch_extraction_algo", "dio")
if not pitch_extraction_algo in ["dio", "harvest", "mangio-crepe", "crepe"]:
return make_response("bad pitch extraction algo", 400)
retrieval_feature_ratio = float(req_json.get("retrieval_feature_ratio", 0.))
out_audio = model(audio, sr, sid, transpose, pitch_extraction_algo, retrieval_feature_ratio)
output_buffer = io.BytesIO()
write(output_buffer, rate=model.tgt_sr, data=out_audio)
output_buffer.seek(0)
response = make_response(send_file(output_buffer, mimetype="audio/wav"), 200)
return response
else:
return make_response("use post method", 400)
if __name__ == "__main__":
app.run()