Commit
·
f688422
1
Parent(s):
26bb643
feat: added list of tables and reduced execution time of TTD
Browse files- app.py +1 -209
- climateqa/engine/talk_to_data/main.py +6 -2
- climateqa/engine/talk_to_data/workflow.py +13 -9
- front/tabs/tab_drias.py +242 -0
- style.css +8 -1
app.py
CHANGED
|
@@ -12,11 +12,10 @@ from climateqa.engine.reranker import get_reranker
|
|
| 12 |
from climateqa.engine.graph import make_graph_agent, make_graph_agent_poc
|
| 13 |
from climateqa.engine.chains.retrieve_papers import find_papers
|
| 14 |
from climateqa.chat import start_chat, chat_stream, finish_chat
|
| 15 |
-
from climateqa.engine.talk_to_data.main import ask_drias, DRIAS_MODELS
|
| 16 |
-
from climateqa.engine.talk_to_data.myVanna import MyVanna
|
| 17 |
|
| 18 |
from front.tabs import create_config_modal, cqa_tab, create_about_tab
|
| 19 |
from front.tabs import MainTabPanel, ConfigPanel
|
|
|
|
| 20 |
from front.utils import process_figures
|
| 21 |
from gradio_modal import Modal
|
| 22 |
|
|
@@ -111,8 +110,6 @@ agent_poc = make_graph_agent_poc(
|
|
| 111 |
# return ask_vanna(vn, db_vanna_path, query)
|
| 112 |
|
| 113 |
|
| 114 |
-
def ask_drias_query(query: str, index_state: int):
|
| 115 |
-
return ask_drias(query, index_state)
|
| 116 |
|
| 117 |
|
| 118 |
async def chat(
|
|
@@ -201,211 +198,6 @@ def update_sources_number_display(
|
|
| 201 |
)
|
| 202 |
|
| 203 |
|
| 204 |
-
# def create_drias_tab():
|
| 205 |
-
# with gr.Tab("Beta - Talk to DRIAS", elem_id="tab-vanna", id=6) as tab_vanna:
|
| 206 |
-
# vanna_direct_question = gr.Textbox(label="Direct Question", placeholder="You can write direct question here",elem_id="direct-question", interactive=True)
|
| 207 |
-
# with gr.Accordion("Details",elem_id = 'vanna-details', open=False) as vanna_details :
|
| 208 |
-
# vanna_sql_query = gr.Textbox(label="SQL Query Used", elem_id="sql-query", interactive=False)
|
| 209 |
-
# show_vanna_table = gr.Button("Show Table", elem_id="show-table")
|
| 210 |
-
# with Modal(visible=False) as vanna_table_modal:
|
| 211 |
-
# vanna_table = gr.DataFrame([], elem_id="vanna-table")
|
| 212 |
-
# close_vanna_modal = gr.Button("Close", elem_id="close-vanna-modal")
|
| 213 |
-
# close_vanna_modal.click(lambda: Modal(visible=False),None, [vanna_table_modal])
|
| 214 |
-
# show_vanna_table.click(lambda: Modal(visible=True),None ,[vanna_table_modal])
|
| 215 |
-
|
| 216 |
-
# vanna_display = gr.Plot()
|
| 217 |
-
# vanna_direct_question.submit(ask_drias_query, [vanna_direct_question], [vanna_sql_query ,vanna_table, vanna_display])
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
def show_results(sql_queries_state, dataframes_state, plots_state):
|
| 221 |
-
if not sql_queries_state or not dataframes_state or not plots_state:
|
| 222 |
-
# If all results are empty, show "No result"
|
| 223 |
-
return (
|
| 224 |
-
gr.update(visible=True),
|
| 225 |
-
gr.update(visible=False),
|
| 226 |
-
gr.update(visible=False),
|
| 227 |
-
gr.update(visible=False),
|
| 228 |
-
gr.update(visible=False),
|
| 229 |
-
gr.update(visible=False),
|
| 230 |
-
gr.update(visible=False),
|
| 231 |
-
)
|
| 232 |
-
else:
|
| 233 |
-
# Show the appropriate components with their data
|
| 234 |
-
return (
|
| 235 |
-
gr.update(visible=False),
|
| 236 |
-
gr.update(visible=True),
|
| 237 |
-
gr.update(visible=True),
|
| 238 |
-
gr.update(visible=True),
|
| 239 |
-
gr.update(visible=True),
|
| 240 |
-
gr.update(visible=True),
|
| 241 |
-
gr.update(visible=True),
|
| 242 |
-
)
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
def filter_by_model(dataframes, figures, index_state, model_selection):
|
| 246 |
-
df = dataframes[index_state]
|
| 247 |
-
if model_selection != "ALL":
|
| 248 |
-
df = df[df["model"] == model_selection]
|
| 249 |
-
figure = figures[index_state](df)
|
| 250 |
-
return df, figure
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
def update_pagination(index, sql_queries):
|
| 254 |
-
pagination = f"{index + 1}/{len(sql_queries)}" if sql_queries else ""
|
| 255 |
-
return pagination
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
def create_drias_tab():
|
| 259 |
-
details_text = """
|
| 260 |
-
Hi, I'm **Talk to Drias**, designed to answer your questions using [**DRIAS - TRACC 2023**](https://www.drias-climat.fr/accompagnement/sections/401) data.
|
| 261 |
-
I'll answer by displaying a list of SQL queries, graphs and data most relevant to your question.
|
| 262 |
-
|
| 263 |
-
❓ **How to use?**
|
| 264 |
-
You can ask me anything about these climate indicators: **temperature**, **precipitation** or **drought**.
|
| 265 |
-
You can specify **location** and/or **year**.
|
| 266 |
-
You can choose from a list of climate models. By default, we take the **average of each model**.
|
| 267 |
-
|
| 268 |
-
For example, you can ask:
|
| 269 |
-
- What will the temperature be like in Paris?
|
| 270 |
-
- What will be the total rainfall in France in 2030?
|
| 271 |
-
- How frequent will extreme events be in Lyon?
|
| 272 |
-
|
| 273 |
-
**Example of indicators in the data**:
|
| 274 |
-
- Mean temperature (annual, winter, summer)
|
| 275 |
-
- Total precipitation (annual, winter, summer)
|
| 276 |
-
- Number of days with remarkable precipitations, with dry ground, with temperature above 30°C
|
| 277 |
-
|
| 278 |
-
⚠️ **Limitations**:
|
| 279 |
-
- You can't ask anything that isn't related to **DRIAS - TRACC 2023** data.
|
| 280 |
-
- You can only ask about **locations in France**.
|
| 281 |
-
- If you specify a year, there may be **no data for that year for some models**.
|
| 282 |
-
- You **cannot compare two models**.
|
| 283 |
-
|
| 284 |
-
🛈 **Information**
|
| 285 |
-
Please note that we **log your questions for meta-analysis purposes**, so avoid sharing any sensitive or personal information.
|
| 286 |
-
"""
|
| 287 |
-
with gr.Tab("Beta - Talk to DRIAS", elem_id="tab-vanna", id=6):
|
| 288 |
-
|
| 289 |
-
with gr.Accordion(label="Details"):
|
| 290 |
-
gr.Markdown(details_text)
|
| 291 |
-
|
| 292 |
-
with gr.Row():
|
| 293 |
-
drias_direct_question = gr.Textbox(
|
| 294 |
-
label="Direct Question",
|
| 295 |
-
placeholder="You can write direct question here",
|
| 296 |
-
elem_id="direct-question",
|
| 297 |
-
interactive=True,
|
| 298 |
-
)
|
| 299 |
-
|
| 300 |
-
result_text = gr.Textbox(
|
| 301 |
-
label="", elem_id="no-result-label", interactive=False, visible=True
|
| 302 |
-
)
|
| 303 |
-
|
| 304 |
-
with gr.Accordion(label="SQL Query Used", visible=False) as query_accordion:
|
| 305 |
-
drias_sql_query = gr.Textbox(
|
| 306 |
-
label="", elem_id="sql-query", interactive=False
|
| 307 |
-
)
|
| 308 |
-
|
| 309 |
-
with gr.Accordion(label="Chart", visible=False) as chart_accordion:
|
| 310 |
-
model_selection = gr.Dropdown(
|
| 311 |
-
label="Model", choices=DRIAS_MODELS, value="ALL", interactive=True
|
| 312 |
-
)
|
| 313 |
-
drias_display = gr.Plot(elem_id="vanna-plot")
|
| 314 |
-
|
| 315 |
-
with gr.Accordion(
|
| 316 |
-
label="Data used", open=False, visible=False
|
| 317 |
-
) as table_accordion:
|
| 318 |
-
drias_table = gr.DataFrame([], elem_id="vanna-table")
|
| 319 |
-
|
| 320 |
-
pagination_display = gr.Markdown(value="", visible=False, elem_id="pagination-display")
|
| 321 |
-
|
| 322 |
-
with gr.Row():
|
| 323 |
-
prev_button = gr.Button("Previous", visible=False)
|
| 324 |
-
next_button = gr.Button("Next", visible=False)
|
| 325 |
-
|
| 326 |
-
sql_queries_state = gr.State([])
|
| 327 |
-
dataframes_state = gr.State([])
|
| 328 |
-
plots_state = gr.State([])
|
| 329 |
-
index_state = gr.State(0)
|
| 330 |
-
|
| 331 |
-
drias_direct_question.submit(
|
| 332 |
-
ask_drias_query,
|
| 333 |
-
inputs=[drias_direct_question, index_state],
|
| 334 |
-
outputs=[
|
| 335 |
-
drias_sql_query,
|
| 336 |
-
drias_table,
|
| 337 |
-
drias_display,
|
| 338 |
-
sql_queries_state,
|
| 339 |
-
dataframes_state,
|
| 340 |
-
plots_state,
|
| 341 |
-
index_state,
|
| 342 |
-
result_text,
|
| 343 |
-
],
|
| 344 |
-
).then(
|
| 345 |
-
show_results,
|
| 346 |
-
inputs=[sql_queries_state, dataframes_state, plots_state],
|
| 347 |
-
outputs=[
|
| 348 |
-
result_text,
|
| 349 |
-
query_accordion,
|
| 350 |
-
table_accordion,
|
| 351 |
-
chart_accordion,
|
| 352 |
-
prev_button,
|
| 353 |
-
next_button,
|
| 354 |
-
pagination_display
|
| 355 |
-
],
|
| 356 |
-
).then(
|
| 357 |
-
update_pagination,
|
| 358 |
-
inputs=[index_state, sql_queries_state],
|
| 359 |
-
outputs=[pagination_display],
|
| 360 |
-
)
|
| 361 |
-
|
| 362 |
-
model_selection.change(
|
| 363 |
-
filter_by_model,
|
| 364 |
-
inputs=[dataframes_state, plots_state, index_state, model_selection],
|
| 365 |
-
outputs=[drias_table, drias_display],
|
| 366 |
-
)
|
| 367 |
-
|
| 368 |
-
def show_previous(index, sql_queries, dataframes, plots):
|
| 369 |
-
if index > 0:
|
| 370 |
-
index -= 1
|
| 371 |
-
return (
|
| 372 |
-
sql_queries[index],
|
| 373 |
-
dataframes[index],
|
| 374 |
-
plots[index](dataframes[index]),
|
| 375 |
-
index,
|
| 376 |
-
)
|
| 377 |
-
|
| 378 |
-
def show_next(index, sql_queries, dataframes, plots):
|
| 379 |
-
if index < len(sql_queries) - 1:
|
| 380 |
-
index += 1
|
| 381 |
-
return (
|
| 382 |
-
sql_queries[index],
|
| 383 |
-
dataframes[index],
|
| 384 |
-
plots[index](dataframes[index]),
|
| 385 |
-
index,
|
| 386 |
-
)
|
| 387 |
-
|
| 388 |
-
prev_button.click(
|
| 389 |
-
show_previous,
|
| 390 |
-
inputs=[index_state, sql_queries_state, dataframes_state, plots_state],
|
| 391 |
-
outputs=[drias_sql_query, drias_table, drias_display, index_state],
|
| 392 |
-
).then(
|
| 393 |
-
update_pagination,
|
| 394 |
-
inputs=[index_state, sql_queries_state],
|
| 395 |
-
outputs=[pagination_display],
|
| 396 |
-
)
|
| 397 |
-
|
| 398 |
-
next_button.click(
|
| 399 |
-
show_next,
|
| 400 |
-
inputs=[index_state, sql_queries_state, dataframes_state, plots_state],
|
| 401 |
-
outputs=[drias_sql_query, drias_table, drias_display, index_state],
|
| 402 |
-
).then(
|
| 403 |
-
update_pagination,
|
| 404 |
-
inputs=[index_state, sql_queries_state],
|
| 405 |
-
outputs=[pagination_display],
|
| 406 |
-
)
|
| 407 |
-
|
| 408 |
-
|
| 409 |
def config_event_handling(
|
| 410 |
main_tabs_components: list[MainTabPanel], config_componenets: ConfigPanel
|
| 411 |
):
|
|
|
|
| 12 |
from climateqa.engine.graph import make_graph_agent, make_graph_agent_poc
|
| 13 |
from climateqa.engine.chains.retrieve_papers import find_papers
|
| 14 |
from climateqa.chat import start_chat, chat_stream, finish_chat
|
|
|
|
|
|
|
| 15 |
|
| 16 |
from front.tabs import create_config_modal, cqa_tab, create_about_tab
|
| 17 |
from front.tabs import MainTabPanel, ConfigPanel
|
| 18 |
+
from front.tabs.tab_drias import create_drias_tab
|
| 19 |
from front.utils import process_figures
|
| 20 |
from gradio_modal import Modal
|
| 21 |
|
|
|
|
| 110 |
# return ask_vanna(vn, db_vanna_path, query)
|
| 111 |
|
| 112 |
|
|
|
|
|
|
|
| 113 |
|
| 114 |
|
| 115 |
async def chat(
|
|
|
|
| 198 |
)
|
| 199 |
|
| 200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
def config_event_handling(
|
| 202 |
main_tabs_components: list[MainTabPanel], config_componenets: ConfigPanel
|
| 203 |
):
|
climateqa/engine/talk_to_data/main.py
CHANGED
|
@@ -18,10 +18,13 @@ def ask_drias(query:str, index_state: int = 0):
|
|
| 18 |
sql_queries = []
|
| 19 |
result_dataframes = []
|
| 20 |
figures = []
|
|
|
|
| 21 |
|
| 22 |
for plot_state in final_state['plot_states'].values():
|
| 23 |
for table_state in plot_state['table_states'].values():
|
| 24 |
if table_state['status'] == 'OK':
|
|
|
|
|
|
|
| 25 |
if 'sql_query' in table_state and table_state['sql_query'] is not None:
|
| 26 |
sql_queries.append(table_state['sql_query'])
|
| 27 |
|
|
@@ -35,8 +38,9 @@ def ask_drias(query:str, index_state: int = 0):
|
|
| 35 |
|
| 36 |
sql_query = sql_queries[index_state]
|
| 37 |
dataframe = result_dataframes[index_state]
|
| 38 |
-
figure = figures[index_state](dataframe)
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
DRIAS_MODELS = [
|
| 42 |
'ALL',
|
|
|
|
| 18 |
sql_queries = []
|
| 19 |
result_dataframes = []
|
| 20 |
figures = []
|
| 21 |
+
table_list = []
|
| 22 |
|
| 23 |
for plot_state in final_state['plot_states'].values():
|
| 24 |
for table_state in plot_state['table_states'].values():
|
| 25 |
if table_state['status'] == 'OK':
|
| 26 |
+
if 'table_name' in table_state:
|
| 27 |
+
table_list.append(' '.join(table_state['table_name'].capitalize().split('_')))
|
| 28 |
if 'sql_query' in table_state and table_state['sql_query'] is not None:
|
| 29 |
sql_queries.append(table_state['sql_query'])
|
| 30 |
|
|
|
|
| 38 |
|
| 39 |
sql_query = sql_queries[index_state]
|
| 40 |
dataframe = result_dataframes[index_state]
|
| 41 |
+
figure = figures[index_state](dataframe)
|
| 42 |
+
|
| 43 |
+
return sql_query, dataframe, figure, sql_queries, result_dataframes, figures, index_state, table_list, ""
|
| 44 |
|
| 45 |
DRIAS_MODELS = [
|
| 46 |
'ALL',
|
climateqa/engine/talk_to_data/workflow.py
CHANGED
|
@@ -87,19 +87,23 @@ def drias_workflow(user_input: str) -> State:
|
|
| 87 |
|
| 88 |
plot_state['tables'] = relevant_tables
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
for n, table in enumerate(plot_state['tables']):
|
| 91 |
if n > 2:
|
| 92 |
break
|
| 93 |
|
| 94 |
table_state: TableState = {
|
| 95 |
'table_name': table,
|
| 96 |
-
'params':
|
| 97 |
'status': 'OK'
|
| 98 |
}
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
if param:
|
| 102 |
-
table_state['params'].update(param)
|
| 103 |
|
| 104 |
sql_query = plot['sql_query'](table, table_state['params'])
|
| 105 |
|
|
@@ -111,7 +115,7 @@ def drias_workflow(user_input: str) -> State:
|
|
| 111 |
|
| 112 |
table_state['sql_query'] = sql_query
|
| 113 |
df = execute_sql_query(sql_query)
|
| 114 |
-
|
| 115 |
if len(df) > 0:
|
| 116 |
have_dataframe = True
|
| 117 |
|
|
@@ -158,9 +162,9 @@ def find_param(state: State, param_name:str, table: str) -> dict[str, Any] | Non
|
|
| 158 |
if param_name == 'location':
|
| 159 |
location = find_location(state['user_input'], table)
|
| 160 |
return location
|
| 161 |
-
if param_name == 'indicator_column':
|
| 162 |
-
|
| 163 |
-
|
| 164 |
if param_name == 'year':
|
| 165 |
year = find_year(state['user_input'])
|
| 166 |
return {'year': year}
|
|
|
|
| 87 |
|
| 88 |
plot_state['tables'] = relevant_tables
|
| 89 |
|
| 90 |
+
params = {}
|
| 91 |
+
for param_name in plot['params']:
|
| 92 |
+
param = find_param(state, param_name, relevant_tables[0])
|
| 93 |
+
if param:
|
| 94 |
+
params.update(param)
|
| 95 |
+
|
| 96 |
for n, table in enumerate(plot_state['tables']):
|
| 97 |
if n > 2:
|
| 98 |
break
|
| 99 |
|
| 100 |
table_state: TableState = {
|
| 101 |
'table_name': table,
|
| 102 |
+
'params': params,
|
| 103 |
'status': 'OK'
|
| 104 |
}
|
| 105 |
+
|
| 106 |
+
table_state["params"]['indicator_column'] = find_indicator_column(table)
|
|
|
|
|
|
|
| 107 |
|
| 108 |
sql_query = plot['sql_query'](table, table_state['params'])
|
| 109 |
|
|
|
|
| 115 |
|
| 116 |
table_state['sql_query'] = sql_query
|
| 117 |
df = execute_sql_query(sql_query)
|
| 118 |
+
|
| 119 |
if len(df) > 0:
|
| 120 |
have_dataframe = True
|
| 121 |
|
|
|
|
| 162 |
if param_name == 'location':
|
| 163 |
location = find_location(state['user_input'], table)
|
| 164 |
return location
|
| 165 |
+
# if param_name == 'indicator_column':
|
| 166 |
+
# indicator_column = find_indicator_column(table)
|
| 167 |
+
# return {'indicator_column': indicator_column}
|
| 168 |
if param_name == 'year':
|
| 169 |
year = find_year(state['user_input'])
|
| 170 |
return {'year': year}
|
front/tabs/tab_drias.py
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from climateqa.engine.talk_to_data.main import ask_drias, DRIAS_MODELS
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def ask_drias_query(query: str, index_state: int):
|
| 7 |
+
return ask_drias(query, index_state)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def show_results(sql_queries_state, dataframes_state, plots_state):
|
| 11 |
+
if not sql_queries_state or not dataframes_state or not plots_state:
|
| 12 |
+
# If all results are empty, show "No result"
|
| 13 |
+
return (
|
| 14 |
+
gr.update(visible=True),
|
| 15 |
+
gr.update(visible=False),
|
| 16 |
+
gr.update(visible=False),
|
| 17 |
+
gr.update(visible=False),
|
| 18 |
+
gr.update(visible=False),
|
| 19 |
+
gr.update(visible=False),
|
| 20 |
+
gr.update(visible=False),
|
| 21 |
+
gr.update(visible=False),
|
| 22 |
+
)
|
| 23 |
+
else:
|
| 24 |
+
# Show the appropriate components with their data
|
| 25 |
+
return (
|
| 26 |
+
gr.update(visible=False),
|
| 27 |
+
gr.update(visible=True),
|
| 28 |
+
gr.update(visible=True),
|
| 29 |
+
gr.update(visible=True),
|
| 30 |
+
gr.update(visible=True),
|
| 31 |
+
gr.update(visible=True),
|
| 32 |
+
gr.update(visible=True),
|
| 33 |
+
gr.update(visible=True),
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def filter_by_model(dataframes, figures, index_state, model_selection):
|
| 38 |
+
df = dataframes[index_state]
|
| 39 |
+
if model_selection != "ALL":
|
| 40 |
+
df = df[df["model"] == model_selection]
|
| 41 |
+
figure = figures[index_state](df)
|
| 42 |
+
return df, figure
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def update_pagination(index, sql_queries):
|
| 46 |
+
pagination = f"{index + 1}/{len(sql_queries)}" if sql_queries else ""
|
| 47 |
+
return pagination
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def show_previous(index, sql_queries, dataframes, plots):
|
| 51 |
+
if index > 0:
|
| 52 |
+
index -= 1
|
| 53 |
+
return (
|
| 54 |
+
sql_queries[index],
|
| 55 |
+
dataframes[index],
|
| 56 |
+
plots[index](dataframes[index]),
|
| 57 |
+
index,
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def show_next(index, sql_queries, dataframes, plots):
|
| 62 |
+
if index < len(sql_queries) - 1:
|
| 63 |
+
index += 1
|
| 64 |
+
return (
|
| 65 |
+
sql_queries[index],
|
| 66 |
+
dataframes[index],
|
| 67 |
+
plots[index](dataframes[index]),
|
| 68 |
+
index,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def display_table_names(table_names):
|
| 73 |
+
return [table_names]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def on_table_click(evt: gr.SelectData, table_names, sql_queries, dataframes, plots):
|
| 77 |
+
index = evt.index[1]
|
| 78 |
+
figure = plots[index](dataframes[index])
|
| 79 |
+
return (
|
| 80 |
+
sql_queries[index],
|
| 81 |
+
dataframes[index],
|
| 82 |
+
figure,
|
| 83 |
+
index,
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
DETAILS_TEXT = """
|
| 88 |
+
Hi, I'm **Talk to Drias**, designed to answer your questions using [**DRIAS - TRACC 2023**](https://www.drias-climat.fr/accompagnement/sections/401) data.
|
| 89 |
+
I'll answer by displaying a list of SQL queries, graphs and data most relevant to your question.
|
| 90 |
+
|
| 91 |
+
❓ **How to use?**
|
| 92 |
+
You can ask me anything about these climate indicators: **temperature**, **precipitation** or **drought**.
|
| 93 |
+
You can specify **location** and/or **year**.
|
| 94 |
+
You can choose from a list of climate models. By default, we take the **average of each model**.
|
| 95 |
+
|
| 96 |
+
For example, you can ask:
|
| 97 |
+
- What will the temperature be like in Paris?
|
| 98 |
+
- What will be the total rainfall in France in 2030?
|
| 99 |
+
- How frequent will extreme events be in Lyon?
|
| 100 |
+
|
| 101 |
+
**Example of indicators in the data**:
|
| 102 |
+
- Mean temperature (annual, winter, summer)
|
| 103 |
+
- Total precipitation (annual, winter, summer)
|
| 104 |
+
- Number of days with remarkable precipitations, with dry ground, with temperature above 30°C
|
| 105 |
+
|
| 106 |
+
⚠️ **Limitations**:
|
| 107 |
+
- You can't ask anything that isn't related to **DRIAS - TRACC 2023** data.
|
| 108 |
+
- You can only ask about **locations in France**.
|
| 109 |
+
- If you specify a year, there may be **no data for that year for some models**.
|
| 110 |
+
- You **cannot compare two models**.
|
| 111 |
+
|
| 112 |
+
🛈 **Information**
|
| 113 |
+
Please note that we **log your questions for meta-analysis purposes**, so avoid sharing any sensitive or personal information.
|
| 114 |
+
"""
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def create_drias_tab():
|
| 118 |
+
sql_queries_state = gr.State([])
|
| 119 |
+
dataframes_state = gr.State([])
|
| 120 |
+
plots_state = gr.State([])
|
| 121 |
+
index_state = gr.State(0)
|
| 122 |
+
table_names_list = gr.State([])
|
| 123 |
+
|
| 124 |
+
with gr.Tab("Beta - Talk to DRIAS", elem_id="tab-vanna", id=6):
|
| 125 |
+
with gr.Accordion(label="Details"):
|
| 126 |
+
gr.Markdown(DETAILS_TEXT)
|
| 127 |
+
|
| 128 |
+
with gr.Row():
|
| 129 |
+
drias_direct_question = gr.Textbox(
|
| 130 |
+
label="Direct Question",
|
| 131 |
+
placeholder="You can write direct question here",
|
| 132 |
+
elem_id="direct-question",
|
| 133 |
+
interactive=True,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
result_text = gr.Textbox(
|
| 137 |
+
label="", elem_id="no-result-label", interactive=False, visible=True
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
table_names_display = gr.DataFrame(
|
| 141 |
+
[], label="List of relevant indicators", headers=None, interactive=False, elem_id="table-names", visible=False
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
with gr.Accordion(label="SQL Query Used", visible=False) as query_accordion:
|
| 145 |
+
drias_sql_query = gr.Textbox(
|
| 146 |
+
label="", elem_id="sql-query", interactive=False
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
with gr.Accordion(label="Chart", visible=False) as chart_accordion:
|
| 152 |
+
model_selection = gr.Dropdown(
|
| 153 |
+
label="Model", choices=DRIAS_MODELS, value="ALL", interactive=True
|
| 154 |
+
)
|
| 155 |
+
drias_display = gr.Plot(elem_id="vanna-plot")
|
| 156 |
+
|
| 157 |
+
with gr.Accordion(
|
| 158 |
+
label="Data used", open=False, visible=False
|
| 159 |
+
) as table_accordion:
|
| 160 |
+
drias_table = gr.DataFrame([], elem_id="vanna-table")
|
| 161 |
+
|
| 162 |
+
pagination_display = gr.Markdown(
|
| 163 |
+
value="", visible=False, elem_id="pagination-display"
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
with gr.Row():
|
| 167 |
+
prev_button = gr.Button("Previous", visible=False)
|
| 168 |
+
next_button = gr.Button("Next", visible=False)
|
| 169 |
+
|
| 170 |
+
drias_direct_question.submit(
|
| 171 |
+
ask_drias_query,
|
| 172 |
+
inputs=[drias_direct_question, index_state],
|
| 173 |
+
outputs=[
|
| 174 |
+
drias_sql_query,
|
| 175 |
+
drias_table,
|
| 176 |
+
drias_display,
|
| 177 |
+
sql_queries_state,
|
| 178 |
+
dataframes_state,
|
| 179 |
+
plots_state,
|
| 180 |
+
index_state,
|
| 181 |
+
table_names_list,
|
| 182 |
+
result_text,
|
| 183 |
+
],
|
| 184 |
+
).then(
|
| 185 |
+
show_results,
|
| 186 |
+
inputs=[sql_queries_state, dataframes_state, plots_state],
|
| 187 |
+
outputs=[
|
| 188 |
+
result_text,
|
| 189 |
+
query_accordion,
|
| 190 |
+
table_accordion,
|
| 191 |
+
chart_accordion,
|
| 192 |
+
prev_button,
|
| 193 |
+
next_button,
|
| 194 |
+
pagination_display,
|
| 195 |
+
table_names_display,
|
| 196 |
+
],
|
| 197 |
+
).then(
|
| 198 |
+
update_pagination,
|
| 199 |
+
inputs=[index_state, sql_queries_state],
|
| 200 |
+
outputs=[pagination_display],
|
| 201 |
+
).then(
|
| 202 |
+
display_table_names,
|
| 203 |
+
inputs=[table_names_list],
|
| 204 |
+
outputs=[table_names_display],
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
model_selection.change(
|
| 208 |
+
filter_by_model,
|
| 209 |
+
inputs=[dataframes_state, plots_state, index_state, model_selection],
|
| 210 |
+
outputs=[drias_table, drias_display],
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
prev_button.click(
|
| 214 |
+
show_previous,
|
| 215 |
+
inputs=[index_state, sql_queries_state, dataframes_state, plots_state],
|
| 216 |
+
outputs=[drias_sql_query, drias_table, drias_display, index_state],
|
| 217 |
+
).then(
|
| 218 |
+
update_pagination,
|
| 219 |
+
inputs=[index_state, sql_queries_state],
|
| 220 |
+
outputs=[pagination_display],
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
next_button.click(
|
| 224 |
+
show_next,
|
| 225 |
+
inputs=[index_state, sql_queries_state, dataframes_state, plots_state],
|
| 226 |
+
outputs=[drias_sql_query, drias_table, drias_display, index_state],
|
| 227 |
+
).then(
|
| 228 |
+
update_pagination,
|
| 229 |
+
inputs=[index_state, sql_queries_state],
|
| 230 |
+
outputs=[pagination_display],
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
table_names_display.select(
|
| 234 |
+
fn=on_table_click,
|
| 235 |
+
inputs=[table_names_list, sql_queries_state, dataframes_state, plots_state],
|
| 236 |
+
outputs=[drias_sql_query, drias_table, drias_display, index_state],
|
| 237 |
+
|
| 238 |
+
).then(
|
| 239 |
+
update_pagination,
|
| 240 |
+
inputs=[index_state, sql_queries_state],
|
| 241 |
+
outputs=[pagination_display],
|
| 242 |
+
)
|
style.css
CHANGED
|
@@ -663,4 +663,11 @@ div#tab-vanna{
|
|
| 663 |
text-align: center;
|
| 664 |
font-weight: bold;
|
| 665 |
font-size: 16px;
|
| 666 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
text-align: center;
|
| 664 |
font-weight: bold;
|
| 665 |
font-size: 16px;
|
| 666 |
+
}
|
| 667 |
+
|
| 668 |
+
#table-names table{
|
| 669 |
+
overflow: hidden;
|
| 670 |
+
}
|
| 671 |
+
#table-names thead{
|
| 672 |
+
display: none;
|
| 673 |
+
}
|