Css, loading bar and some adjustments

#9
by franceth - opened
Files changed (2) hide show
  1. app.py +283 -158
  2. style.css +74 -0
app.py CHANGED
@@ -1,16 +1,16 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import os
4
- # https://discuss.huggingface.co/t/issues-with-sadtalker-zerogpu-spaces-inquiry-about-community-grant/110625/10
5
- if os.environ.get("SPACES_ZERO_GPU") is not None:
6
- import spaces
7
- else:
8
- class spaces:
9
- @staticmethod
10
- def GPU(func):
11
- def wrapper(*args, **kwargs):
12
- return func(*args, **kwargs)
13
- return wrapper
14
  import sys
15
  from qatch.connectors.sqlite_connector import SqliteConnector
16
  from qatch.generate_dataset.orchestrator_generator import OrchestratorGenerator
@@ -23,9 +23,9 @@ import plotly.express as px
23
  import plotly.graph_objects as go
24
  import plotly.colors as pc
25
 
26
- @spaces.GPU
27
- def model_prediction():
28
- pass
29
 
30
  with open('style.css', 'r') as file:
31
  css = file.read()
@@ -149,9 +149,18 @@ def open_accordion(target):
149
  return gr.update(open=False), gr.update(open=False), gr.update(open=True, visible=True), gr.update(open=False), gr.update(open=False)
150
 
151
  # Interfaccia Gradio
152
-
153
  with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
154
- gr.Markdown("# QATCH")
 
 
 
 
 
 
 
 
 
 
155
  data_state = gr.State(None) # Memorizza i dati caricati
156
  upload_acc = gr.Accordion("Upload your data section", open=True, visible=True)
157
  select_table_acc = gr.Accordion("Select tables", open=False, visible=False)
@@ -163,52 +172,52 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
163
 
164
 
165
  #################################
166
- # PARTE DI INSERIMENTO DEL DB #
167
  #################################
168
  with upload_acc:
169
- gr.Markdown("## Caricamento dei Dati")
170
 
171
- file_input = gr.File(label="Trascina e rilascia un file", file_types=[".csv", ".xlsx", ".sqlite"])
172
  with gr.Row():
173
- default_checkbox = gr.Checkbox(label="Usa DataFrame di default")
174
  preview_output = gr.DataFrame(interactive=True, visible=True, value=df_default)
175
- submit_button = gr.Button("Carica Dati", interactive=False) # Disabilitato di default
176
- output = gr.JSON(visible=False) # Output dizionario
177
 
178
- # Funzione per abilitare il bottone se sono presenti dati da caricare
179
  def enable_submit(file, use_default):
180
  return gr.update(interactive=bool(file or use_default))
181
 
182
- # Funzione per deselezionare il checkbox se viene caricato un file
183
  def deselect_default(file):
184
  if file:
185
  return gr.update(value=False)
186
  return gr.update()
187
 
188
- # Abilita il bottone quando i campi di input sono valorizzati
189
  file_input.change(fn=enable_submit, inputs=[file_input, default_checkbox], outputs=[submit_button])
190
  default_checkbox.change(fn=enable_submit, inputs=[file_input, default_checkbox], outputs=[submit_button])
191
 
192
- # Mostra l'anteprima del DataFrame di default quando il checkbox è selezionato
193
  default_checkbox.change(fn=preview_default, inputs=[default_checkbox], outputs=[preview_output])
194
  preview_output.change(fn=update_df, inputs=[preview_output], outputs=[preview_output])
195
 
196
- # Deseleziona il checkbox quando viene caricato un file
197
  file_input.change(fn=deselect_default, inputs=[file_input], outputs=[default_checkbox])
198
 
199
  def handle_output(file, use_default):
200
- """Gestisce l'output quando si preme il bottone 'Carica Dati'."""
201
  result = load_data(file, None, use_default)
202
 
203
- if isinstance(result, dict): # Se result è un dizionario di DataFrame
204
- if len(result) == 1: # Se c'è solo una tabella
205
  return (
206
- gr.update(visible=False), # Nasconde l'output JSON
207
- result, # Salva lo stato dei dati
208
- gr.update(visible=False), # Nasconde la selezione tabella
209
- result, # Mantiene lo stato dei dati
210
- gr.update(interactive=False), # Disabilita il pulsante di submit
211
- gr.update(visible=True, open=True), # Passa direttamente a select_model_acc
212
  gr.update(visible=True, open=False)
213
  )
214
  else:
@@ -218,7 +227,7 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
218
  gr.update(open=True, visible=True),
219
  result,
220
  gr.update(interactive=False),
221
- gr.update(visible=False), # Mantiene il comportamento attuale
222
  gr.update(visible=True, open=True)
223
  )
224
  else:
@@ -239,75 +248,72 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
239
  )
240
 
241
 
242
-
243
  ######################################
244
- # PARTE DI SELEZIONE DELLE TABELLE #
245
  ######################################
246
  with select_table_acc:
247
-
248
- table_selector = gr.CheckboxGroup(choices=[], label="Seleziona le tabelle da visualizzare", value=[])
249
- table_outputs = [gr.DataFrame(label=f"Tabella {i+1}", interactive=True, visible=False) for i in range(5)]
250
- selected_table_names = gr.Textbox(label="Tabelle selezionate", visible=False, interactive=False)
251
 
252
- # Bottone di selezione modelli (inizialmente disabilitato)
253
  open_model_selection = gr.Button("Choose your models", interactive=False)
254
 
255
  def update_table_list(data):
256
- """Aggiorna dinamicamente la lista delle tabelle disponibili."""
257
  if isinstance(data, dict) and data:
