Spaces:
Runtime error
Runtime error
File size: 1,343 Bytes
6c4b607 f61c6a5 b7dc427 fa29ba0 da77f7a 1a6d216 df02cd1 1a6d216 b7dc427 1a6d216 f61c6a5 1a6d216 bd8aa61 7976529 1a6d216 da77f7a 378bf7d da77f7a f61c6a5 378bf7d e0cb517 403610d e2e80ec f61c6a5 a887dc5 f61c6a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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 StreamingResponse
import httpx
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, type: int, lt: str | None = None, offset: int = 0):
query = q.lower()
xss = nh3.clean(query)
data = await doc_searcher.search(text=xss,type=type,law_type=lt,offset=offset)
return data
@app.get("/api/suggestions")
async def get_suggestions(q: str, type: int):
query = q.lower()
xss = nh3.clean(query)
data = await suggestion_searcher.search(text=xss,type=type)
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) |