Update handler.py
Browse files- handler.py +32 -17
handler.py
CHANGED
|
@@ -1,35 +1,50 @@
|
|
| 1 |
from typing import Dict, List, Any
|
| 2 |
-
import
|
| 3 |
|
| 4 |
class EndpointHandler:
|
| 5 |
def __init__(self, path=""):
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
|
|
|
| 10 |
self.nlp = spacy.load("en_core_web_sm")
|
| 11 |
-
except
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def __call__(self, data: Dict[str, Any]) -> List[str]:
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
data (:obj:): The payload of the request.
|
| 19 |
-
Usually contains {"inputs": "Your text here"}
|
| 20 |
"""
|
| 21 |
-
# 1.
|
|
|
|
| 22 |
raw_text = data.pop("inputs", data)
|
| 23 |
-
|
| 24 |
-
# Handle edge case
|
| 25 |
if isinstance(raw_text, list):
|
| 26 |
raw_text = raw_text[0]
|
| 27 |
|
| 28 |
-
# 2.
|
| 29 |
doc = self.nlp(raw_text)
|
| 30 |
|
| 31 |
-
# 3.
|
| 32 |
raw_sentences = [s.text.strip() for s in doc.sents if s.text.strip()]
|
| 33 |
-
|
| 34 |
-
# 4. Return the list of strings
|
| 35 |
return raw_sentences
|
|
|
|
| 1 |
from typing import Dict, List, Any
|
| 2 |
+
import importlib.util
|
| 3 |
|
| 4 |
class EndpointHandler:
|
| 5 |
def __init__(self, path=""):
|
| 6 |
+
"""
|
| 7 |
+
The __init__ method is called when starting the Endpoint.
|
| 8 |
+
We perform the imports and model loading here to match your logic.
|
| 9 |
+
"""
|
| 10 |
+
# 1. Check if spaCy is installed (Your specific error handling)
|
| 11 |
+
if importlib.util.find_spec("spacy") is None:
|
| 12 |
+
raise RuntimeError(
|
| 13 |
+
"SpaCy is required but not installed. Install it with:\n"
|
| 14 |
+
".\\.venv\\Scripts\\python -m pip install spacy\n"
|
| 15 |
+
"Then download the model:\n"
|
| 16 |
+
".\\.venv\\Scripts\\python -m spacy download en_core_web_sm"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
import spacy
|
| 20 |
+
|
| 21 |
+
# 2. Load the model (Your specific error handling)
|
| 22 |
try:
|
| 23 |
+
# We load the model directly by name since it's installed via requirements.txt
|
| 24 |
self.nlp = spacy.load("en_core_web_sm")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
raise RuntimeError(
|
| 27 |
+
"SpaCy model 'en_core_web_sm' is required but not available. "
|
| 28 |
+
"Install it with:\n"
|
| 29 |
+
".\\.venv\\Scripts\\python -m spacy download en_core_web_sm"
|
| 30 |
+
) from e
|
| 31 |
|
| 32 |
def __call__(self, data: Dict[str, Any]) -> List[str]:
|
| 33 |
"""
|
| 34 |
+
The __call__ method is called on every request.
|
|
|
|
|
|
|
| 35 |
"""
|
| 36 |
+
# 1. Extract inputs
|
| 37 |
+
# The payload usually comes as {"inputs": "some text"}
|
| 38 |
raw_text = data.pop("inputs", data)
|
| 39 |
+
|
| 40 |
+
# Handle edge case where inputs might be a list
|
| 41 |
if isinstance(raw_text, list):
|
| 42 |
raw_text = raw_text[0]
|
| 43 |
|
| 44 |
+
# 2. Run your processing logic
|
| 45 |
doc = self.nlp(raw_text)
|
| 46 |
|
| 47 |
+
# 3. Apply your specific list comprehension
|
| 48 |
raw_sentences = [s.text.strip() for s in doc.sents if s.text.strip()]
|
| 49 |
+
|
|
|
|
| 50 |
return raw_sentences
|