File size: 2,346 Bytes
c5833f7
8728555
c5833f7
 
8728555
c5833f7
8728555
 
 
 
 
 
 
 
 
c5833f7
8728555
 
 
 
 
c5833f7
 
 
8728555
 
 
 
 
 
 
c5833f7
 
8728555
c5833f7
8728555
 
 
 
c5833f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8728555
 
 
c5833f7
 
 
8728555
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
from tner import TransformersNER
from spacy import displacy

model = TransformersNER("tner/roberta-large-ontonotes5")

# DUMMY = {
#    'prediction': [['B-person', 'I-person', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-location']],
#    'probability': [[0.9967652559280396, 0.9994561076164246, 0.9986955523490906, 0.9947081804275513, 0.6129112243652344, 0.9984312653541565, 0.9868122935295105, 0.9983410835266113, 0.9995284080505371, 0.9838910698890686]],
#    'input': [['Jacob', 'Collier', 'is', 'a', 'Grammy', 'awarded', 'English', 'artist', 'from', 'London']],
#    'entity_prediction': [[
#        {'type': 'person', 'entity': ['Jacob', 'Collier'], 'position': [0, 1], 'probability': [0.9967652559280396, 0.9994561076164246]},
#        {'type': 'location', 'entity': ['London'], 'position': [9], 'probability': [0.9838910698890686]}
#     ]]
# }

examples = [
    "Jacob Collier is a Grammy awarded artist from England.",
    "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.",
    "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."
]


def predict(text):
    output = model.predict([text])
    tokens = output['input'][0]

    def retain_char_position(p):
        if p == 0:
            return 0
        return len(' '.join(tokens[:p])) + 1

    doc = {
        "text": text,
        "ents": [{
            "start": retain_char_position(entity['position'][0]),
            "end": retain_char_position(entity['position'][-1]) + 1 + len(entity['entity'][-1]),
            "label": entity['type']
        } for entity in output['entity_prediction'][0]],
        "title": None
    }

    html = displacy.render(doc, style="ent", page=True, manual=True, minify=True)
    html = (
        "<div style='max-width:100%; max-height:360px; overflow:auto'>"
        + html
        + "</div>"
    )
    
    return html


iface = gr.Interface(
    fn=predict,
    inputs=gr.inputs.Textbox(
        lines=5,
        placeholder="Input Sentence",
        default=examples[0],
        examples=examples
    ),
    outputs="html",
)
iface.launch()