asahi417 commited on
Commit
8728555
·
1 Parent(s): c13b588
Files changed (2) hide show
  1. app.py +32 -24
  2. requirements.txt +2 -2
app.py CHANGED
@@ -1,36 +1,42 @@
1
  import gradio as gr
2
- from dataclasses import dataclass
3
-
4
- from pytorch_ie.annotations import LabeledSpan
5
- from pytorch_ie.auto import AutoPipeline
6
- from pytorch_ie.core import AnnotationList, annotation_field
7
- from pytorch_ie.documents import TextDocument
8
-
9
  from spacy import displacy
10
 
 
11
 
12
- @dataclass
13
- class ExampleDocument(TextDocument):
14
- entities: AnnotationList[LabeledSpan] = annotation_field(target="text")
15
-
 
 
 
 
 
16
 
17
- model_name_or_path = "pie/example-ner-spanclf-conll03"
18
-
19
- ner_pipeline = AutoPipeline.from_pretrained(model_name_or_path, device=-1, num_workers=0)
 
 
20
 
21
 
22
  def predict(text):
23
- document = ExampleDocument(text)
 
 
 
 
 
 
24
 
25
- ner_pipeline(document, predict_field="entities")
26
-
27
  doc = {
28
- "text": document.text,
29
  "ents": [{
30
- "start": entity.start,
31
- "end": entity.end,
32
- "label": entity.label
33
- } for entity in sorted(document.entities.predictions, key=lambda e: e.start)],
34
  "title": None
35
  }
36
 
@@ -48,8 +54,10 @@ iface = gr.Interface(
48
  fn=predict,
49
  inputs=gr.inputs.Textbox(
50
  lines=5,
51
- default="There is still some uncertainty that Musk - also chief executive of electric car maker Tesla and rocket company SpaceX - will pull off his planned buyout.",
 
 
52
  ),
53
  outputs="html",
54
  )
55
- iface.launch()
 
1
  import gradio as gr
2
+ from tner import TransformersNER
 
 
 
 
 
 
3
  from spacy import displacy
4
 
5
+ model = TransformersNER("tner/roberta-large-ontonotes5")
6
 
7
+ # DUMMY = {
8
+ # 'prediction': [['B-person', 'I-person', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-location']],
9
+ # 'probability': [[0.9967652559280396, 0.9994561076164246, 0.9986955523490906, 0.9947081804275513, 0.6129112243652344, 0.9984312653541565, 0.9868122935295105, 0.9983410835266113, 0.9995284080505371, 0.9838910698890686]],
10
+ # 'input': [['Jacob', 'Collier', 'is', 'a', 'Grammy', 'awarded', 'English', 'artist', 'from', 'London']],
11
+ # 'entity_prediction': [[
12
+ # {'type': 'person', 'entity': ['Jacob', 'Collier'], 'position': [0, 1], 'probability': [0.9967652559280396, 0.9994561076164246]},
13
+ # {'type': 'location', 'entity': ['London'], 'position': [9], 'probability': [0.9838910698890686]}
14
+ # ]]
15
+ # }
16
 
17
+ examples = [
18
+ "Jacob Collier is a Grammy awarded artist from England.",
19
+ "When Sebastian Thrun PERSON started working on self-driving cars at Google ORG in 2007 DATE , few people outside of the company took him seriously.",
20
+ "But Google ORGis starting from behind. The company made a late push into hardware, and Apple ORG’s Siri, available on iPhones, and Amazon ORG’s Alexa software, which runs on its Echo and Dot devices, have clear leads in consumer adoption."
21
+ ]
22
 
23
 
24
  def predict(text):
25
+ output = model.predict([text])
26
+ tokens = output['input'][0]
27
+
28
+ def retain_char_position(p):
29
+ if p == 0:
30
+ return 0
31
+ return len(' '.join(tokens[:p])) + 1
32
 
 
 
33
  doc = {
34
+ "text": text,
35
  "ents": [{
36
+ "start": retain_char_position(entity['position'][0]),
37
+ "end": retain_char_position(entity['position'][-1]) + 1 + len(entity['entity'][-1]),
38
+ "label": entity['type']
39
+ } for entity in output['entity_prediction'][0]],
40
  "title": None
41
  }
42
 
 
54
  fn=predict,
55
  inputs=gr.inputs.Textbox(
56
  lines=5,
57
+ placeholder="Input Sentence",
58
+ default=examples[0],
59
+ examples=examples
60
  ),
61
  outputs="html",
62
  )
63
+ iface.launch()
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- pytorch-ie>=0.6.0
2
- spacy
 
1
+ spacy
2
+ tner