Views
No views yet
1from unsloth import FastLanguageModel
2import torch
3import json
4
5model_name = "ssssayaaaa/llm-jp-3-13b-finetune-2"
6
7max_seq_length = 2048
8dtype = None
9load_in_4bit = True
10
11model, tokenizer = FastLanguageModel.from_pretrained(
12 model_name = model_name,
13 max_seq_length = max_seq_length,
14 dtype = dtype,
15 load_in_4bit = load_in_4bit,
16 token = "HF token",
17)
18FastLanguageModel.for_inference(model)
19
20# データセットの読み込み。
21# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
22datasets = []
23with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
24 item = ""
25 for line in f:
26 line = line.strip()
27 item += line
28 if item.endswith("}"):
29 datasets.append(json.loads(item))
30 item = ""
31
32
33from tqdm import tqdm
34
35# 推論
36results = []
37for dt in tqdm(datasets):
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": dt["task_id"], "input": input, "output": prediction})
48
49with open(f"/content/{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')