MFawad commited on
Commit
fbafa8b
ยท
1 Parent(s): 95f7634

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +45 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ from IPython.display import Image, display, HTML
4
+ from PIL import Image
5
+ import base64
6
+ from transformers import pipeline
7
+ import gradio as gr
8
+ hf_api_key = "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"
9
+
10
+ get_completion = pipeline("ner", model="dslim/bert-base-NER")
11
+
12
+ def ner(input):
13
+ output = get_completion(input)
14
+ return {"text": input, "entities": output}
15
+
16
+ def merge_tokens(tokens):
17
+ merged_tokens = []
18
+ for token in tokens:
19
+ if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
20
+ # If current token continues the entity of the last one, merge them
21
+ last_token = merged_tokens[-1]
22
+ last_token['word'] += token['word'].replace('##', '')
23
+ last_token['end'] = token['end']
24
+ last_token['score'] = (last_token['score'] + token['score']) / 2
25
+ else:
26
+ # Otherwise, add the token to the list
27
+ merged_tokens.append(token)
28
+
29
+ return merged_tokens
30
+
31
+ def ner(input):
32
+ output = get_completion(input)
33
+ merged_tokens = merge_tokens(output)
34
+ return {"text": input, "entities": merged_tokens}
35
+
36
+ gr.close_all()
37
+ demo = gr.Interface(fn=ner,
38
+ inputs=[gr.Textbox(label="Text to find entities", lines=2)],
39
+ outputs=[gr.HighlightedText(label="Text with entities")],
40
+ title="NER with dslim/bert-base-NER๐Ÿ”Ž๐Ÿ—บ๐Ÿ“Œ",
41
+ description="Find entities using the `dslim/bert-base-NER` model under the hood!",
42
+ allow_flagging="never",
43
+ examples=["My name is Fawad, I'm building Named Entity Recognizer App and I live in Karachi, Pakistan", "Paul is my friend and he is new in Islamabad"])
44
+
45
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ IPython
2
+ gradio
3
+ transformers