Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,62 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
from groq import Groq
|
| 5 |
-
except ImportError:
|
| 6 |
-
os.system('pip install groq')
|
| 7 |
-
from groq import Groq
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
groq_key = os.getenv("groq_key")
|
| 12 |
|
| 13 |
# 初始化 Groq 客戶端
|
| 14 |
client = Groq(api_key=groq_key)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
def chatbot_response(user_input):
|
| 18 |
completion = client.chat.completions.create(
|
| 19 |
-
model="llama-3.1-
|
| 20 |
messages=[
|
| 21 |
{
|
| 22 |
"role": "system",
|
| 23 |
-
"content": "
|
| 24 |
},
|
| 25 |
{
|
| 26 |
"role": "user",
|
| 27 |
"content": user_input
|
| 28 |
}
|
| 29 |
],
|
| 30 |
-
temperature=1.
|
| 31 |
max_tokens=1024,
|
| 32 |
top_p=1,
|
| 33 |
-
stream=
|
| 34 |
stop=None,
|
| 35 |
)
|
| 36 |
-
|
| 37 |
-
# 檢查並解析回應
|
| 38 |
response = ""
|
| 39 |
for chunk in completion:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
if delta_content:
|
| 43 |
-
response += delta_content
|
| 44 |
-
|
| 45 |
return response
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
if __name__ == "__main__":
|
| 58 |
-
|
|
|
|
| 59 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from groq import Groq
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# 安裝 groq 套件
|
| 6 |
+
os.system('pip install groq')
|
| 7 |
|
| 8 |
+
# 從環境變量中獲取 Groq 的 API key
|
| 9 |
groq_key = os.getenv("groq_key")
|
| 10 |
|
| 11 |
# 初始化 Groq 客戶端
|
| 12 |
client = Groq(api_key=groq_key)
|
| 13 |
|
| 14 |
+
# 定義聊天機器人回應邏輯
|
| 15 |
def chatbot_response(user_input):
|
| 16 |
completion = client.chat.completions.create(
|
| 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 respond_to_user(input_text, history):
|
| 46 |
+
response = chatbot_response(input_text)
|
| 47 |
+
history.append((input_text, response))
|
| 48 |
+
return history, history
|
| 49 |
+
|
| 50 |
+
interface = gr.Interface(
|
| 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 |
|