API: add response with json
This commit is contained in:
@@ -36,8 +36,8 @@ class VC:
|
|||||||
|
|
||||||
self.config = Config()
|
self.config = Config()
|
||||||
|
|
||||||
def get_vc(self, sid: str, *to_return_protect: int):
|
def get_vc(self, sid: str | Path, *to_return_protect: int):
|
||||||
logger.info("Get sid: " + sid)
|
logger.info("Get sid: " + os.path.basename(sid))
|
||||||
|
|
||||||
return_protect = [
|
return_protect = [
|
||||||
to_return_protect[0] if self.if_f0 != 0 and to_return_protect else 0.5,
|
to_return_protect[0] if self.if_f0 != 0 and to_return_protect else 0.5,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ def get_index_path_from_model(sid):
|
|||||||
for name in files
|
for name in files
|
||||||
if name.endswith(".index") and "trained" not in name
|
if name.endswith(".index") and "trained" not in name
|
||||||
]
|
]
|
||||||
if sid.split(".")[0] in f
|
if str(sid).split(".")[0] in f
|
||||||
),
|
),
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
|
import json
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from fastapi import APIRouter, Response, UploadFile, responses
|
from fastapi import APIRouter, Response, UploadFile, Body, responses, Form, Query
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from scipy.io import wavfile
|
from scipy.io import wavfile
|
||||||
|
from base64 import b64encode
|
||||||
from rvc.modules.vc.modules import VC
|
from rvc.modules.vc.modules import VC
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@@ -12,11 +15,12 @@ router = APIRouter()
|
|||||||
|
|
||||||
@router.post("/inference")
|
@router.post("/inference")
|
||||||
def inference(
|
def inference(
|
||||||
modelpath: str | UploadFile,
|
modelpath: Path | UploadFile,
|
||||||
input: Path | UploadFile,
|
input_audio: Path | UploadFile,
|
||||||
|
res_type: str = Query("blob", enum=["blob", "json"]),
|
||||||
sid: int = 0,
|
sid: int = 0,
|
||||||
f0_up_key: int = 0,
|
f0_up_key: int = 0,
|
||||||
f0_method: str = "rmvpe",
|
f0_method: str = Query("rmvpe", enum=["pm", "harvest", "dio", "rmvpe", "rmvpe_gpu"]),
|
||||||
f0_file: Path | None = None,
|
f0_file: Path | None = None,
|
||||||
index_file: Path | None = None,
|
index_file: Path | None = None,
|
||||||
index_rate: float = 0.75,
|
index_rate: float = 0.75,
|
||||||
@@ -25,11 +29,12 @@ def inference(
|
|||||||
rms_mix_rate: float = 0.25,
|
rms_mix_rate: float = 0.25,
|
||||||
protect: float = 0.33,
|
protect: float = 0.33,
|
||||||
):
|
):
|
||||||
|
print(res_type)
|
||||||
vc = VC()
|
vc = VC()
|
||||||
vc.get_vc(modelpath)
|
vc.get_vc(modelpath)
|
||||||
tgt_sr, audio_opt, times, _ = vc.vc_inference(
|
tgt_sr, audio_opt, times, _ = vc.vc_inference(
|
||||||
sid,
|
sid,
|
||||||
input,
|
input_audio,
|
||||||
f0_up_key,
|
f0_up_key,
|
||||||
f0_method,
|
f0_method,
|
||||||
f0_file,
|
f0_file,
|
||||||
@@ -42,8 +47,12 @@ def inference(
|
|||||||
)
|
)
|
||||||
wavfile.write(wv := BytesIO(), tgt_sr, audio_opt)
|
wavfile.write(wv := BytesIO(), tgt_sr, audio_opt)
|
||||||
print(times)
|
print(times)
|
||||||
return responses.StreamingResponse(
|
if res_type == "blob":
|
||||||
wv,
|
return responses.StreamingResponse(
|
||||||
media_type="audio/wav",
|
wv,
|
||||||
headers={"Content-Disposition": "attachment; filename=inference.wav"},
|
media_type="audio/wav",
|
||||||
)
|
headers={"Content-Disposition": "attachment; filename=inference.wav"},
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return JSONResponse({"time": json.loads(json.dumps(times)), "audio": b64encode(wv.read()).decode('utf-8')})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user