Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
last_token = merged_tokens[-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 |
-
merged_tokens.append(token)
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
output = ner(input)
|
| 21 |
-
merged_word = merge_tokens(output)
|
| 22 |
-
return {'text': input, 'entities': merged_word}
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
title='Multilingual NER', examples=["My name is Keyur Jotaniya, and I live in Rajkot."])
|
| 28 |
-
a.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load the multilingual NER pipeline
|
| 5 |
+
ner = pipeline("ner", model="Davlan/xlm-roberta-base-ner-hrl", grouped_entities=True)
|
| 6 |
|
| 7 |
+
# Inference function
|
| 8 |
+
def extract_entities(text):
|
| 9 |
+
results = ner(text)
|
| 10 |
+
return [(ent['word'], ent['entity_group']) for ent in results]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Gradio interface
|
| 13 |
+
with gr.Blocks() as demo:
|
| 14 |
+
gr.Markdown(instructions)
|
| 15 |
+
with gr.Row():
|
| 16 |
+
inp = gr.Textbox(label="Enter Text", placeholder="Type a sentence in any language...", lines=3)
|
| 17 |
+
out = gr.HighlightedText(label="Named Entities")
|
| 18 |
+
btn = gr.Button("Extract Entities")
|
| 19 |
|
| 20 |
+
btn.click(fn=extract_entities, inputs=inp, outputs=out)
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Launch
|
| 23 |
+
if __name__ == "__main__":
|
| 24 |
+
demo.launch()
|
|
|
|
|
|