Download services/usage_service.py from KevsOno/Kevs_Digital_Business_Intelligence: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/KevsOno/Kevs_Digital_Business_Intelligence/resolve/main/services/usage_service.py
- Command line
-
hf download hf://KevsOno/Kevs_Digital_Business_Intelligence/services/usage_service.py
-
curl -L -o usage_service.py https://huggingface.co/KevsOno/Kevs_Digital_Business_Intelligence/resolve/main/services/usage_service.py
1.09 kB
| from fastapi import HTTPException | |
| from app.core.clients import supabase | |
| from app.core.config import settings | |
| FREE_LIMIT = settings.FREE_LIMIT | |
| def check_usage_gate(user_id: str): | |
| result = ( | |
| supabase.table("user_usage") | |
| .select("request_count, is_premium") | |
| .eq("user_id", user_id) | |
| .execute() | |
| ) | |
| if not result.data: | |
| supabase.table("user_usage").insert({ | |
| "user_id": user_id, | |
| "request_count": 1, "is_premium": False | |
| }).execute() | |
| return { | |
| "remaining": FREE_LIMIT - 1, | |
| "limit": FREE_LIMIT | |
| } | |
| row = result.data[0] | |
| if ( | |
| not row["is_premium"] | |
| and row["request_count"] >= FREE_LIMIT | |
| ): | |
| raise HTTPException( | |
| status_code=402, | |
| detail="Free limit exceeded" | |
| ) | |
| supabase.table("user_usage").update({ | |
| "request_count": row["request_count"] + 1 | |
| }).eq("user_id", user_id).execute() | |
| return { "remaining": max(0, FREE_LIMIT - (row["request_count"] + 1)), | |
| "limit": FREE_LIMIT | |
| } |