File size: 18,447 Bytes
07423df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import gc
import logging
from typing import List

import torch
from h2o_wave import Q

from llm_studio.app_utils.sections.chat import chat_tab
from llm_studio.app_utils.sections.chat_update import chat_update
from llm_studio.app_utils.sections.common import delete_dialog
from llm_studio.app_utils.sections.dataset import (
    dataset_delete_current_datasets,
    dataset_delete_single,
    dataset_display,
    dataset_edit,
    dataset_import,
    dataset_import_uploaded_file,
    dataset_list,
    dataset_list_delete,
    dataset_merge,
    dataset_newexperiment,
)
from llm_studio.app_utils.sections.experiment import (
    experiment_delete,
    experiment_display,
    experiment_download_logs,
    experiment_download_model,
    experiment_download_predictions,
    experiment_list,
    experiment_push_to_huggingface_dialog,
    experiment_rename_ui_workflow,
    experiment_run,
    experiment_start,
    experiment_stop,
)
from llm_studio.app_utils.sections.home import home
from llm_studio.app_utils.sections.project import (
    current_experiment_compare,
    current_experiment_list_compare,
    current_experiment_list_delete,
    current_experiment_list_stop,
    experiment_rename_action_workflow,
    list_current_experiments,
)
from llm_studio.app_utils.sections.settings import settings
from llm_studio.app_utils.setting_utils import (
    load_default_user_settings,
    load_user_settings_and_secrets,
    save_user_settings_and_secrets,
)
from llm_studio.app_utils.utils import add_model_type
from llm_studio.app_utils.wave_utils import report_error, wave_utils_handle_error

logger = logging.getLogger(__name__)


