Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Stolen from: https://huggingface.co/spaces/AyushDey/Named_Entity_Recognition/tree/main
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
ner = pipeline('ner')
|
7 |
+
|
8 |
+
def merge_tokens(tokens):
|
9 |
+
merged_tokens = []
|
10 |
+
for token in tokens:
|
11 |
+
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
|
12 |
+
# If current token continues the entity of the last one, merge them
|
13 |
+
last_token = merged_tokens[-1]
|
14 |
+
last_token['word'] += token['word'].replace('##', '')
|
15 |
+
last_token['end'] = token['end']
|
16 |
+
last_token['score'] = (last_token['score'] + token['score']) / 2
|
17 |
+
else:
|
18 |
+
# Otherwise, add the token to the list
|
19 |
+
merged_tokens.append(token)
|
20 |
+
|
21 |
+
return merged_tokens
|
22 |
+
|
23 |
+
def named(input):
|
24 |
+
output = ner(input)
|
25 |
+
merged_word = merge_tokens(output)
|
26 |
+
return {'text': input, 'entities': merged_word}
|
27 |
+
|
28 |
+
a = gr.Interface(fn=named,
|
29 |
+
inputs=[gr.Textbox(label="Text input", lines= 2)],
|
30 |
+
outputs=[gr.HighlightedText(label='Text with entities')],
|
31 |
+
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"])
|
32 |
+
a.launch()
|