File size: 815 Bytes
23382e9
 
 
 
 
 
 
 
 
 
 
 
 
8d90324
 
 
23382e9
8d90324
bb2d701
23382e9
 
c7a97db
23382e9
83fa20d
8d90324
83fa20d
 
 
8d90324
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
31
32
33
34
35
36
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

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("inputs", "")
        doc = self.nlp(text)
        entities = [
            {"word": ent.text, "entity_group": ent.label_, "score": 1.0}  
            for ent in doc.ents
        ]
        return entities

"""
handler = EndpointHandler()
data = {"text": "Apple is looking at buying U.K. startup for $1 billion"}
result = handler(data)
print(result)
"""