Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,79 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import random
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
|
|
|
|
|
| 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 |
+
# 設置 API 金鑰
|
| 12 |
+
groq_key = os.getenv("groq_key")
|
| 13 |
+
client = Groq(api_key=groq_key)
|
| 14 |
+
|
| 15 |
+
# 定義聊天機器人回應函式
|
| 16 |
+
def chat_response(message, history):
|
| 17 |
+
"""
|
| 18 |
+
用戶端聊天回應函式,與 groq 客戶端交互。
|
| 19 |
+
:param message: 用戶輸入的訊息。
|
| 20 |
+
:param history: 聊天歷史記錄。
|
| 21 |
+
:return: 更新的聊天歷史。
|
| 22 |
+
"""
|
| 23 |
+
# 構建訊息歷史
|
| 24 |
+
messages = [
|
| 25 |
+
{"role": "system", "content": "我是國文老師,很會批改作文"}
|
| 26 |
+
]
|
| 27 |
|
| 28 |
+
# 加入歷史訊息
|
| 29 |
+
for user_msg, bot_msg in history:
|
| 30 |
+
messages.append({"role": "user", "content": user_msg})
|
| 31 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 32 |
|
| 33 |
+
# 加入當前用戶訊息
|
| 34 |
+
messages.append({"role": "user", "content": message})
|
| 35 |
+
|
| 36 |
+
# 生成回應
|
| 37 |
+
try:
|
| 38 |
+
completion = client.chat.completions.create(
|
| 39 |
+
model="llama-3.1-70b-versatile",
|
| 40 |
+
messages=messages,
|
| 41 |
+
temperature=1,
|
| 42 |
+
max_tokens=1024,
|
| 43 |
+
top_p=1,
|
| 44 |
+
stream=True,
|
| 45 |
+
stop=None,
|
| 46 |
+
)
|
| 47 |
+
# 解析並累積回應
|
| 48 |
+
bot_message = ""
|
| 49 |
+
for chunk in completion:
|
| 50 |
+
if chunk.choices[0].delta.content:
|
| 51 |
+
bot_message += chunk.choices[0].delta.content
|
| 52 |
+
history.append((message, bot_message))
|
| 53 |
+
except Exception as e:
|
| 54 |
+
# 如果請求出錯,回應錯誤訊息
|
| 55 |
+
history.append((message, f"Error: {str(e)}"))
|
| 56 |
+
|
| 57 |
+
return history, history
|
| 58 |
+
|
| 59 |
+
# 構建 Gradio 界面
|
| 60 |
+
with gr.Blocks() as demo:
|
| 61 |
+
chatbot = gr.Chatbot(label="Groq 國文老師 Chatbot")
|
| 62 |
+
message = gr.Textbox(label="輸入你的訊息")
|
| 63 |
+
state = gr.State([])
|
| 64 |
+
|
| 65 |
+
def user_input(user_message, history):
|
| 66 |
+
"""
|
| 67 |
+
用戶輸入訊息,更新歷史。
|
| 68 |
+
:param user_message: 用戶的輸入訊息。
|
| 69 |
+
:param history: 聊天歷史記錄。
|
| 70 |
+
:return: 更新後的聊天歷史。
|
| 71 |
+
"""
|
| 72 |
+
return "", history + [[user_message, None]]
|
| 73 |
+
|
| 74 |
+
message.submit(user_input, [message, state], [message, state]) \
|
| 75 |
+
.then(chat_response, [message, state], [chatbot, state])
|
| 76 |
|
| 77 |
+
# 啟動應用程式
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|