Spaces:
Sleeping
Sleeping
Update planner.py
Browse files- 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 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def generate_task_plan(goal: str) -> str:
|
| 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 |
try:
|
| 42 |
response = client.chat.completions.create(
|
| 43 |
model="Qwen/Qwen2.5-Coder-7B-Instruct",
|
| 44 |
-
messages=
|
| 45 |
max_tokens=2048,
|
| 46 |
-
temperature=0.
|
| 47 |
-
top_p=0.
|
| 48 |
stream=False
|
| 49 |
)
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
-
raise Exception(f"
|
|
|
|
| 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)}")
|