Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,59 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from NamedEntity import NER
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
entity_desc = ("This demo uses the [DSLIM BERT model](https://huggingface.co/dslim/bert-base-NER) "
|
47 |
"to identify named entities in a piece of text. It has been trained to recognise "
|
@@ -60,28 +72,77 @@ summary_desc = ("This demo uses the "
|
|
60 |
)
|
61 |
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
def process_entities(txt_data):
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
66 |
|
67 |
-
entity_list = '\n'.join(
|
68 |
|
69 |
heading = 'Entities highlighted in the original text'
|
70 |
-
output = f'## {heading} \n\n {
|
71 |
|
72 |
return entity_list, output
|
73 |
|
74 |
|
|
|
|
|
|
|
|
|
75 |
def process_summary(txt_data):
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
|
79 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
with gr.Tab('Entities'):
|
81 |
-
gr.Markdown("#
|
82 |
with gr.Accordion("See Details", open=False):
|
83 |
gr.Markdown(entity_desc)
|
84 |
-
text_source = gr.Textbox(label="Text to analyse",
|
85 |
text_entities = gr.Textbox(label="Unique entities", lines=3)
|
86 |
mk_output = gr.Markdown(label="Entities Highlighted", value='Highlighted entities appear here')
|
87 |
with gr.Row():
|
@@ -90,8 +151,9 @@ with gr.Blocks() as demo:
|
|
90 |
btn_entities = gr.Button("Get Entities", variant='primary')
|
91 |
|
92 |
# Event Handlers
|
93 |
-
btn_sample_entity.click(fn=lambda:
|
94 |
btn_entities.click(fn=process_entities, inputs=[text_source], outputs=[text_entities, mk_output])
|
95 |
btn_clear_entity.click(fn=lambda: ('', '', ''), outputs=[text_source, text_entities, mk_output])
|
96 |
|
97 |
-
demo.launch()
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from Summary import Summary
|
3 |
from NamedEntity import NER
|
4 |
|
5 |
+
entity_sample_text = \
|
6 |
+
("Mr Roberts had taken his dog for a walk in Hyde Park at around 9pm. "
|
7 |
+
"He saw a group of people shouting at Stephen - a guy who would shortly "
|
8 |
+
"have his Rolex watch and iPhone stolen by the same group of people "
|
9 |
+
"that had surrounded him. A lady named Fiona Walker was crossing the High "
|
10 |
+
"Street that runs alongside the park. She heard Mr Roberts shout for help "
|
11 |
+
"and called the police to assist.\n\n Constable Robbins arrived after about "
|
12 |
+
"20 minutes by which time the group had dispersed. Mr Roberts was able to "
|
13 |
+
"give a description of the people who had stolen Stephen's Rolex watch and iPhone. "
|
14 |
+
"He said that one of the people was wearing a blue Adidas t-shirt and another "
|
15 |
+
"was wearing a red Arsenal football cap. "
|
16 |
+
"It turned out the gang members hailed from Paddington and Mayfair and used Uber to "
|
17 |
+
"move around the area.\n\n"
|
18 |
+
"The gang leader had to appear at "
|
19 |
+
"the Old Bailey on 1st January 2021. He was sentenced to 3 years in prison "
|
20 |
+
"for robbery and assault by Judge Jennifer Sanderson."
|
21 |
+
)
|
22 |
+
|
23 |
+
summary_sample_text = \
|
24 |
+
("The City of London, often simply referred to as The City, is a historic and iconic part of "
|
25 |
+
"the British capital, London. With a rich history dating back over 2,000 years, it stands as a "
|
26 |
+
"testament to the enduring legacy of British culture and finance. Covering an area of "
|
27 |
+
"approximately 1.12 square miles (2.9 square kilometers), it may be small in size, but it packs "
|
28 |
+
"a punch in terms of its global significance. One of the most notable features of The City is "
|
29 |
+
"its status as the financial heart of London and, indeed, the world. The area is home to the "
|
30 |
+
"Bank of England, the London Stock Exchange, and numerous multinational banks and financial "
|
31 |
+
"institutions. The towering skyscrapers and modern architecture that dot the skyline serve as "
|
32 |
+
"a symbol of the city's economic power and influence. The City's historic role in finance "
|
33 |
+
"dates back to the Middle Ages when it became the hub of international trade and commerce. "
|
34 |
+
"Today, it remains a hub for global finance, attracting professionals from all corners of the "
|
35 |
+
"globe. The City's historic and architectural heritage is another captivating aspect. Wandering "
|
36 |
+
"through its labyrinthine streets, one can marvel at the blend of old and new. Ancient structures "
|
37 |
+
"like the Tower of London and St. Paul's Cathedral coexist with sleek modern office buildings. "
|
38 |
+
"The contrast in architectural styles is a testament to London's ability to embrace its rich "
|
39 |
+
"history while continually evolving to meet the demands of the future. Culturally, The City "
|
40 |
+
"offers a unique blend of tradition and innovation. It hosts various cultural events and "
|
41 |
+
"festivals throughout the year, attracting both locals and tourists. The City's vibrant food "
|
42 |
+
"scene is another highlight, with a multitude of restaurants catering to diverse tastes, from "
|
43 |
+
"classic British fare to international cuisine. Despite its bustling urban environment, The City "
|
44 |
+
"also boasts several green spaces. One can escape the hustle and bustle of the financial district "
|
45 |
+
"by strolling along the banks of the River Thames, enjoying the lush gardens of Postman's Park, "
|
46 |
+
"or exploring the serene Barbican Conservatory. Transportation in The City is well-developed, "
|
47 |
+
"making it easily accessible. The London Underground, buses, and extensive pedestrian walkways "
|
48 |
+
"ensure that both residents and visitors can navigate the area efficiently. In conclusion, The "
|
49 |
+
"City of London is a city within a city, a captivating blend of history, finance, culture, and "
|
50 |
+
"architecture. Its enduring importance on the global stage, its rich heritage, and its vibrant "
|
51 |
+
"cultural scene make it a must-visit destination for anyone exploring the dynamic and diverse city "
|
52 |
+
"of London. Whether you are drawn by its financial prowess, architectural beauty, or cultural "
|
53 |
+
"riches, The City has something to offer every visitor, and its enduring appeal is sure to stand "
|
54 |
+
"the test of time."
|
55 |
+
)
|
56 |
+
|
57 |
|
58 |
entity_desc = ("This demo uses the [DSLIM BERT model](https://huggingface.co/dslim/bert-base-NER) "
|
59 |
"to identify named entities in a piece of text. It has been trained to recognise "
|
|
|
72 |
)
|
73 |
|
74 |
|
75 |
+
class GlobalVariables:
|
76 |
+
def __init__(self):
|
77 |
+
self.entities = None
|
78 |
+
self.summary = None
|
79 |
+
|
80 |
+
|
81 |
+
app_globals = GlobalVariables()
|
82 |
+
|
83 |
+
|
84 |
def process_entities(txt_data):
|
85 |
+
if txt_data is None or len(txt_data.strip()) == 0:
|
86 |
+
raise gr.Error("Text to analyse cannot be empty")
|
87 |
+
|
88 |
+
app_globals.entities = NER(txt_data)
|
89 |
+
app_globals.entities.entity_markdown()
|
90 |
|
91 |
+
entity_list = '\n'.join(app_globals.entities.unique_entities)
|
92 |
|
93 |
heading = 'Entities highlighted in the original text'
|
94 |
+
output = f'## {heading} \n\n {app_globals.entities.markdown}'
|
95 |
|
96 |
return entity_list, output
|
97 |
|
98 |
|
99 |
+
def session_data(txt_data):
|
100 |
+
pass
|
101 |
+
|
102 |
+
|
103 |
def process_summary(txt_data):
|
104 |
+
if txt_data is None or len(txt_data.strip()) == 0:
|
105 |
+
raise gr.Error("Text to summarise cannot be empty")
|
106 |
+
|
107 |
+
app_globals.summary = Summary(txt_data)
|
108 |
+
result = app_globals.summary.result
|
109 |
+
|
110 |
+
source_text_length = len(txt_data.split(' '))
|
111 |
+
summary_text_length = len(result.split(' '))
|
112 |
+
|
113 |
+
info = 'Words in source text: ' + str(source_text_length)
|
114 |
+
info += '\nWords in summary: ' + str(summary_text_length)
|
115 |
+
info += ('\nSource text shortened by a factor of: ' +
|
116 |
+
str(round(source_text_length/summary_text_length, 1)) + ' times')
|
117 |
+
|
118 |
+
return info, result
|
119 |
|
120 |
|
121 |
with gr.Blocks() as demo:
|
122 |
+
# The legal summary appliation tab.
|
123 |
+
with gr.Tab('Summaries'):
|
124 |
+
gr.Markdown("# Summarising text")
|
125 |
+
with gr.Accordion("See Details", open=False):
|
126 |
+
gr.Markdown(summary_desc)
|
127 |
+
text_summary_source = gr.Textbox(label="Text to summarise", lines=10)
|
128 |
+
text_summary = gr.Textbox(label="Summary", lines=3)
|
129 |
+
text_info = gr.Textbox(label="Related information", lines=5)
|
130 |
+
|
131 |
+
with gr.Row():
|
132 |
+
btn_sample_summary = gr.Button("Load Sample Text")
|
133 |
+
btn_clear_summary = gr.Button("Clear Summary Data")
|
134 |
+
btn_summary = gr.Button("Get Summary", variant='primary')
|
135 |
+
|
136 |
+
# Event Handler
|
137 |
+
btn_sample_summary.click(fn=lambda: summary_sample_text, outputs=[text_summary_source])
|
138 |
+
btn_clear_summary.click(fn=lambda: ('', '', ''), outputs=[text_summary_source, text_summary, text_info])
|
139 |
+
btn_summary.click(fn=process_summary, inputs=[text_summary_source], outputs=[text_info, text_summary])
|
140 |
+
|
141 |
with gr.Tab('Entities'):
|
142 |
+
gr.Markdown("# Extracting named entities")
|
143 |
with gr.Accordion("See Details", open=False):
|
144 |
gr.Markdown(entity_desc)
|
145 |
+
text_source = gr.Textbox(label="Text to analyse", lines=10)
|
146 |
text_entities = gr.Textbox(label="Unique entities", lines=3)
|
147 |
mk_output = gr.Markdown(label="Entities Highlighted", value='Highlighted entities appear here')
|
148 |
with gr.Row():
|
|
|
151 |
btn_entities = gr.Button("Get Entities", variant='primary')
|
152 |
|
153 |
# Event Handlers
|
154 |
+
btn_sample_entity.click(fn=lambda: entity_sample_text, outputs=[text_source])
|
155 |
btn_entities.click(fn=process_entities, inputs=[text_source], outputs=[text_entities, mk_output])
|
156 |
btn_clear_entity.click(fn=lambda: ('', '', ''), outputs=[text_source, text_entities, mk_output])
|
157 |
|
158 |
+
demo.launch()
|
159 |
+
|