Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from flair.data import Sentence
|
| 3 |
+
from flair.models import SequenceTagger
|
| 4 |
+
|
| 5 |
+
tagger = SequenceTagger.load('best-model.pt')
|
| 6 |
+
|
| 7 |
+
def run_ner(input_text):
|
| 8 |
+
sentence = Sentence(input_text)
|
| 9 |
+
tagger.predict(sentence)
|
| 10 |
+
entities = []
|
| 11 |
+
for entity in sentence.get_spans('ner'):
|
| 12 |
+
entities.append((entity.text, entity.get_label('ner').value, entity.get_label('ner').score))
|
| 13 |
+
return entities
|
| 14 |
+
|
| 15 |
+
demo = gr.Interface(fn=run_ner,
|
| 16 |
+
title='Named Entity Recognition Demo',
|
| 17 |
+
description='This demo performs **Named Entity Recognition** by tagging user-inputted sentence(s). Give it a try by entering a sentence or using one of the provided examples. Common tags include **geo** (geographical entity), **org** (organization), **per** (person), and **tim** (time). In the box on the right, the results will show the tagged words and their corresponding confidence scores.',
|
| 18 |
+
article='*This demo is based on a Named Entity Recognition model trained by Curtis Pond and Julia Nickerson as part of their FourthBrain capstone project. For more information, check out their [GitHub repo](https://github.com/nickersonj/glg-capstone).*',
|
| 19 |
+
inputs=gr.Textbox(label='Input Text', lines=2, placeholder='Type some text here...'),
|
| 20 |
+
outputs=gr.Textbox(label='Named Entity Recognition Results', lines=2, placeholder=''),
|
| 21 |
+
examples=['The indictments were announced Tuesday by the Justice Department in Cairo.', "In 2019, the men's singles winner was Novak Djokovic who defeated Roger Federer in a tournament taking place in the United Kingdom.", 'In a study published by the American Heart Association on January 18, researchers at the Johns Hopkins School of Medicine found that meal timing did not impact weight.'],
|
| 22 |
+
allow_flagging='never'
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
demo.launch()
|