From 1b040143eb14d5344ca0d83333ca7b9ce728b71f Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 5 Feb 2024 22:57:24 -0800 Subject: [PATCH] feat: cache request body --- backend/apps/openai/main.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/apps/openai/main.py b/backend/apps/openai/main.py index 0e6bfb26..ee83979a 100644 --- a/backend/apps/openai/main.py +++ b/backend/apps/openai/main.py @@ -81,10 +81,15 @@ async def speech(request: Request, user=Depends(get_current_user)): body = await request.body() - filename = hashlib.sha256(body).hexdigest() + ".mp3" + print(body) + print(type(body)) + + name = hashlib.sha256(body).hexdigest() + SPEECH_CACHE_DIR = Path(CACHE_DIR).joinpath("./audio/speech/") SPEECH_CACHE_DIR.mkdir(parents=True, exist_ok=True) - file_path = SPEECH_CACHE_DIR.joinpath(filename) + file_path = SPEECH_CACHE_DIR.joinpath(f"{name}.mp3") + file_body_path = SPEECH_CACHE_DIR.joinpath(f"{name}.json") print(file_path) @@ -114,6 +119,9 @@ async def speech(request: Request, user=Depends(get_current_user)): for chunk in r.iter_content(chunk_size=8192): f.write(chunk) + with open(file_body_path, "w") as f: + json.dump(json.loads(body.decode("utf-8")), f) + # Return the saved file return FileResponse(file_path)