EduTechTeam commited on
Commit
c847717
·
verified ·
1 Parent(s): 531ea6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -3,20 +3,24 @@ import gradio as gr
3
  import os
4
  from dotenv import load_dotenv
5
 
6
- # 載入 .env 環境變數(如果你有放 .env 檔案)
7
  load_dotenv()
8
-
9
- # 設定 API Key
10
  openai.api_key = os.getenv("OPENAI_API_KEY")
11
  if openai.api_key is None:
12
  raise ValueError("請設定環境變數 OPENAI_API_KEY,確保 API Key 可用")
13
 
14
- # 建立男友聊天機器人函數,加入 log 訊息
15
  def boyfriend_chatbot(user_input, history):
16
  history = history or []
17
  log_messages = []
18
-
19
- # 系統角色說明
 
 
 
 
 
 
20
  messages = [{"role": "system", "content": "你是一個溫柔、貼心的男友,總是關心對方的感受,給予支持和鼓勵,用輕鬆、暖心的語氣回應。"}]
21
 
22
  for user_msg, bot_msg in history:
@@ -32,27 +36,25 @@ def boyfriend_chatbot(user_input, history):
32
  )
33
  reply = response.choices[0].message.content
34
  history.append((user_input, reply))
35
- log = f"[✅ Success] 回應完成,共 {len(messages)} 則對話。"
36
  except Exception as e:
37
- reply = "抱歉,出現了一些錯誤 😢"
38
- log = f"[❌ Error] {str(e)}"
39
 
40
  return reply, history, log
41
 
42
-
43
- # 使用 Gradio Blocks 來加上 log 顯示
44
  with gr.Blocks(title="貼心男友聊天機器人") as demo:
45
  gr.Markdown("# 💕 貼心男友聊天機器人 💕")
46
  chatbot = gr.Chatbot()
47
- msg = gr.Textbox(label="輸入你的訊息")
48
  log_output = gr.Textbox(label="狀態日誌", interactive=False)
49
- clear = gr.Button("清除對話")
50
 
51
- def respond(user_input, history):
52
- return boyfriend_chatbot(user_input, history)
53
 
54
  msg.submit(respond, inputs=[msg, chatbot], outputs=[chatbot, chatbot, log_output])
55
  clear.click(lambda: ([], "", ""), outputs=[chatbot, msg, log_output])
56
 
57
- # 啟動介面
58
  demo.launch(share=True)
 
3
  import os
4
  from dotenv import load_dotenv
5
 
6
+ # 載入環境變數
7
  load_dotenv()
 
 
8
  openai.api_key = os.getenv("OPENAI_API_KEY")
9
  if openai.api_key is None:
10
  raise ValueError("請設定環境變數 OPENAI_API_KEY,確保 API Key 可用")
11
 
12
+ # 聊天機器人主邏輯
13
  def boyfriend_chatbot(user_input, history):
14
  history = history or []
15
  log_messages = []
16
+
17
+ # 確保 history 是 list of 2-tuples
18
+ if not all(isinstance(pair, (list, tuple)) and len(pair) == 2 for pair in history):
19
+ log = "[❌ Error] 歷史紀錄格式錯誤,已重置"
20
+ history = []
21
+ else:
22
+ log = "[✅ 收到訊息]"
23
+
24
  messages = [{"role": "system", "content": "你是一個溫柔、貼心的男友,總是關心對方的感受,給予支持和鼓勵,用輕鬆、暖心的語氣回應。"}]
25
 
26
  for user_msg, bot_msg in history:
 
36
  )
37
  reply = response.choices[0].message.content
38
  history.append((user_input, reply))
39
+ log += f"\n[✅ Success] 回應完成,共 {len(messages)} 則訊息。"
40
  except Exception as e:
41
+ reply = "抱歉,發生錯誤,我先抱抱你 😢"
42
+ log += f"\n[❌ API Error] {str(e)}"
43
 
44
  return reply, history, log
45
 
46
+ # Gradio UI
 
47
  with gr.Blocks(title="貼心男友聊天機器人") as demo:
48
  gr.Markdown("# 💕 貼心男友聊天機器人 💕")
49
  chatbot = gr.Chatbot()
50
+ msg = gr.Textbox(label="輸入你的訊息", placeholder="說點什麼吧...")
51
  log_output = gr.Textbox(label="狀態日誌", interactive=False)
52
+ clear = gr.Button("🧹 清除對話")
53
 
54
+ def respond(user_input, chat_history):
55
+ return boyfriend_chatbot(user_input, chat_history)
56
 
57
  msg.submit(respond, inputs=[msg, chatbot], outputs=[chatbot, chatbot, log_output])
58
  clear.click(lambda: ([], "", ""), outputs=[chatbot, msg, log_output])
59
 
 
60
  demo.launch(share=True)