258
- table_names = list(data.keys()) # Ritorna solo i nomi delle tabelle
259
- return gr.update(choices=table_names, value=[]) # Reset delle selezioni
260
  return gr.update(choices=[], value=[])
261
 
262
  def show_selected_tables(data, selected_tables):
263
- """Mostra solo le tabelle selezionate dall'utente e abilita il bottone."""
264
  updates = []
265
  if isinstance(data, dict) and data:
266
- available_tables = list(data.keys()) # Nomi effettivamente disponibili
267
- selected_tables = [t for t in selected_tables if t in available_tables] # Filtra selezioni valide
268
 
269
- tables = {name: data[name] for name in selected_tables} # Filtra i DataFrame
270
 
271
  for i, (name, df) in enumerate(tables.items()):
272
- updates.append(gr.update(value=df, label=f"Tabella: {name}", visible=True))
273
 
274
- # Se ci sono meno di 5 tabelle, nascondi gli altri DataFrame
275
  for _ in range(len(tables), 5):
276
  updates.append(gr.update(visible=False))
277
  else:
278
  updates = [gr.update(value=pd.DataFrame(), visible=False) for _ in range(5)]
279
 
280
- # Abilitare/disabilitare il bottone in base alle selezioni
281
- button_state = bool(selected_tables) # True se almeno una tabella è selezionata, False altrimenti
282
- updates.append(gr.update(interactive=button_state)) # Aggiorna stato bottone
283
 
284
  return updates
285
 
286
  def show_selected_table_names(selected_tables):
287
- """Mostra i nomi delle tabelle selezionate quando si preme il bottone."""
288
  if selected_tables:
289
  return gr.update(value=", ".join(selected_tables), visible=False)
290
  return gr.update(value="", visible=False)
291
 
292
- # Aggiorna automaticamente la lista delle checkbox quando `data_state` cambia
293
  data_state.change(fn=update_table_list, inputs=[data_state], outputs=[table_selector])
294
 
295
- # Aggiorna le tabelle visibili e lo stato del bottone in base alle selezioni dell'utente
296
  table_selector.change(fn=show_selected_tables, inputs=[data_state, table_selector], outputs=table_outputs + [open_model_selection])
297
 
298
- # Mostra la lista delle tabelle selezionate quando si preme "Choose your models"
299
  open_model_selection.click(fn=show_selected_table_names, inputs=[table_selector], outputs=[selected_table_names])
300
  open_model_selection.click(open_accordion, inputs=gr.State("model_selection"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc])
301
 
302
 
303
-
304
  ####################################
305
- # PARTE DI SELEZIONE DEL MODELLO #
306
  ####################################
307
  with select_model_acc:
308
  gr.Markdown("**Model Selection**")
309
 
310
- # Supponiamo che `us.read_models_csv` restituisca anche il percorso dell'immagine
311
  model_list_dict = us.read_models_csv(models_path)
312
  model_list = [model["code"] for model in model_list_dict]
313
  model_images = [model["image_path"] for model in model_list_dict]
@@ -315,7 +321,7 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
315
  model_checkboxes = []
316
  rows = []
317
 
318
- # Creazione dinamica di checkbox con immagini (3 per riga)
319
  for i in range(0, len(model_list), 3):
320
  with gr.Row():
321
  cols = []
@@ -332,17 +338,17 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
332
 
333
  selected_models_output = gr.JSON(visible=False)
334
 
335
- # Funzione per ottenere i modelli selezionati
336
  def get_selected_models(*model_selections):
337
  selected_models = [model for model, selected in zip(model_list, model_selections) if selected]
338
  input_data['models'] = selected_models
339
- button_state = bool(selected_models) # True se almeno un modello è selezionato, False altrimenti
340
  return selected_models, gr.update(open=True, visible=True), gr.update(interactive=button_state)
341
 
342
- # Bottone di submit (inizialmente disabilitato)
343
  submit_models_button = gr.Button("Submit Models", interactive=False)
344
 
345
- # Collegamento dei checkbox agli eventi di selezione
346
  for checkbox in model_checkboxes:
347
  checkbox.change(
348
  fn=get_selected_models,
@@ -356,20 +362,87 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
356
  outputs=[selected_models_output, select_model_acc, qatch_acc]
357
  )
358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
  reset_data = gr.Button("Back to upload data section")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360
  reset_data.click(open_accordion, inputs=gr.State("reset"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc, default_checkbox, file_input])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
 
362
 
363
- ###############################
364
- # PARTE DI ESECUZIONE QATCH #
365
- ###############################
366
  with qatch_acc:
367
  def change_text(text):
368
  return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
  def qatch_flow():
370
  orchestrator_generator = OrchestratorGenerator()
371
- #TODO add to target_df column target_df["columns_used"], tables selection
372
- #print(input_data['data']['db'])
373
  target_df = orchestrator_generator.generate_dataset(connector=input_data['data']['db'])
374
 
375
  schema_text = utils_get_db_tables_info.utils_extract_db_schema_as_string(
@@ -379,27 +452,35 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
379
  sql=None
380
  )
381
 
382
- # TODO QUERY PREDICTION
383
  predictions_dict = {model: pd.DataFrame(columns=['id', 'question', 'predicted_sql', 'time', 'query', 'db_path']) for model in model_list}
384
  metrics_conc = pd.DataFrame()
 
385
  for model in input_data["models"]:
 
 
 
386
  for index, row in target_df.iterrows():
