youngtsai commited on
Commit
922302c
·
verified ·
1 Parent(s): b181909

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -83,32 +83,45 @@ def log_to_notion(user_name, user_message, bot_response):
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:
99
  gr.Markdown("# 簡單的Gradio聊天機器人")
100
 
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)
 
83
  print(f"Error logging to Notion: {e}")
84
 
85
  # 定義回應邏輯
86
+ def respond(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(name, message, response)
94
 
95
+ return chat_history, ""
96
+
97
+ # 定義用於存儲用戶名字的狀態
98
+ def set_name(name):
99
+ return name
100
 
101
  # 建立 Gradio 界面
102
  with gr.Blocks(title="簡單的Gradio聊天機器人") as demo:
103
  gr.Markdown("# 簡單的Gradio聊天機器人")
104
 
105
+ # 用戶名稱輸入框
106
+ with gr.Row():
107
+ name_input = gr.Textbox(placeholder="輸入你的名字(僅需填寫一次)", label="你的名字", interactive=True)
108
+ name_button = gr.Button("確認名字")
109
 
110
+ # 聊天區
111
+ chatbot_interface = gr.Chatbot(type="messages")
112
  with gr.Row():
 
113
  user_input = gr.Textbox(placeholder="輸入訊息...", label="你的訊息")
114
  send_button = gr.Button("發送")
115
 
116
+ # 狀態保持,用於存儲用戶名稱
117
+ user_name = gr.State()
118
+
119
+ # 事件綁定
120
+ name_button.click(set_name, inputs=[name_input], outputs=[user_name])
121
  send_button.click(
122
  respond,
123
  inputs=[user_name, user_input, chatbot_interface],
124
+ outputs=[chatbot_interface, user_input]
125
  )
126
 
127
  demo.launch(share=True)