Views
No views yet
pip install torch transformers tqdm unslothFastLanguageModelを使用して読み込みます.1from unsloth import FastLanguageModel
2import torch
3import json
4
5# モデルの名前を指定
6model_name = "iodine2/llm-jp-3-13b-it"
7
8# モデル設定
9max_seq_length = 2048
10dtype = None
11load_in_4bit = True
12
13# モデルとトークナイザを読み込み
14model, tokenizer = FastLanguageModel.from_pretrained(
15 model_name = model_name,
16 max_seq_length = max_seq_length,
17 dtype = dtype,
18 load_in_4bit = load_in_4bit,
19 token = "your HF token", # Hugging Face トークンを設定
20)
21FastLanguageModel.for_inference(model)
22elyza-tasks-100-TV_0.jsonl というJSONLファイルを使用します.1# データセットの読み込み
2datasets = []
3with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
4 item = ""
5 for line in f:
6 line = line.strip()
7 item += line
8 if item.endswith("}"):
9 datasets.append(json.loads(item))
10 item = ""1from tqdm import tqdm
2
3# 推論の準備
4FastLanguageModel.for_inference(model)
5
6results = []
7for dt in tqdm(datasets):
8 input = dt["input"]
9
10 prompt = f"""### 指示\n{input}\n### 回答\n"""
11
12 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
13
14 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
15 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
16
17 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
181import re
2
3# モデル名を動的に取得
4model_name = re.sub(".*/", "", model_name)
5
6# 推論結果をファイルに保存
7with open(f"./{model_name}-outputs.jsonl", 'w', encoding='utf-8') as f:
8 for result in results:
9 json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
10 f.write('\n')