Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,43 @@
|
|
| 1 |
import openai
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
-
|
|
|
|
| 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:
|
| 27 |
messages.append({"role": "user", "content": user_msg})
|
| 28 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 29 |
-
|
| 30 |
-
messages.append({"role": "user", "content": user_input})
|
| 31 |
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
-
|
| 34 |
-
model="gpt-4o",
|
| 35 |
-
messages=messages
|
|
|
|
| 36 |
)
|
| 37 |
-
|
| 38 |
-
history.append((user_input, reply))
|
| 39 |
-
log += f"\n[✅ Success] 回應完成,共 {len(messages)} 則訊息。"
|
| 40 |
except Exception as e:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 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)
|
|
|
|
| 1 |
import openai
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
+
from google.colab import userdata
|
| 6 |
|
| 7 |
+
client = openai.OpenAI(api_key=userdata.get('OPENAI_API_KEY'))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# 建立「男友」聊天機器人函數
|
| 10 |
def boyfriend_chatbot(user_input, history):
|
| 11 |
history = history or []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
messages = [{"role": "system", "content": "你是一個溫柔、貼心的男友,總是關心對方的感受,給予支持和鼓勵,用輕鬆、暖心的語氣回應。"}]
|
| 13 |
+
|
| 14 |
for user_msg, bot_msg in history:
|
| 15 |
messages.append({"role": "user", "content": user_msg})
|
| 16 |
messages.append({"role": "assistant", "content": bot_msg})
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
messages.append({"role": "user", "content": user_input})
|
| 19 |
+
|
| 20 |
try:
|
| 21 |
+
completion = client.chat.completions.create(
|
| 22 |
+
model="gpt-4o-mini",
|
| 23 |
+
messages=messages,
|
| 24 |
+
temperature=0.7
|
| 25 |
)
|
| 26 |
+
response = completion.choices[0].message.content
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
+
response = f"❌ 發生錯誤:{str(e)}"
|
| 29 |
+
|
| 30 |
+
for i in range(len(response)):
|
| 31 |
+
yield response[: i + 1]
|
| 32 |
+
time.sleep(0.05)
|
| 33 |
+
|
| 34 |
+
# 建立 Gradio 介面
|
| 35 |
+
chat_interface = gr.ChatInterface(
|
| 36 |
+
boyfriend_chatbot,
|
| 37 |
+
chatbot=gr.Chatbot(type="messages"),
|
| 38 |
+
type="messages",
|
| 39 |
+
title="💕 貼心男友聊天機器人 💕"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# 啟動介面
|
| 43 |
+
chat_interface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|