wibberlet commited on
Commit
5ded745
1 Parent(s): c692027

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -50
app.py CHANGED
@@ -1,47 +1,59 @@
1
  import gradio as gr
 
2
  from NamedEntity import NER
3
 
4
- text2 = ("UK law presumes the author of a copyright work to be the first owner. The law "
5
- "also recognises that copyright works may be the product of joint authors and "
6
- "co-authors. This means that as soon as the work is created, copyright in the work "
7
- "belongs to the person or persons who created it. This is true even if the author "
8
- "was hired to make the copyright work under a contract for services, such as a "
9
- "wedding photographer. In the absence of an assignment of copyright via contract, "
10
- "the wedding photographer retains copyright in the photographs, and the happy couple "
11
- "merely gains physical prints of the pictures, and a right to privacy preventing the "
12
- "issue, communication or exhibition of copies of the pictures to the public. There "
13
- "are two exceptions to the presumption of first ownership: 1. The owner of copyright "
14
- "in a work created by an employee in the course of his employment will be the "
15
- "employer, unless there is an agreement to the contrary. This applies to literary, "
16
- "dramatic, musical or artistic works, and films. It is not sufficient for the work "
17
- "to have been created during working hours by an employee for the employer to own the "
18
- "work, it must have been created as part of the job that employee was hired to do. "
19
- "However, the employer may be able to make some claim to the work if the employee should "
20
- "have been working for the employer at the time when he created the work, or if the "
21
- "nature or subject matter of the work is so closely related to the type of employment "
22
- "that the line between employment and private time becomes blurred. For these reasons "
23
- "it is important to address copyright in employment contracts where employees are likely "
24
- "to be creating copyright works. 2. Her Majesty the Queen is the first owner of any "
25
- "copyright in works created by officers or servants of the Crown. This includes any "
26
- "copyright works created by civil servants, such as this copyright notice."
27
- )
28
-
29
- text = ("Mr Roberts had taken his dog for a walk in Hyde Park at around 9pm. "
30
- "He saw a group of people shouting at Stephen - a guy who would shortly "
31
- "have his Rolex watch and iPhone stolen by the same group of people "
32
- "that had surrounded him. A lady named Fiona Walker was crossing the High "
33
- "Street that runs alongside the park. She heard Mr Roberts shout for help "
34
- "and called the police to assist.\n\n Constable Robbins arrived after about "
35
- "20 minutes by which time the group had dispersed. Mr Roberts was able to "
36
- "give a description of the people who had stolen Stephen's Rolex watch and iPhone. "
37
- "He said that one of the people was wearing a blue Adidas t-shirt and another "
38
- "was wearing a red Arsenal football cap. "
39
- "It turned out the gang members hailed from Paddington and Mayfair and used Uber to "
40
- "move around the area.\n\n"
41
- "The gang leader had to appear at "
42
- "the Old Bailey on 1st January 2021. He was sentenced to 3 years in prison "
43
- "for robbery and assault by Judge Jennifer Sanderson."
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
- ner = NER(txt_data)
65
- ner.entity_markdown()
 
 
 
66
 
67
- entity_list = '\n'.join(ner.unique_entities)
68
 
69
  heading = 'Entities highlighted in the original text'
70
- output = f'## {heading} \n\n {ner.markdown}'
71
 
72
  return entity_list, output
73
 
74
 
 
 
 
 
75
  def process_summary(txt_data):
76
- return 'The Summary'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
 
79
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  with gr.Tab('Entities'):
81
- gr.Markdown("# Named Entity Recognition")
82
  with gr.Accordion("See Details", open=False):
83
  gr.Markdown(entity_desc)
84
- text_source = gr.Textbox(label="Text to analyse", value=text, lines=10)
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: text, outputs=[text_source])
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
+