| | from typing import Dict, List, Any |
| | import importlib.util |
| |
|
| | class EndpointHandler: |
| | def __init__(self, path=""): |
| | """ |
| | The __init__ method is called when starting the Endpoint. |
| | We perform the imports and model loading here to match your logic. |
| | """ |
| | |
| | if importlib.util.find_spec("spacy") is None: |
| | raise RuntimeError( |
| | "SpaCy is required but not installed. Install it with:\n" |
| | ".\\.venv\\Scripts\\python -m pip install spacy\n" |
| | "Then download the model:\n" |
| | ".\\.venv\\Scripts\\python -m spacy download en_core_web_sm" |
| | ) |
| |
|
| | import spacy |
| |
|
| | |
| | try: |
| | |
| | self.nlp = spacy.load("en_core_web_sm") |
| | except Exception as e: |
| | raise RuntimeError( |
| | "SpaCy model 'en_core_web_sm' is required but not available. " |
| | "Install it with:\n" |
| | ".\\.venv\\Scripts\\python -m spacy download en_core_web_sm" |
| | ) from e |
| |
|
| | def __call__(self, data: Dict[str, Any]) -> List[str]: |
| | """ |
| | The __call__ method is called on every request. |
| | """ |
| | |
| | |
| | raw_text = data.pop("inputs", data) |
| |
|
| | |
| | if isinstance(raw_text, list): |
| | raw_text = raw_text[0] |
| |
|
| | |
| | doc = self.nlp(raw_text) |
| | |
| | |
| | raw_sentences = [s.text.strip() for s in doc.sents if s.text.strip()] |
| |
|
| | return raw_sentences |