Spaces:
Sleeping
Sleeping
updates app
Browse files
app.py
CHANGED
|
@@ -1,14 +1,9 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
-
import time
|
| 3 |
import nh3
|
| 4 |
-
from auth import APIKeyMiddleware
|
| 5 |
from fastapi import FastAPI, Request
|
| 6 |
-
from fastapi
|
| 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 |
|
|
@@ -16,10 +11,6 @@ app = FastAPI()
|
|
| 16 |
|
| 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)
|
|
@@ -27,18 +18,10 @@ async def search(q: str):
|
|
| 27 |
return data
|
| 28 |
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 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 |
-
# )
|
|
|
|
|
|
|
|
|
|
| 1 |
import nh3
|
|
|
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi import Request, HTTPException
|
|
|
|
| 4 |
from neural_searcher import NeuralSearcher
|
| 5 |
from huggingface_hub import login
|
| 6 |
from config import HUGGING_FACE_API_KEY, COLLECTION_NAME, API_KEY
|
|
|
|
| 7 |
|
| 8 |
login(HUGGING_FACE_API_KEY)
|
| 9 |
|
|
|
|
| 11 |
|
| 12 |
neural_searcher = NeuralSearcher(collection_name=COLLECTION_NAME)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.get("/api/search")
|
| 15 |
async def search(q: str):
|
| 16 |
xss = nh3.clean(q)
|
|
|
|
| 18 |
return data
|
| 19 |
|
| 20 |
|
| 21 |
+
@app.middleware("http")
|
| 22 |
+
async def combined_middleware(request: Request, call_next):
|
| 23 |
+
api_key = request.headers.get("X-API-KEY")
|
| 24 |
+
if api_key != API_KEY:
|
| 25 |
+
raise HTTPException(status_code=403, detail="Forbidden")
|
| 26 |
+
response = await call_next(request)
|
| 27 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|