From f5444c208d0e725def7644fe801a56d44e5d9abe Mon Sep 17 00:00:00 2001 From: Sora <654163754@qq.com> Date: Tue, 28 Nov 2023 18:15:28 +0800 Subject: [PATCH] =?UTF-8?q?fix=20server=5Ffastapi.py:=20=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E7=9A=84BytesIO()=E6=9C=AA=E8=A2=AB=E5=9B=9E?= =?UTF-8?q?=E6=94=B6=E5=AF=BC=E8=87=B4=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E9=80=82=E9=85=8D310?= =?UTF-8?q?=E4=BB=A5=E4=B8=8B=E7=89=88=E6=9C=ACpython=20(#202)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server_fastapi.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/server_fastapi.py b/server_fastapi.py index 2e786b6..562fad1 100644 --- a/server_fastapi.py +++ b/server_fastapi.py @@ -19,7 +19,7 @@ import torch import webbrowser import psutil import GPUtil -from typing import Dict, Optional, List, Set +from typing import Dict, Optional, List, Set, Union import os from tools.log import logger from urllib.parse import unquote @@ -188,7 +188,7 @@ if __name__ == "__main__": language: str, auto_translate: bool, auto_split: bool, - ) -> Response | Dict[str, any]: + ) -> Union[Response, Dict[str, any]]: # 检查模型是否存在 if model_id not in loaded_models.models.keys(): return {"status": 10, "detail": f"模型model_id={model_id}未加载"} @@ -243,12 +243,12 @@ if __name__ == "__main__": audios.append(np.zeros(int(44100 * 0.2))) audio = np.concatenate(audios) audio = gradio.processing_utils.convert_to_16_bit_wav(audio) - wavContent = BytesIO() - wavfile.write( - wavContent, loaded_models.models[model_id].hps.data.sampling_rate, audio - ) - response = Response(content=wavContent.getvalue(), media_type="audio/wav") - return response + with BytesIO() as wavContent: + wavfile.write( + wavContent, loaded_models.models[model_id].hps.data.sampling_rate, audio + ) + response = Response(content=wavContent.getvalue(), media_type="audio/wav") + return response @app.post("/voice") def voice(