Views
No views yet
1# ライブラリのンストール
2%%capture
3!pip install unsloth
4!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
5
6from unsloth import FastLanguageModel
7import torch
8import json
9
10!pip install httpx==0.27.2
11
12# model,tokenizerの読み込み。
13model_name = "kochan13/llm-jp-3-13b-19_lora"
14
15max_seq_length = 2048
16dtype = None
17load_in_4bit = True
18
19model, tokenizer = FastLanguageModel.from_pretrained(
20 model_name = model_name,
21 max_seq_length = max_seq_length,
22 dtype = dtype,
23 load_in_4bit = load_in_4bit,
24 token = "my_HF_token", #"HF token",
25)
26FastLanguageModel.for_inference(model)
27
28# データセットの読み込み。
29datasets = []
30with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
31 item = ""
32 for line in f:
33 line = line.strip()
34 item += line
35 if item.endswith("}"):
36 datasets.append(json.loads(item))
37 item = ""
38
39from tqdm import tqdm
40
41# 推論
42results = []
43for dt in tqdm(datasets):
44 input = dt["input"]
45
46 prompt = f"""### 指示\n{input}\n### 回答\n"""
47
48 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
49
50 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
51 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
52
53 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
54
55import os
56
57# 結果をjsonlで保存
58filename = f"{model_name.split('/')[-1]}_output.jsonl" # モデル名の末尾部分だけを使用
59filepath = os.path.join("/content", filename) # Join the directory and filename
60
61# 保存処理
62with open(filepath, 'w', encoding='utf-8') as f:
63 for result in results:
64 json.dump(result, f, ensure_ascii=False)
65 f.write('\n')
66
67print(f"Results saved to: {filepath}")