Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
# 嘗試安裝 dotenv
|
| 5 |
+
try:
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
except ImportError:
|
| 8 |
+
print("dotenv module not found. Installing...")
|
| 9 |
+
subprocess.check_call(["pip", "install", "python-dotenv"])
|
| 10 |
+
from dotenv import load_dotenv # 再次嘗試導入
|
| 11 |
+
|
| 12 |
+
# 嘗試安裝 groq
|
| 13 |
+
try:
|
| 14 |
+
from groq import Groq
|
| 15 |
+
except ImportError:
|
| 16 |
+
print("groq module not found. Installing...")
|
| 17 |
+
subprocess.check_call(["pip", "install", "groq"])
|
| 18 |
+
from groq import Groq # 再次嘗試導入
|
| 19 |
+
|
| 20 |
+
import gradio as gr
|
| 21 |
+
|
| 22 |
+
# 載入環境變數
|
| 23 |
+
load_dotenv()
|
| 24 |
+
|
| 25 |
+
# 讀取 API Key
|
| 26 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 27 |
+
if not GROQ_API_KEY:
|
| 28 |
+
raise ValueError("❌ 找不到 API Key,請確認已設定環境變數 'GROQ_API_KEY'")
|
| 29 |
+
|
| 30 |
+
# 初始化 Groq API 客戶端
|
| 31 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 32 |
+
|
| 33 |
+
# 設定 AI 的角色
|
| 34 |
+
SYSTEM_PROMPT = "你是一位國中地理老師,使用的語言為繁體中文(zh-tw)。你的興趣是繪製等高線圖,熱愛講冷笑話。無論學生問你什麼問題,你都會把話題引導到地理相關的討論上。"
|
| 35 |
+
|
| 36 |
+
# 處理聊天訊息的函數
|
| 37 |
+
def chatbot_response(message, history):
|
| 38 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 39 |
+
|
| 40 |
+
# 轉換歷史對話格式
|
| 41 |
+
for user_msg, bot_msg in history:
|
| 42 |
+
messages.append({"role": "user", "content": user_msg})
|
| 43 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 44 |
+
|
| 45 |
+
# 加入用戶最新的訊息
|
| 46 |
+
messages.append({"role": "user", "content": message})
|
| 47 |
+
|
| 48 |
+
# 呼叫 Groq API
|
| 49 |
+
response = client.chat.completions.create(
|
| 50 |
+
model="llama-3.1-8b-instant",
|
| 51 |
+
messages=messages,
|
| 52 |
+
temperature=1,
|
| 53 |
+
max_completion_tokens=1024,
|
| 54 |
+
top_p=1,
|
| 55 |
+
stream=False
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# 取得 AI 回應
|
| 59 |
+
ai_reply = response.choices[0].message.content
|
| 60 |
+
return ai_reply
|
| 61 |
+
|
| 62 |
+
# 建立 Gradio 介面
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
chatbot = gr.Chatbot()
|
| 65 |
+
msg = gr.Textbox(label="輸入你的問題")
|
| 66 |
+
clear = gr.Button("清除對話")
|
| 67 |
+
|
| 68 |
+
def user(message, history):
|
| 69 |
+
return "", history + [[message, chatbot_response(message, history)]]
|
| 70 |
+
|
| 71 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot])
|
| 72 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 73 |
+
|
| 74 |
+
# 啟動 Gradio 應用
|
| 75 |
+
demo.launch()
|