Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,48 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
try:
|
| 3 |
from groq import Groq
|
|
|
|
| 4 |
except ImportError:
|
| 5 |
-
os.system('pip install groq')
|
| 6 |
from groq import Groq
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
-
# 設定 API
|
| 10 |
groq_key = os.getenv("groq_key")
|
|
|
|
|
|
|
|
|
|
| 11 |
if not groq_key:
|
| 12 |
-
raise ValueError("請設定環境變數 'groq_key'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
def chat_with_groq(history):
|
| 18 |
"""
|
| 19 |
-
與 Groq 聊天機器人互動,
|
| 20 |
-
:param history: List of tuples (user_input, bot_response)
|
| 21 |
-
:return: Updated history with new bot response
|
| 22 |
"""
|
| 23 |
-
# 提取最新的用戶輸入
|
| 24 |
user_message = history[-1][0] if history else "你好!"
|
| 25 |
|
| 26 |
-
# 初始化對話,帶有 role: system 的設定
|
| 27 |
messages = [
|
| 28 |
{
|
| 29 |
"role": "system",
|
|
@@ -31,37 +50,44 @@ def chat_with_groq(history):
|
|
| 31 |
}
|
| 32 |
]
|
| 33 |
|
| 34 |
-
|
| 35 |
-
for user_msg, bot_msg in history[:-1]: # 忽略最後一條,因為它是新的輸入
|
| 36 |
messages.append({"role": "user", "content": user_msg})
|
| 37 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 38 |
|
| 39 |
-
# 添加最新的用戶訊息
|
| 40 |
messages.append({"role": "user", "content": user_message})
|
| 41 |
|
| 42 |
-
# 呼叫 Groq API 生成回應
|
| 43 |
try:
|
| 44 |
-
completion =
|
| 45 |
model="llama3-8b-8192",
|
| 46 |
messages=messages,
|
| 47 |
temperature=1,
|
| 48 |
max_tokens=1024,
|
| 49 |
top_p=1,
|
| 50 |
-
stream=False,
|
| 51 |
stop=None,
|
| 52 |
)
|
| 53 |
-
# 提取 AI 回應
|
| 54 |
response = completion.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
response = f"抱歉,發生了一個錯誤:{str(e)}"
|
| 57 |
|
| 58 |
-
# 將新回應加入歷史記錄
|
| 59 |
history[-1][1] = response
|
| 60 |
return history
|
| 61 |
|
| 62 |
# 建立 Gradio 介面
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
gr.Markdown("## 🎨 AI 美術老師 Chatbot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
chatbot = gr.Chatbot()
|
| 66 |
with gr.Row():
|
| 67 |
msg = gr.Textbox(
|
|
@@ -70,17 +96,32 @@ with gr.Blocks() as demo:
|
|
| 70 |
)
|
| 71 |
clear = gr.Button("清除對話")
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
chat_history = chat_history or []
|
| 76 |
-
chat_history.append((user_message, None))
|
| 77 |
-
return "", chat_history
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# 啟動應用程式
|
| 85 |
if __name__ == "__main__":
|
| 86 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
from datetime import datetime
|
| 3 |
try:
|
| 4 |
from groq import Groq
|
| 5 |
+
from notion_client import Client
|
| 6 |
except ImportError:
|
| 7 |
+
os.system('pip install groq notion-client')
|
| 8 |
from groq import Groq
|
| 9 |
+
from notion_client import Client
|
| 10 |
import gradio as gr
|
| 11 |
|
| 12 |
+
# 設定 API keys
|
| 13 |
groq_key = os.getenv("groq_key")
|
| 14 |
+
notion_key = os.getenv("NOTION_API_KEY")
|
| 15 |
+
notion_db_id = os.getenv("NOTION_DB_ID")
|
| 16 |
+
|
| 17 |
if not groq_key:
|
| 18 |
+
raise ValueError("請設定環境變數 'groq_key'")
|
| 19 |
+
if not notion_key or not notion_db_id:
|
| 20 |
+
raise ValueError("請設定 NOTION_API_KEY 和 NOTION_DB_ID")
|
| 21 |
+
|
| 22 |
+
groq_client = Groq(api_key=groq_key)
|
| 23 |
+
notion_client = Client(auth=notion_key)
|
| 24 |
|
| 25 |
+
def log_to_notion(name, user_input, bot_response):
|
| 26 |
+
"""記錄對話到 Notion database"""
|
| 27 |
+
try:
|
| 28 |
+
notion_client.pages.create(
|
| 29 |
+
parent={"database_id": notion_db_id},
|
| 30 |
+
properties={
|
| 31 |
+
"Name": {"title": [{"text": {"content": name}}]},
|
| 32 |
+
"Timestamp": {"date": {"start": datetime.utcnow().isoformat()}},
|
| 33 |
+
"User Input": {"rich_text": [{"text": {"content": user_input}}]},
|
| 34 |
+
"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]}
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Notion logging error: {str(e)}")
|
| 39 |
|
| 40 |
+
def chat_with_groq(history, name):
|
|
|
|
| 41 |
"""
|
| 42 |
+
與 Groq 聊天機器人互動,並記錄到 Notion
|
|
|
|
|
|
|
| 43 |
"""
|
|
|
|
| 44 |
user_message = history[-1][0] if history else "你好!"
|
| 45 |
|
|
|
|
| 46 |
messages = [
|
| 47 |
{
|
| 48 |
"role": "system",
|
|
|
|
| 50 |
}
|
| 51 |
]
|
| 52 |
|
| 53 |
+
for user_msg, bot_msg in history[:-1]:
|
|
|
|
| 54 |
messages.append({"role": "user", "content": user_msg})
|
| 55 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 56 |
|
|
|
|
| 57 |
messages.append({"role": "user", "content": user_message})
|
| 58 |
|
|
|
|
| 59 |
try:
|
| 60 |
+
completion = groq_client.chat.completions.create(
|
| 61 |
model="llama3-8b-8192",
|
| 62 |
messages=messages,
|
| 63 |
temperature=1,
|
| 64 |
max_tokens=1024,
|
| 65 |
top_p=1,
|
| 66 |
+
stream=False,
|
| 67 |
stop=None,
|
| 68 |
)
|
|
|
|
| 69 |
response = completion.choices[0].message.content.strip()
|
| 70 |
+
|
| 71 |
+
# 記錄到 Notion
|
| 72 |
+
log_to_notion(name, user_message, response)
|
| 73 |
+
|
| 74 |
except Exception as e:
|
| 75 |
response = f"抱歉,發生了一個錯誤:{str(e)}"
|
| 76 |
|
|
|
|
| 77 |
history[-1][1] = response
|
| 78 |
return history
|
| 79 |
|
| 80 |
# 建立 Gradio 介面
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
gr.Markdown("## 🎨 AI 美術老師 Chatbot")
|
| 83 |
+
|
| 84 |
+
# 新增使用者名稱輸入
|
| 85 |
+
name_input = gr.Textbox(
|
| 86 |
+
label="您的名字",
|
| 87 |
+
placeholder="請輸入您的名字...",
|
| 88 |
+
value=""
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
chatbot = gr.Chatbot()
|
| 92 |
with gr.Row():
|
| 93 |
msg = gr.Textbox(
|
|
|
|
| 96 |
)
|
| 97 |
clear = gr.Button("清除對話")
|
| 98 |
|
| 99 |
+
def user_input(user_message, chat_history, name):
|
| 100 |
+
if not name.strip():
|
| 101 |
+
return user_message, chat_history, "請先輸入您的名字"
|
| 102 |
chat_history = chat_history or []
|
| 103 |
+
chat_history.append((user_message, None))
|
| 104 |
+
return "", chat_history, ""
|
| 105 |
|
| 106 |
+
# 新增錯誤訊息顯示
|
| 107 |
+
error_message = gr.Textbox(label="提示訊息", interactive=False)
|
| 108 |
+
|
| 109 |
+
msg.submit(
|
| 110 |
+
user_input,
|
| 111 |
+
[msg, chatbot, name_input],
|
| 112 |
+
[msg, chatbot, error_message],
|
| 113 |
+
queue=False
|
| 114 |
+
).then(
|
| 115 |
+
chat_with_groq,
|
| 116 |
+
[chatbot, name_input],
|
| 117 |
+
chatbot
|
| 118 |
)
|
| 119 |
+
|
| 120 |
+
def clear_chat():
|
| 121 |
+
return None, ""
|
| 122 |
+
|
| 123 |
+
clear.click(clear_chat, None, [chatbot, error_message], queue=False)
|
| 124 |
|
| 125 |
# 啟動應用程式
|
| 126 |
if __name__ == "__main__":
|
| 127 |
+
demo.launch()
|