CasperDylan commited on
Commit
698c406
·
verified ·
1 Parent(s): 969b9fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -59
app.py CHANGED
@@ -1,65 +1,50 @@
 
1
  import gradio as gr
2
- from datetime import datetime
3
-
4
- # 初始化To-Do List
5
- todo_list = []
6
-
7
- def add_todo_item(date, time, title, details, location):
8
- item = {
9
- "date": date,
10
- "time": time,
11
- "title": title,
12
- "details": details,
13
- "location": location,
14
- "completed": False
15
- }
16
- todo_list.append(item)
17
- return update_view()
18
-
19
- def delete_todo_item(index):
20
- del todo_list[index]
21
- return update_view()
22
-
23
- def complete_todo_item(index):
24
- todo_list[index]["completed"] = not todo_list[index]["completed"]
25
- return update_view()
26
-
27
- def update_view():
28
- sorted_list = sorted(todo_list, key=lambda x: (x['date'], x['time']))
29
- overview = ""
30
- for i, item in enumerate(sorted_list):
31
- status = "完成" if item["completed"] else "未完成"
32
- overview += f"{i+1}. {item['date']} {item['time']} - {item['title']} ({status})\n"
33
- overview += f" 細節: {item['details']}\n"
34
- overview += f" 地點: {item['location']}\n"
35
- overview += f" [刪除] [完成/未完成]\n\n"
36
- return overview
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
- with gr.Row():
47
- with gr.Column():
48
- date = gr.Textbox(label="日期 (YYYY-MM-DD)")
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
- add_button.click(add_todo_item, [date, time, title, details, location], overview)
61
- action_button.click(handle_action, [action, index], overview)
 
 
62
 
63
- demo.load(update_view, inputs=None, outputs=overview)
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()