Add a rate limiter for the transcribe API endpoint

This commit is contained in:
sabaimran
2023-12-16 09:18:56 +05:30
parent 73a107690d
commit 5f6dcf9f2e
2 changed files with 9 additions and 1 deletions

View File

@@ -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.") flashStatusInChatInput("⛔️ Configure speech-to-text model on server.")
} else if (err.status === 422) { } else if (err.status === 422) {
flashStatusInChatInput("⛔️ Audio file to large to process.") flashStatusInChatInput("⛔️ Audio file to large to process.")
} else if (err.status === 429) {
flashStatusInChatInput("⛔️ " + err.statusText);
} else { } else {
flashStatusInChatInput("⛔️ Failed to transcribe audio.") flashStatusInChatInput("⛔️ Failed to transcribe audio.")
} }

View File

@@ -606,7 +606,13 @@ async def chat_options(
@api.post("/transcribe") @api.post("/transcribe")
@requires(["authenticated"]) @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 user: KhojUser = request.user.object
audio_filename = f"{user.uuid}-{str(uuid.uuid4())}.webm" audio_filename = f"{user.uuid}-{str(uuid.uuid4())}.webm"
user_message: str = None user_message: str = None