File size: 776 Bytes
23382e9 bbd6166 bb2d701 23382e9 |
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 |
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)
"""
|