| 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() | |