earlalvarado-pi commited on
Commit
23382e9
1 Parent(s): c445b94

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +29 -19
handler.py CHANGED
@@ -1,19 +1,29 @@
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
- """
 
 
 
 
 
 
 
 
 
 
 
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
+ """