387
- if len(target_df) != 0: load_value = f"##Loading... {round((index + 1) / len(target_df) * 100, 2)}%"
388
- else: load_value = "##Loading..."
389
- question = row['query']
390
- #yield gr.Textbox(question), gr.Textbox(), *[predictions_dict[model] for model in input_data["models"]], None
391
- yield gr.Markdown(value=load_value), gr.Textbox(question), gr.Textbox(), metrics_conc, *[predictions_dict[model] for model in model_list]
 
 
 
 
392
  start_time = time.time()
393
 
394
- # Simulazione della predizione
395
- time.sleep(0.03)
396
  prediction = "Prediction_placeholder"
397
-
398
- # Esegui la predizione reale qui
399
  # prediction = predictor.run(model, schema_text, question)
400
 
401
  end_time = time.time()
402
- # Crea una nuova riga come dataframe
403
  new_row = pd.DataFrame([{
404
  'id': index,
405
  'question': question,
@@ -407,25 +488,29 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
407
  'time': end_time - start_time,
408
  'query': row["query"],
409
  'db_path': input_data["data_path"]
410
- }]).dropna(how="all") # Rimuove solo righe completamente vuote
411
- #TODO con un for
 
412
  for col in target_df.columns:
413
  if col not in new_row.columns:
414
  new_row[col] = row[col]
415
- # Aggiorna il dataframe corrispondente al modello man mano
 
416
  if not new_row.empty:
417
  predictions_dict[model] = pd.concat([predictions_dict[model], new_row], ignore_index=True)
418
- #yield gr.Textbox(), gr.Textbox(prediction), *[predictions_dict[model] for model in input_data["models"]], None
419
- yield gr.Markdown(value=load_value), gr.Textbox(), gr.Textbox(prediction), metrics_conc, *[predictions_dict[model] for model in model_list]
420
 
421
- #END
 
 
 
 
422
  evaluator = OrchestratorEvaluator()
423
  for model in input_data["models"]:
424
  metrics_df_model = evaluator.evaluate_df(
425
  df=predictions_dict[model],
426
- target_col_name="query", #'<target_column_name>',
427
- prediction_col_name="predicted_sql", #'<prediction_column_name>',
428
- db_path_name= "db_path", #'<db_path_column_name>'
429
  )
430
  metrics_df_model['model'] = model
431
  metrics_conc = pd.concat([metrics_conc, metrics_df_model], ignore_index=True)
@@ -433,75 +518,118 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
433
  if 'valid_efficiency_score' not in metrics_conc.columns:
434
  metrics_conc['valid_efficiency_score'] = metrics_conc['VES']
435
 
436
- yield gr.Markdown(), gr.Textbox(), gr.Textbox(), metrics_conc, *[predictions_dict[model] for model in model_list]
437
 
438
- #Loading Bar
439
  with gr.Row():
440
- #progress = gr.Progress()
441
  variable = gr.Markdown()
442
 
443
- #NL -> MODEL -> Generated Quesy
444
  with gr.Row():
445
  with gr.Column():
446
- question_display = gr.Textbox()
 
 
 
447
  with gr.Column():
448
- gr.Image()
449
  with gr.Column():
450
- prediction_display = gr.Textbox()
 
 
 
451
 
452
  dataframe_per_model = {}
453
 
454
  with gr.Tabs() as model_tabs:
455
- #for model in input_data["models"]:
456
  for model in model_list:
457
- #TODO fix model tabs
458
- with gr.TabItem(model):
459
  gr.Markdown(f"**Results for {model}**")
 
460
  dataframe_per_model[model] = gr.DataFrame()
 
 
 
 
461
 
 
 
 
 
 
462
 
463
- #question_display.change(fn=change_text, inputs=[gr.State(question)], outputs=[question_display])
464
- selected_models_display = gr.JSON(label="Modelli selezionati")
465
  metrics_df = gr.DataFrame(visible=False)
466
- metrics_df_out= gr.DataFrame(visible=False)
467
-
468
  submit_models_button.click(
469
  fn=qatch_flow,
470
  inputs=[],
471
- outputs=[variable, question_display, prediction_display, metrics_df] + list(dataframe_per_model.values())
472
  )
473
 
474
  submit_models_button.click(
475
  fn=lambda: gr.update(value=input_data),
476
  outputs=[selected_models_display]
477
  )
478
- #Funziona per METRICS
479
- metrics_df.change(fn=change_text, inputs=[metrics_df], outputs=[metrics_df_out])
480
-
481
- # def change_tab(selected_models_output, model_tabs):
482
- # for model in model_list:
483
- # if model in selected_models_output:
484
- # pass#model_tabs[model].visible = True
485
- # else:
486
- # pass#model_tabs[model].visible = False
487
- # return model_tabs
488
 
489
- # selected_models_output.change(fn=change_tab, inputs=[selected_models_output, model_tabs], outputs=[])
 
490
 
491
  proceed_to_metrics_button = gr.Button("Proceed to Metrics")
492
  proceed_to_metrics_button.click(
493
  fn=lambda: (gr.update(open=False, visible=True), gr.update(open=True, visible=True)),
494
  outputs=[qatch_acc, metrics_acc]
495
  )
 
 
 
 
 
496
 
 
 
 
 
 
 
 
 
 
 
 
 
 
497
  reset_data = gr.Button("Back to upload data section")
498
  reset_data.click(open_accordion, inputs=gr.State("reset"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc, default_checkbox, file_input])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
499
 
500
-
501
-
502
- #######################################
503
- # METRICS VISUALIZATION SECTION #
504
- #######################################
505
  with metrics_acc:
506
  #confirmation_text = gr.Markdown("## Metrics successfully loaded")
507
 
@@ -556,7 +684,7 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
556
  template='plotly_dark'
557
  )
558
 
559
- return fig
560
 
561
  def update_plot(selected_metrics, group_by, selected_models):
562
  df = load_data_csv_es()
