Views
No views yet
1from unsloth import FastLanguageModel
2import torch
3import json
4from tqdm import tqdm
5
6print("モデルの読み込み開始---")
7model_name = "KokiMaruyama/01_llm-jp-3-13b-it"
8
9max_seq_length = 2048
10dtype = None
11load_in_4bit = True
12
13model, tokenizer = FastLanguageModel.from_pretrained(
14 model_name = model_name,
15 max_seq_length = max_seq_length,
16 dtype = dtype,
17 load_in_4bit = load_in_4bit,
18 token = "",
19)
20FastLanguageModel.for_inference(model)
21
22print("データセットの読み込み---")
23# データセットの読み込み。
24# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
25data = []
26with open("/content/elyza-tasks-100-TV_0.jsonl", "r") as f:
27 item = ""
28 for line in f:
29 line = line.strip()
30 item += line
31 if item.endswith("}"):
32 data.append(json.loads(item))
33 item = ""
34
35
36# 推論
37print("推論---")
38results = []
39for dt in tqdm(data):
40 input = dt["input"]
41
42 prompt = f"""### 指示\n{input}\n### 回答\n"""
43
44 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
45
46 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
47 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
48
49 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
50
51
52# 出力
53print("出力---")
54with open(f"/content/output.jsonl", 'w', encoding='utf-8') as f:
55 for result in results:
56 json.dump(result, f, ensure_ascii=False)
57 f.write('\n')