Ilyas KHIAT commited on
Commit
430f6e8
·
1 Parent(s): b66e2f4

ADDED AUTH

Browse files
Files changed (3) hide show
  1. auth.py +13 -0
  2. main.py +30 -2
  3. rag.py +0 -1
auth.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import HTTPException, Security
2
+ from fastapi.security import api_key
3
+
4
+ api_key_header = api_key.APIKeyHeader(name="X-API-KEY")
5
+
6
+
7
+ async def validate_api_key(key: str = Security(api_key_header)):
8
+ if key != settings.API_KEY:
9
+ raise HTTPException(
10
+ status_code=401, detail="Unauthorized - API Key is wrong"
11
+ )
12
+ return None
13
+
main.py CHANGED
@@ -1,4 +1,5 @@
1
- from fastapi import FastAPI, HTTPException, UploadFile, File
 
2
  from pydantic import BaseModel, Json
3
  from uuid import uuid4, UUID
4
  from typing import Optional
@@ -38,7 +39,34 @@ if index_name not in existing_indexes:
38
 
39
  index = pc.Index(index_name)
40
 
41
- app = FastAPI()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  class StyleWriter(BaseModel):
44
  style: str
 
1
+ from fastapi import FastAPI, HTTPException, UploadFile, File,Request,Depends,status
2
+ from fastapi.security import OAuth2PasswordBearer
3
  from pydantic import BaseModel, Json
4
  from uuid import uuid4, UUID
5
  from typing import Optional
 
39
 
40
  index = pc.Index(index_name)
41
 
42
+
43
+
44
+ api_keys = [os.environ.get("FASTAPI_API_KEY")]
45
+
46
+ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") # use token authentication
47
+
48
+
49
+ def api_key_auth(api_key: str = Depends(oauth2_scheme)):
50
+ if api_key not in api_keys:
51
+ raise HTTPException(
52
+ status_code=status.HTTP_401_UNAUTHORIZED,
53
+ detail="Forbidden"
54
+ )
55
+
56
+ app = FastAPI(dependencies=[Depends(api_key_auth)])
57
+
58
+ # FASTAPI_KEY_NAME = os.environ.get("FASTAPI_KEY_NAME")
59
+ # FASTAPI_API_KEY = os.environ.get("FASTAPI_API_KEY")
60
+
61
+
62
+ # @app.middleware("http")
63
+ # async def api_key_middleware(request: Request, call_next):
64
+ # if request.url.path not in ["/","/docs","/openapi.json"]:
65
+ # api_key = request.headers.get(FASTAPI_KEY_NAME)
66
+ # if api_key != FASTAPI_API_KEY:
67
+ # raise HTTPException(status_code=403, detail="invalid API key :/")
68
+ # response = await call_next(request)
69
+ # return response
70
 
71
  class StyleWriter(BaseModel):
72
  style: str
rag.py CHANGED
@@ -149,7 +149,6 @@ def prompt_reformatting(prompt:str,context,query:str,style="formel",tonality="ne
149
 
150
  docs_names = []
151
  for chunk in context:
152
- print(chunk.metadata)
153
  chunk_name = chunk.metadata["filename"]
154
  if chunk_name not in docs_names:
155
  docs_names.append(chunk_name)
 
149
 
150
  docs_names = []
151
  for chunk in context:
 
152
  chunk_name = chunk.metadata["filename"]
153
  if chunk_name not in docs_names:
154
  docs_names.append(chunk_name)