@@ -768,9 +896,9 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
768
 
769
  metric_multiselect = gr.CheckboxGroup(choices=metrics, label="Select metrics", value=metrics)
770
  model_multiselect = gr.CheckboxGroup(choices=models, label="Select models", value=models)
771
- group_radio = gr.Radio(choices=list(group_options.keys()), label="Select grouping", value="Model")
772
 
773
- output_plot = gr.Plot()
774
 
775
  query_rate_plot = gr.Plot(value=update_query_rate(models))
776
 
@@ -795,6 +923,10 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
795
  return update_radar(selected_models)
796
 
797
  #metrics_df_out.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
 
 
 
 
798
  metric_multiselect.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
799
  group_radio.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
800
  model_multiselect.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
@@ -807,37 +939,30 @@ with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
807
  reset_data = gr.Button("Back to upload data section")
808
  reset_data.click(open_accordion, inputs=gr.State("reset"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc, default_checkbox, file_input])
809
 
810
- # Hidden button to force UI refresh on load
811
- force_update_button = gr.Button("", visible=False)
812
-
813
- # State variable to track first load
814
- load_trigger = gr.State(value=True)
815
-
816
- # Function to force initial load
817
- def force_update(is_first_load):
818
- if is_first_load:
819
- return (
820
- update_plot(metrics, group_options["Model"], models),
821
- update_query_rate(models),
822
- update_radar(models),
823
- update_ranking_text(models, "valid_efficiency_score"),
824
- update_worst_cases_text(models),
825
- False # Change state to prevent continuous reloads
826
- )
827
- return gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), False
828
-
829
- # The invisible button forces chart loading only the first time
830
- force_update_button.click(
831
- fn=force_update,
832
- inputs=[load_trigger],
833
- outputs=[output_plot, query_rate_plot, radar_plot, ranking_text_display, worst_cases_display, load_trigger]
834
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
835
 
836
- # Simulate button click when UI loads
837
- with gr.Blocks() as demo:
838
- demo.load(
839
- lambda: force_update(True),
840
- outputs=[output_plot, query_rate_plot, radar_plot, ranking_text_display, worst_cases_display, load_trigger]
841
- )
842
-
843
- interface.launch(share=True)
 
1
  import gradio as gr
2
  import pandas as pd
3
  import os
4
+ # # https://discuss.huggingface.co/t/issues-with-sadtalker-zerogpu-spaces-inquiry-about-community-grant/110625/10
5
+ # if os.environ.get("SPACES_ZERO_GPU") is not None:
6
+ # import spaces
7
+ # else:
8
+ # class spaces:
9
+ # @staticmethod
10
+ # def GPU(func):
11
+ # def wrapper(*args, **kwargs):
12
+ # return func(*args, **kwargs)
13
+ # return wrapper
14
  import sys
15
  from qatch.connectors.sqlite_connector import SqliteConnector
16
  from qatch.generate_dataset.orchestrator_generator import OrchestratorGenerator
 
23
  import plotly.graph_objects as go
24
  import plotly.colors as pc
25
 
26
+ # @spaces.GPU
27
+ # def model_prediction():
28
+ # pass
29
 
30
  with open('style.css', 'r') as file:
31
  css = file.read()
 
149
  return gr.update(open=False), gr.update(open=False), gr.update(open=True, visible=True), gr.update(open=False), gr.update(open=False)
150
 
151
  # Interfaccia Gradio
 
152
  with gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css') as interface:
153
+ with gr.Row():
154
+ gr.Column(scale=1)
155
+ gr.Image(
156
+ value="https://github.com/CristianDegni01/Automatic-LLM-Benchmark-Analysis-for-Text2SQL-GRADIO/blob/master/models_logo/QATCH.png?raw=true",
157
+ show_label=False,
158
+ container=False,
159
+ height=200, # in pixel
160
+ width=400
161
+ )
162
+ gr.Column(scale=1)
163
+
164
  data_state = gr.State(None) # Memorizza i dati caricati
165
  upload_acc = gr.Accordion("Upload your data section", open=True, visible=True)
166
  select_table_acc = gr.Accordion("Select tables", open=False, visible=False)
 
172
 
173
 
174
  #################################
175
+ # DATABASE INSERTION #
176
  #################################
177
  with upload_acc:
178
+ gr.Markdown("## Data Upload")
179
 
180
+ file_input = gr.File(label="Drag and drop a file", file_types=[".csv", ".xlsx", ".sqlite"])
181
  with gr.Row():
182
+ default_checkbox = gr.Checkbox(label="Use default DataFrame")
183
  preview_output = gr.DataFrame(interactive=True, visible=True, value=df_default)
184
+ submit_button = gr.Button("Load Data", interactive=False) # Disabled by default
185
+ output = gr.JSON(visible=False) # Dictionary output
186
 
187
+ # Function to enable the button if there is data to load
188
  def enable_submit(file, use_default):
189
  return gr.update(interactive=bool(file or use_default))
190
 
191
+ # Function to uncheck the checkbox if a file is uploaded
192
  def deselect_default(file):
193
  if file:
194
  return gr.update(value=False)
195
  return gr.update()
196
 
197
+ # Enable the button when inputs are provided
198
  file_input.change(fn=enable_submit, inputs=[file_input, default_checkbox], outputs=[submit_button])
199
  default_checkbox.change(fn=enable_submit, inputs=[file_input, default_checkbox], outputs=[submit_button])
200
 
201
+ # Show preview of the default DataFrame when checkbox is selected
202
  default_checkbox.change(fn=preview_default, inputs=[default_checkbox], outputs=[preview_output])
203
  preview_output.change(fn=update_df, inputs=[preview_output], outputs=[preview_output])
204
 
205
+ # Uncheck the checkbox when a file is uploaded
206
  file_input.change(fn=deselect_default, inputs=[file_input], outputs=[default_checkbox])
207
 
208
  def handle_output(file, use_default):
209
+ """Handles the output when the 'Load Data' button is pressed."""
210
  result = load_data(file, None, use_default)
211
 
212
+ if isinstance(result, dict): # If result is a dictionary of DataFrames
213
+ if len(result) == 1: # If there's only one table
214
  return (
215
+ gr.update(visible=False), # Hide JSON output
216
+ result, # Save the data state
217
+ gr.update(visible=False), # Hide table selection
218
+ result, # Maintain the data state
219
+ gr.update(interactive=False), # Disable the submit button
220
+ gr.update(visible=True, open=True), # Proceed to select_model_acc
221
  gr.update(visible=True, open=False)
222
  )
223
  else:
 
227
  gr.update(open=True, visible=True),
228
  result,
229
  gr.update(interactive=False),
230
+ gr.update(visible=False), # Keep current behavior
231
  gr.update(visible=True, open=True)
232
  )
