update server_fastapi.py: 添加2.1模型推理支持。

This commit is contained in:
Sora
2023-12-03 22:41:36 +08:00
parent 2c50b7816b
commit 3623eed896
2 changed files with 51 additions and 39 deletions

View File

@@ -9,7 +9,7 @@ from pydantic import BaseModel
import gradio
import numpy as np
import utils
from fastapi import FastAPI, Query, Request
from fastapi import FastAPI, Query, Request, File, UploadFile, Form
from fastapi.responses import Response, FileResponse
from fastapi.staticfiles import StaticFiles
from io import BytesIO
@@ -180,10 +180,7 @@ if __name__ == "__main__":
async def index():
return FileResponse("./Web/index.html")
class Text(BaseModel):
text: str
def _voice(
async def _voice(
text: str,
model_id: int,
speaker_name: str,
@@ -195,7 +192,10 @@ if __name__ == "__main__":
language: str,
auto_translate: bool,
auto_split: bool,
emotion: Optional[int] = None,
reference_audio=None,
) -> Union[Response, Dict[str, any]]:
"""TTS实现函数"""
# 检查模型是否存在
if model_id not in loaded_models.models.keys():
return {"status": 10, "detail": f"模型model_id={model_id}未加载"}
@@ -214,28 +214,12 @@ if __name__ == "__main__":
language = loaded_models.models[model_id].language
if auto_translate:
text = trans.translate(Sentence=text, to_Language=language.lower())
if not auto_split:
with torch.no_grad():
audio = infer(
text=text,
sdp_ratio=sdp_ratio,
noise_scale=noise,
noise_scale_w=noisew,
length_scale=length,
sid=speaker_name,
language=language,
hps=loaded_models.models[model_id].hps,
net_g=loaded_models.models[model_id].net_g,
device=loaded_models.models[model_id].device,
)
else:
texts = cut_sent(text)
audios = []
with torch.no_grad():
for t in texts:
audios.append(
infer(
text=t,
if reference_audio is not None:
with BytesIO(await reference_audio.read()) as ref_audio:
if not auto_split:
with torch.no_grad():
audio = infer(
text=text,
sdp_ratio=sdp_ratio,
noise_scale=noise,
noise_scale_w=noisew,
@@ -245,11 +229,34 @@ if __name__ == "__main__":
hps=loaded_models.models[model_id].hps,
net_g=loaded_models.models[model_id].net_g,
device=loaded_models.models[model_id].device,
emotion=emotion,
reference_audio=ref_audio,
)
)
audios.append(np.zeros(int(44100 * 0.2)))
audio = np.concatenate(audios)
audio = gradio.processing_utils.convert_to_16_bit_wav(audio)
audio = gradio.processing_utils.convert_to_16_bit_wav(audio)
else:
texts = cut_sent(text)
audios = []
with torch.no_grad():
for t in texts:
audios.append(
infer(
text=t,
sdp_ratio=sdp_ratio,
noise_scale=noise,
noise_scale_w=noisew,
length_scale=length,
sid=speaker_name,
language=language,
hps=loaded_models.models[model_id].hps,
net_g=loaded_models.models[model_id].net_g,
device=loaded_models.models[model_id].device,
emotion=emotion,
reference_audio=ref_audio,
)
)
audios.append(np.zeros(int(44100 * 0.2)))
audio = np.concatenate(audios)
audio = gradio.processing_utils.convert_to_16_bit_wav(audio)
with BytesIO() as wavContent:
wavfile.write(
wavContent, loaded_models.models[model_id].hps.data.sampling_rate, audio
@@ -258,9 +265,9 @@ if __name__ == "__main__":
return response
@app.post("/voice")
def voice(
async def voice(
request: Request, # fastapi自动注入
text: Text,
text: str = Form(...),
model_id: int = Query(..., description="模型ID"), # 模型序号
speaker_name: str = Query(
None, description="说话人名"
@@ -273,13 +280,14 @@ if __name__ == "__main__":
language: str = Query(None, description="语言"), # 若不指定使用语言则使用默认值
auto_translate: bool = Query(False, description="自动翻译"),
auto_split: bool = Query(False, description="自动切分"),
emotion: Optional[int] = Query(None, description="emo"),
reference_audio: UploadFile = File(None),
):
"""语音接口"""
text = text.text
"""语音接口若需要上传参考音频请仅使用post请求"""
logger.info(
f"{request.client.host}:{request.client.port}/voice { unquote(str(request.query_params) )} text={text}"
)
return _voice(
return await _voice(
text=text,
model_id=model_id,
speaker_name=speaker_name,
@@ -291,10 +299,12 @@ if __name__ == "__main__":
language=language,
auto_translate=auto_translate,
auto_split=auto_split,
emotion=emotion,
reference_audio=reference_audio,
)
@app.get("/voice")
def voice(
async def voice(
request: Request, # fastapi自动注入
text: str = Query(..., description="输入文字"),
model_id: int = Query(..., description="模型ID"), # 模型序号
@@ -309,12 +319,13 @@ if __name__ == "__main__":
language: str = Query(None, description="语言"), # 若不指定使用语言则使用默认值
auto_translate: bool = Query(False, description="自动翻译"),
auto_split: bool = Query(False, description="自动切分"),
emotion: Optional[int] = Query(None, description="emo"),
):
"""语音接口"""
logger.info(
f"{request.client.host}:{request.client.port}/voice { unquote(str(request.query_params) )}"
)
return _voice(
return await _voice(
text=text,
model_id=model_id,
speaker_name=speaker_name,
@@ -326,6 +337,7 @@ if __name__ == "__main__":
language=language,
auto_translate=auto_translate,
auto_split=auto_split,
emotion=emotion,
)
@app.get("/models/info")