franceth commited on
Commit
04decb3
·
verified ·
1 Parent(s): da67496

Initial commit running application

Browse files
Files changed (2) hide show
  1. app.py +544 -7
  2. utilities.py +72 -0
app.py CHANGED
@@ -1,7 +1,544 @@
1
- import gradio as gr
2
-
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
-
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import os
4
+ from qatch.connectors.sqlite_connector import SqliteConnector
5
+ from qatch.generate_dataset.orchestrator_generator import OrchestratorGenerator
6
+ from qatch.evaluate_dataset.orchestrator_evaluator import OrchestratorEvaluator
7
+ from predictor.orchestrator_predictor import OrchestratorPredictor
8
+ import utilities as us
9
+ import plotly.express as px
10
+ import plotly.graph_objects as go
11
+
12
+ with open('style.css', 'r') as file:
13
+ css = file.read()
14
+
15
+ # DataFrame di default
16
+ df_default = pd.DataFrame({
17
+ 'Name': ['Alice', 'Bob', 'Charlie'],
18
+ 'Age': [25, 30, 35],
19
+ 'City': ['New York', 'Los Angeles', 'Chicago']
20
+ })
21
+
22
+ models_path = "models.csv"
23
+
24
+ # Variabile globale per tenere traccia dei dati correnti
25
+ df_current = df_default.copy()
26
+
27
+ input_data = {
28
+ 'input_method': "",
29
+ 'data_path': "",
30
+ 'db_name': "",
31
+ 'data': {
32
+ 'data_frames': {}, # dictionary of dataframes
33
+ 'db': None # SQLITE3 database object
34
+ },
35
+ 'models': []
36
+ }
37
+ def load_data(file, path, use_default):
38
+ """Carica i dati da un file, un percorso o usa il DataFrame di default."""
39
+ global df_current
40
+ if use_default:
41
+ input_data["input_method"] = 'default'
42
+ input_data["data_path"] = os.path.join(".", "data", "datainterface", "mytable.sqlite")
43
+ input_data["db_name"] = os.path.splitext(os.path.basename(input_data["data_path"]))[0]
44
+ input_data["data"]['data_frames'] = {'MyTable': df_current}
45
+
46
+ #TODO assegna il db a input_data["data"]['db']
47
+
48
+ df_current = df_default.copy() # Ripristina i dati di default
49
+ return input_data["data"]['data_frames']
50
+
51
+ selected_inputs = sum([file is not None, bool(path), use_default])
52
+ if selected_inputs > 1:
53
+ return 'Errore: Selezionare solo un metodo di input alla volta.'
54
+
55
+ if file is not None:
56
+ try:
57
+ input_data["input_method"] = 'uploaded_file'
58
+ input_data["db_name"] = os.path.splitext(os.path.basename(file))[0]
59
+ input_data["data_path"] = os.path.join(".", "data", f"data_interface{input_data['db_name']}.sqlite")
60
+ input_data["data"] = us.load_data(input_data["data_path"], input_data["db_name"])
61
+ df_current = input_data["data"]['data_frames'].get('MyTable', df_default) # Carica il DataFrame
62
+ print(df_current)
63
+ print(input_data["data"])
64
+ if( input_data["data"]['data_frames'] and not input_data["data"]['db']):
65
+ table2primary_key = {}
66
+ print("ok")
67
+ for table_name, df in input_data["data"]['data_frames'].items():
68
+ # Assign primary keys for each table
69
+ table2primary_key[table_name] = 'id'
70
+ print("ok2")
71
+ input_data["data"]["db"] = SqliteConnector(
72
+ relative_db_path=input_data["data_path"],
73
+ db_name=input_data["db_name"],
74
+ tables= input_data["data"]['data_frames'],
75
+ table2primary_key=table2primary_key
76
+ )
77
+ print(input_data["data"]["db"])
78
+ return input_data["data"]['data_frames']
79
+ except Exception as e:
80
+ return f'Errore nel caricamento del file: {e}'
81
+
82
+ if path:
83
+ if not os.path.exists(path):
84
+ return 'Errore: Il percorso specificato non esiste.'
85
+ try:
86
+ input_data["input_method"] = 'uploaded_file'
87
+ input_data["data_path"] = path
88
+ input_data["db_name"] = os.path.splitext(os.path.basename(path))[0]
89
+ input_data["data"] = us.load_data(input_data["data_path"], input_data["db_name"])
90
+ df_current = input_data["data"]['data_frames'].get('MyTable', df_default) # Carica il DataFrame
91
+
92
+ return input_data["data"]['data_frames']
93
+ except Exception as e:
94
+ return f'Errore nel caricamento del file dal percorso: {e}'
95
+
96
+ return input_data["data"]['data_frames']
97
+
98
+ def preview_default(use_default):
99
+ """Mostra il DataFrame di default se il checkbox è selezionato."""
100
+ if use_default:
101
+ return df_default # Mostra il DataFrame di default
102
+ return df_current # Mostra il DataFrame corrente, che potrebbe essere stato modificato
103
+
104
+ def update_df(new_df):
105
+ """Aggiorna il DataFrame corrente."""
106
+ global df_current # Usa la variabile globale per aggiornarla
107
+ df_current = new_df
108
+ return df_current
109
+
110
+ def open_accordion(target):
111
+ # Apre uno e chiude l'altro
112
+ if target == "reset":
113
+ return gr.update(open=True), gr.update(open=False, visible=False), gr.update(open=False, visible=False), gr.update(open=False, visible=False), gr.update(open=False, visible=False)
114
+ elif target == "model_selection":
115
+ return gr.update(open=False), gr.update(open=False), gr.update(open=True, visible=True), gr.update(open=False), gr.update(open=False)
116
+
117
+ # Interfaccia Gradio
118
+ interface = gr.Blocks(theme='d8ahazard/rd_blue', css_paths='style.css')
119
+
120
+ with interface:
121
+ gr.Markdown("# QATCH")
122
+ data_state = gr.State(None) # Memorizza i dati caricati
123
+ upload_acc = gr.Accordion("Upload your data section", open=True, visible=True)
124
+ select_table_acc = gr.Accordion("Select tables", open=False, visible=False)
125
+ select_model_acc = gr.Accordion("Select models", open=False, visible=False)
126
+ qatch_acc = gr.Accordion("QATCH execution", open=False, visible=False)
127
+ metrics_acc = gr.Accordion("Metrics", open=False, visible=False)
128
+
129
+
130
+
131
+ #################################
132
+ # PARTE DI INSERIMENTO DEL DB #
133
+ #################################
134
+ with upload_acc:
135
+ gr.Markdown("## Caricamento dei Dati")
136
+
137
+ file_input = gr.File(label="Trascina e rilascia un file", file_types=[".csv", ".xlsx", ".sqlite"])
138
+ path_input = gr.Textbox(label="Oppure inserisci il percorso locale del file")
139
+ with gr.Row():
140
+ default_checkbox = gr.Checkbox(label="Usa DataFrame di default")
141
+ preview_output = gr.DataFrame(interactive=True, visible=True, value=df_default)
142
+ submit_button = gr.Button("Carica Dati", interactive=False) # Disabilitato di default
143
+ output = gr.JSON(visible=False) # Output dizionario
144
+
145
+ # Funzione per abilitare il bottone se sono presenti dati da caricare
146
+ def enable_submit(file, path, use_default):
147
+ return gr.update(interactive=bool(file or path or use_default))
148
+
149
+ # Abilita il bottone quando i campi di input sono valorizzati
150
+ file_input.change(fn=enable_submit, inputs=[file_input, path_input, default_checkbox], outputs=[submit_button])
151
+ path_input.change(fn=enable_submit, inputs=[file_input, path_input, default_checkbox], outputs=[submit_button])
152
+ default_checkbox.change(fn=enable_submit, inputs=[file_input, path_input, default_checkbox], outputs=[submit_button])
153
+
154
+ # Mostra l'anteprima del DataFrame di default quando il checkbox è selezionato
155
+ default_checkbox.change(fn=preview_default, inputs=[default_checkbox], outputs=[preview_output])
156
+ preview_output.change(fn=update_df, inputs=[preview_output], outputs=[preview_output])
157
+
158
+ def handle_output(file, path, use_default):
159
+ """Gestisce l'output quando si preme il bottone 'Carica Dati'."""
160
+ result = load_data(file, path, use_default)
161
+
162
+ if isinstance(result, dict): # Se result è un dizionario di DataFrame
163
+ if len(result) == 1: # Se c'è solo una tabella
164
+ return (
165
+ gr.update(visible=False), # Nasconde l'output JSON
166
+ result, # Salva lo stato dei dati
167
+ gr.update(visible=False), # Nasconde la selezione tabella
168
+ result, # Mantiene lo stato dei dati
169
+ gr.update(interactive=False), # Disabilita il pulsante di submit
170
+ gr.update(visible=True, open=True), # Passa direttamente a select_model_acc
171
+ gr.update(visible=True, open=False)
172
+ )
173
+ else:
174
+ return (
175
+ gr.update(visible=False),
176
+ result,
177
+ gr.update(open=True, visible=True),
178
+ result,
179
+ gr.update(interactive=False),
180
+ gr.update(visible=False), # Mantiene il comportamento attuale
181
+ gr.update(visible=True, open=True)
182
+ )
183
+ else:
184
+ return (
185
+ gr.update(visible=False),
186
+ None,
187
+ gr.update(open=False, visible=True),
188
+ None,
189
+ gr.update(interactive=True),
190
+ gr.update(visible=False),
191
+ gr.update(visible=True, open=True)
192
+ )
193
+
194
+ submit_button.click(
195
+ fn=handle_output,
196
+ inputs=[file_input, path_input, default_checkbox],
197
+ outputs=[output, output, select_table_acc, data_state, submit_button, select_model_acc, upload_acc]
198
+ )
199
+
200
+
201
+
202
+ ######################################
203
+ # PARTE DI SELEZIONE DELLE TABELLE #
204
+ ######################################
205
+ with select_table_acc:
206
+ table_selector = gr.CheckboxGroup(choices=[], label="Seleziona le tabelle da visualizzare", value=[])
207
+ table_outputs = [gr.DataFrame(label=f"Tabella {i+1}", interactive=True, visible=False) for i in range(5)]
208
+ selected_table_names = gr.Textbox(label="Tabelle selezionate", visible=False, interactive=False)
209
+
210
+ # Bottone di selezione modelli (inizialmente disabilitato)
211
+ open_model_selection = gr.Button("Choose your models", interactive=False)
212
+
213
+ def update_table_list(data):
214
+ """Aggiorna dinamicamente la lista delle tabelle disponibili."""
215
+ if isinstance(data, dict) and data:
216
+ table_names = list(data.keys()) # Ritorna solo i nomi delle tabelle
217
+ return gr.update(choices=table_names, value=[]) # Reset delle selezioni
218
+ return gr.update(choices=[], value=[])
219
+
220
+ def show_selected_tables(data, selected_tables):
221
+ """Mostra solo le tabelle selezionate dall'utente e abilita il bottone."""
222
+ updates = []
223
+ if isinstance(data, dict) and data:
224
+ available_tables = list(data.keys()) # Nomi effettivamente disponibili
225
+ selected_tables = [t for t in selected_tables if t in available_tables] # Filtra selezioni valide
226
+
227
+ tables = {name: data[name] for name in selected_tables} # Filtra i DataFrame
228
+
229
+ for i, (name, df) in enumerate(tables.items()):
230
+ updates.append(gr.update(value=df, label=f"Tabella: {name}", visible=True))
231
+
232
+ # Se ci sono meno di 5 tabelle, nascondi gli altri DataFrame
233
+ for _ in range(len(tables), 5):
234
+ updates.append(gr.update(visible=False))
235
+ else:
236
+ updates = [gr.update(value=pd.DataFrame(), visible=False) for _ in range(5)]
237
+
238
+ # Abilitare/disabilitare il bottone in base alle selezioni
239
+ button_state = bool(selected_tables) # True se almeno una tabella è selezionata, False altrimenti
240
+ updates.append(gr.update(interactive=button_state)) # Aggiorna stato bottone
241
+
242
+ return updates
243
+
244
+ def show_selected_table_names(selected_tables):
245
+ """Mostra i nomi delle tabelle selezionate quando si preme il bottone."""
246
+ if selected_tables:
247
+ return gr.update(value=", ".join(selected_tables), visible=False)
248
+ return gr.update(value="", visible=False)
249
+
250
+ # Aggiorna automaticamente la lista delle checkbox quando `data_state` cambia
251
+ data_state.change(fn=update_table_list, inputs=[data_state], outputs=[table_selector])
252
+
253
+ # Aggiorna le tabelle visibili e lo stato del bottone in base alle selezioni dell'utente
254
+ table_selector.change(fn=show_selected_tables, inputs=[data_state, table_selector], outputs=table_outputs + [open_model_selection])
255
+
256
+ # Mostra la lista delle tabelle selezionate quando si preme "Choose your models"
257
+ open_model_selection.click(fn=show_selected_table_names, inputs=[table_selector], outputs=[selected_table_names])
258
+ open_model_selection.click(open_accordion, inputs=gr.State("model_selection"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc])
259
+
260
+
261
+
262
+ ####################################
263
+ # PARTE DI SELEZIONE DEL MODELLO #
264
+ ####################################
265
+ with select_model_acc:
266
+ gr.Markdown("**Model Selection**")
267
+
268
+ # Supponiamo che `us.read_models_csv` restituisca anche il percorso dell'immagine
269
+ model_list_dict = us.read_models_csv(models_path)
270
+ model_list = [model["name"] for model in model_list_dict]
271
+ model_images = [model["image_path"] for model in model_list_dict]
272
+
273
+ # Creazione dinamica di checkbox con immagini
274
+ model_checkboxes = []
275
+ for model, image_path in zip(model_list, model_images):
276
+ with gr.Row():
277
+ with gr.Column(scale=1):
278
+
279
+ gr.Image(image_path, show_label=False)
280
+ with gr.Column(scale=2):
281
+ model_checkboxes.append(gr.Checkbox(label=model, value=False))
282
+
283
+ selected_models_output = gr.JSON(visible = False)
284
+
285
+ # Funzione per ottenere i modelli selezionati
286
+ def get_selected_models(*model_selections):
287
+ selected_models = [model for model, selected in zip(model_list, model_selections) if selected]
288
+ input_data['models'] = selected_models
289
+ button_state = bool(selected_models) # True se almeno un modello è selezionato, False altrimenti
290
+ return selected_models, gr.update(open=True, visible=True), gr.update(interactive=button_state)
291
+
292
+ # Bottone di submit (inizialmente disabilitato)
293
+ submit_models_button = gr.Button("Submit Models", interactive=False)
294
+
295
+ # Collegamento dei checkbox agli eventi di selezione
296
+ for checkbox in model_checkboxes:
297
+ checkbox.change(
298
+ fn=get_selected_models,
299
+ inputs=model_checkboxes,
300
+ outputs=[selected_models_output, select_model_acc, submit_models_button]
301
+ )
302
+
303
+ submit_models_button.click(
304
+ fn=lambda *args: (get_selected_models(*args), gr.update(open=False, visible=True), gr.update(open=True, visible=True)),
305
+ inputs=model_checkboxes,
306
+ outputs=[selected_models_output, select_model_acc, qatch_acc]
307
+ )
308
+
309
+ reset_data = gr.Button("Open upload data section")
310
+ reset_data.click(open_accordion, inputs=gr.State("reset"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc])
311
+
312
+
313
+
314
+ ###############################
315
+ # PARTE DI ESECUZIONE QATCH #
316
+ ###############################
317
+ with qatch_acc:
318
+ selected_models_display = gr.JSON(label="Modelli selezionati")
319
+ submit_models_button.click(
320
+ fn=lambda: gr.update(value=input_data),
321
+ outputs=[selected_models_display]
322
+ )
323
+
324
+ proceed_to_metrics_button = gr.Button("Proceed to Metrics")
325
+ proceed_to_metrics_button.click(
326
+ fn=lambda: (gr.update(open=False, visible=True), gr.update(open=True, visible=True)),
327
+ outputs=[qatch_acc, metrics_acc]
328
+ )
329
+
330
+ reset_data = gr.Button("Open upload data section")
331
+ reset_data.click(open_accordion, inputs=gr.State("reset"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc])
332
+
333
+
334
+ #######################################
335
+ # PARTE DI VISUALIZZAZIONE METRICHE #
336
+ #######################################
337
+ with metrics_acc:
338
+ confirmation_text = gr.Markdown("## Metrics successfully loaded")
339
+
340
+ data_path = 'metrics_random2.csv'
341
+
342
+ def load_data_csv_es():
343
+ return pd.read_csv(data_path)
344
+
345
+ def calculate_average_metrics(df, selected_metrics):
346
+ df['avg_metric'] = df[selected_metrics].mean(axis=1)
347
+ return df
348
+
349
+ def plot_metric(df, selected_metrics, group_by, selected_models):
350
+ df = df[df['model'].isin(selected_models)]
351
+ df = calculate_average_metrics(df, selected_metrics)
352
+ avg_metrics = df.groupby(group_by)['avg_metric'].mean().reset_index()
353
+ fig = px.bar(
354
+ avg_metrics, x=group_by[0], y='avg_metric', color=group_by[-1], barmode='group',
355
+ title=f'Media metrica per {group_by[0]}',
356
+ labels={group_by[0]: group_by[0].capitalize(), 'avg_metric': 'Media Metrica'},
357
+ template='plotly_dark'
358
+ )
359
+ return fig
360
+
361
+ def plot_radar(df, selected_models):
362
+ radar_data = []
363
+ for model in selected_models:
364
+ model_df = df[df['model'] == model]
365
+ valid_efficiency = model_df['valid_efficiency_score'].mean()
366
+ avg_time = model_df['time'].mean()
367
+ avg_tuple_order = model_df['tuple_order'].dropna().mean()
368
+
369
+ radar_data.append({
370
+ 'model': model,
371
+ 'valid_efficiency_score': valid_efficiency,
372
+ 'time': avg_time,
373
+ 'tuple_order': avg_tuple_order
374
+ })
375
+
376
+ radar_df = pd.DataFrame(radar_data)
377
+ categories = ['valid_efficiency_score', 'time', 'tuple_order']
378
+
379
+ # Calcola il range dinamico per il grafico
380
+ min_val = radar_df[categories].min().min()
381
+ max_val = radar_df[categories].max().max()
382
+ radar_df[categories] = (radar_df[categories] - min_val) / (max_val - min_val)
383
+
384
+ fig = go.Figure()
385
+ for _, row in radar_df.iterrows():
386
+ fig.add_trace(go.Scatterpolar(
387
+ r=[row[cat] for cat in categories],
388
+ theta=categories,
389
+ fill='toself',
390
+ name=row['model']
391
+ ))
392
+
393
+ fig.update_layout(
394
+ polar=dict(radialaxis=dict(visible=True, range=[min_val, max_val])),
395
+ title='Radar Plot delle Metriche per Modello',
396
+ template='plotly_dark',
397
+ width=700, height=700
398
+ )
399
+
400
+ return fig
401
+
402
+ def plot_query_rate(df, selected_models, show_labels):
403
+ df = df[df['model'].isin(selected_models)]
404
+
405
+ fig = go.Figure()
406
+
407
+ for model in selected_models:
408
+ model_df = df[df['model'] == model].copy()
409
+
410
+ model_df['cumulative_time'] = model_df['time'].cumsum()
411
+ model_df['query_rate'] = 1 / model_df['time']
412
+
413
+ fig.add_trace(go.Scatter(
414
+ x=model_df['cumulative_time'],
415
+ y=model_df['query_rate'],
416
+ mode='lines+markers',
417
+ name=model,
418
+ line=dict(width=2)
419
+ ))
420
+
421
+ if show_labels:
422
+ prev_category = None
423
+ prev_time = -float('inf')
424
+ y_positions = [1.1, 1.3]
425
+ y_idx = 0
426
+
427
+ for i, row in model_df.iterrows():
428
+ current_category = row['test_category']
429
+ if current_category != prev_category and row['cumulative_time'] - prev_time > 5:
430
+ fig.add_vline(x=row['cumulative_time'], line_width=1, line_dash="dash", line_color="gray")
431
+ fig.add_annotation(
432
+ x=row['cumulative_time'],
433
+ y=max(model_df['query_rate']) * y_positions[y_idx % 2],
434
+ text=current_category,
435
+ showarrow=False,
436
+ font=dict(size=10, color="white"),
437
+ textangle=45,
438
+ yshift=10,
439
+ bgcolor="rgba(0,0,0,0.6)"
440
+ )
441
+ prev_category = current_category
442
+ prev_time = row['cumulative_time']
443
+ y_idx += 1
444
+
445
+ fig.update_layout(
446
+ title="Rate di Generazione delle Query per Modello",
447
+ xaxis_title="Tempo Cumulativo (s)",
448
+ yaxis_title="Query al Secondo",
449
+ template='plotly_dark',
450
+ legend_title="Modelli"
451
+ )
452
+
453
+ return fig
454
+
455
+ def update_plot(selected_metrics, group_by, selected_models):
456
+ df = load_data_csv_es()
457
+ return plot_metric(df, selected_metrics, group_by, selected_models)
458
+
459
+ def update_radar(selected_models):
460
+ df = load_data_csv_es()
461
+ return plot_radar(df, selected_models)
462
+
463
+ def update_query_rate(selected_models, show_labels):
464
+ df = load_data_csv_es()
465
+ return plot_query_rate(df, selected_models, show_labels)
466
+
467
+ def plot_query_time_evolution(df, selected_models):
468
+ # Filtriamo i dati per i modelli selezionati
469
+ df = df[df['model'].isin(selected_models)]
470
+
471
+ # Ordinare per modello e tempo per tracciare l'evoluzione
472
+ df_sorted = df.sort_values(by=['model', 'time'])
473
+
474
+ fig = go.Figure()
475
+
476
+ # Aggiungiamo una traccia per ogni modello
477
+ for model in selected_models:
478
+ model_df = df_sorted[df_sorted['model'] == model]
479
+ fig.add_trace(go.Scatter(
480
+ x=model_df.index, y=model_df['time'], mode='lines+markers', name=model,
481
+ line=dict(shape='linear'),
482
+ text=model_df['model']
483
+ ))
484
+
485
+ fig.update_layout(
486
+ title="Evoluzione del Tempo di Generazione per Modello",
487
+ xaxis_title="Indice della Query",
488
+ yaxis_title="Tempo (s)",
489
+ template='plotly_dark'
490
+ )
491
+
492
+ return fig
493
+
494
+
495
+ metrics = ["cell_precision", "cell_recall", "execution_accuracy", "tuple_cardinality", "tuple_constraint"]
496
+ group_options = {
497
+ "SQL Category": ["test_category", "model"],
498
+ "Tabella": ["tbl_name", "model"],
499
+ "Modello": ["model"]
500
+ }
501
+
502
+ df_initial = load_data_csv_es()
503
+ models = df_initial['model'].unique().tolist()
504
+
505
+ #with gr.Blocks(theme=gr.themes.Default(primary_hue='blue')) as demo:
506
+ gr.Markdown("""## Analisi delle prestazioni dei modelli
507
+ Seleziona una o più metriche per calcolare la media e visualizzare gli istogrammi e radar plots.
508
+ """)
509
+
510
+ # Sezione di selezione delle opzioni
511
+ with gr.Row():
512
+ metric_multiselect = gr.CheckboxGroup(choices=metrics, label="Seleziona le metriche")
513
+ model_multiselect = gr.CheckboxGroup(choices=models, label="Seleziona i modelli", value=models)
514
+ group_radio = gr.Radio(choices=list(group_options.keys()), label="Seleziona il raggruppamento", value="SQL Category")
515
+ #show_labels_checkbox = gr.Checkbox(label="Mostra etichette test category", value=True)
516
+
517
+ with gr.Row():
518
+ output_plot = gr.Plot()
519
+ # Dividi la pagina in due colonne
520
+ with gr.Row():
521
+ with gr.Column(scale=1): # Imposta la colonna a occupare metà della larghezza
522
+ radar_plot = gr.Plot(value=update_radar(models))
523
+ with gr.Column(scale=2): # Imposta la seconda colonna a occupare l'altra metà
524
+ show_labels_checkbox = gr.Checkbox(label="Mostra etichette test category", value=True)
525
+ query_rate_plot = gr.Plot(value=update_query_rate(models, True))
526
+
527
+ # Funzioni di callback per il cambiamento dei grafici
528
+ def on_change(selected_metrics, selected_group, selected_models):
529
+ return update_plot(selected_metrics, group_options[selected_group], selected_models)
530
+
531
+ def on_radar_change(selected_models):
532
+ return update_radar(selected_models)
533
+
534
+ show_labels_checkbox.change(update_query_rate, inputs=[model_multiselect, show_labels_checkbox], outputs=query_rate_plot)
535
+ metric_multiselect.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
536
+ group_radio.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
537
+ model_multiselect.change(on_change, inputs=[metric_multiselect, group_radio, model_multiselect], outputs=output_plot)
538
+ model_multiselect.change(on_radar_change, inputs=model_multiselect, outputs=radar_plot)
539
+ model_multiselect.change(update_query_rate, inputs=[model_multiselect, show_labels_checkbox], outputs=query_rate_plot)
540
+
541
+ reset_data = gr.Button("Open upload data section")
542
+ reset_data.click(open_accordion, inputs=gr.State("reset"), outputs=[upload_acc, select_table_acc, select_model_acc, qatch_acc, metrics_acc])
543
+
544
+ interface.launch()
utilities.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import pandas as pd
3
+ import sqlite3
4
+ import gradio as gr
5
+ import os
6
+
7
+ def carica_sqlite(file_path):
8
+ conn = sqlite3.connect(file_path)
9
+ cursor = conn.cursor()
10
+ cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
11
+ tabelle = cursor.fetchall()
12
+ tabelle = [tabella for tabella in tabelle if tabella[0] != 'sqlite_sequence']
13
+
14
+ dfs = {}
15
+ for tabella in tabelle:
16
+ nome_tabella = tabella[0]
17
+ df = pd.read_sql_query(f"SELECT * FROM {nome_tabella}", conn)
18
+ dfs[nome_tabella] = df
19
+ conn.close()
20
+ data_output = {'data_frames': dfs,'db': conn}
21
+ return data_output
22
+
23
+ # Funzione per leggere un file CSV
24
+ def carica_csv(file):
25
+ df = pd.read_csv(file)
26
+ return df
27
+
28
+ # Funzione per leggere un file Excel
29
+ def carica_excel(file):
30
+ xls = pd.ExcelFile(file)
31
+ dfs = {}
32
+ for sheet_name in xls.sheet_names:
33
+ dfs[sheet_name] = xls.parse(sheet_name)
34
+ return dfs
35
+
36
+ def load_data(data_path : str, db_name : str):
37
+ data_output = {'data_frames': {} ,'db': None}
38
+ table_name = os.path.splitext(os.path.basename(data_path))[0]
39
+ if data_path.endswith(".sqlite") :
40
+ data_output = carica_sqlite(data_path)
41
+ elif data_path.endswith(".csv"):
42
+ data_output['data_frames'] = {f"{table_name}_table" : carica_csv(data_path)}
43
+ elif data_path.endswith(".xlsx"):
44
+ data_output['data_frames'] = carica_excel(data_path)
45
+ else:
46
+ raise gr.Error("Formato file non supportato. Carica un file SQLite, CSV o Excel.")
47
+ return data_output
48
+
49
+ def read_api(api_key_path):
50
+ with open(api_key_path, "r", encoding="utf-8") as file:
51
+ api_key = file.read()
52
+ return api_key
53
+
54
+ def read_models_csv(file_path):
55
+ # Reads a CSV file and returns a list of dictionaries
56
+ models = [] # Change {} to []
57
+ with open(file_path, mode="r", newline="") as file:
58
+ reader = csv.DictReader(file)
59
+ for row in reader:
60
+ row["price"] = float(row["price"]) # Convert price to float
61
+ models.append(row) # Append to the list
62
+ return models
63
+
64
+ def csv_to_dict(file_path):
65
+ with open(file_path, mode='r', encoding='utf-8') as file:
66
+ reader = csv.DictReader(file)
67
+ data = []
68
+ for row in reader:
69
+ if "price" in row:
70
+ row["price"] = float(row["price"])
71
+ data.append(row)
72
+ return data