Spaces:
Sleeping
Sleeping
updates app
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ 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,32 +14,31 @@ app = FastAPI()
|
|
| 14 |
neural_searcher = NeuralSearcher(collection_name=COLLECTION_NAME)
|
| 15 |
|
| 16 |
REQUEST_TIMEOUT_ERROR = 30
|
| 17 |
-
|
| 18 |
-
ALLOWED_API_KEY = API_KEY
|
| 19 |
|
| 20 |
|
| 21 |
@app.get("/api/search")
|
| 22 |
async def search(q: str):
|
| 23 |
-
data =
|
| 24 |
return data
|
| 25 |
|
|
|
|
| 26 |
@app.middleware("http")
|
| 27 |
-
async def
|
| 28 |
-
|
| 29 |
|
|
|
|
| 30 |
if api_key != ALLOWED_API_KEY:
|
| 31 |
-
|
| 32 |
|
| 33 |
-
return await call_next(request)
|
| 34 |
-
|
| 35 |
-
@app.middleware("http")
|
| 36 |
-
async def timeout_middleware(request: Request, call_next):
|
| 37 |
try:
|
| 38 |
-
start_time = time.time()
|
| 39 |
return await asyncio.wait_for(call_next(request), timeout=REQUEST_TIMEOUT_ERROR)
|
| 40 |
-
|
| 41 |
except asyncio.TimeoutError:
|
| 42 |
process_time = time.time() - start_time
|
| 43 |
-
return JSONResponse(
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
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 |
+
data = await neural_searcher.search(text=q)
|
| 23 |
return data
|
| 24 |
|
| 25 |
+
|
| 26 |
@app.middleware("http")
|
| 27 |
+
async def combined_middleware(request: Request, call_next):
|
| 28 |
+
start_time = time.time()
|
| 29 |
|
| 30 |
+
api_key = request.headers.get("X-API-KEY", "")
|
| 31 |
if api_key != ALLOWED_API_KEY:
|
| 32 |
+
return JSONResponse(status_code=403, content={"detail": "Access denied."})
|
| 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 |
+
)
|