File size: 1,258 Bytes
cc36c46
 
 
 
 
88734c0
 
 
 
 
 
 
 
 
 
 
 
cc36c46
 
88734c0
 
 
cc36c46
 
 
88734c0
 
 
 
cc36c46
 
 
 
 
 
 
88734c0
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
# 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="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.", 
]

def ner(text):
    output = pipeline_metaphors(text)
    # change name 
    for x in output:
        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(share=True)