Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,31 @@
|
|
| 1 |
import os
|
| 2 |
-
import subprocess
|
| 3 |
-
|
| 4 |
-
# 自動檢查並安裝 gspread
|
| 5 |
-
try:
|
| 6 |
-
import gspread
|
| 7 |
-
except ImportError:
|
| 8 |
-
print("gspread 未安裝,正在安裝...")
|
| 9 |
-
subprocess.check_call(["pip", "install", "gspread"])
|
| 10 |
-
import gspread
|
| 11 |
-
|
| 12 |
import gradio as gr
|
|
|
|
| 13 |
from datetime import datetime
|
| 14 |
|
| 15 |
# 安裝並導入 groq 套件
|
| 16 |
try:
|
| 17 |
from groq import Groq
|
| 18 |
except ImportError:
|
| 19 |
-
os.system(
|
| 20 |
from groq import Groq
|
| 21 |
|
| 22 |
# 設定 Groq 的 API Key
|
| 23 |
-
groq_key = os.getenv(
|
| 24 |
if not groq_key:
|
| 25 |
raise ValueError("groq_key environment variable is not set")
|
| 26 |
|
| 27 |
client = Groq(api_key=groq_key)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
sheet_url = "https://docs.google.com/spreadsheets/d/1BQ23fiiGu5wN4RmRC4XmrKS6qCWHuymlbsoLEEmEN_4/edit?usp=sharing"
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# 定義簡單的 Chatbot
|
| 35 |
class SimpleChatBot:
|
|
@@ -44,21 +40,18 @@ class SimpleChatBot:
|
|
| 44 |
def get_response(self, message, chat_history):
|
| 45 |
messages = self.initial_prompt + chat_history
|
| 46 |
messages.append({"role": "user", "content": message})
|
| 47 |
-
|
|
|
|
| 48 |
completion = client.chat.completions.create(
|
| 49 |
model="llama-3.1-70b-versatile",
|
| 50 |
messages=messages,
|
| 51 |
temperature=1,
|
| 52 |
max_tokens=1024,
|
| 53 |
top_p=1,
|
| 54 |
-
stream=
|
| 55 |
-
stop=None,
|
| 56 |
)
|
| 57 |
-
|
| 58 |
-
response_content = ""
|
| 59 |
-
for chunk in completion:
|
| 60 |
-
response_content += chunk.choices[0].delta.content or ""
|
| 61 |
-
|
| 62 |
return response_content
|
| 63 |
|
| 64 |
chatbot = SimpleChatBot()
|
|
@@ -78,10 +71,10 @@ def respond(message, chat_history):
|
|
| 78 |
response = chatbot.get_response(message, chat_history)
|
| 79 |
chat_history.append({"role": "user", "content": message})
|
| 80 |
chat_history.append({"role": "assistant", "content": response})
|
| 81 |
-
|
| 82 |
# 記錄到 Google Sheet
|
| 83 |
log_to_sheet(message, response)
|
| 84 |
-
|
| 85 |
return chat_history, ""
|
| 86 |
|
| 87 |
# 建立 Gradio 界面
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
import gspread
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
# 安裝並導入 groq 套件
|
| 7 |
try:
|
| 8 |
from groq import Groq
|
| 9 |
except ImportError:
|
| 10 |
+
os.system("pip install groq")
|
| 11 |
from groq import Groq
|
| 12 |
|
| 13 |
# 設定 Groq 的 API Key
|
| 14 |
+
groq_key = os.getenv("groq_key")
|
| 15 |
if not groq_key:
|
| 16 |
raise ValueError("groq_key environment variable is not set")
|
| 17 |
|
| 18 |
client = Groq(api_key=groq_key)
|
| 19 |
|
| 20 |
+
# 使用共享連結的方式操作 Google Sheet
|
| 21 |
sheet_url = "https://docs.google.com/spreadsheets/d/1BQ23fiiGu5wN4RmRC4XmrKS6qCWHuymlbsoLEEmEN_4/edit?usp=sharing"
|
| 22 |
+
|
| 23 |
+
# 使用 gspread 連接 Google Sheet(匿名模式,依賴共享權限)
|
| 24 |
+
try:
|
| 25 |
+
gc = gspread.service_account(filename=None) # 不使用憑證文件
|
| 26 |
+
sheet = gc.open_by_url(sheet_url).sheet1 # 獲取第一個工作表
|
| 27 |
+
except Exception as e:
|
| 28 |
+
raise ValueError(f"無法連接 Google Sheet,請確認連結或權限設置:{e}")
|
| 29 |
|
| 30 |
# 定義簡單的 Chatbot
|
| 31 |
class SimpleChatBot:
|
|
|
|
| 40 |
def get_response(self, message, chat_history):
|
| 41 |
messages = self.initial_prompt + chat_history
|
| 42 |
messages.append({"role": "user", "content": message})
|
| 43 |
+
|
| 44 |
+
# 使用 groq 的 chat.completions API 生成回應
|
| 45 |
completion = client.chat.completions.create(
|
| 46 |
model="llama-3.1-70b-versatile",
|
| 47 |
messages=messages,
|
| 48 |
temperature=1,
|
| 49 |
max_tokens=1024,
|
| 50 |
top_p=1,
|
| 51 |
+
stream=False,
|
|
|
|
| 52 |
)
|
| 53 |
+
|
| 54 |
+
response_content = completion["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
|
|
|
| 55 |
return response_content
|
| 56 |
|
| 57 |
chatbot = SimpleChatBot()
|
|
|
|
| 71 |
response = chatbot.get_response(message, chat_history)
|
| 72 |
chat_history.append({"role": "user", "content": message})
|
| 73 |
chat_history.append({"role": "assistant", "content": response})
|
| 74 |
+
|
| 75 |
# 記錄到 Google Sheet
|
| 76 |
log_to_sheet(message, response)
|
| 77 |
+
|
| 78 |
return chat_history, ""
|
| 79 |
|
| 80 |
# 建立 Gradio 界面
|