Spaces:
Running
Running
| from fastapi import FastAPI, Header, HTTPException | |
| from app.models import QueryRequest, QueryResponse | |
| from app.processor import extract_text_from_url, chunk_text | |
| from app.embedder import embed_chunks | |
| from app.retriever import get_similar_contexts | |
| from app.llm_reasoner import generate_answer | |
| from dotenv import load_dotenv | |
| import os | |
| import json # β Import json | |
| load_dotenv() | |
| app = FastAPI() | |
| def home(): | |
| return {"message": "FastAPI is live!"} | |
| async def run_query(req: QueryRequest, authorization: str = Header(...)): | |
| print(authorization) | |
| if not authorization.endswith("cda0"): | |
| raise HTTPException(status_code=401, detail="Unauthorized token") | |
| print(f"π Document URL: {req.documents}") | |
| print(f"β Questions: {req.questions}") | |
| raw_text = extract_text_from_url(req.documents) | |
| chunks = chunk_text(raw_text) | |
| db = embed_chunks(chunks) | |
| answers = [] | |
| for q in req.questions: | |
| context = get_similar_contexts(db, q) | |
| answer = generate_answer(context, q) | |
| answers.append(answer) | |
| response = QueryResponse(answers=answers) | |
| # β Save the response to a JSON file | |
| with open("response_output2.json", "w", encoding="utf-8") as f: | |
| json.dump(response.dict(), f, indent=4, ensure_ascii=False) | |
| return response | |