-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathapp.py
More file actions
43 lines (33 loc) · 1.08 KB
/
app.py
File metadata and controls
43 lines (33 loc) · 1.08 KB
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
# app.py
import uvicorn
import json
import os
from fastapi import FastAPI, Body
from src.chatbot import PDFChatBot
app = FastAPI()
# 1) Load section information (sections_with_emb.json)
sections_path = "data/extracted/sections_with_emb.json"
with open(sections_path, 'r', encoding='utf-8') as f:
sections_data = json.load(f)
# 2) Load chunk index (sample_chunks_vectors.json)
chunk_index_path = "data/index/sample_chunks_vectors.json"
with open(chunk_index_path, 'r', encoding='utf-8') as f:
chunk_index_data = json.load(f)
chatbot = PDFChatBot(sections_data, chunk_index_data)
@app.post("/ask")
def ask_question(question: str = Body(..., embed=True)):
"""
FastAPI endpoint that returns an answer for the given question.
Parameters
----------
question : str
The user’s question text, passed in the request body.
Returns
-------
dict
A JSON dictionary with a single key ``"answer"``.
"""
answer = chatbot.answer(question)
return {"answer": answer}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)