File size: 2,671 Bytes
f72e9b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
NLP Insight Engine β€” REST API
FastAPI backend exposing the NLP pipeline as a JSON API.
Run: uvicorn api.main:app --reload
"""

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
from utils.pipeline import NLPPipeline

app = FastAPI(
    title="NLP Insight Engine API",
    description=(
        "A free, open-source NLP analysis API. "
        "Supports sentiment analysis, named entity recognition, "
        "keyword extraction, and extractive summarisation."
    ),
    version="1.0.0",
    docs_url="/api/docs",
)

# Lazy-load pipeline on first request
_pipeline = None


def get_pipeline() -> NLPPipeline:
    global _pipeline
    if _pipeline is None:
        _pipeline = NLPPipeline()
    return _pipeline


# ── Request / Response schemas ───────────────────────────────────────────────


class AnalyseRequest(BaseModel):
    text: str = Field(..., min_length=20, max_length=5000, description="Text to analyse")
    sentiment: bool = Field(True, description="Run sentiment analysis")
    ner: bool = Field(True, description="Run named entity recognition")
    keywords: bool = Field(True, description="Run keyword extraction")
    summary: bool = Field(True, description="Run extractive summarisation")


class AnalyseResponse(BaseModel):
    word_count: int
    sentiment: dict | None = None
    entities: list | None = None
    keywords: list | None = None
    summary: str | None = None


# ── Endpoints ────────────────────────────────────────────────────────────────


@app.get("/")
def root():
    return {
        "service": "NLP Insight Engine",
        "version": "1.0.0",
        "docs": "/api/docs",
    }


@app.get("/health")
def health():
    return {"status": "ok"}


@app.post("/api/analyse", response_model=AnalyseResponse)
def analyse(req: AnalyseRequest):
    """Run NLP analysis on the provided text."""
    pipe = get_pipeline()
    text = req.text.strip()

    result = AnalyseResponse(word_count=len(text.split()))

    try:
        if req.sentiment:
            result.sentiment = pipe.analyse_sentiment(text)
        if req.ner:
            result.entities = pipe.extract_entities(text)
        if req.keywords:
            result.keywords = pipe.extract_keywords(text)
        if req.summary:
            result.summary = pipe.summarise(text)
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}")

    return result