en_core_web_sm / handler.py
wjbeeson's picture
Update handler.py
c4f6779 verified
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