Views
No views yet
1from unsloth import FastLanguageModel
2from peft import PeftModel
3import torch
4import json
5from tqdm import tqdm
6import re
7
8model_id = "llm-jp/llm-jp-3-13b"
9adapter_id = "masato0822/llm-jp-3-13b-it"
10
11HF_TOKEN = ""
12
13dtype = None
14load_in_4bit = True
15
16model, tokenizer = FastLanguageModel.from_pretrained(
17 model_name=model_id,
18 dtype=dtype,
19 load_in_4bit=load_in_4bit,
20 trust_remote_code=True,
21)
22
23model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
24
25FastLanguageModel.for_inference(model)
26
27results = []
28for dt in tqdm(datasets):
29 input = dt["input"]
30
31 prompt = f"""### 指示\n{input}\n### 回答\n"""
32
33 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
34
35 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
36 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
37
38 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
39
40json_file_id = re.sub(".*/", "", adapter_id)
41with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
42 for result in results:
43 json.dump(result, f, ensure_ascii=False)
44 f.write('\n')