Update README.md
Browse files
README.md
CHANGED
|
@@ -4,7 +4,69 @@ tags: []
|
|
| 4 |
---
|
| 5 |
# Sample code
|
| 6 |
```python ```
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
``` ```
|
| 9 |
# Model Card for Model ID
|
| 10 |
|
|
|
|
| 4 |
---
|
| 5 |
# Sample code
|
| 6 |
```python ```
|
| 7 |
+
from transformers import (
|
| 8 |
+
AutoModelForCausalLM,
|
| 9 |
+
AutoTokenizer,
|
| 10 |
+
BitsAndBytesConfig,
|
| 11 |
+
)
|
| 12 |
+
from peft import PeftModel
|
| 13 |
+
import torch
|
| 14 |
+
from tqdm import tqdm
|
| 15 |
+
import json
|
| 16 |
+
import re
|
| 17 |
+
from google.colab import files
|
| 18 |
+
|
| 19 |
+
HF_TOKEN = "YOUR TOKEN"
|
| 20 |
+
your_path = '/elyza-tasks-100-TV_0.jsonl'
|
| 21 |
+
|
| 22 |
+
model_id = "llm-jp/llm-jp-3-13b"
|
| 23 |
+
adapter_id = "mss6/f4"
|
| 24 |
+
|
| 25 |
+
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
| 26 |
+
|
| 27 |
+
datasets = []
|
| 28 |
+
with open(your_path, "r") as f:
|
| 29 |
+
item = ""
|
| 30 |
+
for line in f:
|
| 31 |
+
line = line.strip()
|
| 32 |
+
item += line
|
| 33 |
+
if item.endswith("}"):
|
| 34 |
+
datasets.append(json.loads(item))
|
| 35 |
+
item = ""
|
| 36 |
+
|
| 37 |
+
from tqdm import tqdm
|
| 38 |
+
|
| 39 |
+
results = []
|
| 40 |
+
for i in tqdm(range(100)):
|
| 41 |
+
data = datasets[i]
|
| 42 |
+
input = data["input"]
|
| 43 |
+
|
| 44 |
+
prompt = f"""### 指示
|
| 45 |
+
{input}
|
| 46 |
+
### 回答
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
| 50 |
+
attention_mask = torch.ones_like(tokenized_input)
|
| 51 |
+
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
outputs = model.generate(
|
| 54 |
+
tokenized_input,
|
| 55 |
+
attention_mask=attention_mask,
|
| 56 |
+
max_new_tokens=1000,
|
| 57 |
+
do_sample=False,
|
| 58 |
+
repetition_penalty=1.2,
|
| 59 |
+
pad_token_id=tokenizer.eos_token_id
|
| 60 |
+
)[0]
|
| 61 |
+
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
| 62 |
+
|
| 63 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
| 64 |
+
|
| 65 |
+
jsonl_id = re.sub(".*/", "", 'ans')
|
| 66 |
+
with open(f"/content/drive/MyDrive/{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
|
| 67 |
+
for result in results:
|
| 68 |
+
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
|
| 69 |
+
f.write('\n')
|
| 70 |
``` ```
|
| 71 |
# Model Card for Model ID
|
| 72 |
|