Spaces:
Runtime error
Runtime error
David Kagramanyan
commited on
Commit
·
37516c9
1
Parent(s):
a59a366
updated ner display
Browse files
app.py
CHANGED
@@ -5,6 +5,17 @@ import requests
|
|
5 |
|
6 |
from spacy import displacy
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def compute_ner(input_text_message):
|
10 |
endpoint_url = 'https://on1m82uknekghqeh.us-east-1.aws.endpoints.huggingface.cloud'
|
@@ -20,33 +31,53 @@ def compute_ner(input_text_message):
|
|
20 |
|
21 |
response = requests.post(endpoint_url, headers=headers, json=json_data)
|
22 |
|
23 |
-
|
24 |
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
if label == "I-Observation" or label == "B-Observation":
|
31 |
label = "Observation"
|
32 |
-
token["label"] = label
|
33 |
-
entities.append(token)
|
34 |
|
35 |
if label == "I-Evaluation" or label == "B-Evaluation":
|
36 |
label = "Evaluation"
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
|
52 |
examples = ['You are dick',
|
|
|
5 |
|
6 |
from spacy import displacy
|
7 |
|
8 |
+
os.system("python -m spacy download en_core_web_md")
|
9 |
+
import spacy
|
10 |
+
|
11 |
+
|
12 |
+
colors = {
|
13 |
+
"Observation": "#9bddff",
|
14 |
+
"Evaluation": "#f08080",
|
15 |
+
}
|
16 |
+
|
17 |
+
nlp = spacy.load("en_core_web_md") #Esto es para usar displacy y renderizar las entidades
|
18 |
+
nlp.disable_pipes("ner")
|
19 |
|
20 |
def compute_ner(input_text_message):
|
21 |
endpoint_url = 'https://on1m82uknekghqeh.us-east-1.aws.endpoints.huggingface.cloud'
|
|
|
31 |
|
32 |
response = requests.post(endpoint_url, headers=headers, json=json_data)
|
33 |
|
34 |
+
entities = response.json()
|
35 |
|
36 |
+
doc = nlp(input_text_message)
|
37 |
|
38 |
+
potential_entities = []
|
39 |
+
|
40 |
+
for entity in entities:
|
41 |
+
start = entity["start"]
|
42 |
+
end = entity["end"]
|
43 |
+
label = entity["entity"]
|
44 |
|
45 |
if label == "I-Observation" or label == "B-Observation":
|
46 |
label = "Observation"
|
|
|
|
|
47 |
|
48 |
if label == "I-Evaluation" or label == "B-Evaluation":
|
49 |
label = "Evaluation"
|
50 |
+
|
51 |
+
entity["entity"]=label
|
52 |
+
|
53 |
+
ent = doc.char_span(start, end, label=label)
|
54 |
+
if ent != None:
|
55 |
+
doc.ents += (ent,)
|
56 |
+
else:
|
57 |
+
potential_entities.append(entity)
|
58 |
+
|
59 |
+
potential_entities.append({"entity": "NONE", "start": -1, "end": -1})
|
60 |
+
|
61 |
+
start = potential_entities[0]["start"]
|
62 |
+
end = potential_entities[0]["end"]
|
63 |
+
label = potential_entities[0]["entity"]
|
64 |
+
|
65 |
+
for item in potential_entities:
|
66 |
+
if item["entity"] == label and item["start"] == end:
|
67 |
+
end = item["end"]
|
68 |
+
continue
|
69 |
+
else:
|
70 |
+
if item["start"] != start:
|
71 |
+
ent = doc.char_span(start, end, label=label)
|
72 |
+
doc.ents += (ent,)
|
73 |
+
|
74 |
+
start = item["start"]
|
75 |
+
end = item["end"]
|
76 |
+
label = item["entity"]
|
77 |
+
|
78 |
+
options = {"ents": colors.keys(), "colors": colors}
|
79 |
+
|
80 |
+
return displacy.render(doc, style="ent", options=options)
|
81 |
|
82 |
|
83 |
examples = ['You are dick',
|