Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# モデルとトークナイザーの読み込み
|
| 5 |
+
model_name = "rinna/nekomata-7b"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False) # Slow tokenizerを使用
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# 生成用の関数
|
| 10 |
+
def respond(input_text, system_message, max_tokens, temperature, top_p):
|
| 11 |
+
# システムメッセージとユーザー入力を結合
|
| 12 |
+
input_text_combined = f"システム: {system_message}\nユーザー: {input_text}\n"
|
| 13 |
+
|
| 14 |
+
# トークン化
|
| 15 |
+
inputs = tokenizer(input_text_combined, return_tensors="pt")
|
| 16 |
+
|
| 17 |
+
# モデルに入力を渡して生成
|
| 18 |
+
outputs = model.generate(
|
| 19 |
+
**inputs,
|
| 20 |
+
max_length=max_tokens, # 最大トークン数
|
| 21 |
+
top_p=top_p, # nucleus sampling のパラメータ
|
| 22 |
+
do_sample=True, # サンプリングを有効にする
|
| 23 |
+
temperature=temperature, # 生成の温度
|
| 24 |
+
pad_token_id=tokenizer.eos_token_id
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# トークンをテキストにデコード
|
| 28 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
+
|
| 30 |
+
# レスポンスを返す
|
| 31 |
+
return response
|
| 32 |
+
|
| 33 |
+
# Gradioインターフェースの作成
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("## nekomataチャットボット")
|
| 36 |
+
|
| 37 |
+
# 追加の入力フィールドをリストで設定
|
| 38 |
+
additional_inputs = [
|
| 39 |
+
gr.Textbox(value="ユーザーの質問と依頼のみに答えてください。ポジティブに.", label="システムメッセージ"),
|
| 40 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="新規トークン最大"),
|
| 41 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="温度"),
|
| 42 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (核サンプリング)")
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
# ユーザーのメイン入力
|
| 46 |
+
input_text = gr.Textbox(label="ユーザー入力", placeholder="質問やテキストを入力してください")
|
| 47 |
+
|
| 48 |
+
# 出力エリア("respond"という名前に変更)
|
| 49 |
+
output_text = gr.Textbox(label="respond")
|
| 50 |
+
|
| 51 |
+
# ボタンとアクション
|
| 52 |
+
submit_button = gr.Button("送信")
|
| 53 |
+
submit_button.click(respond, inputs=[input_text] + additional_inputs, outputs=output_text)
|
| 54 |
+
|
| 55 |
+
# インターフェースの起動
|
| 56 |
+
demo.launch()
|