dzenzzz commited on
Commit
0af159c
·
verified ·
1 Parent(s): c01ec62

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import nh3
2
+ from fastapi import FastAPI, Request
3
+ from doc_searcher import DocSearcher
4
+ from suggestion_searcher import SuggestionSearcher
5
+ from huggingface_hub import login
6
+ from config import HUGGING_FACE_API_KEY, COLLECTION_NAME, API_KEY, COLLECTION_NAME_SUGGESTION
7
+ from fastapi.responses import JSONResponse
8
+
9
+ login(HUGGING_FACE_API_KEY)
10
+
11
+ app = FastAPI()
12
+
13
+ doc_searcher = DocSearcher(collection_name=COLLECTION_NAME)
14
+ suggestion_searcher = SuggestionSearcher(collection_name=COLLECTION_NAME_SUGGESTION)
15
+
16
+ ALLOWED_API_KEY = str(API_KEY)
17
+
18
+ @app.get("/api/search")
19
+ async def search(q: str):
20
+ query = q.lower()
21
+ xss = nh3.clean(query)
22
+ data = await doc_searcher.search(text=xss)
23
+ return data
24
+
25
+ @app.get("/api/suggestions")
26
+ async def get_suggestions(q: str, type: int):
27
+ query = q.lower()
28
+ xss = nh3.clean(query)
29
+ data = await suggestion_searcher.search(text=xss,type=type)
30
+ return data
31
+
32
+
33
+ # @app.middleware("http")
34
+ # async def api_key_authentication(request: Request, call_next):
35
+ # api_key = request.headers.get("X-API-KEY")
36
+ # if api_key != ALLOWED_API_KEY:
37
+ # return JSONResponse(
38
+ # status_code=403,
39
+ # content={"message": "Forbidden."}
40
+ # )
41
+
42
+ # return await call_next(request)