【要約】 日本語言語モデルJapanese StableLM Base Alpha 7BをWindowsで動かす手順は以下の通りである.まず,「pip install sentencepiece einops」と「pip install transformers」で必要なライブラリをインストールする.HuggingFaceの公式ページ https://huggingface.co/stabilityai/japanese-stablelm-base-alpha-7b に記載された手順に従い,そのページ内のPythonプログラムを実行する.さらに,このページではTkinterライブラリを使用してGUIを構築し,ユーザーからのメッセージをこのモデルに与え、結果を表示する Python アプリのソースコードと実行手順も示している.
アプリの実行画面
【目次】
【関連する外部ページ】
【サイト内の関連ページ】
【関連する外部ページ】
Python の公式ページ: https://www.python.org/
【サイト内の関連ページ】
NVIDIA グラフィックスボードを搭載しているパソコンの場合には, NVIDIA ドライバ, NVIDIA CUDA ツールキット, NVIDIA cuDNN のインストールを行う.
【関連する外部ページ】
コマンドプロンプトを管理者として実行: 別ページ »で説明
PyTorch のページ: https://pytorch.org/index.html
次のコマンドは, PyTorch 2.0 (NVIDIA CUDA 11.8 用) をインストールする. 但し,Anaconda3を使いたい場合には別手順になる.
事前に NVIDIA CUDA のバージョンを確認しておくこと(ここでは,NVIDIA CUDA ツールキット 11.8 が前もってインストール済みであるとする).
PyTorch で,GPU が動作している場合には,「torch.cuda.is_available()」により,True が表示される.
python -m pip install -U --ignore-installed pip python -m pip install -U torch torchvision torchaudio numpy --index-url https://download.pytorch.org/whl/cu118 python -c "import torch; print(torch.__version__, torch.cuda.is_available())"
Anaconda3を使いたい場合には, Anaconda プロンプト (Anaconda Prompt) を管理者として実行し, 次のコマンドを実行する. (PyTorch と NVIDIA CUDA との連携がうまくいかない可能性があるため,Anaconda3を使わないことも検討して欲しい).
conda install -y pytorch torchvision torchaudio pytorch-cuda=11.8 cudnn -c pytorch -c nvidia py -c "import torch; print(torch.__version__, torch.cuda.is_available())"
【サイト内の関連ページ】
【関連する外部ページ】
Japanese StableLM Base Alpha 7B の HuggingFace の公式ページ: https://huggingface.co/stabilityai/japanese-stablelm-base-alpha-7b に記載の手順をそのまま動かす.
コマンドプロンプトを管理者として実行: 別ページ »で説明
pip install sentencepiece einops
pip install transformers
python
エラーメッセージが出ていないことを確認
このプログラムは,ユーザーからのメッセージを受け取り,それを 日本語言語モデルJapanese StableLM Base Alpha 7B に与え,画面に表示する. 履歴は保存され,画面で過去の履歴も閲覧可能である.
GUIの構築には,Tkinterライブラリを用いている. 履歴の表示にはリストボックスとスクロールバーを用い,ユーザーからのメッセージ入力はテキストフィールドで受け付け,そのメッセージの送信のために「Send」ボタンを配置している.
cd %HOMEPATH% notepad stablelm.py
Japanese StableLM Base Alpha 7B の HuggingFace の公式ページ: https://huggingface.co/stabilityai/japanese-stablelm-base-alpha-7bのプログラムに機能を加えたものである.
import torch from transformers import LlamaTokenizer, AutoModelForCausalLM import tkinter as tk import textwrap WIDTH = 50 tokenizer = LlamaTokenizer.from_pretrained("novelai/nerdstash-tokenizer-v1") model = AutoModelForCausalLM.from_pretrained( "stabilityai/japanese-stablelm-base-alpha-7b", trust_remote_code=True, ) model.half() if torch.cuda.is_available(): model = model.to("cuda") def do_chat(user_message): input_ids = tokenizer.encode( user_message.strip(), add_special_tokens=False, return_tensors="pt" ) # this is for reproducibility. # feel free to change to get different result seed = 23 torch.manual_seed(seed) tokens = model.generate( input_ids.to(device=model.device), max_new_tokens=128, temperature=1, top_p=0.95, do_sample=True, ) out = tokenizer.decode(tokens[0], skip_special_tokens=False) return out def send_message(): user_message = entry_field.get("1.0", tk.END) for part in user_message.split('\n'): wrapped_message = textwrap.wrap(part, width=50) for line in wrapped_message: chat_history.insert(tk.END, "Q: " + line) entry_field.delete("1.0", tk.END) ai_message = "A: " + do_chat(user_message) for part in ai_message.split('\n'): wrapped_ai_message = textwrap.wrap(part, width=50) for line in wrapped_ai_message: chat_history.insert(tk.END, line) root = tk.Tk() root.title("Chat with AI") frame = tk.Frame(root) scroll = tk.Scrollbar(frame) chat_history = tk.Listbox(frame, yscrollcommand=scroll.set, width=2 * WIDTH) scroll.pack(side=tk.RIGHT, fill=tk.Y) chat_history.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) frame.pack() entry_field = tk.Text(root, width=2 * WIDTH, height=5) entry_field.pack() send_button = tk.Button(root, text="Send", command=send_message) send_button.pack() root.mainloop()
Python プログラムの実行
Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.
Python のまとめ: 別ページ »にまとめ
プログラムを stablelm.pyのようなファイル名で保存したので, 「python stablelm.py」のようなコマンドで行う.
python stablelm.py
画面下部の枠にメッセージを入力し,「Send」ボタンをクリックすると,画面上部で日本語言語モデルからの回答を確認できる.画面上部は履歴になっており,スクロールバーでスクロールできる.
上のプログラムは, stabilityai/japanese-stablelm-base-alpha-7b を使うようにしています.
対話のための学習が行われている stabilityai/japanese-stablelm-instruct-alpha-7b の方を使う場合には,利用者自身でライセンスを注意深く確認することをお薦めします.あわせて HuggingFace のログイン等を行う必要があるでしょう.