Merge pull request #213 from jiangyuxiaoxiao/master
update server_fastapi.py: 添加2.1模型推理支持。
This commit is contained in:
@@ -17,7 +17,7 @@ def get_emo(path):
|
|||||||
wav, sr = librosa.load(path, 16000)
|
wav, sr = librosa.load(path, 16000)
|
||||||
device = config.bert_gen_config.device
|
device = config.bert_gen_config.device
|
||||||
return process_func(
|
return process_func(
|
||||||
np.expand_dims(wav, 0).astype(np.float),
|
np.expand_dims(wav, 0).astype(np.float64),
|
||||||
sr,
|
sr,
|
||||||
model,
|
model,
|
||||||
processor,
|
processor,
|
||||||
|
|||||||
@@ -5,11 +5,10 @@ import logging
|
|||||||
import gc
|
import gc
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from pydantic import BaseModel
|
|
||||||
import gradio
|
import gradio
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import utils
|
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.responses import Response, FileResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
@@ -180,10 +179,7 @@ if __name__ == "__main__":
|
|||||||
async def index():
|
async def index():
|
||||||
return FileResponse("./Web/index.html")
|
return FileResponse("./Web/index.html")
|
||||||
|
|
||||||
class Text(BaseModel):
|
async def _voice(
|
||||||
text: str
|
|
||||||
|
|
||||||
def _voice(
|
|
||||||
text: str,
|
text: str,
|
||||||
model_id: int,
|
model_id: int,
|
||||||
speaker_name: str,
|
speaker_name: str,
|
||||||
@@ -195,7 +191,10 @@ if __name__ == "__main__":
|
|||||||
language: str,
|
language: str,
|
||||||
auto_translate: bool,
|
auto_translate: bool,
|
||||||
auto_split: bool,
|
auto_split: bool,
|
||||||
|
emotion: Optional[int] = None,
|
||||||
|
reference_audio=None,
|
||||||
) -> Union[Response, Dict[str, any]]:
|
) -> Union[Response, Dict[str, any]]:
|
||||||
|
"""TTS实现函数"""
|
||||||
# 检查模型是否存在
|
# 检查模型是否存在
|
||||||
if model_id not in loaded_models.models.keys():
|
if model_id not in loaded_models.models.keys():
|
||||||
return {"status": 10, "detail": f"模型model_id={model_id}未加载"}
|
return {"status": 10, "detail": f"模型model_id={model_id}未加载"}
|
||||||
@@ -214,28 +213,12 @@ if __name__ == "__main__":
|
|||||||
language = loaded_models.models[model_id].language
|
language = loaded_models.models[model_id].language
|
||||||
if auto_translate:
|
if auto_translate:
|
||||||
text = trans.translate(Sentence=text, to_Language=language.lower())
|
text = trans.translate(Sentence=text, to_Language=language.lower())
|
||||||
if not auto_split:
|
if reference_audio is not None:
|
||||||
with torch.no_grad():
|
with BytesIO(await reference_audio.read()) as ref_audio:
|
||||||
audio = infer(
|
if not auto_split:
|
||||||
text=text,
|
with torch.no_grad():
|
||||||
sdp_ratio=sdp_ratio,
|
audio = infer(
|
||||||
noise_scale=noise,
|
text=text,
|
||||||
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,
|
|
||||||
sdp_ratio=sdp_ratio,
|
sdp_ratio=sdp_ratio,
|
||||||
noise_scale=noise,
|
noise_scale=noise,
|
||||||
noise_scale_w=noisew,
|
noise_scale_w=noisew,
|
||||||
@@ -245,11 +228,34 @@ if __name__ == "__main__":
|
|||||||
hps=loaded_models.models[model_id].hps,
|
hps=loaded_models.models[model_id].hps,
|
||||||
net_g=loaded_models.models[model_id].net_g,
|
net_g=loaded_models.models[model_id].net_g,
|
||||||
device=loaded_models.models[model_id].device,
|
device=loaded_models.models[model_id].device,
|
||||||
|
emotion=emotion,
|
||||||
|
reference_audio=ref_audio,
|
||||||
)
|
)
|
||||||
)
|
audio = gradio.processing_utils.convert_to_16_bit_wav(audio)
|
||||||
audios.append(np.zeros(int(44100 * 0.2)))
|
else:
|
||||||
audio = np.concatenate(audios)
|
texts = cut_sent(text)
|
||||||
audio = gradio.processing_utils.convert_to_16_bit_wav(audio)
|
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:
|
with BytesIO() as wavContent:
|
||||||
wavfile.write(
|
wavfile.write(
|
||||||
wavContent, loaded_models.models[model_id].hps.data.sampling_rate, audio
|
wavContent, loaded_models.models[model_id].hps.data.sampling_rate, audio
|
||||||
@@ -258,9 +264,9 @@ if __name__ == "__main__":
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
@app.post("/voice")
|
@app.post("/voice")
|
||||||
def voice(
|
async def voice(
|
||||||
request: Request, # fastapi自动注入
|
request: Request, # fastapi自动注入
|
||||||
text: Text,
|
text: str = Form(...),
|
||||||
model_id: int = Query(..., description="模型ID"), # 模型序号
|
model_id: int = Query(..., description="模型ID"), # 模型序号
|
||||||
speaker_name: str = Query(
|
speaker_name: str = Query(
|
||||||
None, description="说话人名"
|
None, description="说话人名"
|
||||||
@@ -273,13 +279,14 @@ if __name__ == "__main__":
|
|||||||
language: str = Query(None, description="语言"), # 若不指定使用语言则使用默认值
|
language: str = Query(None, description="语言"), # 若不指定使用语言则使用默认值
|
||||||
auto_translate: bool = Query(False, description="自动翻译"),
|
auto_translate: bool = Query(False, description="自动翻译"),
|
||||||
auto_split: bool = Query(False, description="自动切分"),
|
auto_split: bool = Query(False, description="自动切分"),
|
||||||
|
emotion: Optional[int] = Query(None, description="emo"),
|
||||||
|
reference_audio: UploadFile = File(None),
|
||||||
):
|
):
|
||||||
"""语音接口"""
|
"""语音接口,若需要上传参考音频请仅使用post请求"""
|
||||||
text = text.text
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f"{request.client.host}:{request.client.port}/voice { unquote(str(request.query_params) )} text={text}"
|
f"{request.client.host}:{request.client.port}/voice { unquote(str(request.query_params) )} text={text}"
|
||||||
)
|
)
|
||||||
return _voice(
|
return await _voice(
|
||||||
text=text,
|
text=text,
|
||||||
model_id=model_id,
|
model_id=model_id,
|
||||||
speaker_name=speaker_name,
|
speaker_name=speaker_name,
|
||||||
@@ -291,10 +298,12 @@ if __name__ == "__main__":
|
|||||||
language=language,
|
language=language,
|
||||||
auto_translate=auto_translate,
|
auto_translate=auto_translate,
|
||||||
auto_split=auto_split,
|
auto_split=auto_split,
|
||||||
|
emotion=emotion,
|
||||||
|
reference_audio=reference_audio,
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.get("/voice")
|
@app.get("/voice")
|
||||||
def voice(
|
async def voice(
|
||||||
request: Request, # fastapi自动注入
|
request: Request, # fastapi自动注入
|
||||||
text: str = Query(..., description="输入文字"),
|
text: str = Query(..., description="输入文字"),
|
||||||
model_id: int = Query(..., description="模型ID"), # 模型序号
|
model_id: int = Query(..., description="模型ID"), # 模型序号
|
||||||
@@ -309,12 +318,13 @@ if __name__ == "__main__":
|
|||||||
language: str = Query(None, description="语言"), # 若不指定使用语言则使用默认值
|
language: str = Query(None, description="语言"), # 若不指定使用语言则使用默认值
|
||||||
auto_translate: bool = Query(False, description="自动翻译"),
|
auto_translate: bool = Query(False, description="自动翻译"),
|
||||||
auto_split: bool = Query(False, description="自动切分"),
|
auto_split: bool = Query(False, description="自动切分"),
|
||||||
|
emotion: Optional[int] = Query(None, description="emo"),
|
||||||
):
|
):
|
||||||
"""语音接口"""
|
"""语音接口"""
|
||||||
logger.info(
|
logger.info(
|
||||||
f"{request.client.host}:{request.client.port}/voice { unquote(str(request.query_params) )}"
|
f"{request.client.host}:{request.client.port}/voice { unquote(str(request.query_params) )}"
|
||||||
)
|
)
|
||||||
return _voice(
|
return await _voice(
|
||||||
text=text,
|
text=text,
|
||||||
model_id=model_id,
|
model_id=model_id,
|
||||||
speaker_name=speaker_name,
|
speaker_name=speaker_name,
|
||||||
@@ -326,6 +336,7 @@ if __name__ == "__main__":
|
|||||||
language=language,
|
language=language,
|
||||||
auto_translate=auto_translate,
|
auto_translate=auto_translate,
|
||||||
auto_split=auto_split,
|
auto_split=auto_split,
|
||||||
|
emotion=emotion,
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.get("/models/info")
|
@app.get("/models/info")
|
||||||
|
|||||||
Reference in New Issue
Block a user