Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,42 @@
|
|
1 |
-
from fastapi import FastAPI, Request, HTTPException, Depends
|
2 |
-
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
3 |
-
from pydantic import BaseModel
|
4 |
-
from typing import List
|
5 |
-
from rag_chain import build_rag_chain, load_remote_pdf
|
6 |
-
import uvicorn
|
7 |
-
import os
|
8 |
-
from dotenv import load_dotenv
|
9 |
-
|
10 |
-
# Load env variables
|
11 |
-
load_dotenv()
|
12 |
-
HACKATHON_API_KEY = os.getenv("HACKATHON_API_KEY")
|
13 |
-
|
14 |
-
app = FastAPI()
|
15 |
-
|
16 |
-
# Security scheme
|
17 |
-
bearer_scheme = HTTPBearer()
|
18 |
-
|
19 |
-
class QueryRequest(BaseModel):
|
20 |
-
documents: str
|
21 |
-
questions: List[str]
|
22 |
-
|
23 |
-
class QueryResponse(BaseModel):
|
24 |
-
answers: List[str]
|
25 |
-
|
26 |
-
# Dependency that checks for the token
|
27 |
-
def verify_token(credentials: HTTPAuthorizationCredentials = Depends(bearer_scheme)):
|
28 |
-
if credentials.credentials != HACKATHON_API_KEY:
|
29 |
-
raise HTTPException(status_code=401, detail="Unauthorized")
|
30 |
-
|
31 |
-
@app.post("/hackrx/run", response_model=QueryResponse, dependencies=[Depends(verify_token)])
|
32 |
-
async def run_query(data: QueryRequest):
|
33 |
-
try:
|
34 |
-
pdf_path = load_remote_pdf(data.documents)
|
35 |
-
rag_chain = build_rag_chain(pdf_path)
|
36 |
-
answers = [rag_chain.invoke(q) for q in data.questions]
|
37 |
-
return {"answers": answers}
|
38 |
-
except Exception as e:
|
39 |
-
raise HTTPException(status_code=500, detail=str(e))
|
40 |
-
|
41 |
-
if __name__ == "__main__":
|
42 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
1 |
+
from fastapi import FastAPI, Request, HTTPException, Depends
|
2 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from typing import List
|
5 |
+
from rag_chain import build_rag_chain, load_remote_pdf
|
6 |
+
import uvicorn
|
7 |
+
import os
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
# Load env variables
|
11 |
+
load_dotenv()
|
12 |
+
HACKATHON_API_KEY = os.getenv("HACKATHON_API_KEY")
|
13 |
+
|
14 |
+
app = FastAPI()
|
15 |
+
|
16 |
+
# Security scheme
|
17 |
+
bearer_scheme = HTTPBearer()
|
18 |
+
|
19 |
+
class QueryRequest(BaseModel):
|
20 |
+
documents: str
|
21 |
+
questions: List[str]
|
22 |
+
|
23 |
+
class QueryResponse(BaseModel):
|
24 |
+
answers: List[str]
|
25 |
+
|
26 |
+
# Dependency that checks for the token
|
27 |
+
def verify_token(credentials: HTTPAuthorizationCredentials = Depends(bearer_scheme)):
|
28 |
+
if credentials.credentials != HACKATHON_API_KEY:
|
29 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
30 |
+
|
31 |
+
@app.post("/api/v1/hackrx/run", response_model=QueryResponse, dependencies=[Depends(verify_token)])
|
32 |
+
async def run_query(data: QueryRequest):
|
33 |
+
try:
|
34 |
+
pdf_path = load_remote_pdf(data.documents)
|
35 |
+
rag_chain = build_rag_chain(pdf_path)
|
36 |
+
answers = [rag_chain.invoke(q) for q in data.questions]
|
37 |
+
return {"answers": answers}
|
38 |
+
except Exception as e:
|
39 |
+
raise HTTPException(status_code=500, detail=str(e))
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|