File size: 5,765 Bytes
e1317bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Dict, Any
import sqlite3
import json
import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from pipeline import analyze_document, collection, embedder

# ---- App ----
app = FastAPI(
    title="NLP Document Analyzer API",
    description="Multi-task NLP pipeline — NER, Classification, Summarization, Semantic Search",
    version="2.0.0"
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)

# ---- SQLite ----
DB_PATH = "data/documents.db"

def init_db():
    os.makedirs("data", exist_ok=True)
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS documents (
            id TEXT PRIMARY KEY,
            text TEXT NOT NULL,
            doc_type TEXT,
            confidence REAL,
            entities TEXT,
            summary TEXT,
            extracted_fields TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )
    """)
    conn.commit()
    conn.close()

def save_document(result: Dict[str, Any], text: str):
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute("""
        INSERT OR REPLACE INTO documents
        (id, text, doc_type, confidence, entities, summary, extracted_fields)
        VALUES (?, ?, ?, ?, ?, ?, ?)
    """, (
        result["doc_id"],
        text,
        result["doc_type"],
        result["confidence"],
        json.dumps(result["entities"]),
        result["summary"],
        json.dumps(result["extracted_fields"])
    ))
    conn.commit()
    conn.close()

init_db()

# ---- Models ----
class DocumentRequest(BaseModel):
    text: str

class EntityResponse(BaseModel):
    text: str
    type: str

class DocumentResponse(BaseModel):
    doc_id: str
    doc_type: str
    confidence: float
    entities: List[EntityResponse]
    summary: str
    extracted_fields: Dict[str, Any]

class SearchRequest(BaseModel):
    query: str
    n_results: int = 5

# ---- Endpoints ----

@app.get("/health")
def health():
    return {"status": "healthy", "version": "2.0.0"}


@app.post("/analyze", response_model=DocumentResponse)
def analyze(request: DocumentRequest):
    if not request.text.strip():
        raise HTTPException(status_code=400, detail="Text cannot be empty")
    if len(request.text) > 15000:
        raise HTTPException(status_code=400, detail="Text too long — max 15,000 characters")
    try:
        result = analyze_document(request.text)
        save_document(result, request.text)
        return DocumentResponse(
            doc_id=result["doc_id"],
            doc_type=result["doc_type"],
            confidence=result["confidence"],
            entities=[EntityResponse(**e) for e in result["entities"]],
            summary=result["summary"],
            extracted_fields=result["extracted_fields"]
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/documents")
def get_documents():
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        cursor.execute("SELECT id, text, doc_type, confidence, entities, summary, extracted_fields, created_at FROM documents ORDER BY created_at DESC")
        rows = cursor.fetchall()
        conn.close()
        documents = []
        for row in rows:
            documents.append({
                "doc_id": row[0],
                "text_preview": row[1][:200],
                "doc_type": row[2],
                "confidence": row[3],
                "entities": json.loads(row[4]),
                "summary": row[5],
                "extracted_fields": json.loads(row[6]),
                "created_at": row[7]
            })
        return {"documents": documents, "total": len(documents)}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/search")
def search(request: SearchRequest):
    if not request.query.strip():
        raise HTTPException(status_code=400, detail="Query cannot be empty")
    try:
        count = collection.count()
        if count == 0:
            return {"query": request.query, "results": [], "total": 0}
        query_embedding = embedder.encode(request.query).tolist()
        results = collection.query(
            query_embeddings=[query_embedding],
            n_results=min(request.n_results, count)
        )
        search_results = []
        if results["documents"][0]:
            for doc, meta in zip(results["documents"][0], results["metadatas"][0]):
                search_results.append({
                    "text_preview": doc[:300],
                    "doc_type": meta.get("doc_type", ""),
                    "summary": meta.get("summary", "")
                })
        return {"query": request.query, "results": search_results, "total": len(search_results)}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/stats")
def get_stats():
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        cursor.execute("SELECT COUNT(*) FROM documents")
        total = cursor.fetchone()[0]
        cursor.execute("SELECT doc_type, COUNT(*) FROM documents GROUP BY doc_type ORDER BY COUNT(*) DESC")
        type_counts = dict(cursor.fetchall())
        conn.close()
        return {
            "total_documents": total,
            "documents_by_type": type_counts,
            "vector_store_count": collection.count()
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))