{"slug": "building-agentic-document-intelligence-pipelines-creating-scientific-figures", "title": "Building Agentic Document Intelligence Pipelines: Creating Scientific Figures with AutoFigure", "summary": "AutoFigure, a toolkit from ResearAI, generates publication-style scientific figures from text descriptions and paper-like content, as demonstrated in a tutorial that builds a pipeline diagram for an agentic long-document intelligence system. The tutorial sets up the environment, fixes dependency issues like Pillow compatibility, and uses AutoFigure to convert a detailed pipeline description into a clean 16:9 SVG figure, with support for API-backed generation and offline rendering. The toolkit aims to streamline the creation of scientific diagrams for finance and enterprise document intelligence audiences.", "body_md": "In this tutorial, we explore [ AutoFigure](https://github.com/ResearAI/AutoFigure) as a practical toolkit for generating scientific figures directly from text descriptions, paper-like content, and structured methodological explanations. In this tutorial, we set up the complete AutoFigure environment, fix dependency issues such as Pillow compatibility, and prepare the required rendering tools for SVG and PNG outputs. We then build a custom reference figure, configure an API-backed generation workflow, and use AutoFigure to convert a detailed agentic document intelligence pipeline into a publication-style scientific diagram. Along the way, we also test offline SVG rendering, inspect the generated files, create a sample paper and PDF, and export the final outputs to a reusable gallery and a zip archive.\n\n``` python\nimport os\nimport sys\nimport json\nimport time\nimport glob\nimport shutil\nimport textwrap\nimport subprocess\nimport importlib\nfrom pathlib import Path\nfrom getpass import getpass\nREPO_URL = \"https://github.com/ResearAI/AutoFigure.git\"\nREPO_DIR = Path(\"/content/AutoFigure\")\nOUTPUT_ROOT = Path(\"/content/autofigure_colab_outputs\")\nPROVIDER = os.environ.get(\"AUTOFIGURE_PROVIDER\", \"openrouter\")\nDEFAULT_MODELS = {\n   \"openrouter\": \"google/gemini-3.1-pro-preview\",\n   \"gemini\": \"gemini-3.1-pro-preview\",\n   \"bianxie\": \"gemini-3.1-pro-preview\",\n}\nGENERATION_MODEL = os.environ.get(\n   \"AUTOFIGURE_MODEL\",\n   DEFAULT_MODELS.get(PROVIDER, \"google/gemini-3.1-pro-preview\")\n)\nMAX_ITERATIONS = int(os.environ.get(\"AUTOFIGURE_MAX_ITERATIONS\", \"1\"))\nQUALITY_THRESHOLD = float(os.environ.get(\"AUTOFIGURE_QUALITY_THRESHOLD\", \"8.5\"))\nRUN_TEXT_TO_FIGURE = True\nRUN_PAPER_TO_FIGURE = False\nRUN_MXGRAPH_DEMO = False\nRUN_IMAGE_ENHANCEMENT = False\nTEXT_OUTPUT_FORMAT = \"svg\"\nMXGRAPH_OUTPUT_FORMAT = \"mxgraphxml\"\nART_STYLE = (\n   \"clean publication-ready scientific illustration, precise alignment, subtle shadows, \"\n   \"clear academic typography, high contrast, minimal clutter\"\n)\nFIGURE_DESCRIPTION = \"\"\"\nCreate a publication-ready scientific method figure for an agentic long-document intelligence system.\nThe figure should explain the following pipeline in a left-to-right architecture:\n1. Long documents enter the system. They may be PDFs, scanned reports, markdown files, tables, or mixed-layout documents.\n2. A document normalization layer extracts raw text, section hierarchy, tables, figures, and metadata.\n3. A routing planner decides whether each section should go to summarization, field extraction, table reconstruction, visual analysis, or citation grounding.\n4. Specialized expert modules process the routed chunks:\n  - Summarizer expert creates hierarchical summaries.\n  - Extraction expert returns JSON fields.\n  - Table expert reconstructs exact tables.\n  - Visual expert describes charts and diagrams.\n  - Citation expert links claims to evidence spans.\n5. A low-cost orchestration layer selects smaller or larger LLMs depending on complexity, confidence, and budget.\n6. A verification layer checks schema validity, source grounding, table consistency, and confidence.\n7. The final output is an analyst-ready workspace containing a summary, extracted fields, exact tables, cited answers, and audit logs.\nDesign requirements:\n- Use a wide 16:9 layout.\n- Use clear module boxes, arrows, and labels.\n- Add small callouts for cost control, confidence scoring, and auditability.\n- Avoid decorative clutter.\n- Make the flow understandable for a finance or enterprise document intelligence audience.\n\"\"\"\nMINI_PAPER_MARKDOWN = \"\"\"\n# Efficient Agentic Document Intelligence for Long Financial Reports\n## Abstract\nWe propose an agentic document intelligence architecture for extracting summaries, facts, tables,\nand grounded answers from long, heterogeneous financial documents.\n## Method\nOur method first normalizes each incoming document into a structured document graph. The graph\ncontains section nodes, paragraph nodes, table nodes, figure nodes, and metadata nodes. A routing\nplanner assigns each node to a specialized expert according to modality, complexity, and required\noutput schema.\nThe system uses five experts. The summarization expert produces hierarchical summaries from\nsection-level chunks. The extraction expert fills strict JSON schemas for entities, dates, risks,\nfinancial metrics, and obligations. The table expert reconstructs exact tables and validates row-column\nalignment. The visual expert describes charts and diagrams. The citation expert maps every generated\nclaim to source spans.\nA budget-aware orchestration layer selects model size dynamically. Simple chunks are processed by\nlow-cost models, while complex chunks are escalated to stronger models. A verification layer then\nchecks schema validity, citation support, numerical consistency, and table integrity. Failed checks are\nrouted back for repair.\n## Experiments\nWe evaluate on financial filings and analyst reports using extraction accuracy, grounding precision,\ntable reconstruction quality, and total inference cost.\n\"\"\"\ndef run(cmd, cwd=None, check=True, quiet=False):\n   print(f\"\\n$ {cmd}\")\n   process = subprocess.run(\n       cmd,\n       shell=True,\n       cwd=str(cwd) if cwd else None,\n       text=True,\n       stdout=subprocess.PIPE if quiet else None,\n       stderr=subprocess.STDOUT if quiet else None,\n   )\n   if quiet and process.stdout:\n       print(process.stdout[-5000:])\n   if check and process.returncode != 0:\n       raise RuntimeError(f\"Command failed with exit code {process.returncode}: {cmd}\")\n   return process\ndef heading(title):\n   print(\"\\n\" + \"=\" * 100)\n   print(title)\n   print(\"=\" * 100)\ndef safe_read(path, max_chars=2500):\n   path = Path(path)\n   if not path.exists():\n       return \"\"\n   text = path.read_text(encoding=\"utf-8\", errors=\"ignore\")\n   return text[:max_chars] + (\"\\n... [truncated]\" if len(text) > max_chars else \"\")\ndef clear_loaded_modules(prefixes):\n   for name in list(sys.modules):\n       if any(name == prefix or name.startswith(prefix + \".\") for prefix in prefixes):\n           del sys.modules[name]\ndef get_colab_secret(names):\n   try:\n       from google.colab import userdata\n       for name in names:\n           try:\n               value = userdata.get(name)\n               if value:\n                   return value\n           except Exception:\n               pass\n   except Exception:\n       pass\n   return None\ndef collect_api_key(provider):\n   env_candidates = [\n       \"AUTOFIGURE_API_KEY\",\n       \"OPENROUTER_API_KEY\",\n       \"GOOGLE_API_KEY\",\n       \"GEMINI_API_KEY\",\n       \"BIANXIE_API_KEY\",\n   ]\n   for key_name in env_candidates:\n       value = os.environ.get(key_name)\n       if value:\n           print(f\"Using API key from environment variable: {key_name}\")\n           return value\n   secret_candidates = {\n       \"openrouter\": [\"AUTOFIGURE_API_KEY\", \"OPENROUTER_API_KEY\"],\n       \"gemini\": [\"AUTOFIGURE_API_KEY\", \"GOOGLE_API_KEY\", \"GEMINI_API_KEY\"],\n       \"bianxie\": [\"AUTOFIGURE_API_KEY\", \"BIANXIE_API_KEY\"],\n   }.get(provider, [\"AUTOFIGURE_API_KEY\"])\n   value = get_colab_secret(secret_candidates)\n   if value:\n       print(\"Using API key from Colab Secrets.\")\n       return value\n   value = getpass(f\"Paste your {provider} API key, or press Enter to skip cloud generation: \").strip()\n   return value\n```\n\nWe begin by importing and defining the main paths, provider settings, model configuration, and tutorial options. We also prepare the detailed figure description and sample paper content that we use later for AutoFigure generation. We then create helper functions to run commands, print section headings, read files safely, clear loaded modules, and securely collect API keys.\n\n``` python\ndef display_file_if_possible(path, title=None):\n   path = Path(path) if path else None\n   if not path or not path.exists():\n       print(f\"Missing file: {path}\")\n       return\n   try:\n       from IPython.display import display, Image as IPImage, SVG, Markdown\n       if title:\n           display(Markdown(f\"### {title}\"))\n       suffix = path.suffix.lower()\n       if suffix == \".png\":\n           display(IPImage(filename=str(path)))\n       elif suffix == \".svg\":\n           display(SVG(filename=str(path)))\n       elif suffix in [\".json\", \".md\", \".txt\", \".drawio\"]:\n           print(safe_read(path, max_chars=5000))\n       else:\n           print(path)\n   except Exception as exc:\n       print(f\"Could not display {path}: {exc}\")\ndef make_output_gallery(output_dir):\n   output_dir = Path(output_dir)\n   gallery_path = output_dir / \"gallery.html\"\n   blocks = []\n   for p in sorted(output_dir.rglob(\"*.png\")):\n       rel = p.relative_to(output_dir)\n       blocks.append(f\"\"\"\n       <div class=\"card\">\n         <h3>{rel}</h3>\n         <img src=\"{rel}\" />\n       </div>\n       \"\"\")\n   for p in sorted(output_dir.rglob(\"*.svg\")):\n       rel = p.relative_to(output_dir)\n       svg_text = p.read_text(encoding=\"utf-8\", errors=\"ignore\")\n       blocks.append(f\"\"\"\n       <div class=\"card\">\n         <h3>{rel}</h3>\n         <div class=\"svgbox\">{svg_text}</div>\n       </div>\n       \"\"\")\n   for p in sorted(output_dir.rglob(\"*.drawio\")):\n       rel = p.relative_to(output_dir)\n       code = p.read_text(encoding=\"utf-8\", errors=\"ignore\")[:4000]\n       blocks.append(f\"\"\"\n       <div class=\"card\">\n         <h3>{rel}</h3>\n         <p>Editable draw.io mxGraph XML file.</p>\n         <pre>{code}</pre>\n       </div>\n       \"\"\")\n   for p in sorted(output_dir.rglob(\"generation_report.json\")):\n       rel = p.relative_to(output_dir)\n       try:\n           report_text = json.dumps(json.loads(p.read_text(encoding=\"utf-8\")), indent=2)[:7000]\n       except Exception:\n           report_text = p.read_text(encoding=\"utf-8\", errors=\"ignore\")[:7000]\n       blocks.append(f\"\"\"\n       <div class=\"card\">\n         <h3>{rel}</h3>\n         <pre>{report_text}</pre>\n       </div>\n       \"\"\")\n   html = f\"\"\"\n   <!doctype html>\n   <html>\n   <head>\n     <meta charset=\"utf-8\">\n     <title>AutoFigure Colab Gallery</title>\n     <style>\n       body {{\n         font-family: Arial, sans-serif;\n         margin: 24px;\n         background: #f7f7f7;\n       }}\n       h1 {{\n         margin-bottom: 8px;\n       }}\n       .card {{\n         background: white;\n         padding: 18px;\n         margin: 18px 0;\n         border-radius: 14px;\n         box-shadow: 0 2px 16px rgba(0,0,0,0.08);\n       }}\n       img {{\n         max-width: 100%;\n         border: 1px solid #ddd;\n         border-radius: 10px;\n       }}\n       .svgbox {{\n         border: 1px solid #ddd;\n         border-radius: 10px;\n         padding: 8px;\n         overflow: auto;\n       }}\n       pre {{\n         white-space: pre-wrap;\n         word-break: break-word;\n         max-height: 520px;\n         overflow: auto;\n         background: #fafafa;\n         padding: 12px;\n         border-radius: 10px;\n       }}\n     </style>\n   </head>\n   <body>\n     <h1>AutoFigure Colab Gallery</h1>\n     {''.join(blocks)}\n   </body>\n   </html>\n   \"\"\"\n   gallery_path.write_text(html, encoding=\"utf-8\")\n   return gallery_path\ndef summarize_generation_result(result, label):\n   print(\"\\n\" + \"-\" * 100)\n   print(label)\n   print(\"-\" * 100)\n   print(f\"Success: {result.success}\")\n   print(f\"Final score: {result.final_score}\")\n   print(f\"Iterations used: {result.iterations_used}\")\n   print(f\"SVG path: {result.svg_path}\")\n   print(f\"mxGraph path: {result.mxgraph_path}\")\n   print(f\"Preview path: {result.preview_path}\")\n   print(f\"Enhanced path: {result.enhanced_path}\")\n   print(f\"Enhanced paths: {result.enhanced_paths}\")\n   print(f\"Error: {result.error}\")\n   if result.logs:\n       print(\"\\nRecent logs:\")\n       for log in result.logs[-20:]:\n           print(f\"- {log}\")\n   display_file_if_possible(result.preview_path, f\"{label}: PNG Preview\")\n   if result.svg_path:\n       display_file_if_possible(result.svg_path, f\"{label}: SVG\")\n   if result.mxgraph_path:\n       display_file_if_possible(result.mxgraph_path, f\"{label}: mxGraph XML\")\n   report_candidates = []\n   for candidate in [result.svg_path, result.mxgraph_path, result.preview_path]:\n       if candidate:\n           report_candidates.append(Path(candidate).parent / \"generation_report.json\")\n   for report_path in report_candidates:\n       if report_path.exists():\n           print(\"\\nGeneration report preview:\")\n           print(safe_read(report_path, max_chars=6000))\n           try:\n               import pandas as pd\n               from IPython.display import display\n               report = json.loads(report_path.read_text(encoding=\"utf-8\"))\n               rows = []\n               for row in report.get(\"iteration_history\", []):\n                   rows.append({\n                       \"iteration\": row.get(\"iteration\"),\n                       \"quality_score\": row.get(\"quality_score\"),\n                       \"improvement\": row.get(\"improvement\"),\n                       \"has_critique\": row.get(\"critique\") is not None,\n                   })\n               if rows:\n                   display(pd.DataFrame(rows))\n           except Exception as exc:\n               print(f\"Could not tabulate report: {exc}\")\n           break\n```\n\nWe define utility functions that help us display generated files directly inside Colab, including PNG, SVG, JSON, Markdown, text, and draw.io outputs. We also build an HTML gallery generator so that all AutoFigure outputs can be reviewed on a single, organized page. We then add a result-summary function that prints generation metadata, displays previews, and shows the iteration report in a readable format.\n\n```\nheading(\"1. Installing AutoFigure and Colab dependencies\")\nOUTPUT_ROOT.mkdir(parents=True, exist_ok=True)\nrun(\"apt-get update -qq\", quiet=True)\nrun(\n   \"apt-get install -y -qq \"\n   \"libcairo2 libpango-1.0-0 libpangocairo-1.0-0 \"\n   \"libgdk-pixbuf-2.0-0 libffi-dev shared-mime-info\",\n   quiet=True,\n)\nclear_loaded_modules([\"PIL\", \"autofigure\"])\nrun(f\"{sys.executable} -m pip install -q -U pip 'setuptools<82' wheel jedi\", quiet=True)\nrun(\n   f\"{sys.executable} -m pip install -q --force-reinstall --no-cache-dir \"\n   f\"'Pillow==11.3.0'\",\n   quiet=True,\n)\nif REPO_DIR.exists():\n   print(f\"Repository already exists at {REPO_DIR}. Pulling latest main branch.\")\n   run(\"git fetch origin main\", cwd=REPO_DIR, quiet=True)\n   run(\"git checkout main\", cwd=REPO_DIR, quiet=True)\n   run(\"git pull --ff-only origin main\", cwd=REPO_DIR, check=False, quiet=True)\nelse:\n   run(f\"git clone {REPO_URL} {REPO_DIR}\", quiet=True)\nrun(\n   f\"{sys.executable} -m pip install -q -e '.[pdf,web]' \"\n   f\"reportlab pandas 'Pillow==11.3.0'\",\n   cwd=REPO_DIR,\n   quiet=True,\n)\nrun(\n   f\"{sys.executable} -m pip install -q --force-reinstall --no-cache-dir \"\n   f\"'Pillow==11.3.0'\",\n   quiet=True,\n)\nclear_loaded_modules([\"PIL\", \"autofigure\"])\ntry:\n   from PIL import Image, ImageDraw, ImageFont\n   print(f\"Pillow imported successfully. Version: {Image.__version__}\")\nexcept Exception as exc:\n   print(\"Pillow import still failed after reinstall.\")\n   print(\"Run Runtime -> Restart runtime, then rerun this full cell.\")\n   raise exc\nif RUN_MXGRAPH_DEMO:\n   run(f\"{sys.executable} -m playwright install chromium\", quiet=True)\nsys.path.insert(0, str(REPO_DIR))\nheading(\"2. Importing AutoFigure SDK\")\nfrom autofigure import AutoFigureAgent, Config\nfrom autofigure.generator import (\n   validate_code_syntax,\n   code_to_png,\n   get_initial_prompt_template,\n)\nfrom autofigure.extractor import MethodologyExtractor\nprint(\"AutoFigure imported successfully.\")\nprint(f\"Repository directory: {REPO_DIR}\")\nprint(f\"Output root: {OUTPUT_ROOT}\")\nheading(\"3. Offline SVG preflight: validation and rendering\")\npreflight_dir = OUTPUT_ROOT / \"00_offline_preflight\"\npreflight_dir.mkdir(parents=True, exist_ok=True)\nsample_svg = \"\"\"\n<svg width=\"1333\" height=\"750\" viewBox=\"0 0 1333 750\" xmlns=\"http://www.w3.org/2000/svg\">\n <rect x=\"0\" y=\"0\" width=\"1333\" height=\"750\" fill=\"#ffffff\"/>\n <text x=\"666\" y=\"70\" text-anchor=\"middle\" font-family=\"Arial\" font-size=\"36\" font-weight=\"700\" fill=\"#111111\">\n   AutoFigure Offline Rendering Check\n </text>\n <rect x=\"120\" y=\"220\" width=\"250\" height=\"140\" rx=\"18\" fill=\"#f3f3f3\" stroke=\"#111111\" stroke-width=\"3\"/>\n <text x=\"245\" y=\"285\" text-anchor=\"middle\" font-family=\"Arial\" font-size=\"24\" fill=\"#111111\">Text Prompt</text>\n <text x=\"245\" y=\"325\" text-anchor=\"middle\" font-family=\"Arial\" font-size=\"17\" fill=\"#444444\">method description</text>\n <line x1=\"390\" y1=\"290\" x2=\"565\" y2=\"290\" stroke=\"#111111\" stroke-width=\"4\" marker-end=\"url(#arrow)\"/>\n <rect x=\"585\" y=\"220\" width=\"250\" height=\"140\" rx=\"18\" fill=\"#f3f3f3\" stroke=\"#111111\" stroke-width=\"3\"/>\n <text x=\"710\" y=\"285\" text-anchor=\"middle\" font-family=\"Arial\" font-size=\"24\" fill=\"#111111\">AutoFigure</text>\n <text x=\"710\" y=\"325\" text-anchor=\"middle\" font-family=\"Arial\" font-size=\"17\" fill=\"#444444\">generate → evaluate → refine</text>\n <line x1=\"855\" y1=\"290\" x2=\"1030\" y2=\"290\" stroke=\"#111111\" stroke-width=\"4\" marker-end=\"url(#arrow)\"/>\n <rect x=\"1050\" y=\"220\" width=\"250\" height=\"140\" rx=\"18\" fill=\"#f3f3f3\" stroke=\"#111111\" stroke-width=\"3\"/>\n <text x=\"1175\" y=\"285\" text-anchor=\"middle\" font-family=\"Arial\" font-size=\"24\" fill=\"#111111\">Figure</text>\n <text x=\"1175\" y=\"325\" text-anchor=\"middle\" font-family=\"Arial\" font-size=\"17\" fill=\"#444444\">SVG + PNG output</text>\n <defs>\n   <marker id=\"arrow\" markerWidth=\"12\" markerHeight=\"12\" refX=\"10\" refY=\"6\" orient=\"auto\">\n     <path d=\"M2,2 L10,6 L2,10 Z\" fill=\"#111111\"/>\n   </marker>\n </defs>\n</svg>\n\"\"\".strip()\nis_valid, validation_message = validate_code_syntax(sample_svg, \"svg\")\nprint(f\"SVG syntax valid: {is_valid}\")\nprint(f\"Validation message: {validation_message}\")\nsample_svg_path = preflight_dir / \"offline_preflight.svg\"\nsample_png_path = preflight_dir / \"offline_preflight.png\"\nsample_svg_path.write_text(sample_svg, encoding=\"utf-8\")\nrender_ok, processed_svg = code_to_png(\n   sample_svg,\n   str(sample_png_path),\n   attempt_repair=False,\n   output_format=\"svg\",\n)\nprint(f\"Rendered PNG: {render_ok} -> {sample_png_path}\")\ndisplay_file_if_possible(sample_png_path, \"Offline preflight PNG\")\n```\n\nWe install the required system packages, resolve Pillow compatibility issues, clone the AutoFigure repository, and install the SDK along with its PDF and web dependencies. We then import AutoFigure’s main classes and generator utilities after confirming that the environment is ready. We also run offline SVG validation and PNG rendering tests to ensure the rendering pipeline works before making any API-based generation calls.\n\n```\nheading(\"4. Creating a custom reference figure\")\nreference_dir = OUTPUT_ROOT / \"01_custom_references\"\nreference_dir.mkdir(parents=True, exist_ok=True)\nreference_path = reference_dir / \"reference_architecture_style.png\"\nW, H = 1333, 750\nimg = Image.new(\"RGB\", (W, H), \"white\")\ndraw = ImageDraw.Draw(img)\ntry:\n   title_font = ImageFont.truetype(\"DejaVuSans-Bold.ttf\", 36)\n   box_font = ImageFont.truetype(\"DejaVuSans-Bold.ttf\", 24)\n   small_font = ImageFont.truetype(\"DejaVuSans.ttf\", 18)\nexcept Exception:\n   title_font = None\n   box_font = None\n   small_font = None\ndraw.text(\n   (W // 2, 55),\n   \"Reference Layout: Modular Scientific Pipeline\",\n   anchor=\"mm\",\n   fill=\"black\",\n   font=title_font,\n)\nboxes = [\n   (90, 215, 290, 120, \"Input\", \"documents\"),\n   (365, 215, 290, 120, \"Planner\", \"route by task\"),\n   (640, 215, 290, 120, \"Experts\", \"summary / table / vision\"),\n   (915, 215, 290, 120, \"Verifier\", \"grounded output\"),\n]\nfor i, (x, y, bw, bh, title, subtitle) in enumerate(boxes):\n   draw.rounded_rectangle(\n       [x, y, x + bw, y + bh],\n       radius=22,\n       fill=(245, 245, 245),\n       outline=(20, 20, 20),\n       width=3,\n   )\n   draw.text(\n       (x + bw / 2, y + 45),\n       title,\n       anchor=\"mm\",\n       fill=\"black\",\n       font=box_font,\n   )\n   draw.text(\n       (x + bw / 2, y + 82),\n       subtitle,\n       anchor=\"mm\",\n       fill=(70, 70, 70),\n       font=small_font,\n   )\n   if i < len(boxes) - 1:\n       ax = x + bw + 20\n       ay = y + bh / 2\n       bx = boxes[i + 1][0] - 20\n       by = ay\n       draw.line([ax, ay, bx, by], fill=\"black\", width=5)\n       draw.polygon(\n           [(bx, by), (bx - 18, by - 10), (bx - 18, by + 10)],\n           fill=\"black\",\n       )\ndraw.rounded_rectangle(\n   [180, 500, 1150, 585],\n   radius=24,\n   fill=(252, 252, 252),\n   outline=(80, 80, 80),\n   width=2,\n)\ndraw.text(\n   (665, 542),\n   \"Design cue: aligned modules, sparse labels, strong flow direction, clean academic styling\",\n   anchor=\"mm\",\n   fill=(40, 40, 40),\n   font=small_font,\n)\nimg.save(reference_path)\nprint(f\"Custom reference saved: {reference_path}\")\ndisplay_file_if_possible(reference_path, \"Custom reference figure\")\nheading(\"5. Configuring API-backed AutoFigure\")\nAPI_KEY = collect_api_key(PROVIDER)\nif not API_KEY:\n   print(\"No API key provided. Cloud generation sections will be skipped.\")\nelse:\n   print(f\"Provider: {PROVIDER}\")\n   print(f\"Generation model: {GENERATION_MODEL}\")\n   print(\"API key received. The key is not printed.\")\nconfig = None\nagent = None\nif API_KEY:\n   config = Config(\n       generation_api_key=API_KEY,\n       generation_provider=PROVIDER,\n       generation_model=GENERATION_MODEL,\n       methodology_api_key=API_KEY,\n       methodology_provider=PROVIDER,\n       methodology_model=GENERATION_MODEL,\n       enhancement_api_key=API_KEY if RUN_IMAGE_ENHANCEMENT else None,\n       enhancement_provider=PROVIDER,\n       enhancement_model=os.environ.get(\n           \"AUTOFIGURE_ENHANCEMENT_MODEL\",\n           \"google/gemini-3.1-flash-image-preview\"\n           if PROVIDER == \"openrouter\"\n           else \"gemini-3.1-flash-image-preview\",\n       ),\n       max_iterations=MAX_ITERATIONS,\n       quality_threshold=QUALITY_THRESHOLD,\n       output_dir=str(OUTPUT_ROOT / \"02_text_to_figure\"),\n       custom_references=[str(reference_path)],\n       art_style=ART_STYLE,\n   )\n   validation_errors = config.validate()\n   print(f\"Config validation errors: {validation_errors if validation_errors else 'none'}\")\n   print(f\"References found by config: {len(config.get_references())}\")\n   agent = AutoFigureAgent(config)\nheading(\"6. Prompt template preview\")\nprompt_preview = get_initial_prompt_template(\n   topic=\"paper\",\n   content=FIGURE_DESCRIPTION[:2500],\n   output_format=\"svg\",\n)\nprint(prompt_preview[:2500])\nprint(\"\\n... prompt preview truncated ...\")\nif API_KEY and RUN_TEXT_TO_FIGURE:\n   heading(\"7. Running text-to-figure generation\")\n   text_output_dir = OUTPUT_ROOT / \"02_text_to_figure\"\n   text_output_dir.mkdir(parents=True, exist_ok=True)\n   text_result = agent.generate(\n       description=FIGURE_DESCRIPTION,\n       max_iterations=MAX_ITERATIONS,\n       quality_threshold=QUALITY_THRESHOLD,\n       output_format=TEXT_OUTPUT_FORMAT,\n       enable_enhancement=RUN_IMAGE_ENHANCEMENT,\n       art_style=ART_STYLE,\n       enhancement_input_type=\"code2prompt\",\n       enhancement_count=1,\n       custom_references=[str(reference_path)],\n       output_dir=str(text_output_dir),\n       topic=\"paper\",\n   )\n   summarize_generation_result(text_result, \"Text-to-Figure Result\")\nelse:\n   print(\"Skipping text-to-figure generation.\")\n```\n\nWe create a custom reference image that shows the kind of clean modular scientific layout we want AutoFigure to follow. We then configure AutoFigure with the selected provider, model, API key, output directory, reference image, iteration settings, and visual style. Finally, we preview the internal prompt template and run the main text-to-figure generation workflow to produce a scientific figure from our detailed system description.\n\n```\nheading(\"8. Paper methodology extraction dry check\")\npaper_dir = OUTPUT_ROOT / \"03_paper_to_figure\"\npaper_dir.mkdir(parents=True, exist_ok=True)\npaper_md_path = paper_dir / \"mini_paper.md\"\npaper_md_path.write_text(MINI_PAPER_MARKDOWN, encoding=\"utf-8\")\nif API_KEY:\n   if RUN_PAPER_TO_FIGURE:\n       extractor = MethodologyExtractor(config)\n       extracted = extractor.extract_from_file(str(paper_md_path))\n       print(\"\\nExtracted methodology preview:\")\n       print((extracted or \"\")[:2500])\n   else:\n       print(f\"Created demo paper markdown at: {paper_md_path}\")\n       print(\"Set RUN_PAPER_TO_FIGURE = True to run LLM methodology extraction and figure generation.\")\nelse:\n   print(f\"Created demo paper markdown at: {paper_md_path}\")\n   print(\"No API key available, so LLM methodology extraction is skipped.\")\nif API_KEY and RUN_PAPER_TO_FIGURE:\n   heading(\"9. Running paper-to-figure generation\")\n   paper_result = agent.generate_from_paper(\n       paper_path=str(paper_md_path),\n       max_iterations=MAX_ITERATIONS,\n       output_format=\"svg\",\n       enable_enhancement=RUN_IMAGE_ENHANCEMENT,\n       art_style=ART_STYLE,\n       enhancement_input_type=\"code2prompt\",\n       enhancement_count=1,\n       custom_references=[str(reference_path)],\n       output_dir=str(paper_dir),\n   )\n   summarize_generation_result(paper_result, \"Paper-to-Figure Result\")\nheading(\"10. Creating a tiny PDF and testing PDF text reading\")\npdf_path = paper_dir / \"mini_paper.pdf\"\ntry:\n   from reportlab.lib.pagesizes import letter\n   from reportlab.pdfgen import canvas\n   c = canvas.Canvas(str(pdf_path), pagesize=letter)\n   width, height = letter\n   y = height - 50\n   for line in MINI_PAPER_MARKDOWN.splitlines():\n       line = line.strip()\n       if not line:\n           y -= 12\n           continue\n       for wrapped in textwrap.wrap(line, width=95):\n           c.drawString(50, y, wrapped)\n           y -= 14\n           if y < 60:\n               c.showPage()\n               y = height - 50\n   c.save()\n   print(f\"Created demo PDF: {pdf_path}\")\n   if API_KEY:\n       pdf_text = MethodologyExtractor(config)._read_pdf(pdf_path)\n       print(\"PDF text extraction preview:\")\n       print((pdf_text or \"\")[:1500])\n   else:\n       print(\"PDF created. LLM-based paper-to-figure generation still requires an API key.\")\nexcept Exception as exc:\n   print(f\"PDF creation or read test failed: {exc}\")\nif API_KEY and RUN_MXGRAPH_DEMO:\n   heading(\"11. Running editable mxGraph XML generation\")\n   mxgraph_dir = OUTPUT_ROOT / \"04_mxgraph_drawio\"\n   mxgraph_dir.mkdir(parents=True, exist_ok=True)\n   mx_result = agent.generate(\n       description=FIGURE_DESCRIPTION,\n       max_iterations=MAX_ITERATIONS,\n       quality_threshold=QUALITY_THRESHOLD,\n       output_format=MXGRAPH_OUTPUT_FORMAT,\n       enable_enhancement=False,\n       custom_references=[str(reference_path)],\n       output_dir=str(mxgraph_dir),\n       topic=\"paper\",\n   )\n   summarize_generation_result(mx_result, \"mxGraph / draw.io Result\")\nelse:\n   heading(\"11. mxGraph XML generation skipped\")\n   print(\"Set RUN_MXGRAPH_DEMO = True to generate editable draw.io mxGraph XML.\")\n   print(\"This path installs Chromium through Playwright and may be slower than SVG generation.\")\nheading(\"12. Output inventory and export\")\nall_files = []\nfor path in sorted(OUTPUT_ROOT.rglob(\"*\")):\n   if path.is_file():\n       all_files.append(path)\nprint(f\"Total files under {OUTPUT_ROOT}: {len(all_files)}\")\nfor path in all_files:\n   rel = path.relative_to(OUTPUT_ROOT)\n   size_kb = path.stat().st_size / 1024\n   print(f\"{rel}  ({size_kb:.1f} KB)\")\ngallery_path = make_output_gallery(OUTPUT_ROOT)\nprint(f\"\\nGallery HTML: {gallery_path}\")\nzip_base = \"/content/autofigure_colab_outputs\"\nzip_path = shutil.make_archive(zip_base, \"zip\", root_dir=str(OUTPUT_ROOT))\nprint(f\"Zip archive: {zip_path}\")\ntry:\n   from IPython.display import display, HTML\n   display(\n       HTML(\n           f\"\"\"\n           <h3>AutoFigure tutorial complete</h3>\n           <p><b>Output root:</b> {OUTPUT_ROOT}</p>\n           <p><b>Gallery:</b> {gallery_path}</p>\n           <p><b>Zip:</b> {zip_path}</p>\n           \"\"\"\n       )\n   )\nexcept Exception:\n   pass\nprint(\"\\nDone.\")\nprint(\"If the model is unavailable or access is denied, change PROVIDER and GENERATION_MODEL near the top of the cell.\")\n```\n\nWe create a small paper-style Markdown file and optionally use AutoFigure’s methodology extractor to generate a figure from paper content. We also create a simple PDF version of the paper and test whether the PDF text extraction pipeline works correctly. We finish by optionally running the mxGraph draw.io workflow, listing all generated files, building the HTML gallery, and exporting the complete output folder as a zip archive.\n\nIn conclusion, we completed this tutorial by building a full AutoFigure workflow that moves from environment setup to figure generation, validation, previewing, and export. We saw how AutoFigure helps us transform complex research or system descriptions into structured scientific visuals while still giving us control over references, style, output format, iterations, and optional paper-based extraction. By the end, we have a Colab-ready pipeline that can generate SVG figures and prepare editable drawings. io-style outputs when needed, test PDF extraction, and package all generated assets for later use.\n\nCheck out the [FULL CODES here](https://github.com/MARKTECHPOST-AI-MEDIA-INC/AI-Agents-Projects-Tutorials/blob/main/LLM%20Projects/autofigure_scientific_figure_generation_Marktechpost.ipynb)*.* Also, feel free to follow us on ** Twitter** and don’t forget to join our\n\n**and Subscribe to**\n\n[150k+ML SubReddit](https://www.reddit.com/r/machinelearningnews/)**. Wait! are you on telegram?**\n\n[our Newsletter](https://magic.beehiiv.com/v1/f5e63dd4-5653-4f09-83e2-321a8b1ba526?email={{email}})\n\n[now you can join us on telegram as well.](https://t.me/machinelearningresearchnews)Need to partner with us for promoting your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar etc.? [Connect with us](https://forms.gle/wbash1wF6efRj8G58)\n\nSana Hassan, a consulting intern at Marktechpost and dual-degree student at IIT Madras, is passionate about applying technology and AI to address real-world challenges. With a keen interest in solving practical problems, he brings a fresh perspective to the intersection of AI and real-life solutions.", "url": "https://wpnews.pro/news/building-agentic-document-intelligence-pipelines-creating-scientific-figures", "canonical_source": "https://www.marktechpost.com/2026/08/21/building-agentic-document-intelligence-pipelines-creating-scientific-figures-with-autofigure/", "published_at": "2026-08-21 22:00:25+00:00", "updated_at": "2026-08-21 22:13:07.164113+00:00", "lang": "en", "topics": ["generative-ai", "ai-tools", "artificial-intelligence"], "entities": ["AutoFigure", "ResearAI", "Pillow"], "alternates": {"html": "https://wpnews.pro/news/building-agentic-document-intelligence-pipelines-creating-scientific-figures", "markdown": "https://wpnews.pro/news/building-agentic-document-intelligence-pipelines-creating-scientific-figures.md", "text": "https://wpnews.pro/news/building-agentic-document-intelligence-pipelines-creating-scientific-figures.txt", "jsonld": "https://wpnews.pro/news/building-agentic-document-intelligence-pipelines-creating-scientific-figures.jsonld"}}