Spaces:
Sleeping
Sleeping
Download rate_limit.py from eishit32/UPNISO_API: direct link, hf CLI and curl.
- Browser
- Download file 605 Bytes
-
https://huggingface.co/spaces/eishit32/UPNISO_API/resolve/main/rate_limit.py
- Command line
-
hf download hf://spaces/eishit32/UPNISO_API/rate_limit.py
-
curl -L -o rate_limit.py https://huggingface.co/spaces/eishit32/UPNISO_API/resolve/main/rate_limit.py
605 Bytes
| import time | |
| from fastapi import HTTPException | |
| # Request memory | |
| RATE_LOG = {} | |
| def rate_limiter(key: str, limit: int, window: int = 60): | |
| """ | |
| key = api key | |
| limit = requests allowed | |
| window = time window (seconds) | |
| """ | |
| now = int(time.time()) | |
| if key not in RATE_LOG: | |
| RATE_LOG[key] = [] | |
| # Remove old requests | |
| RATE_LOG[key] = [t for t in RATE_LOG[key] if now - t < window] | |
| if len(RATE_LOG[key]) >= limit: | |
| raise HTTPException( | |
| status_code=429, | |
| detail="Rate limit exceeded. Upgrade plan." | |
| ) | |
| RATE_LOG[key].append(now) | |