Views
No views yet
1%%capture
2!pip install unsloth
3!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
4!pip install -U torch
5!pip install -U peft1from unsloth import FastLanguageModel
2from peft import PeftModel
3import torch
4import json
5from tqdm import tqdm
6import re<adapter_id> and <HF_TOKEN> with appropriate values.1# Base model and LoRA adapter
2model_id = "llm-jp/llm-jp-3-13b"
3adapter_id = "Ohais/llm-jp-3-13b-it_lora"
4
5# Hugging Face Token
6HF_TOKEN = "<your_hf_token>" # Obtain token from https://huggingface.co/settings/tokens
7
8# Load base model using Unsloth
9model, tokenizer = FastLanguageModel.from_pretrained(
10 model_name=model_id,
11 dtype=None, # Automatically set dtype
12 load_in_4bit=True, # Recommended for large models
13 trust_remote_code=True,
14)
15
16# Integrate LoRA adapter
17model = PeftModel.from_pretrained(model, adapter_id, token=HF_TOKEN).jsonl format and upload it to your environment.1# Load task data
2datasets = []
3with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
4 item = ""
5 for line in f:
6 line = line.strip()
7 item += line
8 if item.endswith("}"):
9 datasets.append(json.loads(item))
10 item = ""1# Set model to inference mode
2FastLanguageModel.for_inference(model)
3
4results = []
5for dt in tqdm(datasets):
6 input = dt["input"]
7
8 prompt = f"""### 指示\n{input}\n### 回答\n"""
9
10 inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
11
12 outputs = model.generate(
13 **inputs, max_new_tokens=512, use_cache=True, do_sample=False, repetition_penalty=1.2
14 )
15 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
16
17 results.append({"task_id": dt["task_id"], "input": input, "output": prediction}).jsonl file. Replace <adapter_id> with the appropriate identifier.1# Save results to JSONL
2json_file_id = re.sub(".*/", "", adapter_id)
3with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
4 for result in results:
5 json.dump(result, f, ensure_ascii=False)
6 f.write('\n')