Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- rag_system/api.py +21 -1
- rag_system/config.py +2 -1
rag_system/api.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
import logging
|
| 2 |
import time
|
| 3 |
import uuid as _uuid
|
|
|
|
| 4 |
from contextlib import asynccontextmanager
|
|
|
|
| 5 |
from typing import Optional
|
| 6 |
|
| 7 |
-
from fastapi import FastAPI, HTTPException, UploadFile, File, BackgroundTasks, Body
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
from fastapi.responses import StreamingResponse
|
|
|
|
| 10 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 11 |
|
| 12 |
from .config import get_settings
|
|
@@ -214,6 +217,23 @@ app.add_middleware(
|
|
| 214 |
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
| 215 |
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
@app.middleware("http")
|
| 218 |
async def add_process_time_header(request, call_next):
|
| 219 |
start = time.monotonic()
|
|
|
|
| 1 |
import logging
|
| 2 |
import time
|
| 3 |
import uuid as _uuid
|
| 4 |
+
from hmac import compare_digest
|
| 5 |
from contextlib import asynccontextmanager
|
| 6 |
+
from pathlib import Path
|
| 7 |
from typing import Optional
|
| 8 |
|
| 9 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File, BackgroundTasks, Body, Request
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
from fastapi.responses import StreamingResponse
|
| 12 |
+
from fastapi.responses import JSONResponse
|
| 13 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 14 |
|
| 15 |
from .config import get_settings
|
|
|
|
| 217 |
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
| 218 |
|
| 219 |
|
| 220 |
+
@app.middleware("http")
|
| 221 |
+
async def require_bearer_token(request: Request, call_next):
|
| 222 |
+
if request.method == "OPTIONS" or not settings.api_bearer_token:
|
| 223 |
+
return await call_next(request)
|
| 224 |
+
|
| 225 |
+
auth_header = request.headers.get("authorization", "")
|
| 226 |
+
scheme, _, token = auth_header.partition(" ")
|
| 227 |
+
if scheme.lower() != "bearer" or not token or not compare_digest(token.strip(), settings.api_bearer_token):
|
| 228 |
+
return JSONResponse(
|
| 229 |
+
status_code=401,
|
| 230 |
+
content={"detail": "Unauthorized"},
|
| 231 |
+
headers={"WWW-Authenticate": "Bearer"},
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
return await call_next(request)
|
| 235 |
+
|
| 236 |
+
|
| 237 |
@app.middleware("http")
|
| 238 |
async def add_process_time_header(request, call_next):
|
| 239 |
start = time.monotonic()
|
rag_system/config.py
CHANGED
|
@@ -53,8 +53,9 @@ class Settings(BaseSettings):
|
|
| 53 |
#api
|
| 54 |
api_title: str = "Production RAG API"
|
| 55 |
api_version: str = "1.0.0"
|
| 56 |
-
cors_origins: list[str] = ["
|
| 57 |
rate_limit_per_minute: int = 60
|
|
|
|
| 58 |
|
| 59 |
#guardrails
|
| 60 |
guardrails_use_llama_guard: bool = True
|
|
|
|
| 53 |
#api
|
| 54 |
api_title: str = "Production RAG API"
|
| 55 |
api_version: str = "1.0.0"
|
| 56 |
+
cors_origins: list[str] = ["https://seerag.vercel.app"]
|
| 57 |
rate_limit_per_minute: int = 60
|
| 58 |
+
api_bearer_token: str | None = None
|
| 59 |
|
| 60 |
#guardrails
|
| 61 |
guardrails_use_llama_guard: bool = True
|