Spaces:
Sleeping
Sleeping
Commit
·
0d2dbdc
1
Parent(s):
ebbe4db
Force EN extractive summarizer and health version tag
Browse files- app/api.py +22 -22
app/api.py
CHANGED
@@ -1,22 +1,22 @@
|
|
1 |
# app/api.py
|
2 |
-
from fastapi import FastAPI, UploadFile, File
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
-
from fastapi.responses import JSONResponse
|
5 |
from pathlib import Path
|
6 |
import shutil
|
7 |
import traceback
|
8 |
|
9 |
-
from .rag_system import SimpleRAG, UPLOAD_DIR
|
10 |
from .schemas import AskRequest, AskResponse, UploadResponse, HistoryResponse, HistoryItem
|
11 |
from .store import add_history, get_history
|
12 |
from .utils import ensure_session, http400
|
13 |
|
14 |
-
app = FastAPI(title="RAG API", version="1.
|
15 |
|
16 |
-
# CORS (Streamlit
|
17 |
app.add_middleware(
|
18 |
CORSMiddleware,
|
19 |
-
allow_origins=["*"], #
|
20 |
allow_credentials=True,
|
21 |
allow_methods=["*"],
|
22 |
allow_headers=["*"],
|
@@ -24,41 +24,46 @@ app.add_middleware(
|
|
24 |
|
25 |
rag = SimpleRAG()
|
26 |
|
|
|
|
|
|
|
|
|
|
|
27 |
@app.get("/health")
|
28 |
def health():
|
29 |
-
return {"status": "ok"}
|
30 |
|
31 |
@app.post("/upload_pdf", response_model=UploadResponse)
|
32 |
async def upload_pdf(file: UploadFile = File(...)):
|
33 |
try:
|
34 |
if not file.filename.lower().endswith(".pdf"):
|
35 |
http400("Only PDF files are accepted.")
|
36 |
-
dest = UPLOAD_DIR / file.filename
|
37 |
with dest.open("wb") as f:
|
38 |
shutil.copyfileobj(file.file, f)
|
39 |
chunks_added = rag.add_pdf(dest)
|
40 |
-
return
|
41 |
except Exception as e:
|
42 |
traceback.print_exc()
|
43 |
-
return JSONResponse(status_code=500, content={"detail": f"Server
|
44 |
|
45 |
@app.post("/ask_question", response_model=AskResponse)
|
46 |
async def ask_question(payload: AskRequest):
|
47 |
try:
|
48 |
session_id = ensure_session(payload.session_id)
|
49 |
add_history(session_id, "user", payload.question)
|
|
|
50 |
results = rag.search(payload.question, k=payload.top_k)
|
51 |
contexts = [c for c, _ in results]
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
answer = synthesize_answer(payload.question, contexts)
|
57 |
add_history(session_id, "assistant", answer)
|
58 |
return AskResponse(answer=answer, contexts=contexts, session_id=session_id)
|
59 |
except Exception as e:
|
60 |
traceback.print_exc()
|
61 |
-
return JSONResponse(status_code=500, content={"detail": f"Server
|
62 |
|
63 |
@app.get("/get_history", response_model=HistoryResponse)
|
64 |
async def get_history_endpoint(session_id: str):
|
@@ -68,9 +73,4 @@ async def get_history_endpoint(session_id: str):
|
|
68 |
return HistoryResponse(session_id=session_id, history=history)
|
69 |
except Exception as e:
|
70 |
traceback.print_exc()
|
71 |
-
return JSONResponse(status_code=500, content={"detail": f"Server
|
72 |
-
from fastapi.responses import RedirectResponse
|
73 |
-
|
74 |
-
@app.get("/")
|
75 |
-
def root():
|
76 |
-
return RedirectResponse(url="/docs")
|
|
|
1 |
# app/api.py
|
2 |
+
from fastapi import FastAPI, UploadFile, File
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
from fastapi.responses import JSONResponse, RedirectResponse
|
5 |
from pathlib import Path
|
6 |
import shutil
|
7 |
import traceback
|
8 |
|
9 |
+
from .rag_system import SimpleRAG, UPLOAD_DIR, synthesize_answer as summarize
|
10 |
from .schemas import AskRequest, AskResponse, UploadResponse, HistoryResponse, HistoryItem
|
11 |
from .store import add_history, get_history
|
12 |
from .utils import ensure_session, http400
|
13 |
|
14 |
+
app = FastAPI(title="RAG API", version="1.1.0")
|
15 |
|
16 |
+
# CORS (allow Streamlit or any origin; tighten later if you want)
|
17 |
app.add_middleware(
|
18 |
CORSMiddleware,
|
19 |
+
allow_origins=["*"], # e.g., ["https://HamidOmarov-RAG-Dashboard.hf.space"]
|
20 |
allow_credentials=True,
|
21 |
allow_methods=["*"],
|
22 |
allow_headers=["*"],
|
|
|
24 |
|
25 |
rag = SimpleRAG()
|
26 |
|
27 |
+
@app.get("/")
|
28 |
+
def root():
|
29 |
+
# convenience: open docs instead of 404
|
30 |
+
return RedirectResponse(url="/docs")
|
31 |
+
|
32 |
@app.get("/health")
|
33 |
def health():
|
34 |
+
return {"status": "ok", "version": "1.1.0", "summarizer": "extractive_en"}
|
35 |
|
36 |
@app.post("/upload_pdf", response_model=UploadResponse)
|
37 |
async def upload_pdf(file: UploadFile = File(...)):
|
38 |
try:
|
39 |
if not file.filename.lower().endswith(".pdf"):
|
40 |
http400("Only PDF files are accepted.")
|
41 |
+
dest: Path = UPLOAD_DIR / file.filename
|
42 |
with dest.open("wb") as f:
|
43 |
shutil.copyfileobj(file.file, f)
|
44 |
chunks_added = rag.add_pdf(dest)
|
45 |
+
return UploadResponse(filename=file.filename, chunks_added=chunks_added)
|
46 |
except Exception as e:
|
47 |
traceback.print_exc()
|
48 |
+
return JSONResponse(status_code=500, content={"detail": f"Server error: {str(e)}"})
|
49 |
|
50 |
@app.post("/ask_question", response_model=AskResponse)
|
51 |
async def ask_question(payload: AskRequest):
|
52 |
try:
|
53 |
session_id = ensure_session(payload.session_id)
|
54 |
add_history(session_id, "user", payload.question)
|
55 |
+
|
56 |
results = rag.search(payload.question, k=payload.top_k)
|
57 |
contexts = [c for c, _ in results]
|
58 |
+
|
59 |
+
# Always use the English extractive summarizer
|
60 |
+
answer = summarize(payload.question, contexts)
|
61 |
+
|
|
|
62 |
add_history(session_id, "assistant", answer)
|
63 |
return AskResponse(answer=answer, contexts=contexts, session_id=session_id)
|
64 |
except Exception as e:
|
65 |
traceback.print_exc()
|
66 |
+
return JSONResponse(status_code=500, content={"detail": f"Server error: {str(e)}"})
|
67 |
|
68 |
@app.get("/get_history", response_model=HistoryResponse)
|
69 |
async def get_history_endpoint(session_id: str):
|
|
|
73 |
return HistoryResponse(session_id=session_id, history=history)
|
74 |
except Exception as e:
|
75 |
traceback.print_exc()
|
76 |
+
return JSONResponse(status_code=500, content={"detail": f"Server error: {str(e)}"})
|
|
|
|
|
|
|
|
|
|