Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import os
|
2 |
import subprocess
|
|
|
|
|
3 |
|
4 |
# 自動檢查並安裝必要的套件
|
5 |
def install_and_import(package):
|
@@ -9,33 +11,31 @@ def install_and_import(package):
|
|
9 |
subprocess.check_call(["pip", "install", package])
|
10 |
|
11 |
# 安裝依賴
|
|
|
12 |
install_and_import("gradio")
|
13 |
-
install_and_import("gspread")
|
14 |
install_and_import("groq")
|
15 |
|
16 |
-
|
17 |
-
import gspread
|
18 |
-
from datetime import datetime
|
19 |
from groq import Groq
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# 設定 Groq 的 API Key
|
22 |
groq_key = os.getenv("groq_key")
|
23 |
if not groq_key:
|
24 |
-
raise ValueError("groq_key
|
25 |
|
26 |
client = Groq(api_key=groq_key)
|
27 |
|
28 |
-
#
|
29 |
-
sheet_url = "https://docs.google.com/spreadsheets/d/1BQ23fiiGu5wN4RmRC4XmrKS6qCWHuymlbsoLEEmEN_4/edit?usp=sharing"
|
30 |
-
|
31 |
-
# 使用 gspread 連接 Google Sheet(不需要憑證,依賴共享權限)
|
32 |
-
try:
|
33 |
-
gc = gspread.oauth() # 使用匿名方式連接
|
34 |
-
sheet = gc.open_by_url(sheet_url).sheet1 # 獲取第一個工作表
|
35 |
-
except Exception as e:
|
36 |
-
raise ValueError(f"無法連接 Google Sheet,請確認連結或權限設置:{e}")
|
37 |
-
|
38 |
-
# 定義簡單的 Chatbot
|
39 |
class SimpleChatBot:
|
40 |
def __init__(self):
|
41 |
self.initial_prompt = [
|
@@ -64,14 +64,21 @@ class SimpleChatBot:
|
|
64 |
|
65 |
chatbot = SimpleChatBot()
|
66 |
|
67 |
-
# 記錄聊天內容到
|
68 |
-
def
|
69 |
try:
|
70 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
except Exception as e:
|
74 |
-
print(f"Error logging to
|
75 |
|
76 |
# 定義回應邏輯
|
77 |
def respond(message, chat_history):
|
@@ -80,8 +87,8 @@ def respond(message, chat_history):
|
|
80 |
chat_history.append({"role": "user", "content": message})
|
81 |
chat_history.append({"role": "assistant", "content": response})
|
82 |
|
83 |
-
# 記錄到
|
84 |
-
|
85 |
|
86 |
return chat_history, ""
|
87 |
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
+
from datetime import datetime
|
4 |
+
import gradio as gr
|
5 |
|
6 |
# 自動檢查並安裝必要的套件
|
7 |
def install_and_import(package):
|
|
|
11 |
subprocess.check_call(["pip", "install", package])
|
12 |
|
13 |
# 安裝依賴
|
14 |
+
install_and_import("notion-client")
|
15 |
install_and_import("gradio")
|
|
|
16 |
install_and_import("groq")
|
17 |
|
18 |
+
from notion_client import Client
|
|
|
|
|
19 |
from groq import Groq
|
20 |
|
21 |
+
# 設定 Notion API 和資料庫 ID
|
22 |
+
NOTION_API_KEY = os.getenv("NOTION_API_KEY", "your_notion_api_key")
|
23 |
+
NOTION_DB_ID = os.getenv("NOTION_DB_ID", "your_database_id")
|
24 |
+
|
25 |
+
if NOTION_API_KEY == "your_notion_api_key" or NOTION_DB_ID == "your_database_id":
|
26 |
+
raise ValueError("請設置 Notion API Key 和資料庫 ID!")
|
27 |
+
|
28 |
+
# 初始化 Notion 客戶端
|
29 |
+
notion = Client(auth=NOTION_API_KEY)
|
30 |
+
|
31 |
# 設定 Groq 的 API Key
|
32 |
groq_key = os.getenv("groq_key")
|
33 |
if not groq_key:
|
34 |
+
raise ValueError("groq_key 環境變數未設置!")
|
35 |
|
36 |
client = Groq(api_key=groq_key)
|
37 |
|
38 |
+
# 定義 Chatbot 類
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
class SimpleChatBot:
|
40 |
def __init__(self):
|
41 |
self.initial_prompt = [
|
|
|
64 |
|
65 |
chatbot = SimpleChatBot()
|
66 |
|
67 |
+
# 記錄聊天內容到 Notion 資料庫
|
68 |
+
def log_to_notion(user_message, bot_response):
|
69 |
try:
|
70 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
71 |
+
notion.pages.create(
|
72 |
+
parent={"database_id": NOTION_DB_ID},
|
73 |
+
properties={
|
74 |
+
"Timestamp": {"date": {"start": timestamp}},
|
75 |
+
"User Input": {"rich_text": [{"text": {"content": user_message}}]},
|
76 |
+
"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]},
|
77 |
+
},
|
78 |
+
)
|
79 |
+
print("Logged to Notion successfully.")
|
80 |
except Exception as e:
|
81 |
+
print(f"Error logging to Notion: {e}")
|
82 |
|
83 |
# 定義回應邏輯
|
84 |
def respond(message, chat_history):
|
|
|
87 |
chat_history.append({"role": "user", "content": message})
|
88 |
chat_history.append({"role": "assistant", "content": response})
|
89 |
|
90 |
+
# 記錄到 Notion
|
91 |
+
log_to_notion(message, response)
|
92 |
|
93 |
return chat_history, ""
|
94 |
|