async def handle(q: Q) -> None:
    """Handles all requests in application and calls according functions."""

    # logger.info(f"args: {q.args}")
    # logger.info(f"events: {q.events}")

    if not (
        q.args.__wave_submission_name__ == "experiment/display/chat/chatbot"
        or q.args.__wave_submission_name__ == "experiment/display/chat/clear_history"
    ):
        if "experiment/display/chat/cfg" in q.client:
            del q.client["experiment/display/chat/cfg"]
        if "experiment/display/chat/model" in q.client:
            del q.client["experiment/display/chat/model"]
        if "experiment/display/chat/tokenizer" in q.client:
            del q.client["experiment/display/chat/tokenizer"]
        torch.cuda.empty_cache()
        gc.collect()

    try:
        if q.args.__wave_submission_name__ == "home":
            await home(q)
        elif q.args.__wave_submission_name__ == "settings":
            await settings(q)
        elif q.args.__wave_submission_name__ == "save_settings":
            logger.info("Saving user settings")
            await save_user_settings_and_secrets(q)
            await settings(q)
        elif q.args.__wave_submission_name__ == "load_settings":
            load_user_settings_and_secrets(q)
            await settings(q)
        elif q.args.__wave_submission_name__ == "restore_default_settings":
            load_default_user_settings(q)
            await settings(q)

        elif q.args.__wave_submission_name__ == "report_error":
            await report_error(q)

        elif q.args.__wave_submission_name__ == "dataset/import":
            await dataset_import(q, step=1)
        elif q.args.__wave_submission_name__ == "dataset/list":
            await dataset_list(q)
        elif q.args.__wave_submission_name__ == "dataset/list/delete/abort":
            q.page["dataset/list"].items[0].table.multiple = False
            await dataset_list(q, reset=True)
        elif q.args.__wave_submission_name__ == "dataset/list/abort":
            q.page["dataset/list"].items[0].table.multiple = False
            await dataset_list(q, reset=True)
        elif q.args.__wave_submission_name__ == "dataset/list/delete":
            await dataset_list_delete(q)
        elif q.args.__wave_submission_name__ == "dataset/delete/single":
            dataset_id = q.client["dataset/delete/single/id"]
            dataset_id = q.client["dataset/list/df_datasets"]["id"].iloc[dataset_id]
            await dataset_delete_single(q, int(dataset_id))
        elif q.args.__wave_submission_name__ == "dataset/delete/dialog/single":
            dataset_id = int(q.args["dataset/delete/dialog/single"])
            q.client["dataset/delete/single/id"] = dataset_id
            name = q.client["dataset/list/df_datasets"]["name"].iloc[dataset_id]

            if q.client["delete_dialogs"]:
                await delete_dialog(q, [name], "dataset/delete/single", "dataset")
            else:
                dataset_id = q.client["dataset/list/df_datasets"]["id"].iloc[dataset_id]
                await dataset_delete_single(q, int(dataset_id))

        elif q.args["dataset/delete/dialog"]:
            names = list(
                q.client["dataset/list/df_datasets"]["name"].iloc[
                    list(map(int, q.client["dataset/list/table"]))
                ]
            )

            if not names:
                return

            if q.client["delete_dialogs"]:
                await delete_dialog(q, names, "dataset/delete", "dataset")
            else:
                await dataset_delete_current_datasets(q)

        elif q.args.__wave_submission_name__ == "dataset/delete":
            await dataset_delete_current_datasets(q)
        elif q.args.__wave_submission_name__ == "dataset/edit":
            if q.client["dataset/list/df_datasets"] is not None:
                dataset_id = int(q.args["dataset/edit"])
                dataset_id = q.client["dataset/list/df_datasets"]["id"].iloc[dataset_id]
                await dataset_edit(q, int(dataset_id))
        elif q.args.__wave_submission_name__ == "dataset/newexperiment":
            if q.client["dataset/list/df_datasets"] is not None:
                dataset_id = int(q.args["dataset/newexperiment"])
                dataset_id = q.client["dataset/list/df_datasets"]["id"].iloc[dataset_id]
                await dataset_newexperiment(q, int(dataset_id))
        elif q.args.__wave_submission_name__ == "dataset/newexperiment/from_current":
            idx = q.client["dataset/display/id"]
            dataset_id = q.client["dataset/list/df_datasets"]["id"].iloc[idx]
            await dataset_newexperiment(q, dataset_id)

        elif q.args.__wave_submission_name__ == "dataset/list/table":
            q.client["dataset/display/id"] = int(q.args["dataset/list/table"][0])
            await dataset_display(q)

        elif q.args.__wave_submission_name__ == "dataset/display/visualization":
            await dataset_display(q)
        elif q.args.__wave_submission_name__ == "dataset/display/data":
            await dataset_display(q)
        elif q.args.__wave_submission_name__ == "dataset/display/statistics":
            await dataset_display(q)
        elif q.args["dataset/display/summary"]:
            await dataset_display(q)

        elif (
            q.args.__wave_submission_name__ == "experiment/start/run"
            or q.args.__wave_submission_name__ == "experiment/start/error/proceed"
        ):
            # add model type to cfg file name here
            q.client["experiment/start/cfg_file"] = add_model_type(
                q.client["experiment/start/cfg_file"],
                q.client["experiment/start/cfg_sub"],
            )
            q.client.delete_cards.add("experiment/start")
            await experiment_run(q, pre="experiment/start")
            q.client["experiment/list/mode"] = "train"

        elif (
            q.args.__wave_submission_name__ == "experiment/start_experiment"
            or q.args.__wave_submission_name__ == "experiment/list/new"
        ):
            if q.client["experiment/list/df_experiments"] is not None:
                selected_idx = int(q.args["experiment/list/new"])
                experiment_id = q.client["experiment/list/df_experiments"]["id"].iloc[
                    selected_idx
                ]

                q.client["experiment/start/cfg_category"] = "experiment"
                q.client["experiment/start/cfg_file"] = "experiment"
                q.client["experiment/start/cfg_experiment"] = str(experiment_id)

            await experiment_start(q)
        elif q.args.__wave_submission_name__ == "experiment/start":
            q.client["experiment/start/cfg_category"] = None
            q.client["experiment/start/cfg_file"] = None
            datasets_df = q.client.app_db.get_datasets_df()
            if datasets_df.shape[0] == 0:
                info = "Import dataset before you create an experiment. "
                await dataset_import(q, step=1, info=info)
            else:
                await experiment_start(q)

        elif q.args.__wave_submission_name__ == "experiment/display/download_logs":
            await experiment_download_logs(q)
        elif (
            q.args.__wave_submission_name__ == "experiment/display/download_predictions"
        ):
            await experiment_download_predictions(q)

        elif q.args.__wave_submission_name__ == "experiment/list":
            q.client["experiment/list/mode"] = "train"
            await experiment_list(q)
        elif q.args.__wave_submission_name__ == "experiment/list/current":
            await list_current_experiments(q)
        elif q.args.__wave_submission_name__ == "experiment/list/current/noreset":
            await list_current_experiments(q, reset=False)
        elif q.args.__wave_submission_name__ == "experiment/list/refresh":
            await experiment_list(q)
        elif q.args.__wave_submission_name__ == "experiment/list/abort":
            await list_current_experiments(q)
        elif q.args.__wave_submission_name__ == "experiment/list/stop":
            await current_experiment_list_stop(q)
        elif q.args.__wave_submission_name__ == "experiment/list/delete":
            await current_experiment_list_delete(q)
        elif q.args.__wave_submission_name__ == "experiment/list/rename":
            await experiment_rename_ui_workflow(q)
        elif q.args.__wave_submission_name__ == "experiment/list/compare":
            await current_experiment_list_compare(q)
        elif (
            q.args.__wave_submission_name__ == "experiment/stop"
            or q.args.__wave_submission_name__ == "experiment/list/stop/table"
        ):
            if q.args["experiment/list/stop/table"]:
                idx = int(q.args["experiment/list/stop/table"])
                selected_id = q.client["experiment/list/df_experiments"]["id"].iloc[idx]
                experiment_ids = [selected_id]
            else:
                selected_idxs = q.client["experiment/list/table"]
                experiment_ids = list(
                    q.client["experiment/list/df_experiments"]["id"].iloc[
                        list(map(int, selected_idxs))
                    ]
                )

            await experiment_stop(q, experiment_ids)
            await list_current_experiments(q)
        elif q.args.__wave_submission_name__ == "experiment/list/delete/table/dialog":
            idx = int(q.args["experiment/list/delete/table/dialog"])
            names = [q.client["experiment/list/df_experiments"]["name"].iloc[idx]]
            selected_id = q.client["experiment/list/df_experiments"]["id"].iloc[idx]
            q.client["experiment/delete/single/id"] = selected_id
            if q.client["delete_dialogs"]:
                await delete_dialog(
                    q, names, "experiment/list/delete/table", "experiment"
                )
            else:
                await experiment_delete_all_artifacts(q, [selected_id])

        elif q.args.__wave_submission_name__ == "experiment/delete/dialog":
            selected_idxs = q.client["experiment/list/table"]
            exp_df = q.client["experiment/list/df_experiments"]
            names = list(exp_df["name"].iloc[list(map(int, selected_idxs))])

            if not names:
                return

            if q.client["delete_dialogs"]:
                await delete_dialog(q, names, "experiment/delete", "experiment")
            else:
                experiment_ids = list(exp_df["id"].iloc[list(map(int, selected_idxs))])
                await experiment_delete_all_artifacts(q, experiment_ids)

        elif (
            q.args.__wave_submission_name__ == "experiment/delete"
            or q.args.__wave_submission_name__ == "experiment/list/delete/table"
        ):
            if q.args["experiment/list/delete/table"]:
                selected_id = q.client["experiment/delete/single/id"]
                experiment_ids = [selected_id]
            else:
                selected_idxs = q.client["experiment/list/table"]
                exp_df = q.client["experiment/list/df_experiments"]
                experiment_ids = list(exp_df["id"].iloc[list(map(int, selected_idxs))])

            await experiment_delete_all_artifacts(q, experiment_ids)

        elif q.args.__wave_submission_name__ == "experiment/rename/action":
            await experiment_rename_action_workflow(q)

        elif q.args.__wave_submission_name__ == "experiment/compare":
            await current_experiment_compare(q)
        elif q.args.__wave_submission_name__ == "experiment/compare/charts":
            await current_experiment_compare(q)
        elif q.args.__wave_submission_name__ == "experiment/compare/config":
            await current_experiment_compare(q)
        elif q.args.__wave_submission_name__ == "experiment/compare/diff_toggle":
            q.client["experiment/compare/diff_toggle"] = q.args[
                "experiment/compare/diff_toggle"
            ]
            await current_experiment_compare(q)

        elif q.args.__wave_submission_name__ == "experiment/list/table":
            q.client["experiment/display/id"] = int(q.args["experiment/list/table"][0])
            q.client["experiment/display/logs_path"] = None
            q.client["experiment/display/preds_path"] = None
            q.client["experiment/display/tab"] = None
            await experiment_display(q)

        elif q.args.__wave_submission_name__ == "experiment/display/refresh":
            await experiment_display(q)

        elif q.args.__wave_submission_name__ == "experiment/display/charts":
            await experiment_display(q)
        elif q.args.__wave_submission_name__ == "experiment/display/summary":
            await experiment_display(q)
        elif (
            q.args.__wave_submission_name__ == "experiment/display/train_data_insights"
        ):
            await experiment_display(q)
        elif (
            q.args.__wave_submission_name__
            == "experiment/display/validation_prediction_insights"
        ):
            await experiment_display(q)
        elif (
            q.args.__wave_submission_name__ == "experiment/display/push_to_huggingface"
        ):
            await experiment_push_to_huggingface_dialog(q)
        elif q.args.__wave_submission_name__ == "experiment/display/download_model":
            await experiment_download_model(q)
        elif (
            q.args.__wave_submission_name__
            == "experiment/display/push_to_huggingface_submit"
        ):
            await experiment_push_to_huggingface_dialog(q)

        elif q.args.__wave_submission_name__ == "experiment/display/config":
            await experiment_display(q)
        elif q.args.__wave_submission_name__ == "experiment/display/logs":
            await experiment_display(q)
        elif q.args.__wave_submission_name__ == "experiment/display/chat":
            await experiment_display(q)

        elif q.args.__wave_submission_name__ == "experiment/display/chat/chatbot":
            await chat_update(q)
        elif q.args.__wave_submission_name__ == "experiment/display/chat/clear_history":
            await chat_tab(q, load_model=False)

        elif q.args.__wave_submission_name__ == "dataset/import/local_upload":
            await dataset_import_uploaded_file(q)
        elif q.args.__wave_submission_name__ == "dataset/import/local_path_list":
            await dataset_import(q, step=1)
        elif q.args.__wave_submission_name__ == "dataset/import/2":
            await dataset_import(q, step=2)
        elif q.args.__wave_submission_name__ == "dataset/import/3":
            await dataset_import(q, step=3)
        elif q.args.__wave_submission_name__ == "dataset/import/3/edit":
            await dataset_import(q, step=3, edit=True)
        elif q.args.__wave_submission_name__ == "dataset/import/4":
            await dataset_import(q, step=4)
        elif q.args.__wave_submission_name__ == "dataset/import/4/edit":
            await dataset_import(q, step=4, edit=True)
        elif q.args.__wave_submission_name__ == "dataset/import/6":
            await dataset_import(q, step=6)
        elif (
            q.args.__wave_submission_name__ == "dataset/import/source"
            and not q.args["dataset/list"]
        ):
            await dataset_import(q, step=1)
        elif q.args.__wave_submission_name__ == "dataset/merge":
            await dataset_merge(q, step=1)
        elif q.args.__wave_submission_name__ == "dataset/merge/action":
            await dataset_merge(q, step=2)

        elif q.args.__wave_submission_name__ == "dataset/import/cfg_file":
            await dataset_import(q, step=3)

        # leave at the end of dataset import routing,
        # would also be triggered if user clicks on
        # a continue button in the dataset import wizard
        elif q.args.__wave_submission_name__ == "dataset/import/cfg/train_dataframe":
            await dataset_import(q, step=3)

        elif q.args.__wave_submission_name__ == "experiment/start/cfg_file":
            q.client["experiment/start/cfg_file"] = q.args["experiment/start/cfg_file"]
            await experiment_start(q)
        elif q.args.__wave_submission_name__ == "experiment/start/dataset":
            await experiment_start(q)

        elif q.client["nav/active"] == "experiment/start":
            await experiment_start(q)

    except Exception as unknown_exception:
        logger.error("Unknown exception", exc_info=True)
        await wave_utils_handle_error(
            q,
            error=unknown_exception,
        )


async def experiment_delete_all_artifacts(q: Q, experiment_ids: List[int]):
    await experiment_stop(q, experiment_ids)
    await experiment_delete(q, experiment_ids)
    await list_current_experiments(q)