Views
No views yet
1from unsloth import FastLanguageModel
2from peft import PeftModel
3import torch
4import json
5import yaml
6from tqdm import tqdm
7import re
8
9model_id = "llm-jp/llm-jp-3-13b"
10adapter_id = "kanbac5/llm-jp-3-13b-it-1217llm2024_lora_1217"
11
12with open("api_info.yaml", 'r', encoding="utf-8") as yml:
13 parameters = yaml.safe_load(yml)
14HF_TOKEN = parameters["token"]
15
16dtype = None
17load_in_4bit = True
18
19model, tokenizer = FastLanguageModel.from_pretrained(
20 model_name=model_id,
21 dtype=dtype,
22 load_in_4bit=load_in_4bit,
23 trust_remote_code=True,
24)
25
26model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
27
28datasets = []
29with open("data/elyza-tasks-100-TV_0.jsonl", "r") as f:
30 item = ""
31 for line in f:
32 line = line.strip()
33 item += line
34 if item.endswith("}"):
35 datasets.append(json.loads(item))
36 item = ""
37
38FastLanguageModel.for_inference(model)
39
40results = []
41for dt in tqdm(datasets):
42 input = dt["input"]
43
44 prompt = f"""### 指示\n{input}\n### 回答\n"""
45
46 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
47
48 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
49 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
50
51 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
52
53
54json_file_id = re.sub(".*/", "", adapter_id)
55with open(f"{json_file_id}_output_1217.jsonl", 'w', encoding='utf-8') as f:
56 for result in results:
57 json.dump(result, f, ensure_ascii=False)
58 f.write('\n')
59