earlalvarado-pi
commited on
Commit
•
23382e9
1
Parent(s):
c445b94
Update handler.py
Browse files- handler.py +29 -19
handler.py
CHANGED
@@ -1,19 +1,29 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
import sys
|
3 |
+
|
4 |
+
try:
|
5 |
+
import spacy
|
6 |
+
except ImportError:
|
7 |
+
# Attempt to install spacy
|
8 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "spacy"])
|
9 |
+
import spacy
|
10 |
+
|
11 |
+
import spacy
|
12 |
+
from typing import Dict, Any
|
13 |
+
|
14 |
+
class EndpointHandler:
|
15 |
+
def __init__(self):
|
16 |
+
self.nlp = spacy.load("./")
|
17 |
+
|
18 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
19 |
+
text = data.get("text", "")
|
20 |
+
doc = self.nlp(text)
|
21 |
+
entities = [{"text": ent.text, "start": ent.start_char, "end": ent.end_char, "label": ent.label_} for ent in doc.ents]
|
22 |
+
return {"entities": entities}
|
23 |
+
|
24 |
+
"""
|
25 |
+
handler = EndpointHandler()
|
26 |
+
data = {"text": "Apple is looking at buying U.K. startup for $1 billion"}
|
27 |
+
result = handler(data)
|
28 |
+
print(result)
|
29 |
+
"""
|