File size: 2,575 Bytes
5f36465
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from fastapi import FastAPI, HTTPException, Request, Header
from app.models.schema import QueryRequest, QueryResponse, JustificationItem
from app.utils.search import SemanticSearch
from app.utils.llm_decider import generate_decision
from app.utils.embedder import Embedder
import os
from dotenv import load_dotenv

load_dotenv()

API_KEY = os.getenv("API_KEY")
app = FastAPI()

# βœ… Load the embedder once from Drive
embedder = Embedder()
embedder.load_from_drive(
    index_url="https://drive.google.com/uc?id=1GOSzA4PiEsDZupMEeNsuIEhKpbRMWgxl",
    metadata_url="https://drive.google.com/uc?id=1MPkhB5L0TkXNivb1SjRlhYejDpP9Mp6v"
)

# βœ… Semantic Search wrapper
search_engine = SemanticSearch(embedder)

@app.post("/debug/test")
def debug(payload: dict):
    return {"echo": payload}

@app.get("/")
def health():
    return {"status": "HackRx API running πŸš€"}

@app.head("/")
def health_head():
    return

@app.middleware("http")
async def log_all_requests(request: Request, call_next):
    body = await request.body()
    print("πŸ“₯ RAW Body:", body.decode("utf-8"))
    print("πŸ“₯ Headers:", dict(request.headers))
    response = await call_next(request)
    return response

@app.post("/hackrx/run", response_model=QueryResponse)
def run_handler(request: Request, payload: QueryRequest, authorization: str = Header(None)):
    print("πŸ“© Incoming query:", payload.query)

    if not authorization or not authorization.startswith("Bearer ") or authorization.split()[1] != API_KEY:
        print("❌ Unauthorized request")
        raise HTTPException(status_code=401, detail="Unauthorized")

    try:
        results_df = search_engine.search(payload.query)
        print(f"πŸ” Found {len(results_df)} relevant chunks")

        if results_df.empty:
            raise HTTPException(status_code=404, detail="No relevant information found")

        top_chunks = results_df['text'].tolist()
        print("πŸ“„ Preview chunk:", top_chunks[0][:150])

        # βœ… Use LLM
        parsed = generate_decision(payload.query, top_chunks)

        justification_items = [JustificationItem(**j) for j in parsed.get('justification', [])]

        return QueryResponse(
            decision=parsed.get('decision', "No decision provided"),
            amount=parsed.get('amount', "N/A"),
            justification=justification_items
        )

    except Exception as e:
        print("πŸ”₯ Internal error:", str(e))
        raise HTTPException(status_code=500, detail=f"Internal Error: {str(e)}")