David Kagramanyan commited on
Commit
37516c9
·
1 Parent(s): a59a366

updated ner display

Browse files
Files changed (1) hide show
  1. app.py +50 -19
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
- tokens = response.json()
24
 
25
- entities = []
26
 
27
- for token in tokens:
28
- label = token["entity"]
 
 
 
 
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
- token["label"] = label
38
- entities.append(token)
39
-
40
- params = [{"text": input_text_message,
41
- "ents": entities,
42
- "title": None}]
43
-
44
- return displacy.render(params, style="ent", manual=True, options={
45
- "colors": {
46
- "Observation": "#9bddff",
47
- "Evaluation": "#f08080",
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',