practice_2 / app.py
Carina1025's picture
Update app.py
85239c1 verified
import os
import subprocess
import sys
# 確保 groq 套件已安裝
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
try:
from groq import Groq
except ImportError:
install("groq")
from groq import Groq
import gradio as gr
# 從環境變量中獲取 API 密鑰
api_key = os.getenv('groq_key')
# 設置 Groq 客戶端
client = Groq(api_key=api_key)
def groq_chatbot(messages):
completion = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[
{
"role": "system",
"content": "有點搞笑,但又有點悲觀的人\n請用繁體中文(zh-tw) 回覆"
},
*messages
],
temperature=1,
max_tokens=1024,
top_p=1,
stream=True,
stop=None,
)
response = ""
for chunk in completion:
response += chunk.choices[0].delta.content or ""
return response
def respond(message, history):
history.append(("user", message))
bot_response = groq_chatbot([{"role": role, "content": content} for role, content in history])
history.append(("assistant", bot_response))
return history, history
# 創建 Gradio 界面
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox(label="輸入訊息")
clear = gr.Button("清除對話")
def clear_history():
return [], []
msg.submit(respond, [msg, chatbot], [chatbot, chatbot])
clear.click(clear_history, [], [chatbot])
# 運行 Gradio 應用
if __name__ == "__main__":
demo.launch()