split front element and event listening
Browse files- front/tabs/tab_drias.py +176 -125
front/tabs/tab_drias.py
CHANGED
@@ -1,9 +1,30 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
from climateqa.engine.talk_to_data.main import ask_drias
|
4 |
from climateqa.engine.talk_to_data.config import DRIAS_MODELS, DRIAS_UI_TEXT
|
5 |
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
async def ask_drias_query(query: str, index_state: int):
|
8 |
return await ask_drias(query, index_state)
|
9 |
|
@@ -91,18 +112,13 @@ def on_table_click(evt: gr.SelectData, table_names, sql_queries, dataframes, plo
|
|
91 |
)
|
92 |
|
93 |
|
94 |
-
def
|
95 |
-
|
96 |
-
|
97 |
-
plots_state = gr.State([])
|
98 |
-
index_state = gr.State(0)
|
99 |
-
table_names_list = gr.State([])
|
100 |
-
|
101 |
-
with gr.Tab("Beta - Talk to DRIAS", elem_id="tab-vanna", id=6):
|
102 |
with gr.Accordion(label="Details") as details_accordion:
|
103 |
gr.Markdown(DRIAS_UI_TEXT)
|
104 |
|
105 |
-
|
106 |
examples_hidden = gr.Textbox(visible=False, elem_id="drias-examples-hidden")
|
107 |
examples = gr.Examples(
|
108 |
examples=[
|
@@ -116,7 +132,6 @@ def create_drias_tab():
|
|
116 |
outputs=[examples_hidden],
|
117 |
)
|
118 |
|
119 |
-
|
120 |
with gr.Row():
|
121 |
drias_direct_question = gr.Textbox(
|
122 |
label="Direct Question",
|
@@ -125,7 +140,6 @@ def create_drias_tab():
|
|
125 |
interactive=True,
|
126 |
)
|
127 |
|
128 |
-
|
129 |
result_text = gr.Textbox(
|
130 |
label="", elem_id="no-result-label", interactive=False, visible=True
|
131 |
)
|
@@ -158,124 +172,161 @@ def create_drias_tab():
|
|
158 |
prev_button = gr.Button("Previous", visible=False)
|
159 |
next_button = gr.Button("Next", visible=False)
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
],
|
180 |
-
).then(
|
181 |
-
show_results,
|
182 |
-
inputs=[sql_queries_state, dataframes_state, plots_state],
|
183 |
-
outputs=[
|
184 |
-
result_text,
|
185 |
-
query_accordion,
|
186 |
-
table_accordion,
|
187 |
-
chart_accordion,
|
188 |
-
prev_button,
|
189 |
-
next_button,
|
190 |
-
pagination_display,
|
191 |
-
table_names_display,
|
192 |
-
],
|
193 |
-
).then(
|
194 |
-
update_pagination,
|
195 |
-
inputs=[index_state, sql_queries_state],
|
196 |
-
outputs=[pagination_display],
|
197 |
-
).then(
|
198 |
-
display_table_names,
|
199 |
-
inputs=[table_names_list],
|
200 |
-
outputs=[table_names_display],
|
201 |
)
|
202 |
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
drias_sql_query,
|
212 |
-
drias_table,
|
213 |
-
drias_display,
|
214 |
-
sql_queries_state,
|
215 |
-
dataframes_state,
|
216 |
-
plots_state,
|
217 |
-
index_state,
|
218 |
-
table_names_list,
|
219 |
-
result_text,
|
220 |
-
],
|
221 |
-
).then(
|
222 |
-
show_results,
|
223 |
-
inputs=[sql_queries_state, dataframes_state, plots_state],
|
224 |
-
outputs=[
|
225 |
-
result_text,
|
226 |
-
query_accordion,
|
227 |
-
table_accordion,
|
228 |
-
chart_accordion,
|
229 |
-
prev_button,
|
230 |
-
next_button,
|
231 |
-
pagination_display,
|
232 |
-
table_names_display,
|
233 |
-
],
|
234 |
-
).then(
|
235 |
-
update_pagination,
|
236 |
-
inputs=[index_state, sql_queries_state],
|
237 |
-
outputs=[pagination_display],
|
238 |
-
).then(
|
239 |
-
display_table_names,
|
240 |
-
inputs=[table_names_list],
|
241 |
-
outputs=[table_names_display],
|
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 |
-
inputs=[index_state, sql_queries_state],
|
267 |
-
outputs=[pagination_display],
|
268 |
-
)
|
269 |
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from typing import TypedDict, List, Optional
|
3 |
|
4 |
from climateqa.engine.talk_to_data.main import ask_drias
|
5 |
from climateqa.engine.talk_to_data.config import DRIAS_MODELS, DRIAS_UI_TEXT
|
6 |
|
7 |
|
8 |
+
class DriasUIElements(TypedDict):
|
9 |
+
tab: gr.Tab
|
10 |
+
details_accordion: gr.Accordion
|
11 |
+
examples_hidden: gr.Textbox
|
12 |
+
examples: gr.Examples
|
13 |
+
drias_direct_question: gr.Textbox
|
14 |
+
result_text: gr.Textbox
|
15 |
+
table_names_display: gr.DataFrame
|
16 |
+
query_accordion: gr.Accordion
|
17 |
+
drias_sql_query: gr.Textbox
|
18 |
+
chart_accordion: gr.Accordion
|
19 |
+
model_selection: gr.Dropdown
|
20 |
+
drias_display: gr.Plot
|
21 |
+
table_accordion: gr.Accordion
|
22 |
+
drias_table: gr.DataFrame
|
23 |
+
pagination_display: gr.Markdown
|
24 |
+
prev_button: gr.Button
|
25 |
+
next_button: gr.Button
|
26 |
+
|
27 |
+
|
28 |
async def ask_drias_query(query: str, index_state: int):
|
29 |
return await ask_drias(query, index_state)
|
30 |
|
|
|
112 |
)
|
113 |
|
114 |
|
115 |
+
def create_drias_ui() -> DriasUIElements:
|
116 |
+
"""Create and return all UI elements for the DRIAS tab."""
|
117 |
+
with gr.Tab("Beta - Talk to DRIAS", elem_id="tab-vanna", id=6) as tab:
|
|
|
|
|
|
|
|
|
|
|
118 |
with gr.Accordion(label="Details") as details_accordion:
|
119 |
gr.Markdown(DRIAS_UI_TEXT)
|
120 |
|
121 |
+
# Add examples for common questions
|
122 |
examples_hidden = gr.Textbox(visible=False, elem_id="drias-examples-hidden")
|
123 |
examples = gr.Examples(
|
124 |
examples=[
|
|
|
132 |
outputs=[examples_hidden],
|
133 |
)
|
134 |
|
|
|
135 |
with gr.Row():
|
136 |
drias_direct_question = gr.Textbox(
|
137 |
label="Direct Question",
|
|
|
140 |
interactive=True,
|
141 |
)
|
142 |
|
|
|
143 |
result_text = gr.Textbox(
|
144 |
label="", elem_id="no-result-label", interactive=False, visible=True
|
145 |
)
|
|
|
172 |
prev_button = gr.Button("Previous", visible=False)
|
173 |
next_button = gr.Button("Next", visible=False)
|
174 |
|
175 |
+
return DriasUIElements(
|
176 |
+
tab=tab,
|
177 |
+
details_accordion=details_accordion,
|
178 |
+
examples_hidden=examples_hidden,
|
179 |
+
examples=examples,
|
180 |
+
drias_direct_question=drias_direct_question,
|
181 |
+
result_text=result_text,
|
182 |
+
table_names_display=table_names_display,
|
183 |
+
query_accordion=query_accordion,
|
184 |
+
drias_sql_query=drias_sql_query,
|
185 |
+
chart_accordion=chart_accordion,
|
186 |
+
model_selection=model_selection,
|
187 |
+
drias_display=drias_display,
|
188 |
+
table_accordion=table_accordion,
|
189 |
+
drias_table=drias_table,
|
190 |
+
pagination_display=pagination_display,
|
191 |
+
prev_button=prev_button,
|
192 |
+
next_button=next_button
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
)
|
194 |
|
195 |
+
def setup_drias_events(ui_elements: DriasUIElements) -> None:
|
196 |
+
"""Set up all event handlers for the DRIAS tab."""
|
197 |
+
# Create state variables
|
198 |
+
sql_queries_state = gr.State([])
|
199 |
+
dataframes_state = gr.State([])
|
200 |
+
plots_state = gr.State([])
|
201 |
+
index_state = gr.State(0)
|
202 |
+
table_names_list = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
+
# Handle example selection
|
205 |
+
ui_elements["examples_hidden"].change(
|
206 |
+
lambda x: (gr.Accordion(open=False), gr.Textbox(value=x)),
|
207 |
+
inputs=[ui_elements["examples_hidden"]],
|
208 |
+
outputs=[ui_elements["details_accordion"], ui_elements["drias_direct_question"]]
|
209 |
+
).then(
|
210 |
+
ask_drias_query,
|
211 |
+
inputs=[ui_elements["examples_hidden"], index_state],
|
212 |
+
outputs=[
|
213 |
+
ui_elements["drias_sql_query"],
|
214 |
+
ui_elements["drias_table"],
|
215 |
+
ui_elements["drias_display"],
|
216 |
+
sql_queries_state,
|
217 |
+
dataframes_state,
|
218 |
+
plots_state,
|
219 |
+
index_state,
|
220 |
+
table_names_list,
|
221 |
+
ui_elements["result_text"],
|
222 |
+
],
|
223 |
+
).then(
|
224 |
+
show_results,
|
225 |
+
inputs=[sql_queries_state, dataframes_state, plots_state],
|
226 |
+
outputs=[
|
227 |
+
ui_elements["result_text"],
|
228 |
+
ui_elements["query_accordion"],
|
229 |
+
ui_elements["table_accordion"],
|
230 |
+
ui_elements["chart_accordion"],
|
231 |
+
ui_elements["prev_button"],
|
232 |
+
ui_elements["next_button"],
|
233 |
+
ui_elements["pagination_display"],
|
234 |
+
ui_elements["table_names_display"],
|
235 |
+
],
|
236 |
+
).then(
|
237 |
+
update_pagination,
|
238 |
+
inputs=[index_state, sql_queries_state],
|
239 |
+
outputs=[ui_elements["pagination_display"]],
|
240 |
+
).then(
|
241 |
+
display_table_names,
|
242 |
+
inputs=[table_names_list],
|
243 |
+
outputs=[ui_elements["table_names_display"]],
|
244 |
+
)
|
245 |
|
246 |
+
# Handle direct question submission
|
247 |
+
ui_elements["drias_direct_question"].submit(
|
248 |
+
lambda: gr.Accordion(open=False),
|
249 |
+
inputs=None,
|
250 |
+
outputs=[ui_elements["details_accordion"]]
|
251 |
+
).then(
|
252 |
+
ask_drias_query,
|
253 |
+
inputs=[ui_elements["drias_direct_question"], index_state],
|
254 |
+
outputs=[
|
255 |
+
ui_elements["drias_sql_query"],
|
256 |
+
ui_elements["drias_table"],
|
257 |
+
ui_elements["drias_display"],
|
258 |
+
sql_queries_state,
|
259 |
+
dataframes_state,
|
260 |
+
plots_state,
|
261 |
+
index_state,
|
262 |
+
table_names_list,
|
263 |
+
ui_elements["result_text"],
|
264 |
+
],
|
265 |
+
).then(
|
266 |
+
show_results,
|
267 |
+
inputs=[sql_queries_state, dataframes_state, plots_state],
|
268 |
+
outputs=[
|
269 |
+
ui_elements["result_text"],
|
270 |
+
ui_elements["query_accordion"],
|
271 |
+
ui_elements["table_accordion"],
|
272 |
+
ui_elements["chart_accordion"],
|
273 |
+
ui_elements["prev_button"],
|
274 |
+
ui_elements["next_button"],
|
275 |
+
ui_elements["pagination_display"],
|
276 |
+
ui_elements["table_names_display"],
|
277 |
+
],
|
278 |
+
).then(
|
279 |
+
update_pagination,
|
280 |
+
inputs=[index_state, sql_queries_state],
|
281 |
+
outputs=[ui_elements["pagination_display"]],
|
282 |
+
).then(
|
283 |
+
display_table_names,
|
284 |
+
inputs=[table_names_list],
|
285 |
+
outputs=[ui_elements["table_names_display"]],
|
286 |
+
)
|
287 |
|
288 |
+
# Handle model selection change
|
289 |
+
ui_elements["model_selection"].change(
|
290 |
+
filter_by_model,
|
291 |
+
inputs=[dataframes_state, plots_state, index_state, ui_elements["model_selection"]],
|
292 |
+
outputs=[ui_elements["drias_table"], ui_elements["drias_display"]],
|
293 |
+
)
|
|
|
|
|
|
|
294 |
|
295 |
+
# Handle pagination buttons
|
296 |
+
ui_elements["prev_button"].click(
|
297 |
+
show_previous,
|
298 |
+
inputs=[index_state, sql_queries_state, dataframes_state, plots_state],
|
299 |
+
outputs=[ui_elements["drias_sql_query"], ui_elements["drias_table"], ui_elements["drias_display"], index_state],
|
300 |
+
).then(
|
301 |
+
update_pagination,
|
302 |
+
inputs=[index_state, sql_queries_state],
|
303 |
+
outputs=[ui_elements["pagination_display"]],
|
304 |
+
)
|
305 |
|
306 |
+
ui_elements["next_button"].click(
|
307 |
+
show_next,
|
308 |
+
inputs=[index_state, sql_queries_state, dataframes_state, plots_state],
|
309 |
+
outputs=[ui_elements["drias_sql_query"], ui_elements["drias_table"], ui_elements["drias_display"], index_state],
|
310 |
+
).then(
|
311 |
+
update_pagination,
|
312 |
+
inputs=[index_state, sql_queries_state],
|
313 |
+
outputs=[ui_elements["pagination_display"]],
|
314 |
+
)
|
315 |
+
|
316 |
+
# Handle table selection
|
317 |
+
ui_elements["table_names_display"].select(
|
318 |
+
fn=on_table_click,
|
319 |
+
inputs=[table_names_list, sql_queries_state, dataframes_state, plots_state],
|
320 |
+
outputs=[ui_elements["drias_sql_query"], ui_elements["drias_table"], ui_elements["drias_display"], index_state],
|
321 |
+
).then(
|
322 |
+
update_pagination,
|
323 |
+
inputs=[index_state, sql_queries_state],
|
324 |
+
outputs=[ui_elements["pagination_display"]],
|
325 |
+
)
|
326 |
+
|
327 |
+
def create_drias_tab():
|
328 |
+
"""Main function to create the DRIAS tab with UI and event handling."""
|
329 |
+
ui_elements = create_drias_ui()
|
330 |
+
setup_drias_events(ui_elements)
|
331 |
+
|
332 |
+
|