File size: 1,033 Bytes
e94a6aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

ner = pipeline('ner')

def merged_words(tokens):
    m = []
    for token in tokens:
        if m and token['entity'].startswith('I-') and m[-1]['entity'].endswith(token['entity'][2:]):
            last_token = m[-1]
            last_token['word'] += token['word'].replace('##', '')
            last_token['end'] = token['end']
            last_token['score'] = (last_token['score'] + token[score]) / 2
        else:
            m.append(token)
    return m

def named(input):
    output = ner(input)
    merged_words = merged_words(output)
    return {'text': input, 'entities': merged_words}

a = gr.Interface(fn=name, 
                 inputs=[gr.Textbox(label="Text input", lines= 2)],
                 outputs=[gr.HighlightedText(label='Text with entities')],
                 title='Named Entity Recognition', examples=["My name is Andrew, I'm building DeeplearningAI and I live in California", "My name is Poli, I live in Vienna and work at HuggingFace"])
a.launch()