233
  else:
 
248
  )
249
 
250
 
 
251
  ######################################
252
+ # TABLE SELECTION PART #
253
  ######################################
254
  with select_table_acc:
255
+ table_selector = gr.CheckboxGroup(choices=[], label="Select tables to display", value=[])
256
+ table_outputs = [gr.DataFrame(label=f"Table {i+1}", interactive=True, visible=False) for i in range(5)]
257
+ selected_table_names = gr.Textbox(label="Selected tables", visible=False, interactive=False)
 
258
 
259
+ # Model selection button (initially disabled)
260
  open_model_selection = gr.Button("Choose your models", interactive=False)
261
 
262
  def update_table_list(data):
263
+ """Dynamically updates the list of available tables."""
264
  if isinstance(data, dict) and data:
265
+ table_names = list(data.keys()) # Return only the table names
266
+ return gr.update(choices=table_names, value=[]) # Reset selections
267
  return gr.update(choices=[], value=[])
268
 
269
  def show_selected_tables(data, selected_tables):
270
+ """Displays only the tables selected by the user and enables the button."""
271
  updates = []
272
  if isinstance(data, dict) and data:
273
+ available_tables = list(data.keys()) # Actually available names
274
+ selected_tables = [t for t in selected_tables if t in available_tables] # Filter valid selections
275
 
276
+ tables = {name: data[name] for name in selected_tables} # Filter the DataFrames
277
 
278
  for i, (name, df) in enumerate(tables.items()):
279
+ updates.append(gr.update(value=df, label=f"Table: {name}", visible=True))
280
 
281
+ # If there are fewer than 5 tables, hide the other DataFrames
282
  for _ in range(len(tables), 5):
283
  updates.append(gr.update(visible=False))
284
  else:
285
  updates = [gr.update(value=pd.DataFrame(), visible=False) for _ in range(5)]
286
 
287
+ # Enable/disable the button based on selections
288
+ button_state = bool(selected_tables) # True if at least one table is selected, False otherwise
289
+ updates.append(gr.update(interactive=button_state)) # Update button state
290
 
291
  return updates
292
 
293
  def show_selected_table_names(selected_tables):
294
+ """Displays the names of the selected tables when the button is pressed."""
295
  if selected_tables:
296
  return gr.update(value=", ".join(selected_tables), visible=False)
297
  return gr.update(value="", visible=False)
298
 
299
+ # Automatically updates the checkbox list when `data_state` changes
300
  data_state.change(fn=update_table_list, inputs=[data_state], outputs=[table_selector])
301
 
302
+ # Updates the visible tables and the button state based on user selections
303
  table_selector.change(fn=show_selected_tables, inputs=[data_state, table_selector], outputs=table_outputs + [open_model_selection])
304
 
305
+ # Shows the list of selected tables when "Choose your models" is clicked
306
  open_model_selection.click(fn=show_selected_table_names, inputs=[table_selector], outputs=[selected_table_names])
307
  open_model_selection.click(open_accordion, inputs=gr.State("model_selection"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc])
308
 
309
 
 
310
  ####################################
311
+ # MODEL SELECTION PART #
312
  ####################################
313
  with select_model_acc:
314
  gr.Markdown("**Model Selection**")
315
 
316
+ # Assume that `us.read_models_csv` also returns the image path
317
  model_list_dict = us.read_models_csv(models_path)
318
  model_list = [model["code"] for model in model_list_dict]
319
  model_images = [model["image_path"] for model in model_list_dict]
 
321
  model_checkboxes = []
322
  rows = []
323
 
324
+ # Dynamically create checkboxes with images (3 per row)
325
  for i in range(0, len(model_list), 3):
326
  with gr.Row():
327
  cols = []
 
338
 
339
  selected_models_output = gr.JSON(visible=False)
340
 
341
+ # Function to get selected models
342
  def get_selected_models(*model_selections):
343
  selected_models = [model for model, selected in zip(model_list, model_selections) if selected]
344
  input_data['models'] = selected_models
345
+ button_state = bool(selected_models) # True if at least one model is selected, False otherwise
346
  return selected_models, gr.update(open=True, visible=True), gr.update(interactive=button_state)
347
 
348
+ # Submit button (initially disabled)
349
  submit_models_button = gr.Button("Submit Models", interactive=False)
350
 
351
+ # Link checkboxes to selection events
352
  for checkbox in model_checkboxes:
353
  checkbox.change(
354
  fn=get_selected_models,
 
362
  outputs=[selected_models_output, select_model_acc, qatch_acc]
363
  )
