NamedEntities / app.py
wibberlet's picture
Update app.py
5ded745
raw
history blame
8.58 kB
import gradio as gr
from Summary import Summary
from NamedEntity import NER
entity_sample_text = \
("Mr Roberts had taken his dog for a walk in Hyde Park at around 9pm. "
"He saw a group of people shouting at Stephen - a guy who would shortly "
"have his Rolex watch and iPhone stolen by the same group of people "
"that had surrounded him. A lady named Fiona Walker was crossing the High "
"Street that runs alongside the park. She heard Mr Roberts shout for help "
"and called the police to assist.\n\n Constable Robbins arrived after about "
"20 minutes by which time the group had dispersed. Mr Roberts was able to "
"give a description of the people who had stolen Stephen's Rolex watch and iPhone. "
"He said that one of the people was wearing a blue Adidas t-shirt and another "
"was wearing a red Arsenal football cap. "
"It turned out the gang members hailed from Paddington and Mayfair and used Uber to "
"move around the area.\n\n"
"The gang leader had to appear at "
"the Old Bailey on 1st January 2021. He was sentenced to 3 years in prison "
"for robbery and assault by Judge Jennifer Sanderson."
)
summary_sample_text = \
("The City of London, often simply referred to as The City, is a historic and iconic part of "
"the British capital, London. With a rich history dating back over 2,000 years, it stands as a "
"testament to the enduring legacy of British culture and finance. Covering an area of "
"approximately 1.12 square miles (2.9 square kilometers), it may be small in size, but it packs "
"a punch in terms of its global significance. One of the most notable features of The City is "
"its status as the financial heart of London and, indeed, the world. The area is home to the "
"Bank of England, the London Stock Exchange, and numerous multinational banks and financial "
"institutions. The towering skyscrapers and modern architecture that dot the skyline serve as "
"a symbol of the city's economic power and influence. The City's historic role in finance "
"dates back to the Middle Ages when it became the hub of international trade and commerce. "
"Today, it remains a hub for global finance, attracting professionals from all corners of the "
"globe. The City's historic and architectural heritage is another captivating aspect. Wandering "
"through its labyrinthine streets, one can marvel at the blend of old and new. Ancient structures "
"like the Tower of London and St. Paul's Cathedral coexist with sleek modern office buildings. "
"The contrast in architectural styles is a testament to London's ability to embrace its rich "
"history while continually evolving to meet the demands of the future. Culturally, The City "
"offers a unique blend of tradition and innovation. It hosts various cultural events and "
"festivals throughout the year, attracting both locals and tourists. The City's vibrant food "
"scene is another highlight, with a multitude of restaurants catering to diverse tastes, from "
"classic British fare to international cuisine. Despite its bustling urban environment, The City "
"also boasts several green spaces. One can escape the hustle and bustle of the financial district "
"by strolling along the banks of the River Thames, enjoying the lush gardens of Postman's Park, "
"or exploring the serene Barbican Conservatory. Transportation in The City is well-developed, "
"making it easily accessible. The London Underground, buses, and extensive pedestrian walkways "
"ensure that both residents and visitors can navigate the area efficiently. In conclusion, The "
"City of London is a city within a city, a captivating blend of history, finance, culture, and "
"architecture. Its enduring importance on the global stage, its rich heritage, and its vibrant "
"cultural scene make it a must-visit destination for anyone exploring the dynamic and diverse city "
"of London. Whether you are drawn by its financial prowess, architectural beauty, or cultural "
"riches, The City has something to offer every visitor, and its enduring appeal is sure to stand "
"the test of time."
)
entity_desc = ("This demo uses the [DSLIM BERT model](https://huggingface.co/dslim/bert-base-NER) "
"to identify named entities in a piece of text. It has been trained to recognise "
"four types of entities: location (LOC), organisations (ORG), person (PER) and "
"Miscellaneous (MISC). The model size is approximately 430Mb. \n\n"
"This model is free for commercial use. \n\n"
"A [larger model](https://huggingface.co/dslim/bert-large-NER) is also available (~1.3Gb)."
)
summary_desc = ("This demo uses the "
"[legal-bert-base-uncased model](https://huggingface.co/nlpaueb/legal-bert-base-uncased) "
"intended to assist legal NLP research, computational law, and legal technology "
"applications. The model size is approximately 500Mb. \n\n "
"The model was trained using 12Gb of diverse English legal text across a number of fields. "
"This model is free for commercial use. \n\n"
)
class GlobalVariables:
def __init__(self):
self.entities = None
self.summary = None
app_globals = GlobalVariables()
def process_entities(txt_data):
if txt_data is None or len(txt_data.strip()) == 0:
raise gr.Error("Text to analyse cannot be empty")
app_globals.entities = NER(txt_data)
app_globals.entities.entity_markdown()
entity_list = '\n'.join(app_globals.entities.unique_entities)
heading = 'Entities highlighted in the original text'
output = f'## {heading} \n\n {app_globals.entities.markdown}'
return entity_list, output
def session_data(txt_data):
pass
def process_summary(txt_data):
if txt_data is None or len(txt_data.strip()) == 0:
raise gr.Error("Text to summarise cannot be empty")
app_globals.summary = Summary(txt_data)
result = app_globals.summary.result
source_text_length = len(txt_data.split(' '))
summary_text_length = len(result.split(' '))
info = 'Words in source text: ' + str(source_text_length)
info += '\nWords in summary: ' + str(summary_text_length)
info += ('\nSource text shortened by a factor of: ' +
str(round(source_text_length/summary_text_length, 1)) + ' times')
return info, result
with gr.Blocks() as demo:
# The legal summary appliation tab.
with gr.Tab('Summaries'):
gr.Markdown("# Summarising text")
with gr.Accordion("See Details", open=False):
gr.Markdown(summary_desc)
text_summary_source = gr.Textbox(label="Text to summarise", lines=10)
text_summary = gr.Textbox(label="Summary", lines=3)
text_info = gr.Textbox(label="Related information", lines=5)
with gr.Row():
btn_sample_summary = gr.Button("Load Sample Text")
btn_clear_summary = gr.Button("Clear Summary Data")
btn_summary = gr.Button("Get Summary", variant='primary')
# Event Handler
btn_sample_summary.click(fn=lambda: summary_sample_text, outputs=[text_summary_source])
btn_clear_summary.click(fn=lambda: ('', '', ''), outputs=[text_summary_source, text_summary, text_info])
btn_summary.click(fn=process_summary, inputs=[text_summary_source], outputs=[text_info, text_summary])
with gr.Tab('Entities'):
gr.Markdown("# Extracting named entities")
with gr.Accordion("See Details", open=False):
gr.Markdown(entity_desc)
text_source = gr.Textbox(label="Text to analyse", lines=10)
text_entities = gr.Textbox(label="Unique entities", lines=3)
mk_output = gr.Markdown(label="Entities Highlighted", value='Highlighted entities appear here')
with gr.Row():
btn_sample_entity = gr.Button("Load Sample Text")
btn_clear_entity = gr.Button("Clear Data")
btn_entities = gr.Button("Get Entities", variant='primary')
# Event Handlers
btn_sample_entity.click(fn=lambda: entity_sample_text, outputs=[text_source])
btn_entities.click(fn=process_entities, inputs=[text_source], outputs=[text_entities, mk_output])
btn_clear_entity.click(fn=lambda: ('', '', ''), outputs=[text_source, text_entities, mk_output])
demo.launch()