Hibiki252 commited on
Commit
d98edd0
·
verified ·
1 Parent(s): 50104bd

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +75 -1
README.md CHANGED
@@ -49,4 +49,78 @@ The dataset was compiled from the following publicly available datasets:
49
  ### Interfere Guide
50
  To perform inference, execute the following code.
51
 
52
- (code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  ### Interfere Guide
50
  To perform inference, execute the following code.
51
 
52
+ # 必要なライブラリを読み込み
53
+ from unsloth import FastLanguageModel
54
+ from peft import PeftModel
55
+ import torch
56
+ import json
57
+ from tqdm import tqdm
58
+ import re
59
+
60
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
61
+ model_id = "Hibiki252/gemma-2-27b-4bit"
62
+ adapter_id = "Hibiki252/gemma-2-27b-ten-adapter"
63
+
64
+ # Hugging Face Token を指定。
65
+ HF_TOKEN = <your token>
66
+
67
+ # unslothのFastLanguageModelで元のモデルをロード。
68
+ dtype = None # Noneにしておけば自動で設定
69
+ load_in_4bit = True
70
+
71
+ model, tokenizer = FastLanguageModel.from_pretrained(
72
+ model_name=model_id,
73
+ dtype=dtype,
74
+ load_in_4bit=load_in_4bit,
75
+ trust_remote_code=True,
76
+ )
77
+
78
+ # 元のモデルにLoRAのアダプタを統合。
79
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
80
+
81
+ # タスクとなるデータの読み込み。
82
+ # 事前にデータをアップロードしてください。
83
+ datasets = []
84
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
85
+ item = ""
86
+ for line in f:
87
+ line = line.strip()
88
+ item += line
89
+ if item.endswith("}"):
90
+ datasets.append(json.loads(item))
91
+ item = ""
92
+
93
+ # プロンプト
94
+ base_prompt = (
95
+ """あなたは世界最高峰のAIアシスタントです。以下のルールと指示に従って、入力されたタスクに対して具体的かつ正確な回答をしてください。
96
+ 【ルール】
97
+ - タスクの意図を十分に理解して回答してください。
98
+ - 質問と直接関係ない情報は書かないでください。
99
+ - 特別な指定がない限り、プログラミングコードは出力しないでください。
100
+ - 回答は特別な指示がない限り日本語で答えてください。
101
+ - 必要に応じて根拠や理由を説明してください。
102
+ 以下がタスクです。"""
103
+
104
+ # モデルを用いてタスクの推論。
105
+
106
+ # 推論するためにモデルのモードを変更
107
+ FastLanguageModel.for_inference(model)
108
+
109
+ results = []
110
+ for dt in tqdm(datasets):
111
+ input = dt["input"]
112
+
113
+ prompt = f"""{base_prompt}\n### 指示\n{input}\n### 回答\n"""
114
+
115
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
116
+
117
+ outputs = model.generate(**inputs, max_new_tokens = 4048, use_cache = True, do_sample=False,repetition_penalty=1.2)
118
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
119
+
120
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
121
+
122
+ # 結果をjsonlで保存。
123
+ with open("gemma27b_ten", 'w', encoding='utf-8') as f:
124
+ for result in results:
125
+ json.dump(result, f, ensure_ascii=False)
126
+ f.write('\n')