364
 
365
+ def enable_disable(enable):
366
+ return (
367
+ *[gr.update(interactive=enable) for _ in model_checkboxes],
368
+ gr.update(interactive=enable),
369
+ gr.update(interactive=enable),
370
+ gr.update(interactive=enable),
371
+ gr.update(interactive=enable),
372
+ gr.update(interactive=enable),
373
+ gr.update(interactive=enable),
374
+ *[gr.update(interactive=enable) for _ in table_outputs],
375
+ gr.update(interactive=enable)
376
+ )
377
+
378
  reset_data = gr.Button("Back to upload data section")
379
+
380
+ submit_models_button.click(
381
+ fn=enable_disable,
382
+ inputs=[gr.State(False)],
383
+ outputs=[
384
+ *model_checkboxes,
385
+ submit_models_button,
386
+ preview_output,
387
+ submit_button,
388
+ file_input,
389
+ default_checkbox,
390
+ table_selector,
391
+ *table_outputs,
392
+ open_model_selection
393
+ ]
394
+ )
395
+
396
  reset_data.click(open_accordion, inputs=gr.State("reset"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc, default_checkbox, file_input])
397
+
398
+ reset_data.click(
399
+ fn=enable_disable,
400
+ inputs=[gr.State(True)],
401
+ outputs=[
402
+ *model_checkboxes,
403
+ submit_models_button,
404
+ preview_output,
405
+ submit_button,
406
+ file_input,
407
+ default_checkbox,
408
+ table_selector,
409
+ *table_outputs,
410
+ open_model_selection
411
+ ]
412
+ )
413
 
414
 
415
+ #############################
416
+ # QATCH EXECUTION #
417
+ #############################
418
  with qatch_acc:
419
  def change_text(text):
420
  return text
421
+
422
+ loading_symbols= {1:"𓆟",
423
+ 2: "𓆞 𓆟",
424
+ 3: "𓆟 𓆞 𓆟",
425
+ 4: "𓆞 𓆟 𓆞 𓆟",
426
+ 5: "𓆟 𓆞 𓆟 𓆞 𓆟",
427
+ 6: "𓆞 𓆟 𓆞 𓆟 𓆞 𓆟",
428
+ 7: "𓆟 𓆞 𓆟 𓆞 𓆟 𓆞 𓆟",
429
+ 8: "𓆞 𓆟 𓆞 𓆟 𓆞 𓆟 𓆞 𓆟",
430
+ 9: "𓆟 𓆞 𓆟 𓆞 𓆟 𓆞 𓆟 𓆞 𓆟",
431
+ 10:"𓆞 𓆟 𓆞 𓆟 𓆞 𓆟 𓆞 𓆟 𓆞 𓆟",
432
+ }
433
+
434
+ def generate_loading_text(percent):
435
+ num_symbols = (round(percent) % 11) + 1
436
+ symbols = loading_symbols.get(num_symbols, "𓆟")
437
+ mirrored_symbols = f'<span class="mirrored">{symbols.strip()}</span>'
438
+ css_symbols = f'<span class="fish">{symbols.strip()}</span>'
439
+ return f"<div class='barcontainer'>{css_symbols} <span class='loading'>Generation {percent}%</span>{mirrored_symbols}</div>"
440
+ #return f"{css_symbols}"+f"# Loading {percent}% #"+f"{mirrored_symbols}"
441
+
442
  def qatch_flow():
443
  orchestrator_generator = OrchestratorGenerator()
444
+ # TODO: add to target_df column target_df["columns_used"], tables selection
445
+ # print(input_data['data']['db'])
446
  target_df = orchestrator_generator.generate_dataset(connector=input_data['data']['db'])
447
 
448
  schema_text = utils_get_db_tables_info.utils_extract_db_schema_as_string(
 
452
  sql=None
453
  )
454
 
455
+ # TODO: QUERY PREDICTION
456
  predictions_dict = {model: pd.DataFrame(columns=['id', 'question', 'predicted_sql', 'time', 'query', 'db_path']) for model in model_list}
457
  metrics_conc = pd.DataFrame()
458
+
459
  for model in input_data["models"]:
460
+ model_image_path = next((m["image_path"] for m in model_list_dict if m["code"] == model), None)
461
+ yield gr.Image(model_image_path), gr.Markdown(), gr.Markdown(), gr.Markdown(), metrics_conc, *[predictions_dict[model] for model in model_list]
462
+
463
  for index, row in target_df.iterrows():
464
+
465
+ percent_complete = round(((index+1) / len(target_df)) * 100, 2)
466
+ load_text = f"{generate_loading_text(percent_complete)}"
467
+
468
+ question = row['question']
469
+ display_question = f"<div class='loading' style ='font-size: 1.7rem;'>Natural Language: </div> <div class='sqlquery'>{row['question']}</div>"
470
+ # yield gr.Textbox(question), gr.Textbox(), *[predictions_dict[model] for model in input_data["models"]], None
471
+
472
+ yield gr.Image(), gr.Markdown(load_text), gr.Markdown(display_question), gr.Markdown(), metrics_conc, *[predictions_dict[model] for model in model_list]
473
  start_time = time.time()
474
 
475
+ # Simulate prediction
476
+ time.sleep(0.4)
477
  prediction = "Prediction_placeholder"
478
+ display_prediction = f"<div class='loading' style ='font-size: 1.7rem;'>Generated SQL: </div><div class='sqlquery'>{prediction}</div>"
479
+ # Run real prediction here
480
  # prediction = predictor.run(model, schema_text, question)
481
 
482
  end_time = time.time()
