Update app.py
Browse files
app.py
CHANGED
|
@@ -32,7 +32,6 @@ def respond(
|
|
| 32 |
):
|
| 33 |
"""
|
| 34 |
チャットボットの応答を生成する関数
|
| 35 |
-
Gradio ChatInterfaceの標準形式に対応
|
| 36 |
"""
|
| 37 |
try:
|
| 38 |
# システムメッセージと会話履歴を含むプロンプトを構築
|
|
@@ -57,10 +56,8 @@ def respond(
|
|
| 57 |
if torch.cuda.is_available():
|
| 58 |
inputs = inputs.cuda()
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
response = ""
|
| 62 |
with torch.no_grad():
|
| 63 |
-
# 一度に生成してからストリーミング風に出力
|
| 64 |
outputs = model.generate(
|
| 65 |
inputs,
|
| 66 |
max_new_tokens=max_tokens,
|
|
@@ -82,65 +79,97 @@ def respond(
|
|
| 82 |
if "ユーザー:" in full_response:
|
| 83 |
full_response = full_response.split("ユーザー:")[0].strip()
|
| 84 |
|
| 85 |
-
|
| 86 |
-
for i in range(len(full_response)):
|
| 87 |
-
response = full_response[:i+1]
|
| 88 |
-
yield response
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
-
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
カスタマイズ可能なパラメータを含む
|
| 96 |
-
"""
|
| 97 |
-
demo = gr.ChatInterface(
|
| 98 |
-
respond,
|
| 99 |
title="🤖 Sarashina Chatbot",
|
| 100 |
-
description="Sarashina2.2-3b-instruct モデルを使用した日本語チャットボットです。",
|
| 101 |
-
additional_inputs=[
|
| 102 |
-
gr.Textbox(
|
| 103 |
-
value="あなたは親切で知識豊富な日本語アシスタントです。ユーザーの質問に丁寧に答えてください。",
|
| 104 |
-
label="システムメッセージ",
|
| 105 |
-
lines=3
|
| 106 |
-
),
|
| 107 |
-
gr.Slider(
|
| 108 |
-
minimum=1,
|
| 109 |
-
maximum=1024,
|
| 110 |
-
value=512,
|
| 111 |
-
step=1,
|
| 112 |
-
label="最大新規トークン数"
|
| 113 |
-
),
|
| 114 |
-
gr.Slider(
|
| 115 |
-
minimum=0.1,
|
| 116 |
-
maximum=2.0,
|
| 117 |
-
value=0.7,
|
| 118 |
-
step=0.1,
|
| 119 |
-
label="Temperature (創造性)"
|
| 120 |
-
),
|
| 121 |
-
gr.Slider(
|
| 122 |
-
minimum=0.1,
|
| 123 |
-
maximum=1.0,
|
| 124 |
-
value=0.95,
|
| 125 |
-
step=0.05,
|
| 126 |
-
label="Top-p (多様性制御)",
|
| 127 |
-
),
|
| 128 |
-
],
|
| 129 |
theme=gr.themes.Soft(),
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
demo.launch(
|
| 141 |
server_name="0.0.0.0",
|
| 142 |
server_port=7860,
|
| 143 |
-
share=
|
| 144 |
-
show_api=True,
|
| 145 |
debug=True
|
| 146 |
)
|
|
|
|
| 32 |
):
|
| 33 |
"""
|
| 34 |
チャットボットの応答を生成する関数
|
|
|
|
| 35 |
"""
|
| 36 |
try:
|
| 37 |
# システムメッセージと会話履歴を含むプロンプトを構築
|
|
|
|
| 56 |
if torch.cuda.is_available():
|
| 57 |
inputs = inputs.cuda()
|
| 58 |
|
| 59 |
+
# 応答生成
|
|
|
|
| 60 |
with torch.no_grad():
|
|
|
|
| 61 |
outputs = model.generate(
|
| 62 |
inputs,
|
| 63 |
max_new_tokens=max_tokens,
|
|
|
|
| 79 |
if "ユーザー:" in full_response:
|
| 80 |
full_response = full_response.split("ユーザー:")[0].strip()
|
| 81 |
|
| 82 |
+
return full_response
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
+
return f"エラーが発生しました: {str(e)}"
|
| 86 |
|
| 87 |
+
# Gradio Blocksを使用したカスタムチャットインターフェース
|
| 88 |
+
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
title="🤖 Sarashina Chatbot",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
theme=gr.themes.Soft(),
|
| 91 |
+
description="Sarashina2.2-3b-instruct モデルを使用した日本語チャットボットです。"
|
| 92 |
+
) as demo:
|
| 93 |
+
|
| 94 |
+
gr.Markdown("# 🤖 Sarashina Chatbot")
|
| 95 |
+
gr.Markdown("Sarashina2.2-3b-instruct モデルを使用した日本語チャットボットです。")
|
| 96 |
+
|
| 97 |
+
with gr.Row():
|
| 98 |
+
with gr.Column(scale=3):
|
| 99 |
+
chatbot = gr.Chatbot(height=500)
|
| 100 |
+
msg = gr.Textbox(
|
| 101 |
+
label="メッセージを入力してください",
|
| 102 |
+
placeholder="こんにちは!何かお手伝いできることはありますか?",
|
| 103 |
+
lines=2
|
| 104 |
+
)
|
| 105 |
+
clear = gr.Button("会話をクリア")
|
| 106 |
+
|
| 107 |
+
with gr.Column(scale=1):
|
| 108 |
+
gr.Markdown("### 設定")
|
| 109 |
+
system_message = gr.Textbox(
|
| 110 |
+
value="あなたは親切で知識豊富な日本語アシスタントです。ユーザーの質問に丁寧に答えてください。",
|
| 111 |
+
label="システムメッセージ",
|
| 112 |
+
lines=3
|
| 113 |
+
)
|
| 114 |
+
max_tokens = gr.Slider(
|
| 115 |
+
minimum=1,
|
| 116 |
+
maximum=1024,
|
| 117 |
+
value=512,
|
| 118 |
+
step=1,
|
| 119 |
+
label="最大新規トークン数"
|
| 120 |
+
)
|
| 121 |
+
temperature = gr.Slider(
|
| 122 |
+
minimum=0.1,
|
| 123 |
+
maximum=2.0,
|
| 124 |
+
value=0.7,
|
| 125 |
+
step=0.1,
|
| 126 |
+
label="Temperature (創造性)"
|
| 127 |
+
)
|
| 128 |
+
top_p = gr.Slider(
|
| 129 |
+
minimum=0.1,
|
| 130 |
+
maximum=1.0,
|
| 131 |
+
value=0.95,
|
| 132 |
+
step=0.05,
|
| 133 |
+
label="Top-p (多様性制御)"
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# 例文
|
| 137 |
+
gr.Examples(
|
| 138 |
+
examples=[
|
| 139 |
+
["こんにちは!今日はどんなことを話しましょうか?"],
|
| 140 |
+
["日本の文化について教えてください。"],
|
| 141 |
+
["簡単なレシピを教えてもらえますか?"],
|
| 142 |
+
["プログラミングについて質問があります。"],
|
| 143 |
+
],
|
| 144 |
+
inputs=msg,
|
| 145 |
+
label="例文"
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
def user(message, history):
|
| 149 |
+
return "", history + [[message, None]]
|
| 150 |
+
|
| 151 |
+
def bot(history, system_message, max_tokens, temperature, top_p):
|
| 152 |
+
history[-1][1] = respond(
|
| 153 |
+
history[-1][0],
|
| 154 |
+
history[:-1],
|
| 155 |
+
system_message,
|
| 156 |
+
max_tokens,
|
| 157 |
+
temperature,
|
| 158 |
+
top_p
|
| 159 |
+
)
|
| 160 |
+
return history
|
| 161 |
+
|
| 162 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 163 |
+
bot, [chatbot, system_message, max_tokens, temperature, top_p], chatbot
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 167 |
|
| 168 |
if __name__ == "__main__":
|
| 169 |
demo.launch(
|
| 170 |
server_name="0.0.0.0",
|
| 171 |
server_port=7860,
|
| 172 |
+
share=True, # パブリックリンクを作成
|
| 173 |
+
show_api=True,
|
| 174 |
debug=True
|
| 175 |
)
|