Views
No views yet
1from unsloth import FastLanguageModel
2import torch
3import json
4
5
6HF_TOKEN = "your-token"
7model_name = "qcube/llm-jp-3-13b-finetune6"
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=HF_TOKEN,
19)
20FastLanguageModel.for_inference(model)
21
22# データセットの読み込み。
23# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
24datasets = []
25with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
26 item = ""
27 for line in f:
28 line = line.strip()
29 item += line
30 if item.endswith("}"):
31 datasets.append(json.loads(item))
32 item = ""
33
34
35from tqdm import tqdm
36
37# 推論
38results = []
39for dt in tqdm(datasets):
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(
47 **inputs,
48 max_new_tokens=512,
49 use_cache=True,
50 do_sample=False,
51 repetition_penalty=1.2,
52 )
53 prediction = tokenizer.decode(
54 outputs[0],
55 skip_special_tokens=True,
56 ).split(
57 "\n### 回答"
58 )[-1]
59
60 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
61
62
63with open(f"./llm-jp-3-13b-finetune6-outputs-3.jsonl", "w", encoding="utf-8") as f:
64 for result in results:
65 json.dump(result, f, ensure_ascii=False)
66 f.write("\n")