File size: 3,621 Bytes
cc36c46
 
 
 
 
88734c0
 
 
 
 
 
 
 
 
 
 
f4f893f
 
 
 
 
 
 
 
 
cc36c46
 
88734c0
 
 
cc36c46
 
f4f893f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc36c46
88734c0
f4f893f
88734c0
f4f893f
 
cc36c46
 
f4f893f
 
cc36c46
 
 
 
 
f4f893f
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# REF: https://gradio.app/named_entity_recognition/
from transformers import pipeline

import gradio as gr


model_name="xlm-roberta-base"
# model_name="roberta-large"  
from transformers import AutoTokenizer, AutoModelForTokenClassification


label_list= ['literal',"metaphoric"]
label_dict_relations={ i : l for i, l in enumerate(label_list) }
PATH = "./saved-models/my_model"
model_metaphor_detection = AutoModelForTokenClassification.from_pretrained(PATH, id2label=label_dict_relations)
tokenizer = AutoTokenizer.from_pretrained(model_name)

pipeline_metaphors=pipeline(
    "ner", 
    model=model_metaphor_detection, 
    tokenizer=tokenizer, 
    aggregation_strategy="none",
    # aggregation_strategy="simple",
    )


examples = [
    "It would change the trajectory of your legal career.",
    "Washington and the media just explodes on you, you just don’t know where you are at the moment", 
    "Those statements are deeply concerning.", 
]

# Demo usage
import pprint
detection_results = pipeline_metaphors("It would change the trajectory of your legal career.")
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(detection_results)

"""Example Output; aggregation_strategy="none"
[   {   'end': 2,
        'entity': 'literal',
        'index': 1,
        'score': 0.99981445,
        'start': 0,
        'word': '▁It'},
    {   'end': 8,
        'entity': 'literal',
        'index': 2,
        'score': 0.9999882,
        'start': 3,
        'word': '▁would'},
    {   'end': 15,
        'entity': 'literal',
        'index': 3,
        'score': 0.6243065,
        'start': 9,
        'word': '▁change'},
    {   'end': 19,
        'entity': 'literal',
        'index': 4,
        'score': 0.9999826,
        'start': 16,
        'word': '▁the'},
    {   'end': 27,
        'entity': 'metaphoric',
        'index': 5,
        'score': 0.99631363,
        'start': 20,
        'word': '▁traject'},
    {   'end': 30,
        'entity': 'metaphoric',
        'index': 6,
        'score': 0.9979997,
        'start': 27,
        'word': 'ory'},
    {   'end': 33,
        'entity': 'literal',
        'index': 7,
        'score': 0.9996278,
        'start': 31,
        'word': '▁of'},
    {   'end': 38,
        'entity': 'literal',
        'index': 8,
        'score': 0.99985147,
        'start': 34,
        'word': '▁your'},
    {   'end': 44,
        'entity': 'literal',
        'index': 9,
        'score': 0.99984956,
        'start': 39,
        'word': '▁legal'},
    {   'end': 51,
        'entity': 'literal',
        'index': 10,
        'score': 0.998919,
        'start': 45,
        'word': '▁career'},
    {   'end': 52,
        'entity': 'literal',
        'index': 11,
        'score': 0.99775606,
        'start': 51,
        'word': '.'}]
"""


"""Example Output; aggregation_strategy="simple"
[   {   'end': 19,
    'entity_group': 'literal',
    'score': 0.9060229,
    'start': 0,
    'word': 'It would change the'},
{   'end': 30,
    'entity_group': 'metaphoric',
    'score': 0.9971567,
    'start': 20,
    'word': 'trajectory'},
{   'end': 52,
    'entity_group': 'literal',
    'score': 0.9992008,
    'start': 31,
    'word': 'of your legal career.'}]

"""
# exit(0)

def ner(text):
    output = pipeline_metaphors(text)
    # # change name 
    for x in output:
        if 'entity_group' in x:
            x['entity'] = x['entity_group']
    return {"text": text, "entities": output}    



demo = gr.Interface(ner,
             gr.Textbox(placeholder="Enter sentence here..."), 
             gr.HighlightedText(),
             examples=examples)

demo.launch()