From 5f6dcf9f2e6c28feeb45d32a2ebc998eb019f05a Mon Sep 17 00:00:00 2001 From: sabaimran Date: Sat, 16 Dec 2023 09:18:56 +0530 Subject: [PATCH] Add a rate limiter for the transcribe API endpoint --- src/khoj/interface/web/chat.html | 2 ++ src/khoj/routers/api.py | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/khoj/interface/web/chat.html b/src/khoj/interface/web/chat.html index e85759fb..aa6bd4b9 100644 --- a/src/khoj/interface/web/chat.html +++ b/src/khoj/interface/web/chat.html @@ -642,6 +642,8 @@ To get started, just start typing below. You can also type / to see a list of co flashStatusInChatInput("⛔️ Configure speech-to-text model on server.") } else if (err.status === 422) { flashStatusInChatInput("⛔️ Audio file to large to process.") + } else if (err.status === 429) { + flashStatusInChatInput("⛔️ " + err.statusText); } else { flashStatusInChatInput("⛔️ Failed to transcribe audio.") } diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index 65516a9c..a4063afa 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -606,7 +606,13 @@ async def chat_options( @api.post("/transcribe") @requires(["authenticated"]) -async def transcribe(request: Request, common: CommonQueryParams, file: UploadFile = File(...)): +async def transcribe( + request: Request, + common: CommonQueryParams, + file: UploadFile = File(...), + rate_limiter_per_minute=Depends(ApiUserRateLimiter(requests=1, subscribed_requests=10, window=60)), + rate_limiter_per_day=Depends(ApiUserRateLimiter(requests=10, subscribed_requests=600, window=60 * 60 * 24)), +): user: KhojUser = request.user.object audio_filename = f"{user.uuid}-{str(uuid.uuid4())}.webm" user_message: str = None