Spaces:
Runtime error
Runtime error
Download app.py from RDeepak/RAG-Embedding-Search-EPs: direct link, hf CLI and curl.
- Browser
- Download file 2.49 kB
-
https://huggingface.co/spaces/RDeepak/RAG-Embedding-Search-EPs/resolve/main/app.py
- Command line
-
hf download hf://spaces/RDeepak/RAG-Embedding-Search-EPs/app.py
-
curl -L -o app.py https://huggingface.co/spaces/RDeepak/RAG-Embedding-Search-EPs/resolve/main/app.py
2.49 kB
| # ------------------ ::IMPORT MODULES, FILES, ETC.:: ----------------------- | |
| import os | |
| import orjson | |
| from dotenv import load_dotenv | |
| from fastapi import ( | |
| FastAPI, | |
| Response, | |
| HTTPException, | |
| Depends | |
| ) | |
| from fastapi.security.api_key import APIKeyHeader | |
| from src.pipeline.inference_pipeline import InferenceEpsPipeline | |
| # ---------------------------- ::END:: -------------------------------------- | |
| # -------------------------- ::ENV VARIABLES:: ------------------------------ | |
| load_dotenv() | |
| INFERENCE_KEY = os.getenv("INFERENCE_KEY") | |
| # ---------------------------- ::END:: -------------------------------------- | |
| # ------------------- ::CLASSES INITIALIZATION:: ---------------------------- | |
| app = FastAPI() | |
| inference_pls = InferenceEpsPipeline() | |
| api_key_header = APIKeyHeader(name="Authorization", auto_error=False) | |
| # ---------------------------- ::END:: -------------------------------------- | |
| # ------------------------------ ::FUNCTION:: ------------------------------- | |
| def get_api_key(api_key: str = Depends(api_key_header)): | |
| if api_key == INFERENCE_KEY: | |
| return api_key | |
| else: | |
| raise HTTPException(status_code=401, detail="Invalid API key") | |
| # ---------------------------- ::END:: -------------------------------------- | |
| # ---------------------------- ::ROUTES:: ----------------------------------- | |
| def home(): | |
| return {"message": "Serving"} | |
| # ---------------------------- ::END:: -------------------------------------- | |
| # ---------------------------- ::APIs:: ------------------------------------- | |
| def get_vector_ebd(query: str, api_key: str = Depends(get_api_key)): | |
| ebd = inference_pls.ebd_ep(query) | |
| if isinstance(ebd, list): | |
| json_data = orjson.dumps({"embedding": ebd}).decode("utf-8") | |
| return Response(content=json_data, media_type="application/json") | |
| if ebd == 500: | |
| raise HTTPException(status_code=500, detail="Error! Server error") | |
| def get_recommendations(query: str, api_key: str = Depends(get_api_key)): | |
| ebd = inference_pls.ebd_ep(query) | |
| data = inference_pls.vs_ep(ebd) | |
| if isinstance(data, list): | |
| json_data = orjson.dumps(data).decode("utf-8") | |
| return Response(content=json_data, media_type="application/json") | |
| if ebd == 500: | |
| raise HTTPException(status_code=500, detail="Error! Server error") | |
| # ---------------------------- ::END:: -------------------------------------- | |