File size: 1,642 Bytes
f1b862b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List, Dict, Any, Optional
import os
import uvicorn
import time
import hashlib
from document_processor import DocumentProcessor
from models import *

# Initialize FastAPI app
app = FastAPI(title="Legal Document Analysis API", version="1.0.0")

# Initialize document processor
processor = DocumentProcessor()

@app.on_event("startup")
async def startup_event():
    """Initialize models on startup"""
    await processor.initialize()

@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__":
    uvicorn.run(app, host="0.0.0.0", port=7860)