Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
|
4 |
-
#
|
5 |
try:
|
6 |
from groq import Groq
|
7 |
except ImportError:
|
@@ -16,42 +16,53 @@ if not groq_key:
|
|
16 |
# 初始化 Groq 客戶端
|
17 |
client = Groq(api_key=groq_key)
|
18 |
|
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 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
|
4 |
+
# 安裝 groq 套件如果未安裝
|
5 |
try:
|
6 |
from groq import Groq
|
7 |
except ImportError:
|
|
|
16 |
# 初始化 Groq 客戶端
|
17 |
client = Groq(api_key=groq_key)
|
18 |
|
19 |
+
class SimpleChatBot:
|
20 |
+
def __init__(self):
|
21 |
+
self.initial_prompt = [
|
22 |
+
{
|
23 |
+
"role": "system",
|
24 |
+
"content": "請你當一個玻璃心但是還願意溝通,脆弱中帶點身心韌性的可憐男性,聽到別人罵髒話會很害怕。(Zh-TW)"
|
25 |
+
}
|
26 |
+
]
|
27 |
+
|
28 |
+
def get_response(self, message, chat_history):
|
29 |
+
messages = self.initial_prompt + chat_history
|
30 |
+
messages.append({"role": "user", "content": message})
|
31 |
+
|
32 |
+
completion = client.chat.completions.create(
|
33 |
+
model="llama-3.1-70b-versatile",
|
34 |
+
messages=messages,
|
35 |
+
temperature=1,
|
36 |
+
max_tokens=1024,
|
37 |
+
top_p=1,
|
38 |
+
stream=True,
|
39 |
+
stop=None,
|
40 |
+
)
|
41 |
+
|
42 |
+
response_content = ""
|
43 |
+
for chunk in completion:
|
44 |
+
response_content += chunk.choices[0].delta.content or ""
|
45 |
+
|
46 |
+
return response_content
|
47 |
+
|
48 |
+
chatbot = SimpleChatBot()
|
49 |
+
|
50 |
+
def respond(message, chat_history):
|
51 |
+
chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]
|
52 |
+
response = chatbot.get_response(message, chat_history)
|
53 |
+
chat_history.append({"role": "user", "content": message})
|
54 |
+
chat_history.append({"role": "assistant", "content": response})
|
55 |
+
return chat_history, ""
|
56 |
+
|
57 |
+
with gr.Blocks(title="玻璃心聊天機器人") as demo:
|
58 |
+
gr.Markdown("# 玻璃心聊天機器人")
|
59 |
+
|
60 |
+
chatbot_interface = gr.Chatbot(type="messages")
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
user_input = gr.Textbox(placeholder="輸入訊息...", label="你的訊息")
|
64 |
+
send_button = gr.Button("發送")
|
65 |
+
|
66 |
+
send_button.click(respond, inputs=[user_input, chatbot_interface], outputs=[chatbot_interface, user_input])
|
67 |
+
|
68 |
+
demo.launch(share=True)
|