Spaces:
Running
Running
quentinL52 commited on
Commit ·
a2e2b2d
1
Parent(s): bd5e16e
Add slowapi rate limiting and 800KB file size limit
Browse files- main.py +13 -5
- requirements.txt +2 -1
main.py
CHANGED
|
@@ -4,9 +4,12 @@ import tempfile
|
|
| 4 |
import uuid
|
| 5 |
from langtrace_python_sdk import inject_additional_attributes
|
| 6 |
|
| 7 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 8 |
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from src.services.cv_service import parse_cv
|
| 12 |
from langtrace_python_sdk import langtrace
|
|
@@ -41,6 +44,10 @@ app.add_middleware(
|
|
| 41 |
allow_headers=["*"],
|
| 42 |
)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
from pydantic import BaseModel
|
| 45 |
|
| 46 |
class HealthCheck(BaseModel):
|
|
@@ -51,16 +58,17 @@ async def health_check():
|
|
| 51 |
return HealthCheck()
|
| 52 |
|
| 53 |
@app.post("/parse-cv/", tags=["CV Parsing"])
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
):
|
| 57 |
"""
|
| 58 |
-
|
| 59 |
"""
|
| 60 |
if file.content_type != "application/pdf":
|
| 61 |
raise HTTPException(status_code=400, detail="PDF file required")
|
| 62 |
|
| 63 |
contents = await file.read()
|
|
|
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
|
|
|
| 4 |
import uuid
|
| 5 |
from langtrace_python_sdk import inject_additional_attributes
|
| 6 |
|
| 7 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, Request
|
| 8 |
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
+
from slowapi import Limiter, _rate_limit_exceeded_handler
|
| 11 |
+
from slowapi.util import get_remote_address
|
| 12 |
+
from slowapi.errors import RateLimitExceeded
|
| 13 |
|
| 14 |
from src.services.cv_service import parse_cv
|
| 15 |
from langtrace_python_sdk import langtrace
|
|
|
|
| 44 |
allow_headers=["*"],
|
| 45 |
)
|
| 46 |
|
| 47 |
+
limiter = Limiter(key_func=get_remote_address)
|
| 48 |
+
app.state.limiter = limiter
|
| 49 |
+
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
| 50 |
+
|
| 51 |
from pydantic import BaseModel
|
| 52 |
|
| 53 |
class HealthCheck(BaseModel):
|
|
|
|
| 58 |
return HealthCheck()
|
| 59 |
|
| 60 |
@app.post("/parse-cv/", tags=["CV Parsing"])
|
| 61 |
+
@limiter.limit("5/minute")
|
| 62 |
+
async def parse_cv_endpoint(request: Request, file: UploadFile = File(...)):
|
|
|
|
| 63 |
"""
|
| 64 |
+
Point d'entrée principal pour le parsing de CV.
|
| 65 |
"""
|
| 66 |
if file.content_type != "application/pdf":
|
| 67 |
raise HTTPException(status_code=400, detail="PDF file required")
|
| 68 |
|
| 69 |
contents = await file.read()
|
| 70 |
+
if len(contents) > 800 * 1024:
|
| 71 |
+
raise HTTPException(status_code=413, detail="File too large. Maximum size is 800 KB.")
|
| 72 |
|
| 73 |
|
| 74 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
requirements.txt
CHANGED
|
@@ -19,4 +19,5 @@ litellm
|
|
| 19 |
httpx==0.28.1
|
| 20 |
langtrace-python-sdk
|
| 21 |
sentence-transformers
|
| 22 |
-
setuptools<70.0.0
|
|
|
|
|
|
| 19 |
httpx==0.28.1
|
| 20 |
langtrace-python-sdk
|
| 21 |
sentence-transformers
|
| 22 |
+
setuptools<70.0.0
|
| 23 |
+
slowapi
|