Spaces:
Sleeping
Sleeping
File size: 31,005 Bytes
3c5146b cebc3e5 3c5146b c88fc40 3c5146b c88fc40 60767c5 c88fc40 5af4cda c88fc40 60767c5 c88fc40 5af4cda c88fc40 5af4cda 149c0c9 5af4cda c88fc40 5af4cda c88fc40 5af4cda c88fc40 5af4cda c88fc40 5af4cda 60767c5 5af4cda c88fc40 5af4cda e4dedbc c88fc40 c137367 c88fc40 ca77345 e4dedbc 73a58e4 5af4cda c88fc40 5af4cda 3dc3729 5af4cda 3dc3729 cffdb58 3dc3729 cffdb58 3dc3729 5af4cda ca77345 5af4cda c88fc40 5af4cda c88fc40 5af4cda c88fc40 5af4cda c88fc40 3c5146b c88fc40 3c5146b c88fc40 3c5146b 43a0b8a 3c5146b cbbdc13 3c5146b 1f1b84b 3c5146b 1f1b84b 3c5146b 58301ee 3c5146b 1f1b84b 3c5146b 1f1b84b 3c5146b 123bba7 3c5146b 1f1b84b 3c5146b 1f1b84b 3c5146b 386a178 accbd2c 386a178 3c5146b 43a0b8a 3c5146b 8db3604 587f335 8db3604 3c5146b 43a0b8a 587f335 3c5146b 587f335 3c5146b 877473c 3c5146b 587f335 3c5146b 587f335 3c5146b be04e27 3c5146b be04e27 3c5146b 58301ee 3c5146b be04e27 3c5146b 3f39ce9 be04e27 3f39ce9 3c5146b 386a178 3c5146b 386a178 3c5146b 43a0b8a 3c5146b 386a178 3c5146b 386a178 3c5146b 43a0b8a 3c5146b be04e27 3c5146b cebc3e5 7701723 cebc3e5 7ba53b0 cebc3e5 3c5146b 73a58e4 3c5146b 73a58e4 3c5146b 73a58e4 3c5146b 73a58e4 3c5146b 232b3d8 60767c5 cebc3e5 7701723 149c0c9 7701723 3c5146b cebc3e5 60767c5 cebc3e5 3c5146b 73a58e4 | 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 | """ProjectCraft β AI-powered project management tool. Entry point."""
import asyncio, json, os
import gradio as gr
from agents import orchestrator
from views import gantt, raci, risks, heatmap
from core.planner import ProjectPlan
from export.xlsx_export import export_xlsx
# ---------------------------------------------------------------------------
# Color palette
# ---------------------------------------------------------------------------
COLORS = {
"bg": "#0a0a0a",
"surface": "#111111",
"surface_alt": "#181818",
"green": "#4ade80",
"green_accent": "#22c55e",
"silver": "#e8e8e8",
"silver_muted": "#a0a0a0",
"border": "#2a2a2a",
}
# ---------------------------------------------------------------------------
# Template loader
# ---------------------------------------------------------------------------
import functools
@functools.lru_cache(maxsize=None)
def _load_templates() -> dict:
"""Load all template JSON files from templates/ directory."""
templates = {}
templates_dir = os.path.join(os.path.dirname(__file__), "templates")
template_files = [
"software_launch.json",
"marketing_campaign.json",
"hr_onboarding.json",
"event_planning.json",
]
for filename in template_files:
filepath = os.path.join(templates_dir, filename)
try:
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
name = data.get(
"name",
filename.replace(".json", "").replace("_", " ").title(),
)
templates[name] = data
except FileNotFoundError:
pass
except (json.JSONDecodeError, Exception):
pass
return templates
# ---------------------------------------------------------------------------
# Initial state
# ---------------------------------------------------------------------------
INITIAL_STATE = {
"inputs": {
"objective": "", "start_date": "", "end_date": "",
"start_date_ai": False, "end_date_ai": False,
"tasks": "", "teams": [], "template": None,
"effective_start": "", "effective_end": ""
},
"plan": {
"phases": [], "raci": [], "risks": [], "workload": [],
"estimated_start_date": None, "estimated_end_date": None,
"timeline_note": None, "timeline_warning": None
},
"original_plan": None,
"chat_history": []
}
# ---------------------------------------------------------------------------
# CSS (adaptive theme: dark by default, light when OS prefers it)
# ---------------------------------------------------------------------------
CSS = """
/* === Design tokens: dark defaults === */
:root {
--pc-bg: #0a0a0a;
--pc-surface: #111111;
--pc-surface-alt: #181818;
--pc-text: #e8e8e8;
--pc-text-muted: #a0a0a0;
--pc-border: #4ade80;
--pc-green: #4ade80;
--pc-green-hover: #22c55e;
color-scheme: dark;
}
/* === Light mode overrides === */
@media (prefers-color-scheme: light) {
:root {
--pc-bg: #f5f5f5;
--pc-surface: #ffffff;
--pc-surface-alt: #ebebeb;
--pc-text: #111111;
--pc-text-muted: #555555;
--pc-border: #16a34a;
--pc-green: #16a34a;
--pc-green-hover: #15803d;
color-scheme: light;
}
}
/* === Base === */
body, .gradio-container {
background: var(--pc-bg) !important;
color: var(--pc-text) !important;
}
/* === All text / labels (Gradio uses many class combos) === */
label,
.label-wrap,
.label-wrap span,
.block > label,
.block > label > span,
.svelte-1ipelgc,
span.svelte-1ipelgc,
.gradio-container p,
.gradio-container li,
.gradio-container span,
.gradio-container h1,
.gradio-container h2,
.gradio-container h3,
.prose, .prose p,
.markdown, .markdown p,
.chatbot .message,
.chatbot .message p {
color: var(--pc-text) !important;
}
.prose h1, .markdown h1 {
color: var(--pc-green) !important;
font-size: 1.8rem !important;
font-weight: 800 !important;
letter-spacing: -0.5px;
}
.prose h2, .markdown h2 {
color: var(--pc-green) !important;
font-size: 1.25rem !important;
font-weight: 700 !important;
}
.prose h3, .markdown h3 {
color: var(--pc-green) !important;
font-size: 1.05rem !important;
font-weight: 700 !important;
}
/* === Inputs === */
textarea,
input[type="text"], input[type="number"],
input[type="email"], input[type="search"] {
background: var(--pc-surface) !important;
color: var(--pc-text) !important;
border-color: var(--pc-border) !important;
}
textarea::placeholder,
input::placeholder { color: var(--pc-text-muted) !important; }
select {
background: var(--pc-surface) !important;
color: var(--pc-text) !important;
border-color: var(--pc-border) !important;
}
input[type="checkbox"] { accent-color: var(--pc-green); }
/* === Buttons === */
button.primary, .primary {
background: var(--pc-green) !important;
color: #ffffff !important;
font-weight: 700;
}
button.primary:hover, .primary:hover {
background: var(--pc-green-hover) !important;
}
button:not(.primary) {
background: var(--pc-surface-alt) !important;
color: var(--pc-text) !important;
border-color: var(--pc-border) !important;
}
/* === Tabs === */
.tab-nav button, button[role="tab"] {
color: var(--pc-text) !important;
font-size: 15px !important;
font-weight: 600 !important;
}
.tab-nav button.selected, button[role="tab"][aria-selected="true"] {
color: var(--pc-green) !important;
border-color: var(--pc-green) !important;
background: var(--pc-surface-alt) !important;
}
/* === Plotly SVG text β axis labels and zoom buttons bright in dark mode === */
/* Horizontal bar charts (Gantt) render categorical y-axis labels as direct
<text> children of yaxislayer-above β NOT inside ytick groups. So [class*="tick"]
works for numeric/date x-axis but misses categorical y-axis labels.
[class*="axislayer"] and [class*="yaxis"] cover those containers.
:root prefix raises specificity to 0,3,1 to beat any conflicting Gradio rules. */
:root .js-plotly-plot [class*="tick"] text,
:root .js-plotly-plot [class*="tick"] tspan,
:root .js-plotly-plot [class*="axislayer"] text,
:root .js-plotly-plot [class*="axislayer"] tspan,
:root .js-plotly-plot [class*="yaxis"] text,
:root .js-plotly-plot [class*="yaxis"] tspan,
:root .js-plotly-plot .updatemenu-container text,
:root .js-plotly-plot .updatemenu-container tspan { fill: #e8e8e8 !important; }
/* === Plot containers === */
.pc-plot { min-height: 600px !important; }
/* === Blocks / panels === */
.block, .panel, .form {
background: var(--pc-surface) !important;
border-color: var(--pc-border) !important;
}
/* === Dropdown items === */
.options, .option-list, ul[role="listbox"] {
background: var(--pc-surface) !important;
border-color: var(--pc-border) !important;
}
ul[role="listbox"] li,
.option { color: var(--pc-text) !important; }
/* === Chatbot (Gradio 6) β correct DOM structure ===
In Gradio 6 the .chatbot class is on the INNER prose element inside each
message bubble, NOT on the outer container. That means ".chatbot .bot"
is backwards β .bot is an ANCESTOR of .chatbot, not a descendant.
Correct scoping: .message-wrap contains the .bot / .user bubble divs.
.bubble-wrap is the outer scrollable pane. */
:root .bubble-wrap {
background: var(--pc-surface) !important;
}
:root .message-wrap .bot {
background-color: var(--pc-surface-alt) !important;
border-color: var(--pc-border) !important;
}
:root .message-wrap .user {
background-color: rgba(74,222,128,0.13) !important;
border-color: rgba(74,222,128,0.4) !important;
}
:root .message-wrap .bot *,
:root .message-wrap .user * { color: var(--pc-text) !important; }
/* === Plotly charts β override SVG text in light mode === */
@media (prefers-color-scheme: light) {
:root .js-plotly-plot [class*="tick"] text,
:root .js-plotly-plot [class*="tick"] tspan,
:root .js-plotly-plot [class*="axislayer"] text,
:root .js-plotly-plot [class*="axislayer"] tspan,
:root .js-plotly-plot [class*="yaxis"] text,
:root .js-plotly-plot [class*="yaxis"] tspan,
:root .js-plotly-plot .updatemenu-container text,
:root .js-plotly-plot .updatemenu-container tspan { fill: #333333 !important; }
:root .js-plotly-plot .modebar-btn path { fill: #555555 !important; }
}
/* === Team cards === */
.team-card {
background: var(--pc-surface);
border: 1px solid var(--pc-border);
border-radius: 8px; padding: 10px; margin: 4px 0;
}
.teams-empty {
color: var(--pc-text-muted);
font-size: 13px; padding: 8px;
border: 1px dashed var(--pc-border);
border-radius: 6px; text-align: center;
}
/* === Banners === */
.date-ai-label { color: var(--pc-text-muted); font-size: 12px; font-style: italic; }
.banner-green {
background: rgba(74,222,128,0.1);
border: 1px solid var(--pc-green);
border-radius: 8px; padding: 10px 14px;
color: var(--pc-green); margin-bottom: 8px;
}
.banner-warning {
background: rgba(251,191,36,0.1);
border: 1px solid #fbbf24;
border-radius: 8px; padding: 10px 14px;
color: #b45309; margin-bottom: 8px;
}
@media (prefers-color-scheme: dark) {
.banner-warning { color: #fbbf24; }
}
/* === Loading skeleton === */
.skeleton {
background: linear-gradient(90deg,
var(--pc-surface-alt) 25%, var(--pc-surface) 50%, var(--pc-surface-alt) 75%);
background-size: 200% 100%;
animation: shimmer 1.5s infinite;
min-height: 300px; border-radius: 8px;
}
@keyframes shimmer {
0% { background-position: 200% 0; }
100% { background-position: -200% 0; }
}
"""
SKELETON_HTML = '<div class="skeleton"></div>'
# ---------------------------------------------------------------------------
# Helper: teams display HTML
# ---------------------------------------------------------------------------
def _teams_html(teams: list) -> str:
"""Render team cards as colored pills/badges."""
if not teams:
return '<div class="teams-empty">No teams added yet</div>'
parts = []
for team in teams:
color = team.get("color", "#4ade80")
name = team.get("name", "")
parts.append(
'<div class="team-card" style="display:flex; align-items:center; gap:8px;">'
f'<span style="display:inline-block; width:14px; height:14px; border-radius:50%; '
f'background:{color}; flex-shrink:0;"></span>'
f'<span style="font-size:14px;">{name}</span>'
"</div>"
)
return "\n".join(parts)
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Helper: extract team_name β color dict from state
# ---------------------------------------------------------------------------
def _state_team_colors(state: dict) -> dict[str, str]:
"""Return {team_name: color} from state inputs, for use in Gantt coloring."""
teams = state.get("inputs", {}).get("teams", [])
return {t["name"]: t.get("color", "#4ade80") for t in teams if isinstance(t, dict) and t.get("name")}
# ---------------------------------------------------------------------------
# Event handlers
# ---------------------------------------------------------------------------
def on_template_select(template_name, state):
"""Load template data and populate objective, tasks, teams in state."""
import copy
state = copy.deepcopy(state)
if not template_name or template_name == "\u2014 None \u2014":
state["inputs"]["template"] = None
return (
state["inputs"]["objective"],
state["inputs"]["tasks"],
_teams_html(state["inputs"]["teams"]),
gr.update(choices=[t["name"] for t in state["inputs"]["teams"]]),
state,
)
templates = _load_templates()
template = templates.get(template_name)
if template is None:
return (
state["inputs"]["objective"],
state["inputs"]["tasks"],
_teams_html(state["inputs"]["teams"]),
gr.update(choices=[t["name"] for t in state["inputs"]["teams"]]),
state,
)
state["inputs"]["template"] = template_name
state["inputs"]["objective"] = template.get("objective", "")
state["inputs"]["tasks"] = template.get("tasks", "")
state["inputs"]["teams"] = template.get("teams", [])
team_names = [t["name"] for t in state["inputs"]["teams"]]
return (
state["inputs"]["objective"],
state["inputs"]["tasks"],
_teams_html(state["inputs"]["teams"]),
gr.update(choices=team_names),
state,
)
def on_add_team(name, color, state):
"""Sanitize name, append team dict to state, refresh display."""
import copy
state = copy.deepcopy(state)
sanitized = name.strip() if name else ""
if not sanitized:
team_names = [t["name"] for t in state["inputs"]["teams"]]
return (
_teams_html(state["inputs"]["teams"]),
gr.update(choices=team_names),
"",
"#4ade80",
state,
)
team_id = sanitized.lower().replace(" ", "_")
existing_ids = {t.get("id") for t in state["inputs"]["teams"]}
if team_id not in existing_ids:
state["inputs"]["teams"].append(
{"id": team_id, "name": sanitized, "color": color or "#4ade80"}
)
team_names = [t["name"] for t in state["inputs"]["teams"]]
return (
_teams_html(state["inputs"]["teams"]),
gr.update(choices=team_names),
"",
"#4ade80",
state,
)
def on_remove_team(team_name, state):
"""Filter out team with matching name, refresh display."""
import copy
state = copy.deepcopy(state)
if team_name:
state["inputs"]["teams"] = [
t for t in state["inputs"]["teams"] if t["name"] != team_name
]
team_names = [t["name"] for t in state["inputs"]["teams"]]
return (
_teams_html(state["inputs"]["teams"]),
gr.update(choices=team_names, value=None),
state,
)
async def on_generate(objective, tasks, start_date, end_date, start_ai, end_ai, state):
"""Async generator: immediately yield skeletons, then generate and render the plan."""
import copy
print(f"[on_generate] CALLED β objective={objective!r:.60}")
# --- Step 1: Skeleton / loading feedback ---
# gr.Plot() only accepts figures or None β use None for plots during loading,
# skeleton HTML only for the gr.HTML risks output.
loading_html = SKELETON_HTML + '<div style="color:#a0a0a0;text-align:center;padding:8px;font-size:13px">Generating...</div>'
yield (
gr.update(value=None), # gantt_plot (gr.Plot β no strings)
gr.update(value=None), # raci_plot (gr.Plot β no strings)
gr.update(value=loading_html), # risks_html_out (gr.HTML β strings OK)
gr.update(value=None), # heatmap_plot (gr.Plot β no strings)
gr.update(value=""), # date_banner
gr.update(value=""), # warning_banner
gr.update(value='<div class="banner-green">Generating your project plan...</div>'),
state,
)
# --- Step 2: Update state inputs ---
state = copy.deepcopy(state)
state["inputs"]["objective"] = objective or ""
state["inputs"]["tasks"] = tasks or ""
state["inputs"]["start_date"] = start_date or ""
state["inputs"]["end_date"] = end_date or ""
state["inputs"]["start_date_ai"] = bool(start_ai)
state["inputs"]["end_date_ai"] = bool(end_ai)
# Validate
if not (objective or "").strip():
yield (
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(
value='<div class="banner-warning">'
"Please enter a project objective before generating."
"</div>"
),
state,
)
return
# --- Step 3: Call orchestrator ---
try:
inp = state["inputs"]
result = await orchestrator.generate(
objective=inp.get("objective", ""),
tasks=inp.get("tasks", ""),
teams=inp.get("teams", []),
start_date=inp.get("start_date", ""),
end_date=inp.get("end_date", ""),
start_ai=inp.get("start_date_ai", False),
end_ai=inp.get("end_date_ai", False),
)
except Exception as exc:
yield (
gr.update(value=None), # gantt_plot
gr.update(value=None), # raci_plot
gr.update(value='<div class="banner-warning">Generation failed.</div>'),
gr.update(value=None), # heatmap_plot
gr.update(value=""),
gr.update(value=""),
gr.update(value=f'<div class="banner-warning">Generation failed: {exc}</div>'),
state,
)
return
# --- Step 4: Handle orchestrator error (treat empty string as error too) ---
if isinstance(result, dict) and result.get("plan") is None:
yield (
gr.update(value=None), # gantt_plot
gr.update(value=None), # raci_plot
gr.update(value=f'<div class="banner-warning">{result["error"]}</div>'),
gr.update(value=None), # heatmap_plot
gr.update(value=""),
gr.update(value=""),
gr.update(value=f'<div class="banner-warning">{result["error"]}</div>'),
state,
)
return
# --- Step 5: Persist plan in state (always store as dict) ---
plan_raw = result.get("plan")
print(f"[on_generate] result.error={result.get('error')!r}")
print(f"[on_generate] plan_raw type={type(plan_raw).__name__}, "
f"phases={len(plan_raw.get('phases', [])) if isinstance(plan_raw, dict) else 'N/A'}")
plan_data = plan_raw or {}
if not isinstance(plan_data, dict):
plan_data = {}
state["plan"] = plan_data
state["original_plan"] = copy.deepcopy(plan_data)
_team_colors = _state_team_colors(state)
# --- Step 6: Build ProjectPlan and render charts ---
try:
project_plan = ProjectPlan.model_validate(plan_data)
print(f"[on_generate] ProjectPlan built: {len(project_plan.phases)} phases, "
f"{len(project_plan.raci)} raci, {len(project_plan.risks)} risks, "
f"{len(project_plan.workload)} workload teams")
except Exception as exc:
print(f"[on_generate] ProjectPlan.model_validate FAILED: {exc}")
project_plan = ProjectPlan()
try:
gantt_fig = gantt.render(project_plan, team_colors=_team_colors)
print(f"[on_generate] gantt rendered OK")
except Exception as exc:
print(f"[on_generate] gantt FAILED: {exc}")
gantt_fig = None
try:
raci_fig = raci.render(project_plan)
print(f"[on_generate] raci rendered OK")
except Exception as exc:
print(f"[on_generate] raci FAILED: {exc}")
raci_fig = None
try:
risks_html_content = risks.render(project_plan)
print(f"[on_generate] risks rendered OK β {len(project_plan.risks)} risks in plan")
except Exception as exc:
print(f"[on_generate] risks FAILED: {exc}")
risks_html_content = f'<div class="banner-warning">Could not render risks: {exc}</div>'
try:
heatmap_fig = heatmap.render(project_plan)
print(f"[on_generate] heatmap rendered OK")
except Exception as exc:
print(f"[on_generate] heatmap FAILED: {exc}")
heatmap_fig = None
# --- Step 7: Banners ---
date_banner_html = ""
if plan_data.get("timeline_note"):
date_banner_html = (
f'<div class="banner-green">{plan_data["timeline_note"]}</div>'
)
warning_banner_html = ""
if plan_data.get("timeline_warning"):
warning_banner_html = (
f'<div class="banner-warning">{plan_data["timeline_warning"]}</div>'
)
yield (
gr.update(value=gantt_fig),
gr.update(value=raci_fig),
gr.update(value=risks_html_content),
gr.update(value=heatmap_fig),
gr.update(value=date_banner_html),
gr.update(value=warning_banner_html),
gr.update(value='<div class="banner-green">Plan generated successfully!</div>'),
state,
)
async def on_chat_submit(message, chat_history, state):
"""Handle chat refinement: call orchestrator.refine and re-render all charts."""
import copy
if not message or not message.strip():
return chat_history, gr.update(), gr.update(), gr.update(), gr.update(), state
plan_data = state.get("plan", {})
chat_history = list(chat_history or [])
if not plan_data or not plan_data.get("phases"):
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": "No plan to refine yet. Please generate a plan first."})
return chat_history, gr.update(), gr.update(), gr.update(), gr.update(), state
try:
current_plan = ProjectPlan.model_validate(plan_data)
result = await orchestrator.refine(current_plan, message)
except Exception as exc:
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": f"Refinement failed: {exc}"})
return chat_history, gr.update(), gr.update(), gr.update(), gr.update(), state
if isinstance(result, dict) and result.get("error"):
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": str(result["error"])})
return chat_history, gr.update(), gr.update(), gr.update(), gr.update(), state
# Update state with refined plan (always store as dict)
state = copy.deepcopy(state)
new_plan = result.get("plan") or {}
if not isinstance(new_plan, dict):
new_plan = {}
state["plan"] = new_plan
_team_colors = _state_team_colors(state)
# Re-render charts
try:
project_plan = ProjectPlan.model_validate(new_plan)
except Exception:
project_plan = ProjectPlan()
try:
gantt_fig = gantt.render(project_plan, team_colors=_team_colors)
except Exception:
gantt_fig = None
try:
raci_fig = raci.render(project_plan)
except Exception:
raci_fig = None
try:
risks_html_content = risks.render(project_plan)
except Exception as exc:
risks_html_content = (
f'<div class="banner-warning">Could not render risks: {exc}</div>'
)
try:
heatmap_fig = heatmap.render(project_plan)
except Exception:
heatmap_fig = None
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": "Plan updated! All charts have been refreshed."})
return (
chat_history,
gr.update(value=gantt_fig),
gr.update(value=raci_fig),
gr.update(value=risks_html_content),
gr.update(value=heatmap_fig),
state,
)
# ---------------------------------------------------------------------------
# AI checkbox toggle helpers
# ---------------------------------------------------------------------------
def toggle_start_date(start_ai):
"""Gray out start_date_input when AI is handling it."""
return gr.update(interactive=not bool(start_ai))
def toggle_end_date(end_ai):
"""Gray out end_date_input when AI is handling it."""
return gr.update(interactive=not bool(end_ai))
async def on_export_xlsx(state):
"""Export Gantt image, RACI matrix, and Risk Register to a single XLSX workbook."""
plan_data = state.get("plan", {})
if not plan_data or not plan_data.get("phases"):
return None
try:
project_plan = ProjectPlan.model_validate(plan_data)
path = await export_xlsx(project_plan, filename="projectcraft_plan")
return path or None
except Exception as exc:
print(f"[export_xlsx] FAILED: {exc}")
return None
# ---------------------------------------------------------------------------
# Gradio UI layout
# ---------------------------------------------------------------------------
with gr.Blocks() as demo:
state = gr.State(value=INITIAL_STATE)
with gr.Row():
# ------------------------------------------------------------------ #
# Left column β Input panel (~35%)
# ------------------------------------------------------------------ #
with gr.Column(scale=35):
gr.Markdown("# \U0001f4ca ProjectCraft")
# Template dropdown
_template_names = list(_load_templates().keys())
_template_choices = ["\u2014 None \u2014"] + _template_names
template_dropdown = gr.Dropdown(
label="Template",
choices=_template_choices,
value="\u2014 None \u2014",
)
# Objective
objective_input = gr.Textbox(
label="Project Objective",
lines=4,
placeholder="Describe your project goal...",
)
# Tasks / Milestones
tasks_input = gr.Textbox(
label="Key Tasks / Milestones",
lines=3,
placeholder="List the main tasks...",
)
# Teams section
gr.Markdown("### \U0001f465 Teams")
teams_display = gr.HTML(value=_teams_html([]))
with gr.Row():
team_name_input = gr.Textbox(
label="Team Name",
placeholder="e.g. Engineering",
)
team_color_input = gr.ColorPicker(label="Color", value="#4ade80")
add_team_btn = gr.Button("+ Add Team", size="sm")
remove_team_dropdown = gr.Dropdown(
label="Remove team",
choices=[],
value=None,
)
remove_team_btn = gr.Button("Remove", size="sm")
# Timeline section
gr.Markdown("### \U0001f4c5 Timeline")
start_date_input = gr.Textbox(label="Start Date (YYYY-MM-DD)")
start_ai_checkbox = gr.Checkbox(label="Let AI decide start date")
end_date_input = gr.Textbox(label="End Date (YYYY-MM-DD)")
end_ai_checkbox = gr.Checkbox(label="Let AI decide end date")
# Generate
generate_btn = gr.Button("\U0001f680 Generate Plan", variant="primary")
status_html = gr.HTML("")
# ------------------------------------------------------------------ #
# Right column β Output panel (~65%)
# ------------------------------------------------------------------ #
with gr.Column(scale=65):
# Date / warning banners
date_banner = gr.HTML("")
warning_banner = gr.HTML("")
# Four output tabs
with gr.Tabs():
with gr.Tab("\U0001f4ca Gantt"):
gantt_plot = gr.Plot(visible=True, show_label=False, elem_classes=["pc-plot"])
with gr.Tab("\U0001f465 RACI"):
raci_plot = gr.Plot(visible=True, show_label=False, elem_classes=["pc-plot"])
with gr.Tab("\u26a0\ufe0f Risks"):
risks_html_out = gr.HTML(visible=True)
with gr.Tab("\U0001f525 Workload"):
heatmap_plot = gr.Plot(visible=True, show_label=False, elem_classes=["pc-plot"])
# Export
export_xlsx_btn = gr.DownloadButton("π₯ Export XLSX", size="sm")
# Chat refinement
gr.Markdown("### \U0001f4ac Refine Plan")
chatbot = gr.Chatbot(height=160, show_label=False)
chat_input = gr.Textbox(
label="",
placeholder="e.g. Add a testing phase after development",
)
chat_submit_btn = gr.Button("Send", variant="primary")
# ---------------------------------------------------------------------- #
# Event wiring
# ---------------------------------------------------------------------- #
# AI date toggles β gray out the text fields when checked
start_ai_checkbox.change(
fn=toggle_start_date,
inputs=[start_ai_checkbox],
outputs=[start_date_input],
)
end_ai_checkbox.change(
fn=toggle_end_date,
inputs=[end_ai_checkbox],
outputs=[end_date_input],
)
# Template selection β populate fields
template_dropdown.change(
fn=on_template_select,
inputs=[template_dropdown, state],
outputs=[
objective_input,
tasks_input,
teams_display,
remove_team_dropdown,
state,
],
)
# Add team
add_team_btn.click(
fn=on_add_team,
inputs=[team_name_input, team_color_input, state],
outputs=[
teams_display,
remove_team_dropdown,
team_name_input,
team_color_input,
state,
],
)
# Remove team
remove_team_btn.click(
fn=on_remove_team,
inputs=[remove_team_dropdown, state],
outputs=[teams_display, remove_team_dropdown, state],
)
# Generate plan β async generator (streaming with yield)
generate_btn.click(
fn=on_generate,
inputs=[
objective_input,
tasks_input,
start_date_input,
end_date_input,
start_ai_checkbox,
end_ai_checkbox,
state,
],
outputs=[
gantt_plot,
raci_plot,
risks_html_out,
heatmap_plot,
date_banner,
warning_banner,
status_html,
state,
],
)
# Chat refinement β wired to both button and Enter key
_chat_inputs = [chat_input, chatbot, state]
_chat_outputs = [chatbot, gantt_plot, raci_plot, risks_html_out, heatmap_plot, state]
chat_submit_btn.click(
fn=on_chat_submit,
inputs=_chat_inputs,
outputs=_chat_outputs,
)
chat_input.submit(
fn=on_chat_submit,
inputs=_chat_inputs,
outputs=_chat_outputs,
)
# XLSX export
export_xlsx_btn.click(
fn=on_export_xlsx,
inputs=[state],
outputs=[export_xlsx_btn],
)
# ---------------------------------------------------------------------------
# Launch
# ---------------------------------------------------------------------------
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860, css=CSS, ssr_mode=False)
|