Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fast_inference_] 重构很多api_v2.py,make api great and greater again ! #945

Open
wants to merge 2 commits into
base: fast_inference_
Choose a base branch
from

Conversation

X-T-E-R
Copy link

@X-T-E-R X-T-E-R commented Apr 8, 2024

修改:

  1. 改用FileResponse(tmp_file_path, media_type=f"audio/{format}", filename=f"audio.{format}"),替代原本非流式的返回,支持更多格式,速度能更快,并且会避免占线
  2. 重写接口函数,避免无用代码
@APP.get("/tts")
@APP.post("/tts")
async def tts_get_endpoint(request: Request):
    # 尝试从JSON中获取数据,如果不是JSON,则从查询参数中获取
    if request.method == "GET":
        data = request.query_params
    else:
        data = await request.json()
  1. 利用上了 TTS_Request()类,并使用了更温和的更新规则
class TTS_Request(BaseModel):
    text: str = None
    ……

    def check(self):
        if (self.text_lang in [None, ""]) or self.text_lang.lower() not in tts_config.languages:
            self.text_lang = "auto"
        if (self.prompt_lang in [None, ""]) or self.prompt_lang.lower() not in tts_config.languages:
            self.prompt_lang = "auto"
            
        if self.text in [None, ""]:
            return JSONResponse(status_code=400, content={"message": "text is required"})
        if self.ref_audio_path in [None, ""]:
            # 这里没加验证存在性,TODO
            return JSONResponse(status_code=400, content={"message": "ref_audio_path is required"})
        if self.streaming_mode and self.media_type not in ["wav", "raw", "ogg", "aac"]:
            return JSONResponse(status_code=400, content={"message": f"media_type {self.media_type} is not supported in streaming mode"})
        if self.text_split_method not in cut_method_names:
            return JSONResponse(status_code=400, content={"message": f"text_split_method:{self.text_split_method} is not supported"})
        return None

  1. 重写了更新函数,节省时间,节省生命。没给值就继承默认值
def update(self, req:dict):
        for key in req:
            if hasattr(self, key):
                type_ = type(getattr(self, key))
                value = unquote(str(req[key]))
                if type_ == bool:
                    value = value.lower() in ["true", "1"]
                elif type_ == int:
                    value = int(value)
                elif type_ == float:
                    value = float(value)
                setattr(self, key, value)
                
  1. 将两个language默认值设置为auto, 因此仅需要2个必要参数:
"text": "",                   # str.(required) text to be synthesized
"ref_audio_path": "",         # str.(required) reference audio path.

@X-T-E-R
Copy link
Author

X-T-E-R commented Apr 8, 2024

其中部分代码来自GSVI:https://github.com/X-T-E-R/GPT-SoVITS-Inference 已标注

可能存在一些bug,请试一试。

@X-T-E-R X-T-E-R changed the title [fast_inference_] 重构很多api_v2.py,make api greater again ! [fast_inference_] 重构很多api_v2.py,make api great and greater again ! Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant