File size: 5,401 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 |
import logging
import os
import shutil
from tempfile import NamedTemporaryFile
from bokeh.resources import Resources as BokehResources
from h2o_wave import Q, ui
from llm_studio.app_utils.config import default_cfg
from llm_studio.app_utils.db import Database, Dataset
from llm_studio.app_utils.default_datasets import (
prepare_default_dataset_causal_language_modeling,
prepare_default_dataset_dpo_modeling,
)
from llm_studio.app_utils.sections.common import interface
from llm_studio.app_utils.setting_utils import load_user_settings_and_secrets
from llm_studio.app_utils.utils import (
get_data_dir,
get_database_dir,
get_download_dir,
get_output_dir,
get_user_db_path,
get_user_name,
)
from llm_studio.src.utils.config_utils import load_config_py, save_config_yaml
logger = logging.getLogger(__name__)
async def import_default_data(q: Q):
"""Imports default data"""
try:
if q.client.app_db.get_dataset(1) is None:
logger.info("Downloading default dataset...")
q.page["meta"].dialog = ui.dialog(
title="Creating default datasets",
blocking=True,
items=[ui.progress(label="Please be patient...")],
)
await q.page.save()
dataset = prepare_oasst(q)
q.client.app_db.add_dataset(dataset)
dataset = prepare_dpo(q)
q.client.app_db.add_dataset(dataset)
except Exception as e:
q.client.app_db._session.rollback()
logger.warning(f"Could not download default dataset: {e}")
pass
def prepare_oasst(q: Q) -> Dataset:
path = f"{get_data_dir(q)}/oasst"
if os.path.exists(path):
shutil.rmtree(path)
os.makedirs(path, exist_ok=True)
df = prepare_default_dataset_causal_language_modeling(path)
cfg = load_config_py(
config_path=os.path.join("llm_studio/python_configs", default_cfg.cfg_file),
config_name="ConfigProblemBase",
)
cfg.dataset.train_dataframe = os.path.join(path, "train_full.pq")
cfg.dataset.prompt_column = ("instruction",)
cfg.dataset.answer_column = "output"
cfg.dataset.parent_id_column = "None"
cfg_path = os.path.join(path, f"{default_cfg.cfg_file}.yaml")
save_config_yaml(cfg_path, cfg)
dataset = Dataset(
id=1,
name="oasst",
path=path,
config_file=cfg_path,
train_rows=df.shape[0],
)
return dataset
def prepare_dpo(q):
path = f"{get_data_dir(q)}/dpo"
if os.path.exists(path):
shutil.rmtree(path)
os.makedirs(path, exist_ok=True)
train_df = prepare_default_dataset_dpo_modeling()
train_df.to_parquet(os.path.join(path, "train.pq"), index=False)
from llm_studio.python_configs.text_dpo_modeling_config import ConfigDPODataset
from llm_studio.python_configs.text_dpo_modeling_config import (
ConfigProblemBase as ConfigProblemBaseDPO,
)
cfg: ConfigProblemBaseDPO = ConfigProblemBaseDPO(
dataset=ConfigDPODataset(
train_dataframe=os.path.join(path, "train.pq"),
system_column="system",
prompt_column=("question",),
answer_column="chosen",
rejected_answer_column="rejected",
),
)
cfg_path = os.path.join(path, "text_dpo_modeling_config.yaml")
save_config_yaml(cfg_path, cfg)
dataset = Dataset(
id=2,
name="dpo",
path=path,
config_file=cfg_path,
train_rows=train_df.shape[0],
)
return dataset
async def initialize_client(q: Q) -> None:
"""Initialize the client."""
logger.info(f"Initializing client {q.client.client_initialized}")
if not q.client.client_initialized:
q.client.delete_cards = set()
q.client.delete_cards.add("init_app")
os.makedirs(get_data_dir(q), exist_ok=True)
os.makedirs(get_database_dir(q), exist_ok=True)
os.makedirs(get_output_dir(q), exist_ok=True)
os.makedirs(get_download_dir(q), exist_ok=True)
db_path = get_user_db_path(q)
q.client.app_db = Database(db_path)
logger.info(f"User name: {get_user_name(q)}")
q.client.client_initialized = True
q.client["mode_curr"] = "full"
load_user_settings_and_secrets(q)
await interface(q)
await import_default_data(q)
q.args.__wave_submission_name__ = default_cfg.start_page
return
async def initialize_app(q: Q) -> None:
"""
Initialize the app.
This function is called once when the app is started and stores values in q.app.
"""
logger.info("Initializing app ...")
icons_pth = "llm_studio/app_utils/static/"
(q.app["icon_path"],) = await q.site.upload([f"{icons_pth}/icon.png"])
script_sources = []
with NamedTemporaryFile(mode="w", suffix=".min.js") as f:
# write all Bokeh scripts to one file to make sure
# they are loaded sequentially
for js_raw in BokehResources(mode="inline").js_raw:
f.write(js_raw)
f.write("\n")
(url,) = await q.site.upload([f.name])
script_sources.append(url)
q.app["script_sources"] = script_sources
q.app["initialized"] = True
q.app.version = default_cfg.version
q.app.name = default_cfg.name
q.app.heap_mode = default_cfg.heap_mode
logger.info("Initializing app ... done")
|