update
Browse files- app.py +32 -24
- requirements.txt +2 -2
app.py
CHANGED
@@ -1,36 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
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 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
|
21 |
|
22 |
def predict(text):
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
ner_pipeline(document, predict_field="entities")
|
26 |
-
|
27 |
doc = {
|
28 |
-
"text":
|
29 |
"ents": [{
|
30 |
-
"start": entity
|
31 |
-
"end": entity
|
32 |
-
"label": entity
|
33 |
-
} for entity in
|
34 |
"title": None
|
35 |
}
|
36 |
|
@@ -48,8 +54,10 @@ iface = gr.Interface(
|
|
48 |
fn=predict,
|
49 |
inputs=gr.inputs.Textbox(
|
50 |
lines=5,
|
51 |
-
|
|
|
|
|
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 |
-
|
2 |
-
|
|
|
1 |
+
spacy
|
2 |
+
tner
|