File size: 812 Bytes
f576f97
 
 
 
 
 
 
8d2f457
f576f97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import spacy
from typing import Dict, Any, List


class EndpointHandler:
    def __init__(self, path: str = ""):
        # Load the spaCy model. Make sure you've added it in requirements.txt
        self.nlp = spacy.load(path)

    def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
        # Accept both 'inputs' and 'text' as valid keys
        text = data.get("inputs") or data.get("text") or ""

        if not text:
            return [{"error": "Missing input text"}]

        doc = self.nlp(text)
        results = []

        for ent in doc.ents:
            results.append({
                "text": ent.text,
                "start": ent.start_char,
                "end": ent.end_char,
                "label": ent.label_
            })

        return results