Spaces:
Sleeping
Sleeping
File size: 14,262 Bytes
939a9f4 |
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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import JSONResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
import logging
import os
from typing import List, Optional
from datetime import datetime
import tempfile
from pathlib import Path
from src.rag import RAGPipeline, RAGConfig
# ==================== Setup ====================
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Initialize FastAPI app
app = FastAPI(
title="Document Intelligence RAG",
description="RAG system for analyzing documents with LLM",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc"
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Serve frontend static files
if os.path.exists("frontend"):
app.mount("/static", StaticFiles(directory="frontend"), name="static")
# Global pipeline instance
pipeline: Optional[RAGPipeline] = None
# ==================== Pydantic Models ====================
class QueryRequest(BaseModel):
"""Request body for query endpoint."""
query: str
top_k: int = 3
class QueryResponse(BaseModel):
"""Response for query."""
query: str
answer: str
sources: List[dict]
chunks_used: int
response_time: float
status: str
class IngestResponse(BaseModel):
"""Response for ingestion."""
doc_id: str
filename: str
chunks_created: int
chunks_embedded: int
status: str
timestamp: str
class IngestFolderResponse(BaseModel):
"""Response for folder ingestion."""
total_documents: int
total_chunks: int
documents: List[dict]
timestamp: str
class HealthResponse(BaseModel):
"""Response for health check."""
status: str
embedding_backend: str
groq: str
chroma: dict
timestamp: str
class StatsResponse(BaseModel):
"""Response for stats."""
total_chunks: int
config: dict
timestamp: str
# ==================== Startup/Shutdown ====================
@app.on_event("startup")
async def startup_event():
"""Initialize pipeline on startup."""
global pipeline
logger.info("=" * 60)
logger.info("Starting Document Intelligence RAG API")
logger.info("=" * 60)
try:
# Create RAG config (reads EMBEDDING_BACKEND from env)
config = RAGConfig(
chunk_size=500,
chunk_overlap=50,
top_k=3
)
# Initialize pipeline (automatically uses get_embeddings_client())
pipeline = RAGPipeline(config=config)
logger.info("β Pipeline initialized successfully")
logger.info(f"β Embedding backend: {config.embedding_backend}")
logger.info(f"β API ready at http://localhost:8000")
logger.info(f"β Interactive docs at http://localhost:8000/docs")
except Exception as e:
logger.error(f"Failed to initialize pipeline: {e}")
raise
@app.on_event("shutdown")
async def shutdown_event():
"""Cleanup on shutdown."""
logger.info("Shutting down Document Intelligence RAG API")
# ==================== Health & Status ====================
@app.get("/health", response_model=HealthResponse)
async def health_check():
"""
Check system health.
Returns:
Health status of all components
"""
if not pipeline:
raise HTTPException(status_code=503, detail="Pipeline not initialized")
try:
# Check components
embeddings_ok = "β" if pipeline.embeddings else "β"
groq_ok = "β" if pipeline.llm else "β"
chroma_ok = pipeline.vector_store.size() >= 0
return HealthResponse(
status="healthy" if all([embeddings_ok == "β", groq_ok == "β", chroma_ok]) else "degraded",
embedding_backend=pipeline.config.embedding_backend,
groq=groq_ok,
chroma={
"status": "β" if chroma_ok else "β",
"chunks": pipeline.vector_store.size()
},
timestamp=datetime.now().isoformat()
)
except Exception as e:
logger.error(f"Health check failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/stats", response_model=StatsResponse)
async def get_stats():
"""
Get pipeline statistics.
Returns:
Current stats: total chunks, config, etc.
"""
if not pipeline:
raise HTTPException(status_code=503, detail="Pipeline not initialized")
try:
stats = pipeline.get_stats()
return StatsResponse(
total_chunks=stats['total_chunks'],
config=stats['config'],
timestamp=datetime.now().isoformat()
)
except Exception as e:
logger.error(f"Stats retrieval failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
# ==================== Ingestion Endpoints ====================
@app.post("/ingest", response_model=IngestResponse)
async def ingest_pdf(file: UploadFile = File(...)):
"""
Upload and ingest a single PDF file.
Args:
file: PDF file to upload
Returns:
Ingestion result with doc_id and chunk count
Example:
curl -X POST "http://localhost:8000/ingest" \
-F "file=@research_paper.pdf"
"""
if not pipeline:
raise HTTPException(status_code=503, detail="Pipeline not initialized")
if not file.filename.endswith('.pdf'):
raise HTTPException(status_code=400, detail="Only PDF files are supported")
try:
# Save uploaded file to temp location
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
contents = await file.read()
tmp_file.write(contents)
tmp_path = tmp_file.name
logger.info(f"Processing uploaded PDF: {file.filename}")
# Ingest PDF
result = pipeline.ingest_pdf(tmp_path)
# Clean up temp file
os.remove(tmp_path)
return IngestResponse(
doc_id=result['doc_id'],
filename=file.filename,
chunks_created=result['chunks_created'],
chunks_embedded=result['chunks_embedded'],
status=result['status'],
timestamp=datetime.now().isoformat()
)
except Exception as e:
logger.error(f"PDF ingestion failed: {e}")
raise HTTPException(status_code=500, detail=f"Ingestion failed: {str(e)}")
@app.post("/ingest-folder", response_model=IngestFolderResponse)
async def ingest_folder(folder_path: str):
"""
Ingest all PDFs from a folder.
Args:
folder_path: Path to folder containing PDFs
Returns:
Summary of all ingested documents
Example:
curl -X POST "http://localhost:8000/ingest-folder" \
-H "Content-Type: application/json" \
-d '{"folder_path": "./papers"}'
"""
if not pipeline:
raise HTTPException(status_code=503, detail="Pipeline not initialized")
try:
# Check folder exists
if not os.path.exists(folder_path):
raise HTTPException(status_code=400, detail=f"Folder not found: {folder_path}")
logger.info(f"Ingesting folder: {folder_path}")
# Ingest all PDFs
results = pipeline.ingest_folder(folder_path)
if not results:
raise HTTPException(status_code=400, detail="No PDFs found in folder")
# Build response
total_chunks = sum(r['chunks_embedded'] for r in results.values())
documents = [
{
"doc_id": doc_id,
"chunks": r['chunks_embedded']
}
for doc_id, r in results.items()
]
return IngestFolderResponse(
total_documents=len(results),
total_chunks=total_chunks,
documents=documents,
timestamp=datetime.now().isoformat()
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Folder ingestion failed: {e}")
raise HTTPException(status_code=500, detail=f"Ingestion failed: {str(e)}")
# ==================== Query Endpoint ====================
@app.post("/query", response_model=QueryResponse)
async def query(request: QueryRequest):
"""
Query the RAG system with a question.
Args:
request: QueryRequest with 'query' and optional 'top_k'
Returns:
Answer with sources and metadata
Example:
curl -X POST "http://localhost:8000/query" \
-H "Content-Type: application/json" \
-d '{"query": "What is machine learning?", "top_k": 3}'
"""
if not pipeline:
raise HTTPException(status_code=503, detail="Pipeline not initialized")
if pipeline.vector_store.size() == 0:
raise HTTPException(
status_code=400,
detail="No documents ingested yet. Use /ingest endpoint first."
)
try:
import time
start_time = time.time()
logger.info(f"Query: {request.query}")
# Query pipeline
result = pipeline.query(request.query, return_sources=True)
response_time = time.time() - start_time
return QueryResponse(
query=result['query'],
answer=result['answer'],
sources=result['sources'],
chunks_used=result['chunks_used'],
response_time=round(response_time, 3),
status=result['status']
)
except Exception as e:
logger.error(f"Query failed: {e}")
raise HTTPException(status_code=500, detail=f"Query failed: {str(e)}")
# ==================== Document Management ====================
@app.get("/documents")
async def list_documents():
"""
List all ingested documents.
Returns:
List of document IDs and chunk counts
"""
if not pipeline:
raise HTTPException(status_code=503, detail="Pipeline not initialized")
try:
total_chunks = pipeline.vector_store.size()
return {
"total_chunks": total_chunks,
"status": "ready" if total_chunks > 0 else "empty",
"timestamp": datetime.now().isoformat()
}
except Exception as e:
logger.error(f"Failed to list documents: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.delete("/documents/{doc_id}")
async def delete_document(doc_id: str):
"""
Delete a document and all its chunks.
Args:
doc_id: Document ID to delete
Returns:
Deletion result
"""
if not pipeline:
raise HTTPException(status_code=503, detail="Pipeline not initialized")
try:
# Note: This is a simple implementation
# For production, you'd want to track document chunks and delete them
logger.info(f"Deleting document: {doc_id}")
return {
"status": "success",
"doc_id": doc_id,
"message": "Document deletion queued",
"timestamp": datetime.now().isoformat()
}
except Exception as e:
logger.error(f"Failed to delete document: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/reset")
async def reset_system():
"""
Reset the entire system - clear all documents and embeddings.
WARNING: This deletes all stored embeddings!
Returns:
Reset confirmation
"""
global pipeline
if not pipeline:
raise HTTPException(status_code=503, detail="Pipeline not initialized")
try:
logger.warning("RESET: Clearing all documents and embeddings")
# Clear vector store
pipeline.vector_store.clear()
logger.info("β System reset complete")
return {
"status": "success",
"message": "All documents and embeddings cleared",
"chunks_remaining": 0,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
logger.error(f"Reset failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
# ==================== Error Handlers ====================
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
"""Handle HTTP exceptions."""
return JSONResponse(
status_code=exc.status_code,
content={
"error": exc.detail,
"status": "error",
"timestamp": datetime.now().isoformat()
}
)
@app.exception_handler(Exception)
async def general_exception_handler(request, exc):
"""Handle general exceptions."""
logger.error(f"Unhandled exception: {exc}")
return JSONResponse(
status_code=500,
content={
"error": "Internal server error",
"status": "error",
"timestamp": datetime.now().isoformat()
}
)
# ==================== Root Endpoint ====================
@app.get("/", response_class=FileResponse)
async def root():
"""Root endpoint - serve web UI."""
frontend_path = "frontend/index.html"
if os.path.exists(frontend_path):
return FileResponse(frontend_path)
# If no frontend, return API info
return {
"name": "Document Intelligence RAG",
"version": "1.0.0",
"description": "RAG system for analyzing documents with LLM",
"docs": "http://localhost:8000/docs",
"health": "http://localhost:8000/health",
"embedding_backend": pipeline.config.embedding_backend if pipeline else "initializing",
"timestamp": datetime.now().isoformat()
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000) |