Views
No views yet
1# 必要なライブラリをインストール
2pip install -U bitsandbytes transformers accelerate datasets peft pandas
3pip install -U unsloth # stable release1from unsloth import FastLanguageModel
2from peft import PeftModel
3
4# ベースモデルID
5base_model_id="llm-jp/llm-jp-3-13b"
6# 本アダプタのID
7adapter_id="waiyanan/llm-jp-3-13b-automulti-unsloth-it-r64-lr1e4-ep1_lora"
8# Huggingfaceトークン
9hf_token=<有効なHuggingfaceトークン>
10
11# unslothのFastLanguageModelで元のモデルをロード。
12dtype = None # Noneにしておけば自動で設定
13load_in_4bit = True # 4bit量子化する
14
15model, tokenizer = FastLanguageModel.from_pretrained(
16 model_name = base_model_id,
17 dtype = dtype,
18 load_in_4bit = load_in_4bit,
19 trust_remote_code=True,
20 token=hf_token
21)
22
23model = PeftModel.from_pretrained(model, adapter_id, token=hf_token)1# 最大出力トークン数
2max_token = 1024
3prompt = "こんにちは!よろしくお願いいたします。"
4
5FastLanguageModel.for_inference(model)
6
7inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
8outputs = model.generate(**inputs, max_new_tokens = max_token, use_cache = True, temperature=0.5, top_p=0.9,do_sample=False, repetition_penalty=1.2)
9prediction = tokenizer.decode(outputs[0], skip_special_tokens=True)
10print(prediction)
111import pandas as pd
2from datasets import Dataset
3from tqdm import tqdm
4import json
5
6datasets = []
7# elyza-tasks-100-TV.jsonのファイルパス
8file_path =<path_to_input_file>
9# 最大出力トークン数
10max_token = 1024
11
12# データセットの読み込み
13df = pd.read_json(file_path, orient='records', lines=True)
14# 結果格納用配列
15results = []
16
17# モデルを推論モードにする
18FastLanguageModel.for_inference(model)
19
20for _, r in tqdm(df.iterrows(),total=len(df)):
21 input = r["input"]
22 task_id=r["task_id"]
23 prompt = f"""### 指示\n{input} 簡潔に回答してください \n### 回答\n"""
24
25 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
26
27 outputs = model.generate(**inputs, max_new_tokens = max_token, use_cache = True, temperature=0.5, top_p=0.9,do_sample=False, repetition_penalty=1.2)
28 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
29
30 results.append({"task_id":task_id, "input": input, "output": prediction})
31
32# 結果をjsonlで保存。
33
34ourput_file_path = <path_to_output_file>
35
36with open(ourput_file_path, 'w', encoding='utf-8') as f:
37 for result in results:
38 json.dump(result, f, ensure_ascii=False)
39 f.write('\n')
40