Update README.md
Browse files
README.md
CHANGED
@@ -3,6 +3,76 @@ library_name: transformers
|
|
3 |
tags: []
|
4 |
---
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
# Model Card for Model ID
|
7 |
|
8 |
<!-- Provide a quick summary of what the model is/does. -->
|
|
|
3 |
tags: []
|
4 |
---
|
5 |
|
6 |
+
# How to conduct inference
|
7 |
+
|
8 |
+
```
|
9 |
+
from unsloth import FastLanguageModel
|
10 |
+
from peft import PeftModel
|
11 |
+
import torch
|
12 |
+
import json
|
13 |
+
from tqdm import tqdm
|
14 |
+
import re
|
15 |
+
|
16 |
+
# Base model id and LoRA adapter ID
|
17 |
+
base_model_id = "llm-jp/llm-jp-3-13b"
|
18 |
+
adapter_id = "Rumi/llm-jp_SFT_rn_2024-12-15_05"
|
19 |
+
|
20 |
+
# Log in with your Hugging Face token
|
21 |
+
HF_TOKEN = "hogehoge"
|
22 |
+
from huggingface_hub import login
|
23 |
+
login(HF_TOKEN)
|
24 |
+
|
25 |
+
# Download the original model
|
26 |
+
dtype = None
|
27 |
+
load_in_4bit = True
|
28 |
+
|
29 |
+
base_model, tokenizer = FastLanguageModel.from_pretrained(
|
30 |
+
model_name=base_model_id,
|
31 |
+
dtype=dtype,
|
32 |
+
load_in_4bit=load_in_4bit,
|
33 |
+
trust_remote_code=True,
|
34 |
+
)
|
35 |
+
|
36 |
+
# Merge adapter to the base model
|
37 |
+
model = PeftModel.from_pretrained(base_model, adapter_id, token = HF_TOKEN)
|
38 |
+
|
39 |
+
# Read evaluation dataset
|
40 |
+
datasets = []
|
41 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
42 |
+
item = ""
|
43 |
+
for line in f:
|
44 |
+
line = line.strip()
|
45 |
+
item += line
|
46 |
+
if item.endswith("}"):
|
47 |
+
datasets.append(json.loads(item))
|
48 |
+
item = ""
|
49 |
+
|
50 |
+
# Change the format and conduct the evaluation
|
51 |
+
FastLanguageModel.for_inference(model)
|
52 |
+
|
53 |
+
results = []
|
54 |
+
for dt in tqdm(datasets):
|
55 |
+
input = dt["input"]
|
56 |
+
|
57 |
+
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
58 |
+
|
59 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
60 |
+
|
61 |
+
outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
62 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
63 |
+
|
64 |
+
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
65 |
+
|
66 |
+
# Save result in the jsonl format
|
67 |
+
json_file_id = re.sub(".*/", "", adapter_id)
|
68 |
+
with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
69 |
+
for result in results:
|
70 |
+
json.dump(result, f, ensure_ascii=False)
|
71 |
+
f.write('\n')
|
72 |
+
|
73 |
+
```
|
74 |
+
|
75 |
+
|
76 |
# Model Card for Model ID
|
77 |
|
78 |
<!-- Provide a quick summary of what the model is/does. -->
|