MCPcopy Create free account
hub / github.com/ResearAI/AutoFigure / generate_initial_code

Function generate_initial_code

autofigure/generator.py:638–707  ·  view source on GitHub ↗

Generates initial diagram code (SVG or mxGraph XML) based on the output format. Args: paper_content: The content to visualize. reference_figures: Reference images for design inspiration. topic: Content type ('paper', 'survey', 'blog', 'textbook'). output_for

(paper_content: str, reference_figures: List[Image.Image], topic: str = 'paper', output_format: str = None)

Source from the content-addressed store, hash-verified

636
637
638def generate_initial_code(paper_content: str, reference_figures: List[Image.Image], topic: str = 'paper', output_format: str = None) -> Optional[str]:
639 """
640 Generates initial diagram code (SVG or mxGraph XML) based on the output format.
641
642 Args:
643 paper_content: The content to visualize.
644 reference_figures: Reference images for design inspiration.
645 topic: Content type ('paper', 'survey', 'blog', 'textbook').
646 output_format: 'svg' or 'mxgraphxml'. If None, uses CONFIG['OUTPUT_FORMAT'].
647
648 Returns:
649 The generated code, or None on failure.
650 """
651 output_format = output_format or CONFIG.get('OUTPUT_FORMAT', 'svg')
652 format_name = 'mxGraph XML' if output_format == 'mxgraphxml' else 'SVG'
653
654 prompt = get_initial_prompt_template(topic, paper_content, output_format)
655 try:
656 print(f"Generating initial {format_name} figure for {topic.upper()}, learning from references...")
657
658 multimodal_content = [prompt]
659 for i, ref_fig in enumerate(reference_figures):
660 multimodal_content.extend([
661 f"Reference Figure Example {i+1}:",
662 ref_fig
663 ])
664
665 response = call_google_genai_multimodal(multimodal_content)
666
667 if response is None:
668 raise Exception("LLM returned an empty response")
669
670 # Extract code based on format
671 if output_format == 'mxgraphxml':
672 code_start = response.find('<mxfile')
673 code_end = response.rfind('</mxfile>') + 9
674 end_offset = 9
675 else:
676 code_start = response.find('<svg')
677 code_end = response.rfind('</svg>') + 6
678 end_offset = 6
679
680 if code_start == -1 or code_end == (end_offset - 1):
681 raise Exception(f"No valid {format_name} code found in the response")
682
683 code = response[code_start:code_end]
684
685 print(f"Successfully generated initial {format_name} code: {len(code)} characters")
686
687 # Validate and repair if necessary
688 is_valid, error_msg = validate_code_syntax(code, output_format)
689 if not is_valid:
690 print(f"Generated {format_name} has syntax issues: {error_msg}")
691 print("Attempting to fix syntax issues...")
692 repaired_code = repair_code(code, error_msg, output_format)
693 if repaired_code:
694 code = repaired_code
695 print(f"{format_name} syntax repaired successfully")

Callers 2

start_generationFunction · 0.90

Calls 4

validate_code_syntaxFunction · 0.85
repair_codeFunction · 0.85

Tested by

no test coverage detected