Spaces:
Runtime error
Runtime error
Update app.py
Browse files123453 unmodified lines9101112131415161718192021222324252627282930313233343536373839404122 unmodified lines64656667686970717273747576772 unmodified lines8081828384858687import osimport subprocess# 自動檢查並安裝必要的套件def install_and_import(package):3 unmodified linessubprocess.check_call(["pip", "install", package])# 安裝依賴install_and_import("gradio")install_and_import("gspread")install_and_import("groq")import gradio as grimport gspreadfrom datetime import datetimefrom groq import Groq# 設定 Groq 的 API Keygroq_key = os.getenv("groq_key")if not groq_key:raise ValueError("groq_key environment variable is not set")client = Groq(api_key=groq_key)# 使用共享連結的方式操作 Google Sheetsheet_url = "https://docs.google.com/spreadsheets/d/1BQ23fiiGu5wN4RmRC4XmrKS6qCWHuymlbsoLEEmEN_4/edit?usp=sharing"# 使用 gspread 連接 Google Sheet(不需要憑證,依賴共享權限)try:gc = gspread.oauth() # 使用匿名方式連接sheet = gc.open_by_url(sheet_url).sheet1 # 獲取第一個工作表except Exception as e:raise ValueError(f"無法連接 Google Sheet,請確認連結或權限設置:{e}")# 定義簡單的 Chatbotclass SimpleChatBot:def __init__(self):self.initial_prompt = [22 unmodified lineschatbot = SimpleChatBot()# 記錄聊天內容到 Google Sheetdef log_to_sheet(user_message, bot_response):try:timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")sheet.append_row([timestamp, user_message, bot_response])print("Logged to Google Sheet successfully.")except Exception as e:print(f"Error logging to Google Sheet: {e}")# 定義回應邏輯def respond(message, chat_history):2 unmodified lineschat_history.append({"role": "user", "content": message})chat_history.append({"role": "assistant", "content": response})# 記錄到 Google Sheetlog_to_sheet(message, response)return chat_history, ""12345673 unmodified lines1112131415161718192021222324252627282930313233343536373839404122 unmodified lines6465666768697071727374757677787980818283842 unmodified lines8788899091929394import osimport subprocessfrom datetime import datetimeimport gradio as gr# 自動檢查並安裝必要的套件def install_and_import(package):3 unmodified linessubprocess.check_call(["pip", "install", package])# 安裝依賴install_and_import("notion-client")install_and_import("gradio")install_and_import("groq")from notion_client import Clientfrom groq import Groq# 設定 Notion API 和資料庫 IDNOTION_API_KEY = os.getenv("NOTION_API_KEY", "your_notion_api_key")NOTION_DB_ID = os.getenv("NOTION_DB_ID", "your_database_id")if NOTION_API_KEY == "your_notion_api_key" or NOTION_DB_ID == "your_database_id":raise ValueError("請設置 Notion API Key 和資料庫 ID!")# 初始化 Notion 客戶端notion = Client(auth=NOTION_API_KEY)# 設定 Groq 的 API Keygroq_key = os.getenv("groq_key")if not groq_key:raise ValueError("groq_key 環境變數未設置!")client = Groq(api_key=groq_key)# 定義 Chatbot 類class SimpleChatBot:def __init__(self):self.initial_prompt = [22 unmodified lineschatbot = SimpleChatBot()# 記錄聊天內容到 Notion 資料庫def log_to_notion(user_message, bot_response):try:timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")notion.pages.create(parent={"database_id": NOTION_DB_ID},properties={"Timestamp": {"date": {"start": timestamp}},"User Input": {"rich_text": [{"text": {"content": user_message}}]},"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]},},)print("Logged to Notion successfully.")except Exception as e:print(f"Error logging to Notion: {e}")# 定義回應邏輯def respond(message, chat_history):2 unmodified lineschat_history.append({"role": "user", "content": message})chat_history.append({"role": "assistant", "content": response})# 記錄到 Notionlog_to_notion(message, response)return chat_history, ""
app.py+30-23