Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,49 +10,49 @@ from Gradio_UI import GradioUI
|
|
10 |
|
11 |
@tool
|
12 |
def task_manager(action: str, task_data: str = "") -> str:
|
13 |
-
"""
|
14 |
|
15 |
Args:
|
16 |
-
action:
|
17 |
-
task_data:
|
18 |
|
19 |
Returns:
|
20 |
-
|
21 |
"""
|
22 |
import json
|
23 |
import os
|
24 |
from datetime import datetime
|
25 |
|
26 |
-
#
|
27 |
TASKS_FILE = "tasks.json"
|
28 |
|
29 |
-
#
|
30 |
if not os.path.exists(TASKS_FILE):
|
31 |
with open(TASKS_FILE, "w") as f:
|
32 |
json.dump({"tasks": []}, f)
|
33 |
|
34 |
-
#
|
35 |
with open(TASKS_FILE, "r") as f:
|
36 |
data = json.load(f)
|
37 |
|
38 |
-
#
|
39 |
tasks = data["tasks"]
|
40 |
|
41 |
-
#
|
42 |
if action == "help" or action == "":
|
43 |
-
return """
|
44 |
-
- add:
|
45 |
-
- list:
|
46 |
-
- complete:
|
47 |
-
- delete:
|
48 |
-
- help:
|
49 |
|
50 |
-
#
|
51 |
elif action == "add":
|
52 |
if not task_data:
|
53 |
-
return "
|
54 |
|
55 |
-
#
|
56 |
new_task = {
|
57 |
"id": len(tasks) + 1,
|
58 |
"description": task_data,
|
@@ -60,21 +60,21 @@ def task_manager(action: str, task_data: str = "") -> str:
|
|
60 |
"completed": False
|
61 |
}
|
62 |
|
63 |
-
#
|
64 |
tasks.append(new_task)
|
65 |
|
66 |
-
#
|
67 |
with open(TASKS_FILE, "w") as f:
|
68 |
json.dump(data, f, indent=2)
|
69 |
|
70 |
-
return f"
|
71 |
|
72 |
-
#
|
73 |
elif action == "list":
|
74 |
if not tasks:
|
75 |
-
return "
|
76 |
|
77 |
-
result = "
|
78 |
result += "-" * 50 + "\n"
|
79 |
|
80 |
for task in tasks:
|
@@ -83,51 +83,51 @@ def task_manager(action: str, task_data: str = "") -> str:
|
|
83 |
|
84 |
return result
|
85 |
|
86 |
-
#
|
87 |
elif action == "complete":
|
88 |
try:
|
89 |
task_id = int(task_data)
|
90 |
|
91 |
-
#
|
92 |
for task in tasks:
|
93 |
if task["id"] == task_id:
|
94 |
task["completed"] = True
|
95 |
|
96 |
-
#
|
97 |
with open(TASKS_FILE, "w") as f:
|
98 |
json.dump(data, f, indent=2)
|
99 |
|
100 |
-
return f"
|
101 |
|
102 |
-
return f"
|
103 |
|
104 |
except ValueError:
|
105 |
-
return "
|
106 |
|
107 |
-
#
|
108 |
elif action == "delete":
|
109 |
try:
|
110 |
task_id = int(task_data)
|
111 |
|
112 |
-
#
|
113 |
for i, task in enumerate(tasks):
|
114 |
if task["id"] == task_id:
|
115 |
-
#
|
116 |
deleted_task = tasks.pop(i)
|
117 |
|
118 |
-
#
|
119 |
with open(TASKS_FILE, "w") as f:
|
120 |
json.dump(data, f, indent=2)
|
121 |
|
122 |
-
return f"
|
123 |
|
124 |
-
return f"
|
125 |
|
126 |
except ValueError:
|
127 |
-
return "
|
128 |
|
129 |
else:
|
130 |
-
return f"
|
131 |
|
132 |
@tool
|
133 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
10 |
|
11 |
@tool
|
12 |
def task_manager(action: str, task_data: str = "") -> str:
|
13 |
+
"""A tool for managing daily tasks.
|
14 |
|
15 |
Args:
|
16 |
+
action: The operation to perform ("add", "list", "complete", "delete", or "help")
|
17 |
+
task_data: Additional information related to the task (task description, task ID, etc.)
|
18 |
|
19 |
Returns:
|
20 |
+
A text containing information about the result of the operation
|
21 |
"""
|
22 |
import json
|
23 |
import os
|
24 |
from datetime import datetime
|
25 |
|
26 |
+
# File to store task data
|
27 |
TASKS_FILE = "tasks.json"
|
28 |
|
29 |
+
# Create an empty task list if the file doesn't exist
|
30 |
if not os.path.exists(TASKS_FILE):
|
31 |
with open(TASKS_FILE, "w") as f:
|
32 |
json.dump({"tasks": []}, f)
|
33 |
|
34 |
+
# Load tasks
|
35 |
with open(TASKS_FILE, "r") as f:
|
36 |
data = json.load(f)
|
37 |
|
38 |
+
# Task list
|
39 |
tasks = data["tasks"]
|
40 |
|
41 |
+
# Help message
|
42 |
if action == "help" or action == "":
|
43 |
+
return """Task Manager Tool Usage:
|
44 |
+
- add: Add a new task (e.g.: task_manager("add", "Buy milk from the store"))
|
45 |
+
- list: List all tasks (e.g.: task_manager("list"))
|
46 |
+
- complete: Mark a task as completed (e.g.: task_manager("complete", "1"))
|
47 |
+
- delete: Delete a task (e.g.: task_manager("delete", "1"))
|
48 |
+
- help: Show this help message"""
|
49 |
|
50 |
+
# Add a new task
|
51 |
elif action == "add":
|
52 |
if not task_data:
|
53 |
+
return "Error: Task description cannot be empty!"
|
54 |
|
55 |
+
# Create a new task
|
56 |
new_task = {
|
57 |
"id": len(tasks) + 1,
|
58 |
"description": task_data,
|
|
|
60 |
"completed": False
|
61 |
}
|
62 |
|
63 |
+
# Add the task to the list
|
64 |
tasks.append(new_task)
|
65 |
|
66 |
+
# Save changes
|
67 |
with open(TASKS_FILE, "w") as f:
|
68 |
json.dump(data, f, indent=2)
|
69 |
|
70 |
+
return f"Task added: {task_data} (ID: {new_task['id']})"
|
71 |
|
72 |
+
# List tasks
|
73 |
elif action == "list":
|
74 |
if not tasks:
|
75 |
+
return "Your task list is empty."
|
76 |
|
77 |
+
result = "YOUR TASKS:\n"
|
78 |
result += "-" * 50 + "\n"
|
79 |
|
80 |
for task in tasks:
|
|
|
83 |
|
84 |
return result
|
85 |
|
86 |
+
# Mark task as completed
|
87 |
elif action == "complete":
|
88 |
try:
|
89 |
task_id = int(task_data)
|
90 |
|
91 |
+
# Find task by ID
|
92 |
for task in tasks:
|
93 |
if task["id"] == task_id:
|
94 |
task["completed"] = True
|
95 |
|
96 |
+
# Save changes
|
97 |
with open(TASKS_FILE, "w") as f:
|
98 |
json.dump(data, f, indent=2)
|
99 |
|
100 |
+
return f"Task completed: {task['description']}"
|
101 |
|
102 |
+
return f"Error: Task with ID {task_id} not found."
|
103 |
|
104 |
except ValueError:
|
105 |
+
return "Error: You must specify a valid task ID."
|
106 |
|
107 |
+
# Delete task
|
108 |
elif action == "delete":
|
109 |
try:
|
110 |
task_id = int(task_data)
|
111 |
|
112 |
+
# Find task by ID
|
113 |
for i, task in enumerate(tasks):
|
114 |
if task["id"] == task_id:
|
115 |
+
# Remove task from the list
|
116 |
deleted_task = tasks.pop(i)
|
117 |
|
118 |
+
# Save changes
|
119 |
with open(TASKS_FILE, "w") as f:
|
120 |
json.dump(data, f, indent=2)
|
121 |
|
122 |
+
return f"Task deleted: {deleted_task['description']}"
|
123 |
|
124 |
+
return f"Error: Task with ID {task_id} not found."
|
125 |
|
126 |
except ValueError:
|
127 |
+
return "Error: You must specify a valid task ID."
|
128 |
|
129 |
else:
|
130 |
+
return f"Error: Unknown action '{action}'. Valid actions: add, list, complete, delete, help"
|
131 |
|
132 |
@tool
|
133 |
def get_current_time_in_timezone(timezone: str) -> str:
|