Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,50 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def handle_action(action, index):
|
| 39 |
-
index = int(index) - 1
|
| 40 |
-
if action == "刪除":
|
| 41 |
-
return delete_todo_item(index)
|
| 42 |
-
elif action == "完成/未完成":
|
| 43 |
-
return complete_todo_item(index)
|
| 44 |
-
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
time = gr.Textbox(label="時間 (HH:MM)")
|
| 50 |
-
title = gr.Textbox(label="事項主題")
|
| 51 |
-
details = gr.Textbox(label="事項細節")
|
| 52 |
-
location = gr.Textbox(label="地點")
|
| 53 |
-
add_button = gr.Button("新增")
|
| 54 |
-
with gr.Column():
|
| 55 |
-
overview = gr.Textbox(label="To-Do List 總覽", interactive=False, lines=20)
|
| 56 |
-
action = gr.Radio(choices=["刪除", "完成/未完成"], label="動作")
|
| 57 |
-
index = gr.Number(label="項目編號")
|
| 58 |
-
action_button = gr.Button("執行")
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
|
| 65 |
demo.launch()
|
|
|
|
| 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 客戶端
|
| 9 |
+
groq_key = os.getenv("groq_key")
|
| 10 |
+
client = Groq(api_key=groq_key)
|
| 11 |
+
|
| 12 |
+
def chatbot_response(user_message):
|
| 13 |
+
messages = [
|
| 14 |
+
{
|
| 15 |
+
"role": "system",
|
| 16 |
+
"content": "請你當一個玻璃心但是還願意溝通,脆弱中帶點身心韌性的可憐男性,聽到別人罵髒話會很害怕。(Zh-TW)"
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"role": "user",
|
| 20 |
+
"content": user_message
|
| 21 |
+
}
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
completion = client.chat.completions.create(
|
| 25 |
+
model="llama-3.1-70b-versatile",
|
| 26 |
+
messages=messages,
|
| 27 |
+
temperature=1,
|
| 28 |
+
max_tokens=1024,
|
| 29 |
+
top_p=1,
|
| 30 |
+
stream=False,
|
| 31 |
+
stop=None,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
response_message = completion.choices[0].message['content']
|
| 35 |
+
return response_message
|
| 36 |
+
|
| 37 |
+
# 建立 Gradio 界面
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
+
chatbot = gr.Chatbot()
|
| 40 |
+
user_input = gr.Textbox(placeholder="輸入訊息...")
|
| 41 |
+
send_button = gr.Button("發送")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
def user_message_handler(user_message, history):
|
| 44 |
+
bot_response = chatbot_response(user_message)
|
| 45 |
+
history.append((user_message, bot_response))
|
| 46 |
+
return history, ""
|
| 47 |
|
| 48 |
+
send_button.click(user_message_handler, [user_input, chatbot], [chatbot, user_input])
|
| 49 |
|
| 50 |
demo.launch()
|