Instructions to use azuki-digital/gemma-2-27b-it-2_lora with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use azuki-digital/gemma-2-27b-it-2_lora with Transformers:
# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("azuki-digital/gemma-2-27b-it-2_lora", dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- Unsloth Studio new
How to use azuki-digital/gemma-2-27b-it-2_lora with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for azuki-digital/gemma-2-27b-it-2_lora to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for azuki-digital/gemma-2-27b-it-2_lora to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for azuki-digital/gemma-2-27b-it-2_lora to start chatting
Load model with FastModel
pip install unsloth from unsloth import FastModel model, tokenizer = FastModel.from_pretrained( model_name="azuki-digital/gemma-2-27b-it-2_lora", max_seq_length=2048, )
Taka commited on
Update README.md
Browse files
README.md
CHANGED
|
@@ -20,104 +20,4 @@ base_model:
|
|
| 20 |
|
| 21 |
gemma-2利用にあたり、ライセンス上制約の懸念のあるデータセットは利用していない。
|
| 22 |
|
| 23 |
-
|
| 24 |
-
unsloth版のサンプルコード(Google Colab L4使用)をベースとし、推論は1時間以内で終了するようになっている。
|
| 25 |
-
なお、unsloth版でgemmaを直接使用しようとすると、意図せず別のモデルがダウンロードされることが報告されていることから、当該事象を回避するため、ローカルに一度ダウンロードしたものを使用する形に変更している。
|
| 26 |
-
|
| 27 |
-
```
|
| 28 |
-
# 必要なライブラリをインストール
|
| 29 |
-
%%capture
|
| 30 |
-
!pip install unsloth
|
| 31 |
-
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
| 32 |
-
!pip install -U torch
|
| 33 |
-
!pip install -U peft
|
| 34 |
-
```
|
| 35 |
-
|
| 36 |
-
```
|
| 37 |
-
HF_TOKEN = "" #必要なトークンを設定してください
|
| 38 |
-
```
|
| 39 |
-
|
| 40 |
-
```
|
| 41 |
-
# HFからモデルリポジトリをダウンロード
|
| 42 |
-
!huggingface-cli login --token $HF_TOKEN
|
| 43 |
-
!huggingface-cli download google/gemma-2-27b --local-dir gemma-2-27b/
|
| 44 |
-
```
|
| 45 |
-
|
| 46 |
-
```
|
| 47 |
-
# 必要なライブラリを読み込み
|
| 48 |
-
from unsloth import FastLanguageModel
|
| 49 |
-
from peft import PeftModel
|
| 50 |
-
import torch
|
| 51 |
-
import json
|
| 52 |
-
from tqdm import tqdm
|
| 53 |
-
import re
|
| 54 |
-
```
|
| 55 |
-
|
| 56 |
-
```
|
| 57 |
-
# ベースとなるモデルと学習したLoRAのアダプタ
|
| 58 |
-
model_id = "/content/gemma-2-27b"
|
| 59 |
-
adapter_id = "Taka2024/gemma-2-27b-it-2_lora"
|
| 60 |
-
```
|
| 61 |
-
|
| 62 |
-
```
|
| 63 |
-
# unslothのFastLanguageModelで元のモデルをロード。
|
| 64 |
-
dtype = None # Noneにしておけば自動で設定
|
| 65 |
-
load_in_4bit = True # 今回は27Bモデルを扱うためTrue
|
| 66 |
-
|
| 67 |
-
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 68 |
-
model_name=model_id,
|
| 69 |
-
dtype=dtype,
|
| 70 |
-
load_in_4bit=load_in_4bit,
|
| 71 |
-
trust_remote_code=True,
|
| 72 |
-
)
|
| 73 |
-
```
|
| 74 |
-
|
| 75 |
-
```
|
| 76 |
-
# 元のモデルにLoRAのアダプタを統合。
|
| 77 |
-
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
| 78 |
-
```
|
| 79 |
-
|
| 80 |
-
```
|
| 81 |
-
# タスクとなるデータの読み込み。
|
| 82 |
-
# 事前にデータをアップロードしてください。
|
| 83 |
-
datasets = []
|
| 84 |
-
with open("/content/elyza-tasks-100-TV_0.jsonl", "r") as f:
|
| 85 |
-
item = ""
|
| 86 |
-
for line in f:
|
| 87 |
-
line = line.strip()
|
| 88 |
-
item += line
|
| 89 |
-
if item.endswith("}"):
|
| 90 |
-
datasets.append(json.loads(item))
|
| 91 |
-
item = ""
|
| 92 |
-
```
|
| 93 |
-
|
| 94 |
-
```
|
| 95 |
-
# モデルを用いてタスクの推論。
|
| 96 |
-
# 推論するためにモデルのモードを変更
|
| 97 |
-
FastLanguageModel.for_inference(model)
|
| 98 |
-
|
| 99 |
-
results = []
|
| 100 |
-
for dt in tqdm(datasets):
|
| 101 |
-
input = dt["input"]
|
| 102 |
-
|
| 103 |
-
prompt = f"""### あなたは日本人のための優秀なコンシェルジュです。指示には必ずわかりやすい日本語で回答してください。\n### 指示\n{input}\n### 回答\n"""
|
| 104 |
-
|
| 105 |
-
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
| 106 |
-
|
| 107 |
-
outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
| 108 |
-
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
| 109 |
-
|
| 110 |
-
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
| 111 |
-
```
|
| 112 |
-
|
| 113 |
-
```
|
| 114 |
-
# 結果をjsonlで保存。
|
| 115 |
-
|
| 116 |
-
# ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
|
| 117 |
-
json_file_id = re.sub(".*/", "", adapter_id)
|
| 118 |
-
with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
| 119 |
-
for result in results:
|
| 120 |
-
json.dump(result, f, ensure_ascii=False)
|
| 121 |
-
f.write('\n')
|
| 122 |
-
```
|
| 123 |
-
|
|
|
|
| 20 |
|
| 21 |
gemma-2利用にあたり、ライセンス上制約の懸念のあるデータセットは利用していない。
|
| 22 |
|
| 23 |
+
Loraアダプタのみ保存している。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|