File size: 1,876 Bytes
80a10c3 c4f6779 80a10c3 c4f6779 80a10c3 c4f6779 80a10c3 c4f6779 80a10c3 c4f6779 80a10c3 c4f6779 80a10c3 c4f6779 80a10c3 c4f6779 80a10c3 c4f6779 80a10c3 c4f6779 80a10c3 | 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 | 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.
"""
# 1. Check if spaCy is installed (Your specific error handling)
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
# 2. Load the model (Your specific error handling)
try:
# We load the model directly by name since it's installed via requirements.txt
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.
"""
# 1. Extract inputs
# The payload usually comes as {"inputs": "some text"}
raw_text = data.pop("inputs", data)
# Handle edge case where inputs might be a list
if isinstance(raw_text, list):
raw_text = raw_text[0]
# 2. Run your processing logic
doc = self.nlp(raw_text)
# 3. Apply your specific list comprehension
raw_sentences = [s.text.strip() for s in doc.sents if s.text.strip()]
return raw_sentences |