| 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 |