Views
No views yet
1
2from unsloth import FastLanguageModel
3import torch
4import json
5
6model_name = "luomin-tokyo/llm-jp-3-13b-finetune-min-121502"
7HF_TOKEN = "replace with yours"
8
9max_seq_length = 512
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
37results = []
38for dt in tqdm(datasets):
39 input = dt["input"]
40
41 prompt = f"""### 指示\n{input}\n### 回答\n"""
42
43 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
44
45 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
46 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
47
48 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
49
50
51with open(f"/content/{model_name}_output.jsonl", 'w', encoding='utf-8') as f:
52 for result in results:
53 json.dump(result, f, ensure_ascii=False)
54 f.write('\n')