File size: 12,459 Bytes
df97cfa f732d7c df97cfa f732d7c d91928f df97cfa d91928f 0d4eedd d91928f 0d4eedd d91928f df97cfa f732d7c 135325d f732d7c df97cfa b555022 df97cfa b555022 f732d7c df97cfa dc15b84 df97cfa dc15b84 df97cfa b555022 f732d7c 0d4eedd f732d7c df97cfa d91928f f732d7c df97cfa f732d7c 4f91616 d91928f f732d7c df97cfa d91928f f732d7c 4f91616 d91928f f732d7c 0d4eedd d91928f 0d4eedd f732d7c dc15b84 f732d7c dc15b84 f732d7c dc15b84 f732d7c dc15b84 f732d7c dc15b84 f732d7c df97cfa deb1174 df97cfa f732d7c df97cfa ecf7aeb f732d7c dc15b84 f732d7c ecf7aeb f732d7c dc15b84 f732d7c ecf7aeb f732d7c dc15b84 deb1174 df97cfa ecf7aeb df97cfa a1a5519 df97cfa dc15b84 df97cfa dc15b84 df97cfa ecf7aeb df97cfa ecf7aeb df97cfa f732d7c df97cfa ecf7aeb df97cfa ecf7aeb df97cfa 0d4eedd df97cfa |
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 |
import datetime
import json
import os
import shutil
from typing import Optional
from typing import Tuple
import gradio as gr
import torch
from fastchat.serve.inference import compress_module
from fastchat.serve.inference import raise_warning_for_old_weights
from huggingface_hub import Repository
from huggingface_hub import hf_hub_download
from huggingface_hub import snapshot_download
from peft import LoraConfig
from peft import get_peft_model
from peft import set_peft_model_state_dict
from transformers import AutoModelForCausalLM
from transformers import GenerationConfig
from transformers import LlamaTokenizer
print(datetime.datetime.now())
NUM_THREADS = 4
print(NUM_THREADS)
print("starting server ...")
BASE_MODEL = "decapoda-research/llama-13b-hf"
LORA_WEIGHTS = "izumi-lab/llama-13b-japanese-lora-v0-1ep"
HF_TOKEN = os.environ.get("HF_TOKEN", None)
DATASET_REPOSITORY = os.environ.get("DATASET_REPOSITORY", None)
repo = None
LOCAL_DIR = "/home/user/data/"
PROMPT_LANG = "en"
assert PROMPT_LANG in ["ja", "en"]
if HF_TOKEN and DATASET_REPOSITORY:
try:
shutil.rmtree(LOCAL_DIR)
except Exception:
pass
repo = Repository(
local_dir=LOCAL_DIR,
clone_from=DATASET_REPOSITORY,
use_auth_token=HF_TOKEN,
repo_type="dataset",
)
repo.git_pull()
tokenizer = LlamaTokenizer.from_pretrained(BASE_MODEL)
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
try:
if torch.backends.mps.is_available():
device = "mps"
except Exception:
pass
resume_from_checkpoint = snapshot_download(
repo_id=LORA_WEIGHTS, use_auth_token=HF_TOKEN
)
checkpoint_name = hf_hub_download(
repo_id=LORA_WEIGHTS, filename="adapter_model.bin", use_auth_token=HF_TOKEN
)
if device == "cuda":
model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL, load_in_8bit=True, device_map="auto", torch_dtype=torch.float16
)
elif device == "mps":
model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
device_map={"": device},
load_in_8bit=True,
torch_dtype=torch.float16,
)
else:
model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
device_map={"": device},
load_in_8bit=True,
low_cpu_mem_usage=True,
torch_dtype=torch.float16,
)
config = LoraConfig.from_pretrained(resume_from_checkpoint)
model = get_peft_model(model, config)
adapters_weights = torch.load(checkpoint_name)
set_peft_model_state_dict(model, adapters_weights)
raise_warning_for_old_weights(BASE_MODEL, model)
compress_module(model, device)
# if device == "cuda" or device == "mps":
# model = model.to(device)
def generate_prompt(instruction: str, input: Optional[str] = None):
print(f"input: {input}")
if input:
if PROMPT_LANG == "ja":
return f"ไปฅไธใฏใฟในใฏใ่ชฌๆใใๆ็คบใจใใใชใๆ่ใ้ฉ็จใใๅ
ฅๅใฎ็ตใฟๅใใใงใใ\n\n### ๆ็คบ:\n{instruction}\n\n### ๅ
ฅๅ:\n{input}\n\n### Response:\n"
elif PROMPT_LANG == "en":
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Input:
{input}
### Response:"""
else:
raise ValueError("PROMPT_LANG")
else:
if PROMPT_LANG == "ja":
return f"ไปฅไธใฏใฟในใฏใ่ชฌๆใใๆ็คบใจใใใชใๆ่ใ้ฉ็จใใๅ
ฅๅใฎ็ตใฟๅใใใงใใ\n\n### ๆ็คบ:\n{instruction}\n\n### ่ฟ็ญ:\n"
elif PROMPT_LANG == "en":
return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Response:"""
else:
raise ValueError("PROMPT_LANG")
if device != "cpu":
model.half()
model.eval()
if torch.__version__ >= "2":
model = torch.compile(model)
def save_inputs_and_outputs(now, inputs, outputs, generate_kwargs):
current_hour = now.strftime("%Y-%m-%d_%H")
file_name = f"prompts_{LORA_WEIGHTS.split('/')[-1]}_{current_hour}.jsonl"
if repo is not None:
repo.git_pull(rebase=True)
with open(os.path.join(LOCAL_DIR, file_name), "a", encoding="utf-8") as f:
json.dump(
{
"inputs": inputs,
"outputs": outputs,
"generate_kwargs": generate_kwargs,
},
f,
ensure_ascii=False,
)
f.write("\n")
repo.push_to_hub()
# we cant add typing now
# https://github.com/gradio-app/gradio/issues/3514
def evaluate(
instruction,
input=None,
temperature=0.7,
max_tokens=384,
):
num_beams: int = 1
top_p: float = 1.0
top_k: int = 0
prompt = generate_prompt(instruction, input)
inputs = tokenizer(prompt, return_tensors="pt")
if len(inputs["input_ids"][0]) > max_tokens:
return f"please reduce length. Currently, {len(inputs['input_ids'][0])} token are used.", gr.update(interactive=True), gr.update(interactive=True)
input_ids = inputs["input_ids"].to(device)
generation_config = GenerationConfig(
temperature=temperature,
top_p=top_p,
top_k=top_k,
num_beams=num_beams,
pad_token_id=tokenizer.pad_token_id,
eos_token=tokenizer.eos_token_id,
)
with torch.no_grad():
generation_output = model.generate(
input_ids=input_ids,
generation_config=generation_config,
return_dict_in_generate=True,
output_scores=True,
max_new_tokens=max_tokens-len(input_ids),
)
s = generation_output.sequences[0]
output = tokenizer.decode(s, skip_special_tokens=True)
if prompt.endswith("Response:"):
output = output.split("### Response:")[1].strip()
elif prompt.endswith("่ฟ็ญ:"):
output = output.split("### ่ฟ็ญ:")[1].strip()
else:
raise ValueError(f"No valid prompt ends. {prompt}")
if HF_TOKEN and DATASET_REPOSITORY:
try:
now = datetime.datetime.now()
current_time = now.strftime("%Y-%m-%d %H:%M:%S")
print(f"[{current_time}] Pushing prompt and completion to the Hub")
save_inputs_and_outputs(
now,
prompt,
output,
{
"temperature": temperature,
"top_p": top_p,
"top_k": top_k,
"num_beams": num_beams,
"max_tokens": max_tokens,
},
)
except Exception as e:
print(e)
return output, gr.update(interactive=True), gr.update(interactive=True)
def reset_textbox():
return gr.update(value=""), gr.update(value=""), gr.update(value="")
def no_interactive() -> Tuple[gr.Request, gr.Request]:
return gr.update(interactive=False), gr.update(interactive=False)
title = """<h1 align="center">LLaMA-13B Japanese LoRA</h1>"""
theme = gr.themes.Default(primary_hue="green")
description = """The official demo for **[izumi-lab/llama-13b-japanese-lora-v0-1ep](https://huggingface.co/izumi-lab/llama-13b-japanese-lora-v0-1ep)**. It is a 13B-parameter LLaMA model finetuned to follow instructions. It is trained on the [izumi-lab/llm-japanese-dataset](https://huggingface.co/datasets/izumi-lab/llm-japanese-dataset) dataset. For more information, please visit [the project's website](https://llm.msuzuki.me)."""
with gr.Blocks(
css="""#col_container { margin-left: auto; margin-right: auto;}""",
theme=theme,
) as demo:
gr.HTML(title)
gr.Markdown(description)
with gr.Column(elem_id="col_container", visible=False) as main_block:
with gr.Row():
with gr.Column():
instruction = gr.Textbox(
lines=3, label="Instruction (Pre-Prompt + Instruction + Input is limitted to 256 tokens)", placeholder="ใใใซใกใฏ"
)
inputs = gr.Textbox(lines=1, label="Input", placeholder="none")
with gr.Row():
with gr.Column(scale=3):
clear_button = gr.Button("Clear").style(full_width=True)
with gr.Column(scale=5):
submit_button = gr.Button("Submit").style(full_width=True)
outputs = gr.Textbox(lines=4, label="Output")
# inputs, top_p, temperature, top_k, repetition_penalty
with gr.Accordion("Parameters", open=True):
temperature = gr.Slider(
minimum=0,
maximum=1.0,
value=0.7,
step=0.05,
interactive=True,
label="Temperature",
)
max_tokens = gr.Slider(
minimum=20,
maximum=384,
value=128,
step=1,
interactive=True,
label="Max length (Pre-prompt + instruction + input + output))",
)
with gr.Column(elem_id="user_consent_container") as user_consent_block:
# Get user consent
gr.Markdown(
"""
## User Consent for Data Collection, Use, and Sharing:
By using our app, you acknowledge and agree to the following terms regarding the data you provide:
- **Collection**: We may collect inputs you type into our app.
- **Use**: We may use the collected data for research purposes, to improve our services, and to develop new products or services, including commercial applications.
- **Sharing and Publication**: Your input data may be published, shared with third parties, or used for analysis and reporting purposes.
- **Data Retention**: We may retain your input data for as long as necessary.
By continuing to use our app, you provide your explicit consent to the collection, use, and potential sharing of your data as described above. If you do not agree with our data collection, use, and sharing practices, please do not use our app.
## ใใผใฟๅ้ใๅฉ็จใๅ
ฑๆใซ้ขใใใฆใผใถใผใฎๅๆ๏ผ
ๆฌใขใใชใไฝฟ็จใใใใจใซใใใๆไพใใใใผใฟใซ้ขใใไปฅไธใฎๆกไปถใ่ช่ญใๅๆใใใใฎใจใใพใ๏ผ
- **ๅ้**: ็ง้ใฏใใขใใชใซๅ
ฅๅใใใใใญในใใใผใฟใๅ้ใใๅ ดๅใใใใพใใ
- **ๅฉ็จ**: ๅ้ใใใใใผใฟใฏใ็ ็ฉถ็ฎ็ใใตใผใในใฎๆนๅใๆฐใใ่ฃฝๅใใตใผใใน๏ผๅๆฅญใขใใชใฑใผใทใงใณใๅซใ๏ผใฎ้็บใซไฝฟ็จใใๅ ดๅใใใใพใใ
- **ๅ
ฑๆใใใณๅ
ฌ้**: ๅ
ฅๅใใผใฟใฏๅ
ฌ้ใใใใใ็ฌฌไธ่
ใจๅ
ฑๆใใใใใๅๆใๅ ฑๅใฎ็ฎ็ใงไฝฟ็จใใใๅ ดๅใใใใพใใ
- **ใใผใฟไฟๆ**: ๅ
ฅๅใใผใฟใฏๅฟ
่ฆใชๆ้ใซใใใฃใฆไฟๆใใๅ ดๅใใใใพใใ
ๆฌใขใใชใๅผใ็ถใไฝฟ็จใใใใจใซใใใไธ่จใฎใใใซใใผใฟใฎๅ้ใๅฉ็จใใใใณๆฝๅจ็ใชๅ
ฑๆใซใคใใฆๆ็คบ็ใชๅๆใๆไพใใพใใใใผใฟใฎๅ้ใๅฉ็จใๅ
ฑๆใฎๆนๆณใซๅๆใใชใๅ ดๅใฏใๆฌใขใใชใไฝฟ็จใใชใใงใใ ใใใ
"""
)
accept_button = gr.Button("I Agree")
def enable_inputs():
return user_consent_block.update(visible=False), main_block.update(
visible=True
)
accept_button.click(
fn=enable_inputs,
inputs=[],
outputs=[user_consent_block, main_block],
queue=False,
)
inputs.submit(no_interactive, [], [submit_button, clear_button])
inputs.submit(
evaluate,
[instruction, inputs, temperature, max_tokens],
[outputs, submit_button, clear_button],
)
submit_button.click(no_interactive, [], [submit_button, clear_button])
submit_button.click(
evaluate,
[instruction, inputs, temperature, max_tokens],
[outputs, submit_button, clear_button],
)
clear_button.click(reset_textbox, [], [instruction, inputs, outputs], queue=False)
demo.queue(max_size=20, concurrency_count=NUM_THREADS, api_open=False).launch(
share=True, server_name="0.0.0.0", server_port=7860
)
|