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"1from unsloth import FastLanguageModel
2import torch
3import json1# Base model
2model_name = "kkkeee/llm-jp-3-13b-it15"
3
4# Hugging Face Token
5HF_TOKEN = "<your_hf_token>" # Obtain token from https://huggingface.co/settings/tokens
6
7# Load base model using Unsloth
8max_seq_length = 2048
9dtype = None
10load_in_4bit = True
11
12model, tokenizer = FastLanguageModel.from_pretrained(
13 model_name = model_name,
14 max_seq_length = max_seq_length,
15 dtype = dtype,
16 load_in_4bit = load_in_4bit,
17 token = HF_TOKEN,
18)
19FastLanguageModel.for_inference(model)
20
211# 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 = ""1from tqdm import tqdm
2
3# 推論
4results = []
5for data in tqdm(datasets):
6 input = data["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(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
13 output = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
14
15 results.append({"task_id": data["task_id"], "input": input, "output": output})1with open(f"/content/output.jsonl", 'w', encoding='utf-8') as f:
2 for result in results:
3 json.dump(result, f, ensure_ascii=False)
4 f.write('\n')