483
+ # Create a new row as dataframe
484
  new_row = pd.DataFrame([{
485
  'id': index,
486
  'question': question,
 
488
  'time': end_time - start_time,
489
  'query': row["query"],
490
  'db_path': input_data["data_path"]
491
+ }]).dropna(how="all") # Remove only completely empty rows
492
+
493
+ # TODO: use a for loop
494
  for col in target_df.columns:
495
  if col not in new_row.columns:
496
  new_row[col] = row[col]
497
+
498
+ # Update model's prediction dataframe incrementally
499
  if not new_row.empty:
500
  predictions_dict[model] = pd.concat([predictions_dict[model], new_row], ignore_index=True)
 
 
501
 
502
+ # yield gr.Textbox(), gr.Textbox(prediction), *[predictions_dict[model] for model in input_data["models"]], None
503
+ yield gr.Image(), gr.Markdown(load_text), gr.Markdown(), gr.Markdown(display_prediction), metrics_conc, *[predictions_dict[model] for model in model_list]
504
+
505
+ yield gr.Image(), gr.Markdown(load_text), gr.Markdown(), gr.Markdown(display_prediction), metrics_conc, *[predictions_dict[model] for model in model_list]
506
+ # END
507
  evaluator = OrchestratorEvaluator()
508
  for model in input_data["models"]:
509
  metrics_df_model = evaluator.evaluate_df(
510
  df=predictions_dict[model],
511
+ target_col_name="query",
512
+ prediction_col_name="predicted_sql",
513
+ db_path_name="db_path"
514
  )
515
  metrics_df_model['model'] = model
516
  metrics_conc = pd.concat([metrics_conc, metrics_df_model], ignore_index=True)
 
518
  if 'valid_efficiency_score' not in metrics_conc.columns:
519
  metrics_conc['valid_efficiency_score'] = metrics_conc['VES']
520
 
521
+ yield gr.Image(), gr.Markdown(), gr.Markdown(), gr.Markdown(), metrics_conc, *[predictions_dict[model] for model in model_list]
522
 
523
+ # Loading Bar
524
  with gr.Row():
525
+ # progress = gr.Progress()
526
  variable = gr.Markdown()
527
 
528
+ # NL -> MODEL -> Generated Query
529
  with gr.Row():
530
  with gr.Column():
531
+ with gr.Column():
532
+ question_display = gr.Markdown()
533
+ with gr.Column():
534
+ gr.Markdown("<div class='leftarrow'>⤴</div>")
535
  with gr.Column():
536
+ model_logo = gr.Image(visible=True, show_label=False)
537
  with gr.Column():
538
+ with gr.Column():
539
+ prediction_display = gr.Markdown()
540
+ with gr.Column():
541
+ gr.Markdown("<div class='rightarrow'>⤴</div>")
542
 
543
  dataframe_per_model = {}
544
 
545
  with gr.Tabs() as model_tabs:
546
+ tab_dict = {}
547
  for model in model_list:
548
+ with gr.TabItem(model, visible=(model in input_data["models"])) as tab:
 
549
  gr.Markdown(f"**Results for {model}**")
550
+ tab_dict[model] = tab
551
  dataframe_per_model[model] = gr.DataFrame()
552
+ # download_pred_model = gr.DownloadButton(label="Download Prediction per Model", visible=False)
553
+
554
+ def change_tab():
555
+ return [gr.update(visible=(model in input_data["models"])) for model in model_list]
556
 
557
+ submit_models_button.click(
558
+ change_tab,
559
+ inputs=[],
560
+ outputs=[tab_dict[model] for model in model_list] # Update TabItem visibility
561
+ )
562
 
563
+ selected_models_display = gr.JSON(label="Final input data", visible=False)
 
564
  metrics_df = gr.DataFrame(visible=False)
565
+ metrics_df_out = gr.DataFrame(visible=False)
566
+
567
  submit_models_button.click(
568
  fn=qatch_flow,
569
  inputs=[],
570
+ outputs=[model_logo, variable, question_display, prediction_display, metrics_df] + list(dataframe_per_model.values())
571
  )
572
 
573
  submit_models_button.click(
574
  fn=lambda: gr.update(value=input_data),
575
  outputs=[selected_models_display]
576
  )
 
 
 
 
 
 
 
 
 
 
577
 
578
+ # Works for METRICS
579
+ metrics_df.change(fn=change_text, inputs=[metrics_df], outputs=[metrics_df_out])
580
 
581
  proceed_to_metrics_button = gr.Button("Proceed to Metrics")
582
  proceed_to_metrics_button.click(
583
  fn=lambda: (gr.update(open=False, visible=True), gr.update(open=True, visible=True)),
584
  outputs=[qatch_acc, metrics_acc]
585
  )
586
+
587
+ def allow_download(metrics_df_out):
588
+ path = os.path.join(".", "data", "data_results", "results.csv")
589
+ metrics_df_out.to_csv(path, index=False)
590
+ return gr.update(value=path, visible=True)
591
 
592
+ download_metrics = gr.DownloadButton(label="Download Metrics Evaluation", visible=False)
593
+
594
+ submit_models_button.click(
595
+ fn=lambda: gr.update(visible=False),
596
+ outputs=[download_metrics]
597
+ )
598
+ #TODO WHY?
599
+ # download_metrics.click(
600
+ # fn=lambda: gr.update(open=True, visible=True),
601
+ # outputs=[download_metrics]
602
+ # )
603
+ metrics_df_out.change(fn=allow_download, inputs=[metrics_df_out], outputs=[download_metrics])
604
+
605
  reset_data = gr.Button("Back to upload data section")
606
  reset_data.click(open_accordion, inputs=gr.State("reset"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc, default_checkbox, file_input])
