Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from transformers import pipeline
|
4 |
+
get_completion = pipeline("ner", model="dslim/bert-base-NER")
|
5 |
+
|
6 |
+
def merge_tokens(tokens):
|
7 |
+
merged_tokens = []
|
8 |
+
for token in tokens:
|
9 |
+
if (merged_tokens and token['word'].startswith('##')) or (merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:])):
|
10 |
+
last_token = merged_tokens[-1]
|
11 |
+
last_token['word'] += token['word'].replace('##', '')
|
12 |
+
last_token['end'] = token['end']
|
13 |
+
last_token['score'] = (last_token['score'] + token['score']) / 2
|
14 |
+
merged_tokens[-1] = last_token
|
15 |
+
|
16 |
+
else:
|
17 |
+
# Otherwise, add the token to the list
|
18 |
+
merged_tokens.append(token)
|
19 |
+
|
20 |
+
return merged_tokens
|
21 |
+
|
22 |
+
def ner_merged(input):
|
23 |
+
output = get_completion(input)
|
24 |
+
merged_tokens = merge_tokens(output)
|
25 |
+
return {"text": input, "entities": merged_tokens}
|
26 |
+
|
27 |
+
demo = gr.Interface(fn=ner_merged,
|
28 |
+
# inputs=[gr.Textbox(label="Text to find entities", lines=2)],
|
29 |
+
# outputs=[gr.HighlightedText(label="Text with entities")],
|
30 |
+
# title="NER with dslim/bert-base-NER",
|
31 |
+
# description="Find entities using the `dslim/bert-base-NER` model under the hood!",
|
32 |
+
inputs=[gr.Textbox(label="Type or paste text to find Named Entities or even select and submit below examples", lines=2)],
|
33 |
+
outputs=[gr.HighlightedText(label="Text with Named Entities identified")],
|
34 |
+
title="Named Entity Recognition test and demo app by Srinivas.V ",
|
35 |
+
description="Find entities",
|
36 |
+
allow_flagging="never",
|
37 |
+
examples=["My name is Srinivas and I live in Dubai, United Arab Emirates. I love DeepLearningAI",
|
38 |
+
"I am a Data Scientist and I am a citizen of Bharat"])
|
39 |
+
demo.launch()
|