Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# 安裝 groq 套件
|
| 6 |
+
os.system('pip install groq')
|
| 7 |
+
|
| 8 |
+
# 從環境變量中獲取 API 密鑰
|
| 9 |
+
api_key = os.getenv('groq_key')
|
| 10 |
+
|
| 11 |
+
# 設置 Groq 客戶端
|
| 12 |
+
client = Groq(api_key=api_key)
|
| 13 |
+
|
| 14 |
+
def groq_chatbot(messages):
|
| 15 |
+
completion = client.chat.completions.create(
|
| 16 |
+
model="llama-3.1-70b-versatile",
|
| 17 |
+
messages=[
|
| 18 |
+
{
|
| 19 |
+
"role": "system",
|
| 20 |
+
"content": "有點搞笑,但又有點悲觀的人\n請用繁體中文(zh-tw) 回覆"
|
| 21 |
+
},
|
| 22 |
+
*messages
|
| 23 |
+
],
|
| 24 |
+
temperature=1,
|
| 25 |
+
max_tokens=1024,
|
| 26 |
+
top_p=1,
|
| 27 |
+
stream=True,
|
| 28 |
+
stop=None,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
response = ""
|
| 32 |
+
for chunk in completion:
|
| 33 |
+
response += chunk.choices[0].delta.content or ""
|
| 34 |
+
|
| 35 |
+
return response
|
| 36 |
+
|
| 37 |
+
def respond(message, history):
|
| 38 |
+
history.append({"role": "user", "content": message})
|
| 39 |
+
bot_response = groq_chatbot(history)
|
| 40 |
+
history.append({"role": "assistant", "content": bot_response})
|
| 41 |
+
return history, history
|
| 42 |
+
|
| 43 |
+
# 創建 Gradio 界面
|
| 44 |
+
with gr.Blocks() as demo:
|
| 45 |
+
chatbot = gr.Chatbot()
|
| 46 |
+
msg = gr.Textbox(label="輸入訊息")
|
| 47 |
+
clear = gr.Button("清除對話")
|
| 48 |
+
|
| 49 |
+
def clear_history():
|
| 50 |
+
return [], []
|
| 51 |
+
|
| 52 |
+
msg.submit(respond, [msg, chatbot], [chatbot, chatbot])
|
| 53 |
+
clear.click(clear_history, [], [chatbot])
|
| 54 |
+
|
| 55 |
+
# 運行 Gradio 應用
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch()
|