Sakil commited on
Commit
db0f8ba
·
1 Parent(s): c4c96c5

code added

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+ completion_pipeline=pipeline("ner", model="dslim/bert-base-NER")
4
+ def merge_tokens(tokens):
5
+ merged_tokens = []
6
+ for token in tokens:
7
+ if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
8
+ # If current token continues the entity of the last one, merge them
9
+ last_token = merged_tokens[-1]
10
+ last_token['word'] += token['word'].replace('##', '')
11
+ last_token['end'] = token['end']
12
+ last_token['score'] = (last_token['score'] + token['score'])
13
+ else:
14
+ # Otherwise, add the token to the list
15
+ merged_tokens.append(token)
16
+
17
+ return merged_tokens
18
+
19
+ def ner(input):
20
+ output = completion_pipeline(input)
21
+ merged_tokens = merge_tokens(output)
22
+ return {"text": input, "entities": merged_tokens}
23
+
24
+ demo = gr.Interface(fn=ner,
25
+ inputs=[gr.Textbox(label="Text to find entities", lines=2)],
26
+ outputs=[gr.HighlightedText(label="Text with entities")],
27
+ title="Named Entity",
28
+ description="You can use this application for finding entities in your data.",
29
+ allow_flagging="never",
30
+ examples=["My name is Sakil Ansari, I'm watching movies", "Nepal is a beautiful country.",
31
+ "Batman is a great character.",
32
+ "Murphy was the first cast member to sign the film."])
33
+
34
+ demo.launch(share=True)