en_core_web_sm / handler.py
earlalvarado-pi's picture
Update handler.py
bb2d701 verified
raw
history blame
776 Bytes
import subprocess
import sys
try:
import spacy
except ImportError:
# Attempt to install spacy
subprocess.check_call([sys.executable, "-m", "pip", "install", "spacy"])
import spacy
import spacy
from typing import Dict, Any
class EndpointHandler:
def __init__(self, path=""):
self.nlp = spacy.load(path)
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
text = data.get("text", "")
doc = self.nlp(text)
entities = [{"text": ent.text, "start": ent.start_char, "end": ent.end_char, "label": ent.label_} for ent in doc.ents]
return {"entities": entities}
"""
handler = EndpointHandler()
data = {"text": "Apple is looking at buying U.K. startup for $1 billion"}
result = handler(data)
print(result)
"""