Views
No views yet
1%%capture
2!pip install unsloth
3!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
4
5from unsloth import FastLanguageModel
6import torch
7import json
8
9model_name = "nomukoh/llm-jp-3-13b-it"
10max_seq_length = 2048
11dtype = None
12load_in_4bit = True
13
14model, tokenizer = FastLanguageModel.from_pretrained(
15 model_name = model_name,
16 max_seq_length = max_seq_length,
17 dtype = dtype,
18 load_in_4bit = load_in_4bit,
19 token = "your_token",
20)
21FastLanguageModel.for_inference(model)
22
23# データセットの読み込み
24datasets = []
25with open("/content/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
34from tqdm import tqdm
35
36# 推論
37results = []
38for dt in tqdm(datasets):
39 input_text = dt["input"]
40
41 prompt = f"""### 指示\n{input_text}\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_text, "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')
55