-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
304 lines (233 loc) · 12 KB
/
main.py
1
2
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import gradio as gr
from app.defaults import AppDefaults
from app.controller import AppController
# ===========================
# Conroller & Default Classes
# ===========================
control = AppController()
initial = AppDefaults()
# ===========================
# App Components
# ===========================
# UI components
title = gr.Markdown("# <center>🤖 ILOSTAT Simple Summarizer</center>")
subtitle = gr.Markdown(
"## <center>A proof of concept for summarizing ILOSTAT data using chat completion models</center>"
)
# Dropdown for geographic regions with dynamic choices
areas_dropdown = gr.Dropdown(
label="Select a geographic region", choices=initial.areas, value=initial.area
)
# Dropdown for dataflows (indicators) with dynamic choices
dataflows_dropdown = gr.Dropdown(
label="Select an indicator from ILOSTAT",
choices=initial.dataflows,
value=initial.dataflow,
)
# Description of the selected dataflow from ILOSTAT
dataflow_description = gr.HTML("Description goes here")
# Output dataframe
output_dataframe = gr.DataFrame(value=initial.dataframe)
# Button to submit form data
get_data_button = gr.Button("Update data")
# Button to generate chat completion
get_chat_completion_button = gr.Button("Generate summary")
# Output textarea for chat completion
chat_completion_textarea = gr.TextArea()
# Ouput textarea for the prompt
prompt_markdown = gr.Markdown(label="The prompt for the model")
with gr.Blocks(fill_height=True) as demo:
# ===========================
# Application State
# ===========================
# Dimensions available for the current Dataflow
dimensions = gr.State(initial.dimensions)
# The start year for the query
start_year = gr.State(None)
# The end year for the query
end_year = gr.State(None)
# The Dimensions currently selected by the user
current_dimensions = gr.State(initial.current_dimensions)
# ===========================
# App Rendering Section
# ===========================
title.render()
subtitle.render()
gr.Markdown("---")
# Layout using rows and columns for the UI components
with gr.Row():
# Left column for inputs
with gr.Column():
gr.Markdown("### 🌍 Select a region and dataflow")
# Render dropdown for area selection
with gr.Row():
areas_dropdown.render()
# Render dropdown for dataflow selection
with gr.Row():
dataflows_dropdown.render()
# Dynamically render dimension dropdowns based on selected dataflow
@gr.render(inputs=dimensions)
def render_dimensions(dims):
if dims:
gr.Markdown("### 🔦 Filter the data")
with gr.Group():
with gr.Row():
# First let's get the time period
time_period = next(
(
dim
for dim in dims
if "TIME_PERIOD" in dim["dimension"]
),
None,
)
# Let's remove it from the other dimensions so we can render it separately
if time_period:
dims.remove(time_period)
# Dropdown for starting year
start_year_dropdown = gr.Dropdown(
label="Starting year",
choices=time_period["values"],
value=time_period["values"][0],
interactive=True,
)
start_year_dropdown.change(
lambda year: year,
inputs=start_year_dropdown,
outputs=start_year,
)
# Dropdown for ending year
end_year_dropdown = gr.Dropdown(
label="Ending year",
choices=time_period["values"],
value=time_period["values"][-1],
interactive=True,
)
end_year_dropdown.change(
lambda year: year,
inputs=end_year_dropdown,
outputs=end_year,
)
for dimension in dims:
code, label = dimension["dimension"]
choices = dimension["values"]
# handler = create_dimension_handler(code)
dimension_controller = control.dimension_controller(code)
dimension_dropdown = gr.Dropdown(
label=label,
choices=choices,
interactive=True,
)
dimension_dropdown.change(
dimension_controller.update,
inputs=[current_dimensions, dimension_dropdown],
outputs=current_dimensions,
)
# Render the submit button
get_data_button.render()
# Right column for outputs
with gr.Column(scale=2):
with gr.Tab("🔢 Data"):
output_dataframe.render()
with gr.Tab("✨ AI Summary", interactive=True):
chat_completion_textarea.render()
get_chat_completion_button.render()
with gr.Tab("📊 Chart"):
output_chart = gr.Plot()
output_dataframe.change(
control.set_chart,
inputs=output_dataframe,
outputs=output_chart,
)
with gr.Tab("📚 Metadata"):
gr.Markdown("### Description of the data from ILOSTAT")
dataflow_description.render()
with gr.Tab("✍️ Prompt"):
prompt_markdown.render()
with gr.Tab("💁 What's going on?"):
gr.Markdown(
"""
## What is this?
This is a proof of concept for summarizing data from the [ILOSTAT SDMX API](https://ilostat.ilo.org/resources/sdmx-tools/) using a chat completion model.
### How it works
1. **Select** a geographic region and an indicator from ILOSTAT.
2. **Filter** the data by choosing dimensions.
3. Click the **"Update data"** button to generate a prompt.
4. View a **chart** for a visual representation of the data.
5. Check the **"Prompt"** tab to see the generated prompt.
6. Go to the **"AI Summary"** tab and click **"Generate summary"** to create a summary using a chat completion model.
### What problem does it solve?
Current large language models (LLMs) struggle with summarizing tabular data. These models are trained primarily on unstructured text, leaving them without a framework to understand two-dimensional data structures like tables. Moreover, they often lack the numerical reasoning skills required to interpret relationships between numbers in tables, such as trends over time.
This app addresses the problem by generating a prompt that distills key insights from tabular data, enabling a chat completion model to produce meaningful summaries.
## What model(s) is it using?
- [facebook/bart-large-cnn](https://huggingface.co/facebook/bart-large-cnn) is used to summarise metadata about detaflows
- [Llama-3.3-70B-Instruct](https://huggingface.co/meta-llama/Llama-3.3-70B-Instruct) is used by default to generate the data descriptions. In future versions, this could be paramtized to experiment with different chat completion models.
### Why is this useful?
Text-based descriptions generated by this app can make data more accessible:
- They assist people with visual impairments or limited access to visual content by providing summaries of data.
- Combined with contextual information from news stories or reports, these summaries can help generate data-driven narratives, offering insights into complex labor and workplace issues.
### What this isn't
This app is **not** a production-ready application. It’s a proof of concept showcasing how chat completion models can summarize data from ILOSTAT. It is not intended for deployment in production environments.
### What's next?
- Combine this approach with a **Retrieval-Augmented Generation (RAG)** system to generate static pages for the ILO’s website, ilo.org.
- Use the same methodology to develop a **chatbot** providing an open interface to the ILO’s knowledge base, including its statistical resources.
### Where’s the code?
The code for this app is available on [GitHub](https://github.com/justintemps/ilostat-simple-summarizer).
### Who made this?
This app was created by [Justin Smith](https://github.com/justintemps), Senior Digital Communication Officer with the ILO Department of Communication and Public Information. It uses the [Gradio](https://gradio.app/) library for building interactive machine learning applications.
### Acknowledgements
Special thanks to the ILOSTAT team, especially [Weichen Lee](https://github.com/wc-lei), for his support with the ILO SDMX API.
"""
)
# ===========================
# Component Event Handlers
# ===========================
# Event to populate dataflows based on selected area
areas_dropdown.change(control.set_dataflows, areas_dropdown, dataflows_dropdown)
# Event to set dimension details based on selected dataflow
dataflows_dropdown.input(
control.set_dimensions, [areas_dropdown, dataflows_dropdown], dimensions
)
# Initialize current dimensions when dimensions change
dimensions.change(
control.init_current_dimensions, inputs=dimensions, outputs=current_dimensions
)
# Event to handle submit button click, processing current dimensions and outputting results
get_data_button.click(
control.set_dataframe,
inputs=[
areas_dropdown,
dataflows_dropdown,
current_dimensions,
start_year,
end_year,
],
outputs=output_dataframe,
)
# Update the dataflow description but only when the dataframe is updated
output_dataframe.change(
control.set_description,
inputs=dataflows_dropdown,
outputs=dataflow_description,
)
# Update the generated prompt but oly when the dataframe is updated
output_dataframe.change(
control.set_prompt,
inputs=[
areas_dropdown,
dataflows_dropdown,
output_dataframe,
],
outputs=prompt_markdown,
)
# Event to handle chat completion button click, processing the output dataframe and outputting summary
get_chat_completion_button.click(
fn=control.chat_completion,
inputs=prompt_markdown,
outputs=chat_completion_textarea,
)
# ===========================
# Main Program Entry Point
# ===========================
if __name__ == "__main__":
demo.launch()