šŸ” Code Extractor

function demo_graphics_generation

Maturity: 44

Demonstrates the generation of three types of graphics (bar chart, process diagram, and mathematical illustration) using the GraphicsGenerator class with e-ink optimized styling.

File:
/tf/active/vicechatdev/e-ink-llm/demo_hybrid_mode.py
Lines:
16 - 95
Complexity:
moderate

Purpose

This async demo function showcases the capabilities of the GraphicsGenerator by creating sample graphics of different types. It serves as a testing and demonstration tool for the graphics generation system, illustrating how to configure GraphicSpec objects with various parameters and style preferences for e-ink display optimization. The function generates a quarterly sales bar chart, a software development process flowchart, and a quadratic function visualization, returning all three results.

Source Code

async def demo_graphics_generation():
    """Demonstrate individual graphics generation"""
    print("šŸŽØ Graphics Generation Demo")
    print("=" * 50)
    
    # Initialize graphics generator (API key would be needed for illustrations)
    generator = GraphicsGenerator(api_key="demo")
    
    # Demo 1: Bar Chart
    print("\nšŸ“Š Generating Bar Chart...")
    chart_spec = GraphicSpec(
        id="demo_chart",
        type=GraphicType.CHART,
        description="Quarterly Sales Performance",
        parameters={
            "type": "bar",
            "data": [125000, 150000, 175000, 200000],
            "labels": ["Q1 2024", "Q2 2024", "Q3 2024", "Q4 2024"],
            "title": "Quarterly Sales ($)",
            "ylabel": "Sales Amount"
        },
        style_preferences={
            "eink_optimized": True,
            "high_contrast": True
        }
    )
    
    chart_result = await generator.generate_graphic(chart_spec)
    if chart_result and chart_result.image_data:
        print(f"āœ… Chart generated successfully ({len(chart_result.image_data)} bytes)")
    else:
        print("āŒ Chart generation failed")
    
    # Demo 2: Process Diagram
    print("\nšŸ“‹ Generating Process Diagram...")
    diagram_spec = GraphicSpec(
        id="demo_diagram",
        type=GraphicType.DIAGRAM,
        description="Software Development Process",
        parameters={
            "steps": ["Planning", "Design", "Development", "Testing", "Deployment"],
            "style": "flowchart",
            "direction": "horizontal"
        },
        style_preferences={
            "eink_optimized": True,
            "simple_style": True
        }
    )
    
    diagram_result = await generator.generate_graphic(diagram_spec)
    if diagram_result and diagram_result.image_data:
        print(f"āœ… Diagram generated successfully ({len(diagram_result.image_data)} bytes)")
    else:
        print("āŒ Diagram generation failed")
    
    # Demo 3: Mathematical Illustration  
    print("\nšŸ“ Generating Mathematical Illustration...")
    illustration_spec = GraphicSpec(
        id="demo_illustration",
        type=GraphicType.ILLUSTRATION,
        description="Quadratic Function Visualization",
        parameters={
            "concept": "quadratic_function",
            "style": "educational",
            "annotations": True
        },
        style_preferences={
            "eink_optimized": True,
            "educational": True
        }
    )
    
    illustration_result = await generator.generate_graphic(illustration_spec)
    if illustration_result and illustration_result.image_data:
        print(f"āœ… Illustration generated successfully ({len(illustration_result.image_data)} bytes)")
    else:
        print("āŒ Illustration generation failed")
    
    return [chart_result, diagram_result, illustration_result]

Return Value

Returns a list containing three GraphicResult objects (or None values if generation failed) corresponding to the chart, diagram, and illustration generated during the demo. Each GraphicResult contains image_data (bytes) and metadata about the generated graphic.

Dependencies

  • asyncio
  • tempfile
  • pathlib
  • json
  • graphics_generator
  • hybrid_response_handler
  • hybrid_pdf_generator
  • traceback

Required Imports

import asyncio
from graphics_generator import GraphicsGenerator, GraphicSpec, GraphicType

Usage Example

import asyncio
from graphics_generator import GraphicsGenerator, GraphicSpec, GraphicType

async def demo_graphics_generation():
    # Function implementation here
    pass

# Run the demo
if __name__ == '__main__':
    results = asyncio.run(demo_graphics_generation())
    for i, result in enumerate(results):
        if result and result.image_data:
            print(f'Graphic {i+1}: {len(result.image_data)} bytes')
        else:
            print(f'Graphic {i+1}: Failed')

Best Practices

  • This is a demo function and uses a placeholder API key ('demo'); replace with a valid API key for production use
  • The function must be run in an async context using asyncio.run() or within an existing event loop
  • Check if result objects and their image_data attributes are not None before using them
  • The function demonstrates e-ink optimization settings which should be adjusted based on target display device
  • Consider adding error handling and logging for production implementations
  • The returned results list may contain None values if any generation step fails
  • Each GraphicSpec is configured with specific parameters appropriate to its type (CHART, DIAGRAM, ILLUSTRATION)

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class ChartGenerator 72.9% similar

    A class that generates various types of charts (bar, line, pie, scatter) optimized for e-ink displays with high contrast and clear visibility.

    From: /tf/active/vicechatdev/e-ink-llm/graphics_generator.py
  • class GraphicsGenerator 68.0% similar

    GraphicsGenerator is a coordinator class that orchestrates the generation of different types of graphics (charts, diagrams, illustrations, and sketches) by delegating to specialized generator classes.

    From: /tf/active/vicechatdev/e-ink-llm/graphics_generator.py
  • function main_v59 65.2% similar

    Orchestrates a comprehensive demonstration of E-Ink LLM hybrid mode capabilities, running three sequential demos showcasing graphics generation, placeholder parsing, and complete hybrid response processing.

    From: /tf/active/vicechatdev/e-ink-llm/demo_hybrid_mode.py
  • class IllustrationGenerator 61.8% similar

    A class that generates educational illustrations and technical drawings for mathematical and scientific concepts using matplotlib and programmatic rendering.

    From: /tf/active/vicechatdev/e-ink-llm/graphics_generator.py
  • function demo_improvement_comparison 60.2% similar

    A demonstration function that displays a before-and-after comparison of response formatting improvements, showing the evolution from verbose to compact, symbol-rich formatting optimized for e-ink displays.

    From: /tf/active/vicechatdev/e-ink-llm/test_improvements.py
← Back to Browse