Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -66,13 +66,14 @@ class SimpleChatBot:
|
|
66 |
chatbot = SimpleChatBot()
|
67 |
|
68 |
# 記錄聊天內容到 Notion 資料庫
|
69 |
-
def log_to_notion(user_message, bot_response):
|
70 |
try:
|
71 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
72 |
notion.pages.create(
|
73 |
parent={"database_id": NOTION_DB_ID},
|
74 |
properties={
|
75 |
"Timestamp": {"date": {"start": timestamp}},
|
|
|
76 |
"User Input": {"rich_text": [{"text": {"content": user_message}}]},
|
77 |
"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]},
|
78 |
},
|
@@ -82,16 +83,16 @@ def log_to_notion(user_message, bot_response):
|
|
82 |
print(f"Error logging to Notion: {e}")
|
83 |
|
84 |
# 定義回應邏輯
|
85 |
-
def respond(message, chat_history):
|
86 |
chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]
|
87 |
response = chatbot.get_response(message, chat_history)
|
88 |
chat_history.append({"role": "user", "content": message})
|
89 |
chat_history.append({"role": "assistant", "content": response})
|
90 |
|
91 |
# 記錄到 Notion
|
92 |
-
log_to_notion(message, response)
|
93 |
|
94 |
-
return chat_history, ""
|
95 |
|
96 |
# 建立 Gradio 界面
|
97 |
with gr.Blocks(title="簡單的Gradio聊天機器人") as demo:
|
@@ -100,9 +101,14 @@ with gr.Blocks(title="簡單的Gradio聊天機器人") as demo:
|
|
100 |
chatbot_interface = gr.Chatbot(type="messages")
|
101 |
|
102 |
with gr.Row():
|
|
|
103 |
user_input = gr.Textbox(placeholder="輸入訊息...", label="你的訊息")
|
104 |
send_button = gr.Button("發送")
|
105 |
|
106 |
-
send_button.click(
|
|
|
|
|
|
|
|
|
107 |
|
108 |
demo.launch(share=True)
|
|
|
66 |
chatbot = SimpleChatBot()
|
67 |
|
68 |
# 記錄聊天內容到 Notion 資料庫
|
69 |
+
def log_to_notion(user_name, user_message, bot_response):
|
70 |
try:
|
71 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
72 |
notion.pages.create(
|
73 |
parent={"database_id": NOTION_DB_ID},
|
74 |
properties={
|
75 |
"Timestamp": {"date": {"start": timestamp}},
|
76 |
+
"Name": {"title": [{"text": {"content": user_name or "Anonymous"}}]},
|
77 |
"User Input": {"rich_text": [{"text": {"content": user_message}}]},
|
78 |
"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]},
|
79 |
},
|
|
|
83 |
print(f"Error logging to Notion: {e}")
|
84 |
|
85 |
# 定義回應邏輯
|
86 |
+
def respond(user_name, message, chat_history):
|
87 |
chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]
|
88 |
response = chatbot.get_response(message, chat_history)
|
89 |
chat_history.append({"role": "user", "content": message})
|
90 |
chat_history.append({"role": "assistant", "content": response})
|
91 |
|
92 |
# 記錄到 Notion
|
93 |
+
log_to_notion(user_name, message, response)
|
94 |
|
95 |
+
return chat_history, "", ""
|
96 |
|
97 |
# 建立 Gradio 界面
|
98 |
with gr.Blocks(title="簡單的Gradio聊天機器人") as demo:
|
|
|
101 |
chatbot_interface = gr.Chatbot(type="messages")
|
102 |
|
103 |
with gr.Row():
|
104 |
+
user_name = gr.Textbox(placeholder="輸入你的名字(可空白)", label="你的名字")
|
105 |
user_input = gr.Textbox(placeholder="輸入訊息...", label="你的訊息")
|
106 |
send_button = gr.Button("發送")
|
107 |
|
108 |
+
send_button.click(
|
109 |
+
respond,
|
110 |
+
inputs=[user_name, user_input, chatbot_interface],
|
111 |
+
outputs=[chatbot_interface, user_name, user_input]
|
112 |
+
)
|
113 |
|
114 |
demo.launch(share=True)
|