Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 Key
|
| 9 |
+
groq_key = os.getenv("groq_key")
|
| 10 |
+
|
| 11 |
+
# 初始化 Groq 客戶端
|
| 12 |
+
client = Groq(api_key=groq_key)
|
| 13 |
+
|
| 14 |
+
# 定義 Chatbot 回應函數
|
| 15 |
+
def chatbot_response(user_input):
|
| 16 |
+
completion = client.chat.completions.create(
|
| 17 |
+
model="llama-3.1-70b-versatile",
|
| 18 |
+
messages=[
|
| 19 |
+
{
|
| 20 |
+
"role": "system",
|
| 21 |
+
"content": "You have a humorous temperament, and are capable of discussing serious matters with a sense of humor.\nYou speak Traditional Chinese(zh-tw)."
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"role": "user",
|
| 25 |
+
"content": user_input
|
| 26 |
+
}
|
| 27 |
+
],
|
| 28 |
+
temperature=1.14,
|
| 29 |
+
max_tokens=1024,
|
| 30 |
+
top_p=1,
|
| 31 |
+
stream=False,
|
| 32 |
+
stop=None,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# 檢查並解析回應
|
| 36 |
+
response = ""
|
| 37 |
+
for chunk in completion:
|
| 38 |
+
if hasattr(chunk, 'choices') and len(chunk.choices) > 0:
|
| 39 |
+
delta_content = getattr(chunk.choices[0], 'delta', {}).get('content', "")
|
| 40 |
+
if delta_content:
|
| 41 |
+
response += delta_content
|
| 42 |
+
|
| 43 |
+
return response
|
| 44 |
+
|
| 45 |
+
# 設定 Gradio Chatbot 介面
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=chatbot_response,
|
| 48 |
+
inputs="text",
|
| 49 |
+
outputs="text",
|
| 50 |
+
title="Groq Chatbot",
|
| 51 |
+
description="一個用於與 Groq API 互動的 Chatbot",
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# 啟動 Gradio 應用程式
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
iface.launch()
|