Sakil's picture
Update app.py
471b1f0
from transformers import pipeline
import gradio as gr
completion_pipeline=pipeline("ner", model="dslim/bert-base-NER")
def merge_tokens(tokens):
merged_tokens = []
for token in tokens:
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
# If current token continues the entity of the last one, merge them
last_token = merged_tokens[-1]
last_token['word'] += token['word'].replace('##', '')
last_token['end'] = token['end']
last_token['score'] = (last_token['score'] + token['score'])
else:
# Otherwise, add the token to the list
merged_tokens.append(token)
return merged_tokens
def ner(input):
output = completion_pipeline(input)
merged_tokens = merge_tokens(output)
return {"text": input, "entities": merged_tokens}
demo = gr.Interface(fn=ner,
inputs=[gr.Textbox(label="Text to find entities", lines=2)],
outputs=[gr.HighlightedText(label="Text with entities")],
title="Named Entity",
description="You can use this application for finding entities in your data.",
allow_flagging="never",
examples=["My name is Sakil Ansari, I'm watching movies", "Nepal is a beautiful country.",
"Batman is a great character.",
"Murphy was the first cast member to sign the film."])
demo.launch()