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