Views
No views yet
1!pip install unsloth
2!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 peft1from 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 = f"JunichiroMorita/llm-jp-3-13b-it_lora_20241216"
10
11HF_TOKEN = 'your_hugging_face_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
25datasets = []
26with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
27 item = ""
28 for line in f:
29 line = line.strip()
30 item += line
31 if item.endswith("}"):
32 datasets.append(json.loads(item))
33 item = ""
34
35FastLanguageModel.for_inference(model)
36
37results = []
38for dt in tqdm(datasets):
39 input = dt["input"]
40
41 prompt = f"""### 指示\n{input}\n\n### 回答\n"""
42
43 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
44
45 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
46 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答\n')[-1]
47
48 results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
49
50with open(f'./llm-jp-3-13b-it_lora_20241216_output.jsonl', 'w', encoding='utf-8') as f:
51 for result in results:
52 json.dump(result, f, ensure_ascii=False)
53 f.write('\n')