Spaces:
Sleeping
Sleeping
updates app
Browse files- app.py +22 -22
- auth.py +12 -0
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
import asyncio
|
| 2 |
import time
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
from starlette.status import HTTP_504_GATEWAY_TIMEOUT
|
| 6 |
from neural_searcher import NeuralSearcher
|
| 7 |
from huggingface_hub import login
|
| 8 |
from config import HUGGING_FACE_API_KEY, COLLECTION_NAME, API_KEY
|
|
|
|
| 9 |
|
| 10 |
login(HUGGING_FACE_API_KEY)
|
| 11 |
|
|
@@ -14,31 +17,28 @@ app = FastAPI()
|
|
| 14 |
neural_searcher = NeuralSearcher(collection_name=COLLECTION_NAME)
|
| 15 |
|
| 16 |
REQUEST_TIMEOUT_ERROR = 30
|
| 17 |
-
ALLOWED_API_KEY = API_KEY
|
| 18 |
|
|
|
|
| 19 |
|
| 20 |
@app.get("/api/search")
|
| 21 |
async def search(q: str):
|
| 22 |
-
|
|
|
|
| 23 |
return data
|
| 24 |
|
| 25 |
|
| 26 |
-
@app.middleware("http")
|
| 27 |
-
async def combined_middleware(request: Request, call_next):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"processing_time": process_time
|
| 42 |
-
},
|
| 43 |
-
status_code=HTTP_504_GATEWAY_TIMEOUT
|
| 44 |
-
)
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import time
|
| 3 |
+
import nh3
|
| 4 |
+
from auth import APIKeyMiddleware
|
| 5 |
+
from fastapi import FastAPI, Request
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
from starlette.status import HTTP_504_GATEWAY_TIMEOUT
|
| 8 |
from neural_searcher import NeuralSearcher
|
| 9 |
from huggingface_hub import login
|
| 10 |
from config import HUGGING_FACE_API_KEY, COLLECTION_NAME, API_KEY
|
| 11 |
+
from fastapi.security.api_key import APIKey
|
| 12 |
|
| 13 |
login(HUGGING_FACE_API_KEY)
|
| 14 |
|
|
|
|
| 17 |
neural_searcher = NeuralSearcher(collection_name=COLLECTION_NAME)
|
| 18 |
|
| 19 |
REQUEST_TIMEOUT_ERROR = 30
|
|
|
|
| 20 |
|
| 21 |
+
app.add_middleware(APIKeyMiddleware)
|
| 22 |
|
| 23 |
@app.get("/api/search")
|
| 24 |
async def search(q: str):
|
| 25 |
+
xss = nh3.clean(q)
|
| 26 |
+
data = await neural_searcher.search(text=xss)
|
| 27 |
return data
|
| 28 |
|
| 29 |
|
| 30 |
+
# @app.middleware("http")
|
| 31 |
+
# async def combined_middleware(request: Request, call_next):
|
| 32 |
+
# start_time = time.time()
|
| 33 |
+
|
| 34 |
+
# try:
|
| 35 |
+
# return await asyncio.wait_for(call_next(request), timeout=REQUEST_TIMEOUT_ERROR)
|
| 36 |
+
# except asyncio.TimeoutError:
|
| 37 |
+
# process_time = time.time() - start_time
|
| 38 |
+
# return JSONResponse(
|
| 39 |
+
# content={
|
| 40 |
+
# "detail": "Request processing time exceeded limit",
|
| 41 |
+
# "processing_time": process_time
|
| 42 |
+
# },
|
| 43 |
+
# status_code=HTTP_504_GATEWAY_TIMEOUT
|
| 44 |
+
# )
|
|
|
|
|
|
|
|
|
|
|
|
auth.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from config import API_KEY
|
| 2 |
+
|
| 3 |
+
from fastapi import Request, HTTPException
|
| 4 |
+
from starlette.middleware.base import BaseHTTPMiddleware
|
| 5 |
+
|
| 6 |
+
class APIKeyMiddleware(BaseHTTPMiddleware):
|
| 7 |
+
async def dispatch(self, request: Request, call_next):
|
| 8 |
+
api_key = request.headers.get("X-API-KEY")
|
| 9 |
+
if api_key != API_KEY:
|
| 10 |
+
raise HTTPException(status_code=403, detail="Forbidden")
|
| 11 |
+
response = await call_next(request)
|
| 12 |
+
return response
|
requirements.txt
CHANGED
|
@@ -8,3 +8,5 @@ python-dotenv
|
|
| 8 |
qdrant-client
|
| 9 |
qdrant-client[fastembed]>=1.8.2
|
| 10 |
sentence-transformers
|
|
|
|
|
|
|
|
|
| 8 |
qdrant-client
|
| 9 |
qdrant-client[fastembed]>=1.8.2
|
| 10 |
sentence-transformers
|
| 11 |
+
fastapi-csrf-protect
|
| 12 |
+
nh3
|