-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
279 lines (222 loc) · 8.8 KB
/
app.py
File metadata and controls
279 lines (222 loc) · 8.8 KB
1
2
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
"""
AI Smart Task Planner - Standalone Application
Run this file directly: python app.py
"""
import os
import json
import uuid
from datetime import datetime
from typing import List, Dict
import gradio as gr
from openai import OpenAI
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configuration
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
if not OPENAI_API_KEY:
print("⚠️ WARNING: OPENAI_API_KEY not found in environment variables")
print("Please set it in .env file or as environment variable")
print("Example: export OPENAI_API_KEY='sk-proj-...'")
client = OpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
class SmartTaskPlanner:
"""AI-powered task planner with LLM reasoning"""
def __init__(self):
self.client = client
self.database = []
self.task_history = []
def generate_plan(self, goal: str, timeframe: str = "2 weeks",
additional_context: str = "") -> Dict:
"""Generate task plan using LLM reasoning"""
if not self.client:
return self._generate_fallback_plan(goal, timeframe)
try:
tasks = self._generate_tasks_with_llm(goal, timeframe, additional_context)
plan = {
"id": str(uuid.uuid4())[:8],
"goal": goal,
"timeframe": timeframe,
"tasks": tasks,
"created_at": datetime.now().isoformat(),
"total_tasks": len(tasks),
"estimated_total_time": self._calculate_total_time(tasks)
}
self.database.append(plan)
self.task_history.append(plan)
return plan
except Exception as e:
print(f"Error: {str(e)}")
return self._generate_fallback_plan(goal, timeframe)
def _generate_tasks_with_llm(self, goal: str, timeframe: str, context: str) -> List[Dict]:
"""Core LLM reasoning"""
system_prompt = """You are an expert project manager. Create comprehensive task breakdowns with dependencies, timelines, and risk assessment."""
user_prompt = f"""Break down this goal into 6-12 actionable tasks:
GOAL: {goal}
TIMEFRAME: {timeframe}
{f"CONTEXT: {context}" if context else ""}
For each task provide:
- name: Clear task name
- description: What needs to be done
- duration: Time estimate
- dependencies: Prerequisites (or "None")
- priority: High/Medium/Low
- deliverables: Expected outputs
- risks: Potential issues
Return as JSON array."""
response = self.client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
temperature=0.7,
max_tokens=3000
)
result = response.choices[0].message.content
# Parse JSON
if "```json" in result:
result = result.split("```json")[1].split("```")[0].strip()
elif "```" in result:
result = result.split("```")[1].split("```")[0].strip()
tasks = json.loads(result)
if isinstance(tasks, dict) and "tasks" in tasks:
tasks = tasks["tasks"]
return self._validate_tasks(tasks)
def _validate_tasks(self, tasks: List[Dict]) -> List[Dict]:
"""Validate task structure"""
validated = []
for i, task in enumerate(tasks, 1):
validated_task = {
"id": i,
"name": task.get("name", f"Task {i}"),
"description": task.get("description", "No description"),
"duration": task.get("duration", "1 day"),
"dependencies": task.get("dependencies", "None"),
"priority": task.get("priority", "Medium"),
"deliverables": task.get("deliverables", "Task completion"),
"risks": task.get("risks", "None identified"),
"status": "pending"
}
validated.append(validated_task)
return validated
def _calculate_total_time(self, tasks: List[Dict]) -> str:
"""Calculate total project time"""
total_hours = 0
for task in tasks:
duration = task.get("duration", "1 day").lower()
if "hour" in duration:
hours = int(''.join(filter(str.isdigit, duration.split()[0])))
total_hours += hours
elif "day" in duration:
days = int(''.join(filter(str.isdigit, duration.split()[0])))
total_hours += days * 8
elif "week" in duration:
weeks = int(''.join(filter(str.isdigit, duration.split()[0])))
total_hours += weeks * 40
if total_hours < 8:
return f"{total_hours} hours"
elif total_hours < 40:
return f"{total_hours // 8} days"
else:
return f"{total_hours // 40} weeks"
def _generate_fallback_plan(self, goal: str, timeframe: str) -> Dict:
"""Fallback when AI unavailable"""
tasks = [
{
"id": 1,
"name": "Planning & Research",
"description": f"Research and plan: {goal}",
"duration": "2 days",
"dependencies": "None",
"priority": "High",
"deliverables": "Project plan",
"risks": "Insufficient info",
"status": "pending"
},
{
"id": 2,
"name": "Setup",
"description": "Environment and tools setup",
"duration": "1 day",
"dependencies": "Planning & Research",
"priority": "High",
"deliverables": "Ready environment",
"risks": "Technical issues",
"status": "pending"
},
{
"id": 3,
"name": "Implementation",
"description": "Core development work",
"duration": "1 week",
"dependencies": "Setup",
"priority": "High",
"deliverables": "Working prototype",
"risks": "Complexity",
"status": "pending"
}
]
return {
"id": str(uuid.uuid4())[:8],
"goal": goal,
"timeframe": timeframe,
"tasks": tasks,
"created_at": datetime.now().isoformat(),
"total_tasks": len(tasks),
"estimated_total_time": timeframe
}
def format_plan_output(self, plan: Dict) -> str:
"""Format for display"""
output = f"""# 🎯 Task Plan
**ID**: {plan['id']}
**Goal**: {plan['goal']}
**Timeframe**: {plan['timeframe']}
**Tasks**: {plan['total_tasks']}
---
"""
for task in plan['tasks']:
priority_icon = {"High": "🔴", "Medium": "🟡", "Low": "🟢"}.get(task['priority'], "⚪")
output += f"""## {task['id']}. {task['name']} {priority_icon}
{task['description']}
⏱️ **Duration**: {task['duration']}
🔗 **Dependencies**: {task['dependencies']}
📦 **Deliverables**: {task['deliverables']}
⚠️ **Risks**: {task['risks']}
---
"""
return output
# Initialize
planner = SmartTaskPlanner()
# Gradio functions
def generate_task_plan(goal, timeframe, context):
if not goal.strip():
return "⚠️ Please enter a goal", "", ""
plan = planner.generate_plan(goal, timeframe, context)
formatted = planner.format_plan_output(plan)
json_export = json.dumps(plan, indent=2)
stats = f"**Plans Generated**: {len(planner.database)}"
return formatted, json_export, stats
# Create UI
with gr.Blocks(theme=gr.themes.Soft()) as app:
gr.Markdown("# 🤖 AI Smart Task Planner")
with gr.Row():
goal_input = gr.Textbox(label="Goal", lines=3)
timeframe_input = gr.Textbox(label="Timeframe", value="2 weeks")
context_input = gr.Textbox(label="Context (Optional)", lines=2)
generate_btn = gr.Button("Generate Plan", variant="primary")
with gr.Tabs():
with gr.Tab("Plan"):
plan_output = gr.Markdown()
with gr.Tab("JSON"):
json_output = gr.Code(language="json")
with gr.Tab("Stats"):
stats_output = gr.Markdown()
generate_btn.click(
fn=generate_task_plan,
inputs=[goal_input, timeframe_input, context_input],
outputs=[plan_output, json_output, stats_output]
)
if __name__ == "__main__":
print("🚀 Starting AI Smart Task Planner...")
app.launch(share=True)