Spaces:
Runtime error
Runtime error
peterchuang
commited on
Commit
•
992901b
1
Parent(s):
f828929
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,65 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
from groq import Groq
|
7 |
-
except ImportError:
|
8 |
-
os.system('pip install groq')
|
9 |
-
from groq import Groq
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
raise ValueError("groq_key environment variable is not set")
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
max_tokens=1024,
|
36 |
-
top_p=1,
|
37 |
-
stream=True,
|
38 |
-
stop=None,
|
39 |
-
)
|
40 |
-
|
41 |
-
response_content = ""
|
42 |
-
for chunk in completion:
|
43 |
-
response_content += chunk.choices[0].delta.content or ""
|
44 |
-
|
45 |
-
return response_content
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
def
|
50 |
-
|
51 |
-
response = chatbot.get_response(message, chat_history)
|
52 |
-
chat_history.append({"role": "user", "content": message})
|
53 |
-
chat_history.append({"role": "assistant", "content": response})
|
54 |
-
return chat_history, ""
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
from groq import Groq
|
4 |
+
from gradio import ChatMessage
|
5 |
|
6 |
+
# 從環境變數中獲取 API 密鑰
|
7 |
+
api_key = os.getenv("groq_key")
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# 初始化 groq 客戶端
|
10 |
+
client = Groq()
|
11 |
+
client.set_api_key(api_key)
|
|
|
12 |
|
13 |
+
# 定義回應函數
|
14 |
+
def respond(message, chat_history):
|
15 |
+
# 構建聊天記錄
|
16 |
+
messages = [{"role": "system", "content": "你是一個友善的 AI 助手,請幫助使用者解答問題。"}]
|
17 |
+
for entry in chat_history:
|
18 |
+
if entry['role'] == 'user':
|
19 |
+
messages.append({"role": "user", "content": entry['content']})
|
20 |
+
else:
|
21 |
+
messages.append({"role": "assistant", "content": entry['content']})
|
22 |
+
|
23 |
+
# 添加當前用戶消息
|
24 |
+
messages.append({"role": "user", "content": message})
|
25 |
|
26 |
+
# 獲取回應
|
27 |
+
completion = client.chat.completions.create(
|
28 |
+
model="llama-3.1-70b-versatile",
|
29 |
+
messages=messages,
|
30 |
+
temperature=1,
|
31 |
+
max_tokens=1024,
|
32 |
+
top_p=1,
|
33 |
+
stream=True,
|
34 |
+
stop=None,
|
35 |
+
)
|
36 |
|
37 |
+
reply = ""
|
38 |
+
for chunk in completion:
|
39 |
+
reply += chunk.choices[0].delta.content or ""
|
40 |
+
|
41 |
+
# 添加當前回應到聊天記錄
|
42 |
+
chat_history.append(ChatMessage(role="user", content=message))
|
43 |
+
chat_history.append(ChatMessage(role="assistant", content=reply))
|
44 |
+
return chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# 創建 Gradio Chatbot 界面
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
chatbot = gr.Chatbot(type='messages')
|
49 |
+
msg = gr.Textbox(label="輸入訊息")
|
50 |
+
clear = gr.Button("清除聊天")
|
51 |
|
52 |
+
def user(user_message, history):
|
53 |
+
return "", history + [ChatMessage(role="user", content=user_message)]
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
def bot(history):
|
56 |
+
user_message = history[-1]['content']
|
57 |
+
history = respond(user_message, history[:-1])
|
58 |
+
return history
|
59 |
+
|
60 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
61 |
+
bot, chatbot, chatbot
|
62 |
+
)
|
63 |
+
clear.click(lambda: [], None, chatbot, queue=False)
|
64 |
+
|
65 |
+
demo.launch()
|
|