earlalvarado-pi commited on
Commit
c445b94
1 Parent(s): 2efce7e

Adding custom handler

Browse files
Files changed (1) hide show
  1. handler.py +19 -0
handler.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spacy
2
+ from typing import Dict, Any
3
+
4
+ class EndpointHandler:
5
+ def __init__(self):
6
+ self.nlp = spacy.load("./")
7
+
8
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
9
+ text = data.get("text", "")
10
+ doc = self.nlp(text)
11
+ entities = [{"text": ent.text, "start": ent.start_char, "end": ent.end_char, "label": ent.label_} for ent in doc.ents]
12
+ return {"entities": entities}
13
+
14
+ """
15
+ handler = EndpointHandler()
16
+ data = {"text": "Apple is looking at buying U.K. startup for $1 billion"}
17
+ result = handler(data)
18
+ print(result)
19
+ """