Bhaskar2611 commited on
Commit
ee9409b
·
verified ·
1 Parent(s): fab914c

Update planner.py

Browse files
Files changed (1) hide show
  1. planner.py +59 -36
planner.py CHANGED
@@ -1,52 +1,75 @@
1
  # planner.py
2
  import os
 
3
  from huggingface_hub import InferenceClient
4
 
5
- # Get HF token from environment (set as Secret in HF Spaces)
6
  HF_TOKEN = os.getenv("HF_TOKEN")
 
7
 
8
- # Use the SAME model as your working chatbot
9
- client = InferenceClient(
10
- model="Qwen/Qwen2.5-Coder-7B-Instruct",
11
- token=HF_TOKEN
12
- )
 
 
 
 
 
13
 
14
  def generate_task_plan(goal: str) -> str:
15
- """
16
- Uses Qwen2.5-Coder (instruct model) via chat API to break down a goal into tasks.
17
- """
18
- messages = [
19
- {
20
- "role": "user",
21
- "content": (
22
- "You are a strict project planner. Break down the following goal into actionable tasks.\n\n"
23
- "RULES:\n"
24
- "- The project must be COMPLETE within the timeframe stated in the goal.\n"
25
- "- If the goal says 'in 2 weeks', you have EXACTLY 14 days — NO task can be due after Day 14.\n"
26
- "- All deadlines must be between Day 1 and Day 14 (inclusive).\n"
27
- "- Do NOT invent extra days. If the goal says 'in X days/weeks', convert it to total days and respect it.\n"
28
- "- Number tasks clearly (1., 2., 3., ...).\n"
29
- "- For each task, include: 'Due: Day N', 'Depends on: ...', and a short description.\n"
30
- "- Keep tasks realistic and parallelizable where possible.\n"
31
- "- NEVER output a task with 'Due: Day 15' or beyond if the limit is 14 days.\n\n"
32
- "Example (for a 5-day goal):\n"
33
- "1. Draft requirements - Due: Day 1 - Depends on: None\n"
34
- " Description: ...\n\n"
35
- f"GOAL TO PLAN: \"{goal}\"\n\n"
36
- "Now generate the plan. STRICTLY FOLLOW THE TIME LIMIT."
37
- )
38
- }
39
- ]
 
 
 
 
 
40
 
41
  try:
42
  response = client.chat.completions.create(
43
  model="Qwen/Qwen2.5-Coder-7B-Instruct",
44
- messages=messages,
45
  max_tokens=2048,
46
- temperature=0.3,
47
- top_p=0.95,
48
  stream=False
49
  )
50
- return response.choices[0].message.content.strip()
 
 
 
 
 
 
 
 
 
 
 
 
51
  except Exception as e:
52
- raise Exception(f"LLM inference failed: {str(e)}")
 
1
  # planner.py
2
  import os
3
+ import re
4
  from huggingface_hub import InferenceClient
5
 
 
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
+ client = InferenceClient(model="Qwen/Qwen2.5-Coder-7B-Instruct", token=HF_TOKEN)
8
 
9
+ def extract_total_days(goal: str) -> int:
10
+ """Extract number of days from goal like 'in 3 weeks' → 21"""
11
+ goal_lower = goal.lower()
12
+ # Match "X days", "X weeks", etc.
13
+ if m := re.search(r'(\d+)\s*days?', goal_lower):
14
+ return int(m.group(1))
15
+ if m := re.search(r'(\d+)\s*weeks?', goal_lower):
16
+ return int(m.group(1)) * 7
17
+ # Default to 14 if unclear
18
+ return 14
19
 
20
  def generate_task_plan(goal: str) -> str:
21
+ total_days = extract_total_days(goal)
22
+
23
+ prompt = f"""You are a professional project manager. Break down the goal into a STRICT task list.
24
+
25
+ GOAL: "{goal}"
26
+ TOTAL DAYS AVAILABLE: {total_days}
27
+
28
+ RULES (VIOLATING ANY = FAILURE):
29
+ 1. Output ONLY a numbered list of tasks (1., 2., 3., ...). NO introduction, NO summary, NO extra text.
30
+ 2. Every task must have:
31
+ - Task name
32
+ - "Due: Day N" where 1 N {total_days}
33
+ - "Depends on: [Task # or 'None']"
34
+ - "Description: [one-sentence explanation]"
35
+ 3. Distribute work across all {total_days} days do NOT cram everything into early days.
36
+ 4. Allow parallel work (e.g., marketing can start while dev is ongoing).
37
+ 5. Final launch and post-launch must happen ON or BEFORE Day {total_days}.
38
+ 6. NEVER say "Day 14" if total days is {total_days}. Use the correct max day.
39
+
40
+ FORMAT EXAMPLE (for 5-day goal):
41
+ 1. Draft requirements - Due: Day 1 - Depends on: None
42
+ Description: Define core features and user stories.
43
+ 2. Design UI - Due: Day 3 - Depends on: Task 1
44
+ Description: Create mockups for main screens.
45
+ 3. Develop backend - Due: Day 4 - Depends on: Task 1
46
+ Description: Build API and database.
47
+ 4. Launch - Due: Day 5 - Depends on: Tasks 2 and 3
48
+ Description: Deploy and announce product.
49
+
50
+ NOW GENERATE THE PLAN FOR {total_days} DAYS. OUTPUT ONLY THE NUMBERED LIST:"""
51
 
52
  try:
53
  response = client.chat.completions.create(
54
  model="Qwen/Qwen2.5-Coder-7B-Instruct",
55
+ messages=[{"role": "user", "content": prompt}],
56
  max_tokens=2048,
57
+ temperature=0.2, # Very low for precision
58
+ top_p=0.9,
59
  stream=False
60
  )
61
+ raw = response.choices[0].message.content.strip()
62
+
63
+ # Optional: Remove any leading/trailing prose (safety net)
64
+ lines = raw.split('\n')
65
+ cleaned = []
66
+ for line in lines:
67
+ if line.strip() and (line.strip()[0].isdigit() or line.strip().startswith(('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.'))):
68
+ cleaned.append(line)
69
+ elif cleaned: # Stop if we've started and hit non-task text
70
+ break
71
+
72
+ return '\n'.join(cleaned) if cleaned else raw
73
+
74
  except Exception as e:
75
+ raise Exception(f"Planning failed: {str(e)}")