Spaces:
Runtime error

sq / app.py
dzenzzz's picture
adds suggestions
f61c6a5
raw
history blame
1.17 kB
import nh3
from fastapi import FastAPI, Request
from doc_searcher import DocSearcher
from suggestion_searcher import SuggestionSearcher
from huggingface_hub import login
from config import HUGGING_FACE_API_KEY, COLLECTION_NAME, API_KEY, COLLECTION_NAME_SUGGESTION
from fastapi.responses import JSONResponse
login(HUGGING_FACE_API_KEY)
app = FastAPI()
doc_searcher = DocSearcher(collection_name=COLLECTION_NAME)
suggestion_searcher = SuggestionSearcher(collection_name=COLLECTION_NAME_SUGGESTION)
ALLOWED_API_KEY = str(API_KEY)
@app.get("/api/search")
async def search(q: str):
xss = nh3.clean(q)
data = await doc_searcher.search(text=xss)
return data
@app.get("/api/suggestions")
async def get_suggestions(q: str):
xss = nh3.clean(q)
data = await suggestion_searcher.search(text=xss)
return data
# @app.middleware("http")
# async def api_key_authentication(request: Request, call_next):
# api_key = request.headers.get("X-API-KEY")
# if api_key != ALLOWED_API_KEY:
# return JSONResponse(
# status_code=403,
# content={"message": "Forbidden."}
# )
# return await call_next(request)