Spaces:
Running
Running
File size: 8,520 Bytes
cc30e2f be24a36 0ed8825 be24a36 e2c0fd1 be24a36 cc30e2f 0ed8825 be24a36 e2c0fd1 be24a36 0ed8825 be24a36 0ed8825 be24a36 0ed8825 be24a36 0ed8825 be24a36 0ed8825 be24a36 0ed8825 be24a36 0ed8825 be24a36 cc30e2f 0ed8825 cc30e2f be24a36 cc30e2f 0ed8825 cc30e2f 0ed8825 cc30e2f 0ed8825 cc30e2f 0ed8825 cc30e2f e2c0fd1 0ed8825 be24a36 cc30e2f be24a36 cc30e2f be24a36 cc30e2f be24a36 cc30e2f | 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | # app/api/routes.py
from fastapi import APIRouter, UploadFile, Form, HTTPException
from fastapi.responses import StreamingResponse
import uuid, os, json, asyncio, time
from collections import defaultdict
from bson import ObjectId
from app.core.pdf_processor import extract_text_from_pdf
from app.core.embedding_engine import embed_and_store, embedder, qdrant, COLLECTION_NAME
from qdrant_client.http.models import Filter, FieldCondition, MatchValue
from app.core.mongo import conversations
from qdrant_client import QdrantClient
from app.core.config import QDRANT_URL, QDRANT_API_KEY
from app.graph.graph_builder import build_graph
from app.core.text_splitter import split_text
router = APIRouter()
UPLOAD_DIR = "uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
# Qdrant Client
qdrant_client = QdrantClient(
url=QDRANT_URL,
api_key=QDRANT_API_KEY,
check_compatibility=False)
# In-memory (for fallback)
chat_histories = defaultdict(list)
# ---------------------------------------------------
# β
Upload endpoint
# ---------------------------------------------------
@router.post("/upload")
async def upload_pdf(file: UploadFile):
doc_id = str(uuid.uuid4())
file_path = os.path.join(UPLOAD_DIR, f"{doc_id}.pdf")
with open(file_path, "wb") as f:
f.write(await file.read())
text = extract_text_from_pdf(file_path)
# chunks = [text[i:i + 1000] for i in range(0, len(text), 1000)]
chunks= split_text(text)
# β
STEP 4: Limit chunks
MAX_CHUNKS = 300
if len(chunks) > MAX_CHUNKS:
print(f"β οΈ Too many chunks ({len(chunks)}), trimming to {MAX_CHUNKS}")
chunks = chunks[:MAX_CHUNKS]
try:
await asyncio.to_thread(embed_and_store, chunks, doc_id)
except RuntimeError as e:
# β
surface embedding failures instead of silently succeeding
raise HTTPException(status_code=500, detail=f"Embedding failed: {str(e)}")
# β
Create MongoDB record
conversations.insert_one({
"doc_id": doc_id,
"name": file.filename,
"file_path": file_path,
"qdrant_collection": COLLECTION_NAME,
"history": [],
"created_at": time.time(),
})
return {"doc_id": doc_id, "message": "PDF uploaded and processed successfully."}
# ---------------------------------------------------
# β
Streaming Upload endpoint (with Mongo insert)
# ---------------------------------------------------
@router.post("/upload-stream")
async def upload_stream(file: UploadFile):
doc_id = str(uuid.uuid4())
file_path = os.path.join(UPLOAD_DIR, f"{doc_id}.pdf")
with open(file_path, "wb") as f:
content = await file.read()
f.write(content)
async def generate_upload_events():
try:
yield f"data: {json.dumps({'status': 'β
File uploaded successfully. Starting processing...'})}\n\n"
await asyncio.sleep(0.3)
yield f"data: {json.dumps({'status': 'π Extracting text from PDF...'})}\n\n"
text = extract_text_from_pdf(file_path)
yield f"data: {json.dumps({'status': 'π Chunking document...'})}\n\n"
chunks = split_text(text)
MAX_CHUNKS = 300
if len(chunks) > MAX_CHUNKS:
print(f"β οΈ Too many chunks ({len(chunks)}), trimming to {MAX_CHUNKS}")
chunks = chunks[:MAX_CHUNKS]
# await asyncio.sleep(0.3)
yield f"data: {json.dumps({'status': f'π§ Embedding {len(chunks)} chunks...'})}\n\n"
try:
await asyncio.to_thread(embed_and_store, chunks, doc_id)
except RuntimeError as e:
# β
stream the error back to frontend instead of silent failure
yield f"data: {json.dumps({'status': f'β Embedding error: {str(e)}'})}\n\n"
yield "event: end\ndata: {}\n\n"
return
# await asyncio.sleep(0.3)
# β
Save chat info to MongoDB
conversations.insert_one({
"doc_id": doc_id,
"name": file.filename,
"file_path": file_path,
"qdrant_collection": COLLECTION_NAME,
"history": [],
"created_at": time.time(),
})
yield f"data: {json.dumps({'status': 'π Done! Youβre good to go.', 'doc_id': doc_id})}\n\n"
yield "event: end\ndata: {}\n\n"
except Exception as e:
yield f"data: {json.dumps({'status': f'β οΈ Error: {str(e)}'})}\n\n"
yield "event: end\ndata: {}\n\n"
return StreamingResponse(generate_upload_events(), media_type="text/event-stream")
# ---------------------------------------------------
# β
Fetch All Chats (for Sidebar)
# ---------------------------------------------------
@router.get("/chats")
async def get_all_chats():
try:
chats = list(conversations.find({}, {"_id": 1, "name": 1, "doc_id": 1}))
for c in chats:
c["_id"] = str(c["_id"])
return {"chats": chats}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ---------------------------------------------------
# β
Delete Chat (Qdrant + Mongo + File)
# ---------------------------------------------------
@router.delete("/chat/{chat_id}")
async def delete_chat(chat_id: str):
try:
chat = conversations.find_one({"_id": ObjectId(chat_id)})
if not chat:
raise HTTPException(status_code=404, detail="Chat not found")
doc_id = chat.get("doc_id")
# β
Delete embeddings from Qdrant
try:
qdrant.delete(
collection_name=COLLECTION_NAME,
points_selector=Filter(
must=[FieldCondition(key="doc_id", match=MatchValue(value=doc_id))]
),
)
except Exception as e:
print(f"β οΈ Qdrant delete failed: {e}")
# β
Delete uploaded PDF file
file_path = chat.get("file_path") or os.path.join("uploads", f"{doc_id}.pdf")
if os.path.exists(file_path):
os.remove(file_path)
# β
Delete from MongoDB
conversations.delete_one({"_id": ObjectId(chat_id)})
return {"status": "success", "message": "Chat and embeddings deleted successfully"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ---------------------------------------------------
# β
Chat Query Endpoint (Persistent Memory)
# ---------------------------------------------------
@router.post("/query")
async def query_pdf(doc_id: str = Form(...), question: str = Form(...)):
print(f"API DEBUG β doc_id: {doc_id} | question: {question} ")
# β
Save user message
conversations.update_one(
{"doc_id": doc_id},
{"$push": {"history": {"role": "user", "content": question}}},
upsert=True
)
# β
Retrieve history
doc = conversations.find_one({"doc_id": doc_id})
history_list = doc["history"][-10:] if doc and "history" in doc else []
structured_history = "\n".join(
[f"{h['role'].title()}: {h['content']}" for h in history_list]
)
# β
Vector Search
graph= build_graph()
# remove later
print("FINAL β sending to graph:", {
"query": question,
"doc_id": doc_id
})
initial_state = {
"query": str(question),
"doc_id": str(doc_id),
"history": structured_history,
"route": None,
"context": None,
"final_answer": None
}
# remove later
print("FINAL STATE β", initial_state)
result = graph.invoke(initial_state)
answer = result["final_answer"]
# context = result.get("context", "")
# ------------
# Save assistant response
conversations.update_one(
{"doc_id": doc_id},
{"$push": {"history": {"role": "assistant", "content": answer}}},
upsert=True
)
print("EVALUATION β", result.get("evaluation"))
return {
"answer": answer,
# "context_used": context,
"sources": result.get("sources", []),
"evaluation": result.get("evaluation", []),
"history_count": len(history_list)
}
@router.get("/conversations/{doc_id}")
async def get_conversation(doc_id: str):
doc = conversations.find_one({"doc_id": doc_id})
if not doc:
return {"history": []}
return {"history": doc.get("history", [])}
|