Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,23 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
import warnings
|
@@ -25,7 +44,7 @@ print("モデルの読み込みが完了しました。")
|
|
25 |
def respond(message, history):
|
26 |
"""
|
27 |
チャットボットの応答を生成する関数
|
28 |
-
|
29 |
"""
|
30 |
try:
|
31 |
# システムメッセージとして使用するプロンプト
|
@@ -36,12 +55,22 @@ def respond(message, history):
|
|
36 |
if system_message.strip():
|
37 |
conversation += f"システム: {system_message}\n"
|
38 |
|
39 |
-
# 会話履歴を追加(
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# 現在のメッセージを追加
|
47 |
conversation += f"ユーザー: {message}\nアシスタント: "
|
@@ -92,7 +121,7 @@ type="messages"を設定してOpenAI形式のメッセージを使用
|
|
92 |
"""
|
93 |
demo = gr.ChatInterface(
|
94 |
respond,
|
95 |
-
type="messages"
|
96 |
title="🤖 Sarashina Chatbot",
|
97 |
description="Sarashina2.2-3b-instruct モデルを使用した日本語チャットボットです。",
|
98 |
theme=gr.themes.Soft(),
|
|
|
1 |
+
def respond(message, history):
|
2 |
+
"""
|
3 |
+
チャットボットの応答を生成する関数
|
4 |
+
historyはタプルのリスト形式: [(user_msg, bot_msg), ...]
|
5 |
+
"""
|
6 |
+
try:
|
7 |
+
# システムメッセージとして使用するプロンプト
|
8 |
+
system_message = "あなたは親切で知識豊富な日本語アシスタントです。ユーザーの質問に丁寧に答えてください。"
|
9 |
+
|
10 |
+
# 会話履歴を文字列形式に変換
|
11 |
+
conversation = ""
|
12 |
+
if system_message.strip():
|
13 |
+
conversation += f"システム: {system_message}\n"
|
14 |
+
|
15 |
+
# 会話履歴を追加(タプル形式)
|
16 |
+
for user_msg, bot_msg in history:
|
17 |
+
if user_msg:
|
18 |
+
conversation += f"ユーザー: {user_msg}\n"
|
19 |
+
if bot_msg:
|
20 |
+
conversation += f"アシスタント: {bot_msg}\n"import gradio as gr
|
21 |
import torch
|
22 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
23 |
import warnings
|
|
|
44 |
def respond(message, history):
|
45 |
"""
|
46 |
チャットボットの応答を生成する関数
|
47 |
+
ChatInterfaceではtype="messages"でもhistoryの形式が異なる場合があります
|
48 |
"""
|
49 |
try:
|
50 |
# システムメッセージとして使用するプロンプト
|
|
|
55 |
if system_message.strip():
|
56 |
conversation += f"システム: {system_message}\n"
|
57 |
|
58 |
+
# 会話履歴を追加(historyの形式を確認して処理)
|
59 |
+
if history:
|
60 |
+
for item in history:
|
61 |
+
if isinstance(item, dict):
|
62 |
+
# messages形式の場合
|
63 |
+
if item.get("role") == "user":
|
64 |
+
conversation += f"ユーザー: {item.get('content', '')}\n"
|
65 |
+
elif item.get("role") == "assistant":
|
66 |
+
conversation += f"アシスタント: {item.get('content', '')}\n"
|
67 |
+
elif isinstance(item, (list, tuple)) and len(item) >= 2:
|
68 |
+
# タプル形式の場合 (user_msg, bot_msg)
|
69 |
+
user_msg, bot_msg = item[0], item[1]
|
70 |
+
if user_msg:
|
71 |
+
conversation += f"ユーザー: {user_msg}\n"
|
72 |
+
if bot_msg:
|
73 |
+
conversation += f"アシスタント: {bot_msg}\n"
|
74 |
|
75 |
# 現在のメッセージを追加
|
76 |
conversation += f"ユーザー: {message}\nアシスタント: "
|
|
|
121 |
"""
|
122 |
demo = gr.ChatInterface(
|
123 |
respond,
|
124 |
+
# type="messages"を削除 - デフォルトのタプル形式を使用
|
125 |
title="🤖 Sarashina Chatbot",
|
126 |
description="Sarashina2.2-3b-instruct モデルを使用した日本語チャットボットです。",
|
127 |
theme=gr.themes.Soft(),
|