Spaces:
Sleeping
Sleeping
added middle ware to handle larger file sized
Browse files
app.py
CHANGED
|
@@ -9,6 +9,9 @@ from deepgram import DeepgramClient, PrerecordedOptions
|
|
| 9 |
from typing import List, Dict
|
| 10 |
from fastapi.responses import PlainTextResponse
|
| 11 |
import re
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
# Load environment variables from .env file
|
|
@@ -290,6 +293,18 @@ app.add_middleware(
|
|
| 290 |
allow_headers=["*"],
|
| 291 |
)
|
| 292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
@app.get("/", tags=["Home"])
|
| 294 |
def api_home():
|
| 295 |
return {'detail': 'Welcome to FastAPI TextGen Tutorial!'}
|
|
|
|
| 9 |
from typing import List, Dict
|
| 10 |
from fastapi.responses import PlainTextResponse
|
| 11 |
import re
|
| 12 |
+
from starlette.middleware.base import BaseHTTPMiddleware
|
| 13 |
+
from starlette.requests import Request
|
| 14 |
+
from starlette.responses import Response
|
| 15 |
|
| 16 |
|
| 17 |
# Load environment variables from .env file
|
|
|
|
| 293 |
allow_headers=["*"],
|
| 294 |
)
|
| 295 |
|
| 296 |
+
# Middleware to handle large request body size (e.g., up to 50MB)
|
| 297 |
+
class LargeRequestMiddleware(BaseHTTPMiddleware):
|
| 298 |
+
async def dispatch(self, request: Request, call_next):
|
| 299 |
+
request_body_size = request.headers.get('content-length')
|
| 300 |
+
if request_body_size and int(request_body_size) > 50 * 1024 * 1024: # 50MB limit
|
| 301 |
+
return Response("Request body too large", status_code=413)
|
| 302 |
+
response = await call_next(request)
|
| 303 |
+
return response
|
| 304 |
+
|
| 305 |
+
# Add LargeRequestMiddleware to app
|
| 306 |
+
app.add_middleware(LargeRequestMiddleware)
|
| 307 |
+
|
| 308 |
@app.get("/", tags=["Home"])
|
| 309 |
def api_home():
|
| 310 |
return {'detail': 'Welcome to FastAPI TextGen Tutorial!'}
|