Views
No views yet
1!pip install unsloth
2!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
3!pip install -U torch
4!pip install -U peft
5
6from unsloth import FastLanguageModel
7from peft import PeftModel
8import torch
9import json
10from tqdm import tqdm
11import re
12
13model_id = "llm-jp/llm-jp-3-13b"
14adapter_id = "ymachida36/llm-jp-3-13b-it3_lora"
15
16HF_TOKEN = <your-token>
17
18dtype = None
19load_in_4bit = True
20model, tokenizer = FastLanguageModel.from_pretrained(
21 model_name=model_id,
22 dtype=dtype,
23 load_in_4bit=load_in_4bit,
24 trust_remote_code=True,
25)
26
27model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
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
39FastLanguageModel.for_inference(model)
40
41results = []
42for dt in tqdm(datasets):
43 input = dt["input"]
44
45 prompt = f"""### 指示\n{input}\n### 回答\n"""
46
47 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
48
49 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
50 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
51
52 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
53
54
55json_file_id = re.sub(".*/", "", adapter_id)
56with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
57 for result in results:
58 json.dump(result, f, ensure_ascii=False)
59 f.write('\n')