Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
ner = pipeline('ner')
|
5 |
+
|
6 |
+
def merged_words(tokens):
|
7 |
+
m = []
|
8 |
+
for token in tokens:
|
9 |
+
if m and token['entity'].startswith('I-') and m[-1]['entity'].endswith(token['entity'][2:]):
|
10 |
+
last_token = m[-1]
|
11 |
+
last_token['word'] += token['word'].replace('##', '')
|
12 |
+
last_token['end'] = token['end']
|
13 |
+
last_token['score'] = (last_token['score'] + token[score]) / 2
|
14 |
+
else:
|
15 |
+
m.append(token)
|
16 |
+
return m
|
17 |
+
|
18 |
+
def named(input):
|
19 |
+
output = ner(input)
|
20 |
+
merged_words = merged_words(output)
|
21 |
+
return {'text': input, 'entities': merged_words}
|
22 |
+
|
23 |
+
a = gr.Interface(fn=name,
|
24 |
+
inputs=[gr.Textbox(label="Text input", lines= 2)],
|
25 |
+
outputs=[gr.HighlightedText(label='Text with entities')],
|
26 |
+
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"])
|
27 |
+
a.launch()
|
28 |
+
|