ishmeet-yo commited on
Commit
6194f24
·
verified ·
1 Parent(s): a48808a

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +19 -16
app/main.py CHANGED
@@ -1,4 +1,5 @@
1
- from fastapi import FastAPI, Request
 
2
  from fastapi.templating import Jinja2Templates
3
  from fastapi.staticfiles import StaticFiles
4
 
@@ -9,24 +10,26 @@ app = FastAPI()
9
  templates = Jinja2Templates(directory="templates")
10
  app.mount("/static", StaticFiles(directory="static"), name="static")
11
 
12
- chunks, heads = load_data()
 
13
 
14
- @app.get("/")
15
- def home(request: Request):
16
  return templates.TemplateResponse(
17
  "index.html",
18
- {"request": request}
19
  )
20
 
21
- @app.post("/search")
22
- async def search(request: Request):
23
- body = await request.json()
24
- query = body["query"]
25
 
26
- retrieved = retrieve_chunks(query, chunks, heads)
27
- answer = "\n\n".join(retrieved[:2])
28
-
29
- return {
30
- "answer": answer,
31
- "sources": retrieved
32
- }
 
 
1
+ from fastapi import FastAPI, Request, Form
2
+ from fastapi.responses import HTMLResponse
3
  from fastapi.templating import Jinja2Templates
4
  from fastapi.staticfiles import StaticFiles
5
 
 
10
  templates = Jinja2Templates(directory="templates")
11
  app.mount("/static", StaticFiles(directory="static"), name="static")
12
 
13
+ # Load your RAG data once
14
+ documents, embeddings, model = load_data()
15
 
16
+ @app.get("/", response_class=HTMLResponse)
17
+ async def home(request: Request):
18
  return templates.TemplateResponse(
19
  "index.html",
20
+ {"request": request, "query": "", "answer": None},
21
  )
22
 
23
+ @app.post("/", response_class=HTMLResponse)
24
+ async def ask(request: Request, query: str = Form(...)):
25
+ results = retrieve_chunks(query, documents, embeddings, model)
26
+ answer = results[0] if results else "No relevant answer found."
27
 
28
+ return templates.TemplateResponse(
29
+ "index.html",
30
+ {
31
+ "request": request,
32
+ "query": query,
33
+ "answer": answer,
34
+ },
35
+ )