Spaces:
Sleeping
Sleeping
Update app/main.py
Browse files- 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 |
-
|
|
|
|
| 13 |
|
| 14 |
-
@app.get("/")
|
| 15 |
-
def home(request: Request):
|
| 16 |
return templates.TemplateResponse(
|
| 17 |
"index.html",
|
| 18 |
-
{"request": request}
|
| 19 |
)
|
| 20 |
|
| 21 |
-
@app.post("/
|
| 22 |
-
async def
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 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 |
+
)
|