Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
-
from datetime import datetime
|
| 3 |
-
import pytz
|
| 4 |
import gradio as gr
|
| 5 |
-
from notion_client import Client
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
from groq import Groq
|
| 10 |
except ImportError:
|
| 11 |
os.system('pip install groq')
|
| 12 |
from groq import Groq
|
| 13 |
|
| 14 |
-
#
|
| 15 |
client = Groq(api_key=os.getenv('groq_key'))
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"title": [
|
| 29 |
-
{
|
| 30 |
-
"text": {
|
| 31 |
-
"content": name
|
| 32 |
-
}
|
| 33 |
-
}
|
| 34 |
-
]
|
| 35 |
-
},
|
| 36 |
-
"Timestamp": {
|
| 37 |
-
"date": {
|
| 38 |
-
"start": datetime.now(pytz.UTC).isoformat()
|
| 39 |
-
}
|
| 40 |
-
},
|
| 41 |
-
"User Input": {
|
| 42 |
-
"rich_text": [
|
| 43 |
-
{
|
| 44 |
-
"text": {
|
| 45 |
-
"content": user_message
|
| 46 |
-
}
|
| 47 |
-
}
|
| 48 |
-
]
|
| 49 |
-
},
|
| 50 |
-
"Bot Response": {
|
| 51 |
-
"rich_text": [
|
| 52 |
-
{
|
| 53 |
-
"text": {
|
| 54 |
-
"content": bot_response
|
| 55 |
-
}
|
| 56 |
-
}
|
| 57 |
-
]
|
| 58 |
-
}
|
| 59 |
-
}
|
| 60 |
-
)
|
| 61 |
-
except Exception as e:
|
| 62 |
-
print(f"Error logging to Notion: {e}")
|
| 63 |
|
| 64 |
def process_message(message, history):
|
| 65 |
-
|
|
|
|
| 66 |
messages = [
|
| 67 |
{
|
| 68 |
"role": "system",
|
| 69 |
"content": "你是一個高中數學老師,使用的語言是英文。學生用中文問妳任何字彙,你都可以告訴他那個中文對應的英文和例句,以及在數學上的可能用法以及數學例題和解法。\n說明數學上的可能用法時,先用中文講一遍再用B1程度的英文複述一遍\n"
|
| 70 |
}
|
| 71 |
]
|
| 72 |
-
|
| 73 |
-
# Add chat history
|
| 74 |
for user_msg, bot_msg in history:
|
| 75 |
messages.append({"role": "user", "content": user_msg})
|
| 76 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 77 |
-
|
| 78 |
-
# Add the new message
|
| 79 |
messages.append({"role": "user", "content": message})
|
| 80 |
|
| 81 |
-
#
|
| 82 |
completion = client.chat.completions.create(
|
| 83 |
model="llama-3.3-70b-versatile",
|
| 84 |
messages=messages,
|
|
@@ -88,64 +56,37 @@ def process_message(message, history):
|
|
| 88 |
stream=True,
|
| 89 |
stop=None,
|
| 90 |
)
|
| 91 |
-
|
| 92 |
-
# Initialize response text
|
| 93 |
response_text = ""
|
| 94 |
-
|
| 95 |
-
# Stream the response
|
| 96 |
for chunk in completion:
|
| 97 |
delta_content = chunk.choices[0].delta.content
|
| 98 |
if delta_content is not None:
|
| 99 |
response_text += delta_content
|
| 100 |
yield response_text
|
| 101 |
|
| 102 |
-
#
|
| 103 |
with gr.Blocks() as demo:
|
| 104 |
-
name_input = gr.Textbox(
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
chatbot = gr.Chatbot(
|
| 110 |
-
height=600,
|
| 111 |
-
show_label=False,
|
| 112 |
-
container=True,
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
msg = gr.Textbox(
|
| 116 |
-
placeholder="輸入您的問題...",
|
| 117 |
-
show_label=False,
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
clear = gr.Button("清除對話")
|
| 121 |
|
| 122 |
-
def user(user_message, history):
|
| 123 |
return "", history + [[user_message, None]]
|
| 124 |
|
| 125 |
-
def bot(
|
| 126 |
history[-1][1] = ""
|
| 127 |
for response in process_message(history[-1][0], history[:-1]):
|
| 128 |
history[-1][1] = response
|
| 129 |
yield history
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
if history[-1][1]: # Only log if there's a response
|
| 133 |
-
log_to_notion(name, user_message, history[-1][1])
|
| 134 |
|
| 135 |
-
msg.submit(
|
| 136 |
-
|
| 137 |
-
[msg, chatbot],
|
| 138 |
-
[msg, chatbot],
|
| 139 |
-
queue=False
|
| 140 |
-
).then(
|
| 141 |
-
bot,
|
| 142 |
-
[msg, chatbot, name_input],
|
| 143 |
-
chatbot
|
| 144 |
)
|
| 145 |
-
|
| 146 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 147 |
|
| 148 |
-
#
|
| 149 |
if __name__ == "__main__":
|
| 150 |
demo.queue()
|
| 151 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from notion_client import Client # 安裝 notion-client 套件
|
| 4 |
|
| 5 |
+
# 初始化 Notion Client
|
| 6 |
+
notion = Client(auth=os.getenv('NOTION_API_KEY'))
|
| 7 |
+
|
| 8 |
+
# Notion Database ID
|
| 9 |
+
NOTION_DB_ID = os.getenv('NOTION_DB_ID')
|
| 10 |
+
|
| 11 |
+
# 安裝並導入 Groq
|
| 12 |
try:
|
| 13 |
from groq import Groq
|
| 14 |
except ImportError:
|
| 15 |
os.system('pip install groq')
|
| 16 |
from groq import Groq
|
| 17 |
|
| 18 |
+
# 初始化 Groq 用戶端
|
| 19 |
client = Groq(api_key=os.getenv('groq_key'))
|
| 20 |
|
| 21 |
+
def log_to_notion(name, user_input, bot_response):
|
| 22 |
+
"""將對話日誌記錄到 Notion 資料表。"""
|
| 23 |
+
notion.pages.create(
|
| 24 |
+
parent={"database_id": NOTION_DB_ID},
|
| 25 |
+
properties={
|
| 26 |
+
"Name": {"title": [{"text": {"content": name}}]},
|
| 27 |
+
"Timestamp": {"date": {"start": datetime.now().isoformat()}}},
|
| 28 |
+
"User Input": {"rich_text": [{"text": {"content": user_input}}]},
|
| 29 |
+
"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]},
|
| 30 |
+
},
|
| 31 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def process_message(message, history):
|
| 34 |
+
"""處理用戶輸入,並生成機器人回應。"""
|
| 35 |
+
# 準備歷史消息
|
| 36 |
messages = [
|
| 37 |
{
|
| 38 |
"role": "system",
|
| 39 |
"content": "你是一個高中數學老師,使用的語言是英文。學生用中文問妳任何字彙,你都可以告訴他那個中文對應的英文和例句,以及在數學上的可能用法以及數學例題和解法。\n說明數學上的可能用法時,先用中文講一遍再用B1程度的英文複述一遍\n"
|
| 40 |
}
|
| 41 |
]
|
| 42 |
+
# 添加歷史記錄
|
|
|
|
| 43 |
for user_msg, bot_msg in history:
|
| 44 |
messages.append({"role": "user", "content": user_msg})
|
| 45 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 46 |
+
# 添加新消息
|
|
|
|
| 47 |
messages.append({"role": "user", "content": message})
|
| 48 |
|
| 49 |
+
# 從 Groq 獲取回應
|
| 50 |
completion = client.chat.completions.create(
|
| 51 |
model="llama-3.3-70b-versatile",
|
| 52 |
messages=messages,
|
|
|
|
| 56 |
stream=True,
|
| 57 |
stop=None,
|
| 58 |
)
|
|
|
|
|
|
|
| 59 |
response_text = ""
|
|
|
|
|
|
|
| 60 |
for chunk in completion:
|
| 61 |
delta_content = chunk.choices[0].delta.content
|
| 62 |
if delta_content is not None:
|
| 63 |
response_text += delta_content
|
| 64 |
yield response_text
|
| 65 |
|
| 66 |
+
# 創建 Gradio 界面
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
+
name_input = gr.Textbox(placeholder="輸入您的名字...", label="Name")
|
| 69 |
+
chatbot = gr.Chatbot(height=600, show_label=False, container=True)
|
| 70 |
+
msg = gr.Textbox(placeholder="輸入您的問題...", show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
clear = gr.Button("清除對話")
|
| 72 |
|
| 73 |
+
def user(user_message, user_name, history):
|
| 74 |
return "", history + [[user_message, None]]
|
| 75 |
|
| 76 |
+
def bot(history, name):
|
| 77 |
history[-1][1] = ""
|
| 78 |
for response in process_message(history[-1][0], history[:-1]):
|
| 79 |
history[-1][1] = response
|
| 80 |
yield history
|
| 81 |
+
# 將對話記錄到 Notion
|
| 82 |
+
log_to_notion(name, history[-1][0], history[-1][1])
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
msg.submit(user, [msg, name_input, chatbot], [msg, chatbot], queue=False).then(
|
| 85 |
+
bot, [chatbot, name_input], chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
)
|
|
|
|
| 87 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 88 |
|
| 89 |
+
# 啟動應用
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.queue()
|
| 92 |
+
demo.launch()
|