はじめに
「plamo-2-translate」はPreferred Networksによって開発された翻訳特化の大規模言語モデルです。以前GradioとvLLMを使った記事を書きました。touch-sp.hatenablog.com
今回はvLLMに代わってOllamaを使いました。
実行画面

Pythonスクリプト
できるだけシンプルに書いてみました。ollama.chat() を使えば、デフォルトで http://localhost:11434 に接続しますimport gradio as gr from ollama import chat def translate_text(text, direction): """テキスト翻訳(双方向対応)""" if not text.strip(): return "" # 翻訳方向に応じてシステムプロンプトとユーザープロンプトを設定 if direction == "英語→日本語": system_content = "Japanese" else: # "日本語→英語" system_content = "English" prompt = [ { "role": "system", "content": system_content, }, { "role": "user", "content": text, } ] # Ollamaモデルにリクエストを送信 stream = chat( model="mitmul/plamo-2-translate:latest", messages=prompt, stream=True ) result = "" for new_text in stream: result += new_text.message.content yield result # Gradioインターフェースの構築 demo = gr.Interface( fn=translate_text, inputs=[ gr.Textbox(lines=10, max_lines=40, label="翻訳元テキスト"), gr.Radio( choices=["英語→日本語", "日本語→英語"], value="英語→日本語", label="翻訳方向" ) ], outputs=gr.Textbox( lines=10, max_lines=40, # v6では `show_copy_button` が削除されたので、代わりに `buttons` を指定 buttons=["copy"], label="翻訳結果" ), title="plamo-2-translate 翻訳アプリ(Ollama版)", description="テキストを入力し、翻訳方向を選択してください。英語⇔日本語の双方向翻訳が可能です。" ) # アプリケーションの起動 if __name__ == "__main__": demo.launch(inbrowser=True)
環境構築
Ollamaバージョン
ollama version is 0.13.5
Python環境構築
pyproject.tomlを載せておきます。 (バージョンはあえて固定しています)uvを使うとuv syncだけで環境構築できると思います。
[project] name = "translate" version = "0.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.13" dependencies = [ "gradio==6.2.0", "ollama==0.6.1", ]