607
+ #WHY NOT WORKING?
608
+ reset_data.click(
609
+ fn=lambda: gr.update(visible=False),
610
+ outputs=[download_metrics]
611
+ )
612
+
613
+ reset_data.click(
614
+ fn=enable_disable,
615
+ inputs=[gr.State(True)],
616
+ outputs=[
617
+ *model_checkboxes,
618
+ submit_models_button,
619
+ preview_output,
620
+ submit_button,
621
+ file_input,
622
+ default_checkbox,
623
+ table_selector,
624
+ *table_outputs,
625
+ open_model_selection
626
+ ]
627
+ )
628
+
629
 
630
+ ##########################################
631
+ # METRICS VISUALIZATION SECTION #
632
+ ##########################################
 
 
633
  with metrics_acc:
634
  #confirmation_text = gr.Markdown("## Metrics successfully loaded")
635
 
 
684
  template='plotly_dark'
685
  )
686
 
687
+ return gr.Plot(fig, visible=True)
688
 
689
  def update_plot(selected_metrics, group_by, selected_models):
690
  df = load_data_csv_es()
 
896
 
897
  metric_multiselect = gr.CheckboxGroup(choices=metrics, label="Select metrics", value=metrics)
898
  model_multiselect = gr.CheckboxGroup(choices=models, label="Select models", value=models)
899
+ group_radio = gr.Radio(choices=list(group_options.keys()), label="Select grouping", value="Table")
900
 
901
+ output_plot = gr.Plot(visible=False)
902
 
903
  query_rate_plot = gr.Plot(value=update_query_rate(models))
904
 
 
923
  return update_radar(selected_models)
924
 
925
  #metrics_df_out.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
926
+ proceed_to_metrics_button.click(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
927
+
928
+ proceed_to_metrics_button.click(update_query_rate, inputs=[model_multiselect], outputs=query_rate_plot)
929
+
930
  metric_multiselect.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
931
  group_radio.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
932
  model_multiselect.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
 
939
  reset_data = gr.Button("Back to upload data section")
940
  reset_data.click(open_accordion, inputs=gr.State("reset"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc, default_checkbox, file_input])
941
 
942
+ reset_data.click(
943
+ fn=lambda: gr.update(visible=False),
944
+ outputs=[download_metrics]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
945
  )
946
+ reset_data.click(
947
+ fn=lambda: gr.update(visible=False),
948
+ outputs=[download_metrics]
949
+ )
950
+ reset_data.click(
951
+ fn=enable_disable,
952
+ inputs=[gr.State(True)],
953
+ outputs=[
954
+ *model_checkboxes,
955
+ submit_models_button,
956
+ preview_output,
957
+ submit_button,
958
+ file_input,
959
+ default_checkbox,
960
+ table_selector,
961
+ *table_outputs,
962
+ open_model_selection
963
+ ]
964
+ )
965
+
966
+
967
 
968
+ interface.launch()
 
 
 
 
 
 
 
style.css CHANGED
@@ -28,3 +28,77 @@
28
  width: 100% !important;
29
  max-width: none !important;
30
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  width: 100% !important;
29
  max-width: none !important;
30
  }
31
+
32
+ .mirrored {
33
+ display: inline-block;
34
+ transform: scaleX(-1); /* Riflette il testo orizzontalmente */
35
+ font-family: 'Poppins', sans-serif;
36
+ font-size: 1.5rem;
37
+ font-weight: 700;
38
+ letter-spacing: 1px;
39
+ text-align: center;
40
+ color: #222;
41
+ background: linear-gradient(45deg, #1a41d9, #6c69d2);
42
+ -webkit-background-clip: text;
43
+ -webkit-text-fill-color: transparent;
44
+ padding: 20px;
45
+ margin: 20px 0;
46
+ position: center;
47
+ }
48
+ .fish{
49
+ font-family: 'Poppins', sans-serif;
50
+ font-size: 1.5rem;
51
+ font-weight: 700;
52
+ letter-spacing: 1px;
53
+ text-align: center;
54
+ color: #222;
55
+ background: linear-gradient(45deg, #1a41d9, #6c69d2);
56
+ -webkit-background-clip: text;
57
+ -webkit-text-fill-color: transparent;
58
+ padding: 20px;
59
+ margin: 20px 0;
60
+ position: center;
61
+ }
62
+
63
+ .loading {
64
+ font-family: 'Poppins', sans-serif;
65
+ font-size: 2.7rem;
66
+ font-weight: 700;
67
+ text-transform: uppercase;
68
+ letter-spacing: 1px;
69
+ text-align: center;
70
+ color: #222;
71
+ background: linear-gradient(45deg, #40abe9, #1e99e5);
72
+ -webkit-background-clip: text;
73
+ -webkit-text-fill-color: transparent;
74
+ padding: 20px;
75
+ /*margin: 20px 0;*/
76
+ position: center;
77
+ }
78
+ .barcontainer {
79
+ display: flex;
80
+ justify-content: center;
81
+ align-items: center;
82
+ }
83
+ .leftarrow, .rightarrow {
84
+ display: flex;
85
+ justify-content: center;
86
+ align-items: center;
87
+ font-size: 2.7rem;
88
+ color: #1d60dd;
89
+ }
90
+ .leftarrow {
91
+ transform: rotate(-270deg);
92
+ }
93
+
94
+ .sqlquery {
95
+ background-color: #272822;
96
+ color: #f8f8f2;
97
+ font-family: 'Courier New', monospace;
98
+ padding: 15px;
99
+ border-radius: 5px;
100
+ overflow-x: auto;
101
+ white-space: pre-wrap;
102
+ word-wrap: break-word;
103
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
104
+ }