Keyurjotaniya007 commited on
Commit
f731deb
·
verified ·
1 Parent(s): 59e9c92

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +31 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ ner = pipeline('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 named(input):
22
+ output = ner(input)
23
+ merged_word = merge_tokens(output)
24
+ return {'text': input, 'entities': merged_word}
25
+
26
+ a = gr.Interface(fn=named,
27
+ inputs=[gr.Textbox(label="Text input", lines= 2)],
28
+ outputs=[gr.HighlightedText(label='Text with entities')],
29
+ title='Named Entity Recognition', 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"])
30
+ a.launch()
31
+
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ torch
2
+ transformers