Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,67 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
groq_key = os.getenv(
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# 初始化 Groq 客戶端
|
| 12 |
client = Groq(api_key=groq_key)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
model="llama-3.1-8b-instant",
|
| 18 |
-
messages=[
|
| 19 |
{
|
| 20 |
"role": "system",
|
| 21 |
"content": "你是一個有幽默感的聊天機器人,你擅長用詼諧的方式來討論嚴肅的事情,尤其是政治方面的議題。你使用的語言是繁體中文(zh-tw)。"
|
| 22 |
-
},
|
| 23 |
-
{
|
| 24 |
-
"role": "user",
|
| 25 |
-
"content": user_input
|
| 26 |
}
|
| 27 |
-
]
|
| 28 |
-
temperature=1.18,
|
| 29 |
-
max_tokens=1024,
|
| 30 |
-
top_p=1,
|
| 31 |
-
stream=True,
|
| 32 |
-
stop=None,
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
response = ""
|
| 36 |
-
for chunk in completion:
|
| 37 |
-
response += chunk.choices[0].delta.content or ""
|
| 38 |
-
|
| 39 |
-
return response
|
| 40 |
-
|
| 41 |
-
# 建立 Gradio 聊天機器人介面
|
| 42 |
-
chatbot = gr.Chatbot()
|
| 43 |
-
input_text = gr.inputs.Textbox(placeholder="輸入你的訊息...")
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
fn=respond_to_user,
|
| 52 |
-
inputs=[input_text, chatbot],
|
| 53 |
-
outputs=[chatbot, chatbot],
|
| 54 |
-
live=True,
|
| 55 |
-
allow_flagging='never',
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
# 啟動應用
|
| 59 |
-
if __name__ == "__main__":
|
| 60 |
-
interface.launch()
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
|
| 4 |
+
# Install the groq package if it is not installed
|
| 5 |
+
try:
|
| 6 |
+
from groq import Groq
|
| 7 |
+
except ImportError:
|
| 8 |
+
os.system('pip install groq')
|
| 9 |
+
from groq import Groq
|
| 10 |
|
| 11 |
+
# Set up the Groq client with the secret key
|
| 12 |
+
groq_key = os.getenv('groq_key')
|
| 13 |
+
if not groq_key:
|
| 14 |
+
raise ValueError("groq_key environment variable is not set")
|
| 15 |
|
|
|
|
| 16 |
client = Groq(api_key=groq_key)
|
| 17 |
|
| 18 |
+
class SimpleChatBot:
|
| 19 |
+
def __init__(self):
|
| 20 |
+
self.initial_prompt = [
|
|
|
|
|
|
|
| 21 |
{
|
| 22 |
"role": "system",
|
| 23 |
"content": "你是一個有幽默感的聊天機器人,你擅長用詼諧的方式來討論嚴肅的事情,尤其是政治方面的議題。你使用的語言是繁體中文(zh-tw)。"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
def get_response(self, message, chat_history):
|
| 28 |
+
messages = self.initial_prompt + chat_history
|
| 29 |
+
messages.append({"role": "user", "content": message})
|
| 30 |
+
|
| 31 |
+
completion = client.chat.completions.create(
|
| 32 |
+
model="llama-3.1-70b-versatile",
|
| 33 |
+
messages=messages,
|
| 34 |
+
temperature=1,
|
| 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 |
+
chatbot = SimpleChatBot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def respond(message, chat_history):
|
| 50 |
+
chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]
|
| 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 |
+
with gr.Blocks(title="簡單的Gradio聊天機器人") as demo:
|
| 57 |
+
gr.Markdown("# 簡單的Gradio聊天機器人")
|
| 58 |
+
|
| 59 |
+
chatbot_interface = gr.Chatbot(type="messages")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
user_input = gr.Textbox(placeholder="輸入訊息...", label="你的訊息")
|
| 63 |
+
send_button = gr.Button("發送")
|
| 64 |
+
|
| 65 |
+
send_button.click(respond, inputs=[user_input, chatbot_interface], outputs=[chatbot_interface, user_input])
|
| 66 |
+
|
| 67 |
+
demo.launch(share=True)
|