File size: 863 Bytes
c10350f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8
# author: xusong <xusong28@jd.com>
# time: 2022/8/25 16:57


"""

## ner demo
- https://gradio.app/named_entity_recognition/
- https://huggingface.co/dslim/bert-base-NER?text=My+name+is+Wolfgang+and+I+live+in+Berlin

"""

from transformers import pipeline

import gradio as gr

ner_pipeline = pipeline("ner")

examples = [
    "Does Chicago have any stores and does Joe live here?",
]

import json

def ner(text):
    output = ner_pipeline(text)
    for ent in output:
        ent["score"] = float(ent["score"])
    aa = {"text": text, "entities": output}
    return aa, output


demo = gr.Interface(
    ner,
    inputs=gr.Textbox(placeholder="Enter sentence here..."),
    outputs=
    [
        gr.HighlightedText(
            label="NER",
            show_legend=True,
        ),
        gr.JSON(),
    ],
    examples=examples)

demo.launch()