tbdatasci commited on
Commit
66f8e80
·
1 Parent(s): 430f7d9
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+
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['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
10
+ # If current token continues the entity of the last one, merge them
11
+ last_token = merged_tokens[-1]
12
+ last_token['word'] += token['word'].replace('##', '')
13
+ last_token['end'] = token['end']
14
+ last_token['score'] = (last_token['score'] + token['score']) / 2
15
+ else:
16
+ # Otherwise, add the token to the list
17
+ merged_tokens.append(token)
18
+
19
+ return merged_tokens
20
+
21
+ def ner(input):
22
+ output = get_completion(input)
23
+ merged_tokens = merge_tokens(output)
24
+ return {"text": input, "entities": merged_tokens}
25
+
26
+
27
+ gr.close_all()
28
+ demo = gr.Interface(fn=ner,
29
+ inputs=[gr.Textbox(label="Text to find entities", lines=2)],
30
+ outputs=[gr.HighlightedText(label="Text with entities")],
31
+ title="Named Entity Recognition Device",
32
+ description="Find entities in the text!",
33
+ allow_flagging="never",
34
+ examples=["My name is Andrew, I'm building DeeplearningAI and I live in California.", "My name is Tyler, I live in Florida for income tax purposes and like to build cool stuff."])
35
+
36
+ demo.launch()