Spaces:
Sleeping
Sleeping
Commit
·
a037cf8
1
Parent(s):
9c1d195
Fix imports/order and provide full working FastAPI app
Browse files- app/api.py +41 -65
app/api.py
CHANGED
@@ -1,13 +1,14 @@
|
|
1 |
# app/api.py
|
2 |
-
from
|
|
|
|
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
-
from fastapi.responses import JSONResponse
|
5 |
from pydantic import BaseModel
|
6 |
-
from typing import List, Optional
|
7 |
|
8 |
-
from .rag_system import SimpleRAG, UPLOAD_DIR
|
9 |
|
10 |
-
app = FastAPI(title="RAG API", version="1.2.
|
11 |
|
12 |
app.add_middleware(
|
13 |
CORSMiddleware,
|
@@ -19,7 +20,23 @@ app.add_middleware(
|
|
19 |
|
20 |
rag = SimpleRAG()
|
21 |
|
22 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
@app.get("/debug/translate")
|
24 |
def debug_translate():
|
25 |
try:
|
@@ -30,75 +47,34 @@ def debug_translate():
|
|
30 |
except Exception as e:
|
31 |
return JSONResponse(status_code=500, content={"ok": False, "error": str(e)})
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
from .rag_system import SimpleRAG, UPLOAD_DIR, synthesize_answer as summarize
|
38 |
-
from .schemas import AskRequest, AskResponse, UploadResponse, HistoryResponse, HistoryItem
|
39 |
-
from .store import add_history, get_history
|
40 |
-
from .utils import ensure_session, http400
|
41 |
-
|
42 |
-
app = FastAPI(title="RAG API", version="1.1.0")
|
43 |
-
|
44 |
-
# CORS (allow Streamlit or any origin; tighten later if you want)
|
45 |
-
app.add_middleware(
|
46 |
-
CORSMiddleware,
|
47 |
-
allow_origins=["*"], # e.g., ["https://HamidOmarov-RAG-Dashboard.hf.space"]
|
48 |
-
allow_credentials=True,
|
49 |
-
allow_methods=["*"],
|
50 |
-
allow_headers=["*"],
|
51 |
-
)
|
52 |
-
|
53 |
-
rag = SimpleRAG()
|
54 |
-
|
55 |
@app.get("/")
|
56 |
def root():
|
57 |
-
# convenience: open docs instead of 404
|
58 |
return RedirectResponse(url="/docs")
|
59 |
|
60 |
@app.get("/health")
|
61 |
def health():
|
62 |
-
return {"status": "ok", "version":
|
63 |
|
64 |
@app.post("/upload_pdf", response_model=UploadResponse)
|
65 |
async def upload_pdf(file: UploadFile = File(...)):
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
traceback.print_exc()
|
76 |
-
return JSONResponse(status_code=500, content={"detail": f"Server error: {str(e)}"})
|
77 |
|
78 |
@app.post("/ask_question", response_model=AskResponse)
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
results = rag.search(payload.question, k=payload.top_k)
|
85 |
-
contexts = [c for c, _ in results]
|
86 |
-
|
87 |
-
# Always use the English extractive summarizer
|
88 |
-
answer = summarize(payload.question, contexts)
|
89 |
-
|
90 |
-
add_history(session_id, "assistant", answer)
|
91 |
-
return AskResponse(answer=answer, contexts=contexts, session_id=session_id)
|
92 |
-
except Exception as e:
|
93 |
-
traceback.print_exc()
|
94 |
-
return JSONResponse(status_code=500, content={"detail": f"Server error: {str(e)}"})
|
95 |
|
96 |
@app.get("/get_history", response_model=HistoryResponse)
|
97 |
-
|
98 |
-
|
99 |
-
hist_raw = get_history(session_id)
|
100 |
-
history = [HistoryItem(**h) for h in hist_raw]
|
101 |
-
return HistoryResponse(session_id=session_id, history=history)
|
102 |
-
except Exception as e:
|
103 |
-
traceback.print_exc()
|
104 |
-
return JSONResponse(status_code=500, content={"detail": f"Server error: {str(e)}"})
|
|
|
1 |
# app/api.py
|
2 |
+
from typing import List, Optional
|
3 |
+
|
4 |
+
from fastapi import FastAPI, UploadFile, File
|
5 |
from fastapi.middleware.cors import CORSMiddleware
|
6 |
+
from fastapi.responses import JSONResponse, RedirectResponse
|
7 |
from pydantic import BaseModel
|
|
|
8 |
|
9 |
+
from .rag_system import SimpleRAG, UPLOAD_DIR
|
10 |
|
11 |
+
app = FastAPI(title="RAG API", version="1.2.3")
|
12 |
|
13 |
app.add_middleware(
|
14 |
CORSMiddleware,
|
|
|
20 |
|
21 |
rag = SimpleRAG()
|
22 |
|
23 |
+
# ---------- Models ----------
|
24 |
+
class UploadResponse(BaseModel):
|
25 |
+
filename: str
|
26 |
+
chunks_added: int
|
27 |
+
|
28 |
+
class AskRequest(BaseModel):
|
29 |
+
question: str
|
30 |
+
top_k: int = 5
|
31 |
+
|
32 |
+
class AskResponse(BaseModel):
|
33 |
+
answer: str
|
34 |
+
contexts: List[str]
|
35 |
+
|
36 |
+
class HistoryResponse(BaseModel):
|
37 |
+
total_chunks: int
|
38 |
+
|
39 |
+
# ---------- Debug ----------
|
40 |
@app.get("/debug/translate")
|
41 |
def debug_translate():
|
42 |
try:
|
|
|
47 |
except Exception as e:
|
48 |
return JSONResponse(status_code=500, content={"ok": False, "error": str(e)})
|
49 |
|
50 |
+
# ---------- Core ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
@app.get("/")
|
52 |
def root():
|
|
|
53 |
return RedirectResponse(url="/docs")
|
54 |
|
55 |
@app.get("/health")
|
56 |
def health():
|
57 |
+
return {"status": "ok", "version": app.version, "summarizer": "extractive_en+translate+fallback"}
|
58 |
|
59 |
@app.post("/upload_pdf", response_model=UploadResponse)
|
60 |
async def upload_pdf(file: UploadFile = File(...)):
|
61 |
+
dest = UPLOAD_DIR / file.filename
|
62 |
+
with open(dest, "wb") as f:
|
63 |
+
while True:
|
64 |
+
chunk = await file.read(1024 * 1024)
|
65 |
+
if not chunk:
|
66 |
+
break
|
67 |
+
f.write(chunk)
|
68 |
+
added = rag.add_pdf(dest)
|
69 |
+
return UploadResponse(filename=file.filename, chunks_added=added)
|
|
|
|
|
70 |
|
71 |
@app.post("/ask_question", response_model=AskResponse)
|
72 |
+
def ask_question(payload: AskRequest):
|
73 |
+
hits = rag.search(payload.question, k=max(1, payload.top_k))
|
74 |
+
contexts = [c for c, _ in hits]
|
75 |
+
answer = rag.synthesize_answer(payload.question, contexts)
|
76 |
+
return AskResponse(answer=answer, contexts=contexts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
@app.get("/get_history", response_model=HistoryResponse)
|
79 |
+
def get_history():
|
80 |
+
return HistoryResponse(total_chunks=len(rag.chunks))
|
|
|
|
|
|
|
|
|
|
|
|