Skip to content

Commit

Permalink
Add code to push spaces chatbot
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudotensor committed Apr 19, 2023
1 parent 4265104 commit ba92b50
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 21 deletions.
13 changes: 2 additions & 11 deletions create_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import numpy as np
from tqdm import tqdm

from utils import flatten_list


def parse_rst_file(filepath):
with open(filepath, 'r') as f:
Expand Down Expand Up @@ -525,17 +527,6 @@ def test_show_prompts():
print(generate_prompt(data_point, 'plain', False, False)[0])


def flatten_list(lis):
"""Given a list, possibly nested to any level, return it flattened."""
new_lis = []
for item in lis:
if type(item) == type([]):
new_lis.extend(flatten_list(item))
else:
new_lis.append(item)
return new_lis


def test_get_open_datasets():
# HF changed things so don't get raw list of all datasets, so not have to filter, but can't do negative filter
open_tags = ['license:Apache License 2.0',
Expand Down
7 changes: 6 additions & 1 deletion finetune.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,24 @@ def train(
ddp: bool = True, # set to False if OOM with True, for multi-GPU model parallelism
local_files_only: bool = False, # else will download new versions, normally unwanted
resume_download: bool = True,
use_auth_token: bool = False, # True requires CLI did huggingface-cli login before running
use_auth_token: Union[str, bool] = False, # True requires CLI did huggingface-cli login before running
warmup_steps: int = 100,
logging_steps: int = 1,
save_steps: int = None, # must be round multiple of eval_steps
add_eos_token: bool = False,
):
# allow set token directly
use_auth_token = os.environ.get("HUGGINGFACE_API_TOKEN", use_auth_token)

prompt_type = str(prompt_type) # migration from integers
assert prompt_type in prompt_types

world_size = int(os.getenv("WORLD_SIZE", 1))
local_rank = int(os.getenv("LOCAL_RANK", 0))
rank = int(os.getenv("RANK", 0))
print(f"local_rank: {local_rank}")
print(f"global rank: {rank}")

gpus = max(world_size, torch.cuda.device_count())
run_id = run_id or 0
if not data_path:
Expand Down
30 changes: 22 additions & 8 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import os
import typing

from utils import set_seed, flatten_list
SEED = 1236
from utils import set_seed
set_seed(SEED)

os.environ['HF_HUB_DISABLE_TELEMETRY'] = '1'
Expand Down Expand Up @@ -55,7 +55,7 @@ def main(
share: bool = True,
local_files_only: bool = False,
resume_download: bool = True,
use_auth_token: bool = False, # True requires CLI did huggingface-cli login before running
use_auth_token: Union[str, bool] = False, # True requires CLI did huggingface-cli login before running

src_lang: str = "English",
tgt_lang: str = "Russian",
Expand Down Expand Up @@ -86,6 +86,13 @@ def main(
eval_sharegpt_prompts_only_seed: int = 1234,
eval_sharegpt_as_output: bool = False,
):
# allow set token directly
use_auth_token = os.environ.get("HUGGINGFACE_API_TOKEN", use_auth_token)
# override share if in spaces
if os.environ.get("HUGGINGFACE_SPACES"):
share = False
base_model = 'h2oai/h2ogpt-oasst1-512-12b'
load_8bit = True

# get defaults
model_lower = base_model.lower()
Expand Down Expand Up @@ -246,7 +253,7 @@ def get_device():
return device


def get_non_lora_model(base_model, model_loader, load_half, model_kwargs, reward_type, force_1_gpu=True, use_auth_token=True):
def get_non_lora_model(base_model, model_loader, load_half, model_kwargs, reward_type, force_1_gpu=True, use_auth_token=False):
"""
Ensure model gets on correct device
:param base_model:
Expand Down Expand Up @@ -316,7 +323,7 @@ def get_model(
reward_type: bool = None,
local_files_only: bool = False,
resume_download: bool = True,
use_auth_token: bool = True,
use_auth_token: Union[str, bool] = False,
compile: bool = True,
**kwargs,
):
Expand Down Expand Up @@ -499,7 +506,12 @@ def go_gradio(**kwargs):
Hash: {get_githash()}
"""
else:
description = ""
description = "For more information, visit [the project's website](https://github.com/h2oai/h2ogpt).<br>"
if os.environ.get("HUGGINGFACE_SPACES"):
description += """<p><b> DISCLAIMERS: </b><ul><i><li>The data used to train this model include The Pile and other sources. These may contain objectionable content, so the model may reproduce that material. Use application and responses at own risk.</i></li>"""
if kwargs['load_8bit']:
description += """<i><li> Model is loaded in 8-bit to fit on HF GPUs, so model may perform worse than 16-bit.</i></li>"""
description += """<i><li>Model loading and unloading disabled on HF SPACES to avoid GPU OOM for multi-user environment.</i></li></ul></p>"""

if kwargs['verbose']:
task_info_md = f"""
Expand Down Expand Up @@ -534,7 +546,6 @@ def go_gradio(**kwargs):
# css_code = 'body{background-image:url("https://h2o.ai/content/experience-fragments/h2o/us/en/site/header/master/_jcr_content/root/container/header_copy/logo.coreimg.svg/1678976605175/h2o-logo.svg");}'
# demo = gr.Blocks(theme='gstaff/xkcd', css=css_code)

from create_data import flatten_list
model_options = flatten_list(list(prompt_type_to_model_name.values())) + kwargs['extra_model_options']
if kwargs['base_model'].strip() not in model_options:
lora_options = [kwargs['base_model'].strip()] + model_options
Expand Down Expand Up @@ -676,7 +687,9 @@ def go_gradio(**kwargs):
model_choice = gr.Dropdown(model_options_state.value[0], label="Choose Model", value=kwargs['base_model'])
lora_choice = gr.Dropdown(lora_options_state.value[0], label="Choose LORA", value=kwargs['lora_weights'], visible=kwargs['show_lora'])
with gr.Column(scale=1):
load_model_button = gr.Button("Load Model/LORA")
load_msg = "Load Model/LORA" if not os.environ.get("HUGGINGFACE_SPACES") \
else "LOAD DISABLED ON HF SPACES"
load_model_button = gr.Button(load_msg)
model_used = gr.Textbox(label="Current Model", value=kwargs['base_model'])
lora_used = gr.Textbox(label="Current LORA", value=kwargs['lora_weights'], visible=kwargs['show_lora'])
with gr.Row(scale=1):
Expand Down Expand Up @@ -941,7 +954,8 @@ def chatbot_list(x, model_used_in):
outputs=[model_state, model_used, lora_used, prompt_type])
prompt_update_args = dict(fn=dropdown_prompt_type_list, inputs=prompt_type, outputs=prompt_type)
chatbot_update_args = dict(fn=chatbot_list, inputs=[text_output, model_used], outputs=text_output)
load_model_event = load_model_button.click(**load_model_args).then(**prompt_update_args).then(**chatbot_update_args)
if not os.environ.get("HUGGINGFACE_SPACES"):
load_model_event = load_model_button.click(**load_model_args).then(**prompt_update_args).then(**chatbot_update_args)

def dropdown_model_list(list0, x):
new_state = [list0[0] + [x]]
Expand Down
1 change: 1 addition & 0 deletions spaces/chatbot/LICENSE
1 change: 1 addition & 0 deletions spaces/chatbot/client_test.py
1 change: 1 addition & 0 deletions spaces/chatbot/finetune.py
1 change: 1 addition & 0 deletions spaces/chatbot/generate.py
1 change: 1 addition & 0 deletions spaces/chatbot/h2o-logo.svg
1 change: 1 addition & 0 deletions spaces/chatbot/prompter.py
19 changes: 19 additions & 0 deletions spaces/chatbot/repo_to_spaces.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# start in h2ogpt repo
ln -sr generate.py h2o-logo.svg LICENSE stopping.py prompter.py finetune.py utils.py client_test.py requirements.txt spaces/chatbot/
cd ..

git clone https://huggingface.co/spaces/h2oai/h2ogpt-chatbot
cd h2ogpt-chatbot
rm -rf app.py h2o-logo.svg LICENSE stopping.py prompter.py finetune.py utils.py client_test.py requirements.txt
cd ../h2ogpt/spaces/chatbot/
cp generate.py h2o-logo.svg LICENSE stopping.py prompter.py finetune.py utils.py client_test.py requirements.txt ../../../h2ogpt-chatbot/
cd ../../../h2ogpt-chatbot/

mv generate.py app.py

git add app.py h2o-logo.svg LICENSE stopping.py prompter.py finetune.py utils.py client_test.py requirements.txt
git commit -m "Add application file and dependencies"
# ensure write token used and login with git control: huggingface-cli login --token <HUGGINGFACE_API_TOKEN> --add-to-git-credential
git push
1 change: 1 addition & 0 deletions spaces/chatbot/requirements.txt
1 change: 1 addition & 0 deletions spaces/chatbot/stopping.py
1 change: 1 addition & 0 deletions spaces/chatbot/utils.py
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions spaces/demo/h2oai_pipeline.py
File renamed without changes.
1 change: 0 additions & 1 deletion spaces/h2oai_pipeline.py

This file was deleted.

11 changes: 11 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ def set_seed(seed: int):
torch.backends.cudnn.benchmark = False
os.environ['PYTHONHASHSEED'] = str(seed)
return random_state


def flatten_list(lis):
"""Given a list, possibly nested to any level, return it flattened."""
new_lis = []
for item in lis:
if type(item) == type([]):
new_lis.extend(flatten_list(item))
else:
new_lis.append(item)
return new_lis

0 comments on commit ba92b50

Please sign in to comment.