Views
No views yet
1
2%%capture
3!pip install unsloth
4!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
5
6from unsloth import FastLanguageModel
7import torch
8import json
9
10model_name = "morizon/llm-jp-3-13b-ichi_all"
11
12# Hugging Faceで取得したTokenをこちらに貼る。
13HF_TOKEN = "your_token"
14
15max_seq_length = 2048
16dtype = None
17load_in_4bit = True
18
19model, tokenizer = FastLanguageModel.from_pretrained(
20 model_name = model_name,
21 max_seq_length = max_seq_length,
22 dtype = dtype,
23 load_in_4bit = load_in_4bit,
24 token = "HF_TOKEN",
25)
26FastLanguageModel.for_inference(model)
27
28# データセットの読み込み。
29# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
30datasets = []
31with open("/content/elyza-tasks-100-TV_0.jsonl", "r") as f:
32 item = ""
33 for line in f:
34 line = line.strip()
35 item += line
36 if item.endswith("}"):
37 datasets.append(json.loads(item))
38 item = ""
39
40from tqdm import tqdm
41
42# 推論
43results = []
44for dt in tqdm(datasets):
45 input = dt["input"]
46
47 prompt = f"""### 指示\n{input}\n### 回答\n"""
48
49 inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
50
51 outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
52 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
53
54 results.append({"task_id": dt["task_id"], "input": input, "output" : prediction})
55
56from datetime import datetime
57import pytz
58import json
59
60# 日本時間を取得
61jst = pytz.timezone('Asia/Tokyo')
62current_time = datetime.now(jst).strftime('%Y%m%d_%H%M%S')
63
64# ファイル名に現在時刻を追加
65file_name = f"/content/{model_name}_output_{current_time}.jsonl"
66
67# ファイルを開いて書き込む
68with open(file_name, 'w', encoding='utf-8') as f:
69 for result in results:
70 json.dump(result, f, ensure_ascii=False)
71 f.write('\n')
72