fix server_fastapi.py: 解决可能的BytesIO()未被回收导致内存泄露的问题,适配310以下版本python (#202)

This commit is contained in:
Sora
2023-11-28 18:15:28 +08:00
committed by GitHub
parent edb5d05e34
commit f5444c208d

View File

@@ -19,7 +19,7 @@ import torch
import webbrowser import webbrowser
import psutil import psutil
import GPUtil import GPUtil
from typing import Dict, Optional, List, Set from typing import Dict, Optional, List, Set, Union
import os import os
from tools.log import logger from tools.log import logger
from urllib.parse import unquote from urllib.parse import unquote
@@ -188,7 +188,7 @@ if __name__ == "__main__":
language: str, language: str,
auto_translate: bool, auto_translate: bool,
auto_split: bool, auto_split: bool,
) -> Response | Dict[str, any]: ) -> Union[Response, Dict[str, any]]:
# 检查模型是否存在 # 检查模型是否存在
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}未加载"}
@@ -243,12 +243,12 @@ if __name__ == "__main__":
audios.append(np.zeros(int(44100 * 0.2))) audios.append(np.zeros(int(44100 * 0.2)))
audio = np.concatenate(audios) audio = np.concatenate(audios)
audio = gradio.processing_utils.convert_to_16_bit_wav(audio) audio = gradio.processing_utils.convert_to_16_bit_wav(audio)
wavContent = BytesIO() 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
) )
response = Response(content=wavContent.getvalue(), media_type="audio/wav") response = Response(content=wavContent.getvalue(), media_type="audio/wav")
return response return response
@app.post("/voice") @app.post("/voice")
def voice( def voice(