Ashish08 commited on
Commit
f99c3a8
1 Parent(s): 853a4c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -1,19 +1,33 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- get_completion = pipeline("ner", model="dslim/bert-base-NER")
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def ner(input):
7
- output = get_completion(input)
8
- return {"text": input, "entities": output}
 
9
 
10
  gr.close_all()
11
  demo = gr.Interface(fn=ner,
12
  inputs=[gr.Textbox(label="Text to find entities", lines=2)],
13
  outputs=[gr.HighlightedText(label="Text with entities")],
14
- title="Named Entity Recognition - NER ",
15
  description="Find entities using the `dslim/bert-base-NER` model under the hood!",
16
  allow_flagging="never",
17
- #Here we introduce a new tag, examples, easy to use examples for your application
18
- examples=["My name is Andrew and I live in California", "My name is Poli and work at HuggingFace"])
19
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
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']) / 2
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 = get_completion(input, parameters=None, ENDPOINT_URL=API_URL)
21
+ merged_tokens = merge_tokens(output)
22
+ return {"text": input, "entities": merged_tokens}
23
 
24
  gr.close_all()
25
  demo = gr.Interface(fn=ner,
26
  inputs=[gr.Textbox(label="Text to find entities", lines=2)],
27
  outputs=[gr.HighlightedText(label="Text with entities")],
28
+ title="NER with dslim/bert-base-NER",
29
  description="Find entities using the `dslim/bert-base-NER` model under the hood!",
30
  allow_flagging="never",
31
+ examples=["My name is Andrew, I'm building DeeplearningAI and I live in California", "My name is Poli, I live in Vienna and work at HuggingFace"])
32
+
33
  demo.launch()