from contextlib import asynccontextmanager from fastapi import FastAPI from document_processor import DocumentProcessor from models import * import time import hashlib # Initialize document processor processor = DocumentProcessor() @asynccontextmanager async def lifespan(app: FastAPI): # Startup events print("🚀 Initializing Document Processor...") await processor.initialize() print("✅ Application startup complete") yield # Shutdown events (if you need any cleanup) print("🛑 Shutting down application...") # Create FastAPI app with lifespan handler app = FastAPI( title="Legal Document Analysis API", version="1.0.0", lifespan=lifespan # Pass the lifespan handler here ) @app.post("/analyze_document") async def analyze_document(data: AnalyzeDocumentInput): """Unified endpoint for complete document analysis""" try: start_time = time.time() if not data.document_text: return {"error": "No document text provided"} # Generate document ID for caching doc_id = hashlib.sha256(data.document_text.encode()).hexdigest()[:16] # Process document completely result = await processor.process_document(data.document_text, doc_id) processing_time = time.time() - start_time result["processing_time"] = f"{processing_time:.2f}s" result["doc_id"] = doc_id return result except Exception as e: return {"error": str(e)} # Keep backward compatibility endpoints @app.post("/chunk") def chunk_text(data: ChunkInput): return processor.chunk_text(data) @app.post("/summarize_batch") def summarize_batch(data: SummarizeBatchInput): return processor.summarize_batch(data) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=7860)