Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,4 @@
|
|
| 1 |
-
|
| 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,7 +25,7 @@ print("モデルの読み込みが完了しました。")
|
|
| 44 |
def respond(message, history):
|
| 45 |
"""
|
| 46 |
チャットボットの応答を生成する関数
|
| 47 |
-
|
| 48 |
"""
|
| 49 |
try:
|
| 50 |
# システムメッセージとして使用するプロンプト
|
|
@@ -55,22 +36,12 @@ def respond(message, history):
|
|
| 55 |
if system_message.strip():
|
| 56 |
conversation += f"システム: {system_message}\n"
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 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アシスタント: "
|
|
@@ -117,11 +88,10 @@ def respond(message, history):
|
|
| 117 |
|
| 118 |
"""
|
| 119 |
Gradio ChatInterfaceを使用したシンプルなチャットボット
|
| 120 |
-
|
| 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(),
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import warnings
|
|
|
|
| 25 |
def respond(message, history):
|
| 26 |
"""
|
| 27 |
チャットボットの応答を生成する関数
|
| 28 |
+
historyはタプルのリスト形式: [(user_msg, bot_msg), ...]
|
| 29 |
"""
|
| 30 |
try:
|
| 31 |
# システムメッセージとして使用するプロンプト
|
|
|
|
| 36 |
if system_message.strip():
|
| 37 |
conversation += f"システム: {system_message}\n"
|
| 38 |
|
| 39 |
+
# 会話履歴を追加(タプル形式)
|
| 40 |
+
for user_msg, bot_msg in history:
|
| 41 |
+
if user_msg:
|
| 42 |
+
conversation += f"ユーザー: {user_msg}\n"
|
| 43 |
+
if bot_msg:
|
| 44 |
+
conversation += f"アシスタント: {bot_msg}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# 現在のメッセージを追加
|
| 47 |
conversation += f"ユーザー: {message}\nアシスタント: "
|
|
|
|
| 88 |
|
| 89 |
"""
|
| 90 |
Gradio ChatInterfaceを使用したシンプルなチャットボット
|
| 91 |
+
デフォルトのタプル形式を使用
|
| 92 |
"""
|
| 93 |
demo = gr.ChatInterface(
|
| 94 |
respond,
|
|
|
|
| 95 |
title="🤖 Sarashina Chatbot",
|
| 96 |
description="Sarashina2.2-3b-instruct モデルを使用した日本語チャットボットです。",
|
| 97 |
theme=gr.themes.Soft(),
|