Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,79 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import time
|
| 4 |
|
| 5 |
-
# 安裝
|
| 6 |
try:
|
| 7 |
from groq import Groq
|
| 8 |
except ImportError:
|
| 9 |
os.system('pip install groq')
|
| 10 |
from groq import Groq
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
if not
|
| 15 |
-
raise ValueError("請確保已設定環境變數 groq_key
|
| 16 |
|
| 17 |
# 初始化 Groq 客戶端
|
| 18 |
-
client = Groq(api_key=
|
| 19 |
-
|
| 20 |
-
# 定義
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
-
chatbot = gr.Chatbot(
|
| 53 |
-
msg = gr.Textbox(label="輸入您的訊息")
|
| 54 |
-
clear = gr.
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
msg.submit(
|
|
|
|
| 58 |
|
| 59 |
-
# 啟動應用
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
+
# 嘗試安裝和匯入 groq
|
| 5 |
try:
|
| 6 |
from groq import Groq
|
| 7 |
except ImportError:
|
| 8 |
os.system('pip install groq')
|
| 9 |
from groq import Groq
|
| 10 |
|
| 11 |
+
# 從環境變數中讀取 groq_key
|
| 12 |
+
groq_key = os.getenv("groq_key")
|
| 13 |
+
if not groq_key:
|
| 14 |
+
raise ValueError("請確保已設定環境變數 'groq_key'.")
|
| 15 |
|
| 16 |
# 初始化 Groq 客戶端
|
| 17 |
+
client = Groq(api_key=groq_key)
|
| 18 |
+
|
| 19 |
+
# 定義角色設定
|
| 20 |
+
system_message = {
|
| 21 |
+
"role": "system",
|
| 22 |
+
"content": "你是一個美術老師,請用繁體中文稱讚學生的問題,問什麼問題都會引導到藝術"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# 定義與 Groq 的互動邏輯
|
| 26 |
+
def chat_with_groq(history):
|
| 27 |
+
"""
|
| 28 |
+
與 Groq API 對話的函數,將歷史對話傳給 API 並返回模型的回應。
|
| 29 |
+
"""
|
| 30 |
+
# 把歷史對話轉換成 Groq 的訊息格式
|
| 31 |
+
messages = [system_message] + [
|
| 32 |
+
{"role": "user" if i % 2 == 0 else "assistant", "content": msg}
|
| 33 |
+
for i, msg in enumerate(sum(history, ()))
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
# 呼叫 Groq 的 completion API
|
| 37 |
+
completion = client.chat.completions.create(
|
| 38 |
+
model="llama3-8b-8192",
|
| 39 |
+
messages=messages,
|
| 40 |
+
temperature=1,
|
| 41 |
+
max_tokens=1024,
|
| 42 |
+
top_p=1,
|
| 43 |
+
stream=False,
|
| 44 |
+
stop=None,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# 提取 API 回應
|
| 48 |
+
response = completion.choices[0].message.content
|
| 49 |
+
return response
|
| 50 |
+
|
| 51 |
+
# 定義 Gradio 的互動函數
|
| 52 |
+
def gradio_chatbot(user_message, history):
|
| 53 |
+
"""
|
| 54 |
+
與使用者互動的 Gradio 函數,處理新的訊息並更新對話歷史。
|
| 55 |
+
"""
|
| 56 |
+
# 更新歷史對話
|
| 57 |
+
if history is None:
|
| 58 |
+
history = []
|
| 59 |
+
history.append((user_message, None))
|
| 60 |
+
|
| 61 |
+
# 呼叫 chat_with_groq 並獲得回應
|
| 62 |
+
bot_response = chat_with_groq(history)
|
| 63 |
+
history[-1] = (user_message, bot_response)
|
| 64 |
+
|
| 65 |
+
return history, history
|
| 66 |
+
|
| 67 |
+
# 建立 Gradio 界面
|
| 68 |
with gr.Blocks() as demo:
|
| 69 |
+
chatbot = gr.Chatbot() # 建立 Chatbot 元件
|
| 70 |
+
msg = gr.Textbox(label="輸入您的問題:", placeholder="請輸入訊息...")
|
| 71 |
+
clear = gr.Button("清除對話")
|
| 72 |
|
| 73 |
+
# 定義互動邏輯
|
| 74 |
+
msg.submit(gradio_chatbot, [msg, chatbot], [chatbot, chatbot])
|
| 75 |
+
clear.click(lambda: None, None, chatbot) # 清空對話
|
| 76 |
|
| 77 |
+
# 啟動 Gradio 應用
|
| 78 |
if __name__ == "__main__":
|
| 79 |
demo.launch()
|