dzenzzz commited on
Commit
7f50106
·
1 Parent(s): 6c4b607

updates app

Browse files
Files changed (1) hide show
  1. app.py +8 -25
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.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
 
@@ -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
- # @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
- # )
 
 
 
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