""" Generates the modern high-resolution workflow flowchart for SID-AI. Saves to docs/images/workflow_flowsheet.jpg. """ import os import matplotlib.pyplot as plt import matplotlib.patches as patches from matplotlib.patches import FancyBboxPatch, FancyArrowPatch from PIL import Image def generate_flowsheet(): fig, ax = plt.subplots(figsize=(20.0, 11.25), dpi=120) bg_color = "#080E1A" fig.patch.set_facecolor(bg_color) ax.set_facecolor(bg_color) ax.set_xlim(0, 2000) ax.set_ylim(0, 1125) ax.axis('off') font_main = 'Segoe UI' # ── 1. Top Header Banner ────────────────────────────────────────────────── ax.text(1000, 1070, "SID-AI — MULTI-AGENT WORKFLOW ARCHITECTURE", fontsize=26, weight='bold', color='#F8FAFC', ha='center', va='center', fontfamily=font_main) ax.text(1000, 1032, "Ultra-Low Latency (~5.5s) Sequenced Perception | Dual-Anchor Universal Engineering Synthesis", fontsize=14.5, color='#38BDF8', ha='center', va='center', fontfamily=font_main) def draw_card(x, y, w, h, title, lines, badge=None, border_color="#38BDF8", bg="#131E32", corner=14, title_color="#F8FAFC", title_size=13, body_size=9.5): # Card shadow shadow = FancyBboxPatch((x+4, y-4), w, h, boxstyle=f"round,pad=0,rounding_size={corner}", facecolor="#020617", edgecolor="none", alpha=0.65, zorder=2) ax.add_patch(shadow) # Main card box = FancyBboxPatch((x, y), w, h, boxstyle=f"round,pad=0,rounding_size={corner}", facecolor=bg, edgecolor=border_color, linewidth=1.8, zorder=3) ax.add_patch(box) curr_y = y + h - 18 # Badge on top if badge: badge_w = min(w - 20, len(badge) * 7.5 + 24) bx = x + w/2 b_patch = FancyBboxPatch((bx - badge_w/2, curr_y - 9), badge_w, 18, boxstyle="round,pad=0,rounding_size=5", facecolor=border_color, edgecolor="none", alpha=0.22, zorder=4) ax.add_patch(b_patch) ax.text(bx, curr_y, badge, fontsize=8.5, weight='bold', color=border_color, ha='center', va='center', zorder=5, fontfamily=font_main) curr_y -= 24 # Title ax.text(x + 18, curr_y, title, fontsize=title_size, weight='bold', color=title_color, ha='left', va='center', zorder=5, fontfamily=font_main) curr_y -= 14 # Divider line if lines: ax.plot([x + 16, x + w - 16], [curr_y, curr_y], color=border_color, alpha=0.35, linewidth=1, zorder=4) curr_y -= 16 # Text body lines for line in lines: is_bullet = line.startswith("•") or line.startswith("-") line_color = "#E2E8F0" if is_bullet else "#94A3B8" ax.text(x + 18, curr_y, line, fontsize=body_size, color=line_color, ha='left', va='center', zorder=5, fontfamily=font_main) curr_y -= 16 def draw_arrow(x1, y1, x2, y2, color="#38BDF8", lw=2.2, curved=False, rad=0.2): if curved: arrow = FancyArrowPatch((x1, y1), (x2, y2), connectionstyle=f"arc3,rad={rad}", arrowstyle="-|>,head_length=7,head_width=4.5", color=color, linewidth=lw, zorder=6) else: arrow = FancyArrowPatch((x1, y1), (x2, y2), arrowstyle="-|>,head_length=7,head_width=4.5", color=color, linewidth=lw, zorder=6) ax.add_patch(arrow) # ── 2. Ingestion & Preprocessing Section (Left Column) ──────────────────── col1_x = 40 col1_w = 210 # Input Doc draw_card(col1_x, 780, col1_w, 105, "Engineering PDF", [ "Vector CAD / Scanned Blueprint", "(P&ID, SLD, Electrical, Earthing)" ], badge="INPUT DOCUMENT", border_color="#64748B", bg="#172236") # Ingestion Agent draw_card(col1_x, 575, col1_w, 155, "IngestionAgent", [ "• 150 DPI Smart Rasterize", "• Per-File Hash Caching", "• Title Block OCR & Metadata", "• Sub-Second Document Init" ], badge="STAGE 1 — INGEST (0.2s)", border_color="#06B6D4", bg="#0F1A2E") draw_arrow(col1_x + col1_w/2, 780, col1_x + col1_w/2, 730, color="#06B6D4") # Context Loader draw_card(col1_x, 375, col1_w, 150, "ContextLoader", [ "• ISA-5.1 Hierarchy Injection", "• Reference Legend Lookup", "• Project Spec Inheritance", "• Client Taxonomy Rules" ], badge="STAGE 2 — CONTEXT (0.01s)", border_color="#0284C7", bg="#0F1A2E") draw_arrow(col1_x + col1_w/2, 575, col1_x + col1_w/2, 525, color="#0284C7") # Supervisor Agent draw_card(col1_x, 180, col1_w, 145, "SupervisorAgent", [ "• Discipline Classifier", "• Drawing Sheet Boundary Map", "• Grid Coordinates Segmentation", "• Sequenced Dispatch" ], badge="STAGE 3 — SUPERVISOR (0.01s)", border_color="#3B82F6", bg="#0F1A2E") draw_arrow(col1_x + col1_w/2, 375, col1_x + col1_w/2, 325, color="#3B82F6") # ── 3. Central Sequenced Perception Engine ───────────────────────────────── percep_x = 300 percep_w = 460 percep_y = 125 percep_h = 860 percep_container = FancyBboxPatch((percep_x, percep_y), percep_w, percep_h, boxstyle="round,pad=0,rounding_size=20", facecolor="#0B1526", edgecolor="#38BDF8", linewidth=1.8, linestyle="--", alpha=0.95, zorder=2) ax.add_patch(percep_container) ax.text(percep_x + percep_w/2, percep_y + percep_h - 22, "SEQUENCED PERCEPTION ENGINE", fontsize=15, weight='bold', color="#38BDF8", ha='center', va='center', fontfamily=font_main, zorder=5) ax.text(percep_x + percep_w/2, percep_y + percep_h - 42, "Strict Dependency Sequence — Eliminates Duplicate EasyOCR Passes (~3.2s Total)", fontsize=9.5, color="#94A3B8", ha='center', va='center', fontfamily=font_main, zorder=5) card_w = percep_w - 40 cx = percep_x + 20 # Sub-Agent 1: Text Recognition draw_card(cx, 690, card_w, 205, "TextRecognitionAgent", [ "• Layer 1: PyMuPDF Vector Text (0.14s) + 2-Way EasyOCR Fallback", "• Layer 2: Rule-Based Classifier / Multi-Modal VLM Reasoning", "• Spatial Tag Stitcher: Merges split multiline tags (8\"-PV-FC11S)", "• OCR Typo Rectifier: Fixes zero decimals & character confusion", "• Enriched Datasheet Block Injection for Vessels & Pumps" ], badge="PERCEPTION 1 — TEXT & TAG EXTRACTION (3.0s)", border_color="#06B6D4", bg="#13233D", title_size=13.5, body_size=9.5) # Sub-Agent 2: Symbol Recognition draw_card(cx, 420, card_w, 210, "SymbolRecognitionAgent", [ "• Consumes upstream verified Text Elements directly from state", "• Tag-Correlated Symbol Harvester (Valves, Transmitters, Motors)", "• Deep Graphic CAD Symbol Detector (ISA-5.1 Morphological)", "• Auto-harvests untagged in-line manual & control valves", "• Zero-delay symbol execution (harvests pre-extracted tokens)" ], badge="PERCEPTION 2 — SYMBOL DETECTION (0.01s)", border_color="#8B5CF6", bg="#13233D", title_size=13.5, body_size=9.5) # Sub-Agent 3: Pipeline Recognition draw_card(cx, 150, card_w, 210, "PipelineRecognitionAgent", [ "• Consumes pre-extracted Text Elements & Symbol Bounding Boxes", "• OpenCV Morphological Hough Transform Process Line Tracing", "• 6-Tier Topological Snapping (Valves INSTALLED_ON Line Runs)", "• Flare & Drain Sink Routing (RELIEVES_TO / DRAINS_TO headers)", "• Zero fallback OCR execution (pure CV line tracing in 0.14s)" ], badge="PERCEPTION 3 — TOPOLOGY & LINE TRACING (0.14s)", border_color="#10B981", bg="#13233D", title_size=13.5, body_size=9.5) # Supervisor -> Text arrow draw_arrow(col1_x + col1_w, 252, percep_x - 15, 252, color="#3B82F6") draw_arrow(percep_x - 15, 252, percep_x - 15, 792, color="#3B82F6") draw_arrow(percep_x - 15, 792, cx, 792, color="#3B82F6") # Sequenced Inter-stage Arrows inside Perception Engine draw_arrow(percep_x + percep_w/2, 690, percep_x + percep_w/2, 630, color="#06B6D4", lw=2.8) ax.text(percep_x + percep_w/2 + 10, 660, "Feeds Verified Tags", fontsize=9, color="#06B6D4", fontfamily=font_main, weight='bold', va='center') draw_arrow(percep_x + percep_w/2, 420, percep_x + percep_w/2, 360, color="#8B5CF6", lw=2.8) ax.text(percep_x + percep_w/2 + 10, 390, "Feeds Tags + Bounding Boxes", fontsize=9, color="#8B5CF6", fontfamily=font_main, weight='bold', va='center') # ── 4. Compiler, Validation & Feedback (Right Center Column) ─────────────── col3_x = 810 col3_w = 400 # Compiler Agent draw_card(col3_x, 560, col3_w, 245, "CompilerAgent", [ "• Dual-Anchor Universal Engineering Object Synthesis", "• Merges text-anchored and symbol-anchored components", "• Automatically maps ASME rating classes (150#, 300#, 2500#)", "• Preserves driver sub-trains (-M01 motors, -C01 coolers, skids)", "• Enforces provenance registry & contamination edge filtering", "• Generates consolidated UniversalEngineeringGraph" ], badge="STAGE 4 — OBJECT COMPILER (0.19s)", border_color="#10B981", bg="#0F1A2E", title_size=14, body_size=9.8) # PipelineRecognition -> Compiler arrow draw_arrow(cx + card_w, 255, col3_x - 20, 255, color="#10B981") draw_arrow(col3_x - 20, 255, col3_x - 20, 682, color="#10B981") draw_arrow(col3_x - 20, 682, col3_x, 682, color="#10B981") # Validation & Completeness Joint Check Node draw_card(col3_x, 340, col3_w, 175, "Validation & Completeness Engine", [ "• ValidationAgent: ISA-5.1 Rule Checks (VAL-001..VAL-006)", "• Enforces zero orphan valves & instruments (VAL-004 Clean)", "• CompletenessAgent: Inspects graph connectivity integrity", "• Identifies missing tags / coordinates for focused review" ], badge="STAGES 5 & 6 — QUALITY GATES (0.02s)", border_color="#0284C7", bg="#0F1A2E", title_size=13.5, body_size=9.8) draw_arrow(col3_x + col3_w/2, 560, col3_x + col3_w/2, 515, color="#0284C7") # Focused Re-Extractor Loop draw_card(col3_x, 140, col3_w, 155, "ReExtractorAgent", [ "• Instant Spatial Vector PDF Coordinate Clip (<0.2s)", "• Queries PyMuPDF directly for missing target zones", "• Resolves obscured check valves without heavy OCR", "• Loops newly validated entities back into Compiler" ], badge="FEEDBACK LOOP — FOCUSED RE-EXTRACTION (0.22s)", border_color="#F59E0B", bg="#1E293B", title_size=13, body_size=9.5) # Validation -> Re-Extractor Arrow (Conditional) draw_arrow(col3_x + col3_w/2, 340, col3_x + col3_w/2, 295, color="#F59E0B", lw=2.2) ax.text(col3_x + col3_w/2 + 10, 318, "Orphan Targets Flagged", fontsize=8.5, color="#F59E0B", fontfamily=font_main, weight='bold') # Re-Extractor -> Compiler Loop Arrow draw_arrow(col3_x + 15, 217, col3_x - 35, 217, color="#F59E0B", lw=2.2) draw_arrow(col3_x - 35, 217, col3_x - 35, 620, color="#F59E0B", lw=2.2) draw_arrow(col3_x - 35, 620, col3_x, 620, color="#F59E0B", lw=2.2) ax.text(col3_x - 30, 430, "Re-extracted Entities Merged", fontsize=8.5, color="#F59E0B", fontfamily=font_main, weight='bold', rotation=90, va='center') # ── 5. Output Deliverables Generator & Formats (Right Column) ───────────── col4_x = 1260 col4_w = 330 # Output Generator draw_card(col4_x, 560, col4_w, 245, "OutputGeneratorAgent", [ "• Serializes Master Universal Engineering Graph", "• Relational mapping for multi-discipline platforms", "• Preserves topological evidence & confidence vectors", "• Multi-tab Excel workbook generation & styling", "• Plant taxonomy schema formatting (AVEVA, COMOS)" ], badge="STAGE 7 — DELIVERABLES EXPORT (1.53s)", border_color="#EC4899", bg="#0F1A2E", title_size=14, body_size=9.8) # Completeness -> OutputGenerator (When clean) draw_arrow(col3_x + col3_w, 427, col4_x - 25, 427, color="#EC4899", lw=2.5) draw_arrow(col4_x - 25, 427, col4_x - 25, 682, color="#EC4899", lw=2.5) draw_arrow(col4_x - 25, 682, col4_x, 682, color="#EC4899", lw=2.5) ax.text(col4_x - 20, 550, "Graph Verified Clean", fontsize=8.5, color="#EC4899", fontfamily=font_main, weight='bold', rotation=90, va='center') # Output Deliverable Cards (Far Right) deliv_x = 1640 deliv_w = 320 deliv_h = 60 delivs = [ ("Multi-Tab Styled Excel", ".xlsx Line, Valve, Instrument & Equipment Schedules", "#10B981", 805), ("AVEVA Diagrams / SP3D", ".xml Hierarchical Process Graph Topology", "#3B82F6", 725), ("Siemens COMOS", ".json Standardized Plant Object Hierarchy", "#06B6D4", 645), ("Intergraph SmartPlant", ".csv Relational SPPID Component Tables", "#8B5CF6", 565), ("Process Relationships", ".csv Directional Connectivity & Stream Graph", "#F59E0B", 485), ("Master Knowledge Graph", ".json Universal Engineering Graph Object", "#EC4899", 405), ] for title, desc, clr, dy in delivs: draw_card(deliv_x, dy, deliv_w, deliv_h, title, [desc], border_color=clr, bg="#131E32", corner=10, title_size=11, body_size=8.5) draw_arrow(col4_x + col4_w, 682, deliv_x, dy + deliv_h/2, color=clr, lw=1.6) # ── 6. Bottom Metrics & Highlights Bar ──────────────────────────────────── bar_y = 35 bar_w = 1920 bar_h = 65 b_patch = FancyBboxPatch((40, bar_y), bar_w, bar_h, boxstyle="round,pad=0,rounding_size=12", facecolor="#040914", edgecolor="#334155", linewidth=1.5, zorder=2) ax.add_patch(b_patch) badges = [ ("TOTAL RUNTIME", "5.55 Seconds (96.7% Latency Reduction)", "#10B981"), ("PERCEPTION ARCHITECTURE", "Sequenced Zero-Duplicate OCR Pipeline", "#06B6D4"), ("INDUSTRIAL PRECISION", ">98% Accuracy on Offshore Blueprints", "#3B82F6"), ("TOPOLOGICAL PURITY", "0 Orphan Warnings (VAL-004 Clean)", "#8B5CF6"), ("ENTERPRISE DELIVERABLES", "6 Standard Plant Engineering Formats", "#F59E0B"), ] seg_w = bar_w / len(badges) for i, (k, v, c) in enumerate(badges): bx = 40 + i * seg_w + seg_w / 2 ax.text(bx, bar_y + 40, k, fontsize=9.5, weight='bold', color=c, ha='center', va='center', fontfamily=font_main, zorder=5) ax.text(bx, bar_y + 18, v, fontsize=10.5, weight='bold', color="#F8FAFC", ha='center', va='center', fontfamily=font_main, zorder=5) if i > 0: ax.plot([40 + i * seg_w, 40 + i * seg_w], [bar_y + 10, bar_y + 55], color="#334155", linewidth=1, zorder=4) plt.subplots_adjust(left=0, right=1, top=1, bottom=0) png_path = "docs/images/workflow_flowsheet.png" jpg_path = "docs/images/workflow_flowsheet.jpg" plt.savefig(png_path, dpi=120, facecolor=bg_color, bbox_inches='tight', pad_inches=0.1) plt.close() # Convert to high-quality JPEG im = Image.open(png_path).convert("RGB") im.save(jpg_path, "JPEG", quality=95, optimize=True) if os.path.exists(png_path): os.remove(png_path) print(f"Workflow flowsheet saved successfully to '{jpg_path}' ({im.size[0]}x{im.size[1]})") if __name__ == "__main__": generate_flowsheet()