function demo_graphics_generation
Demonstrates the generation of three types of graphics (bar chart, process diagram, and mathematical illustration) using the GraphicsGenerator class with e-ink optimized styling.
/tf/active/vicechatdev/e-ink-llm/demo_hybrid_mode.py
16 - 95
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
asynciotempfilepathlibjsongraphics_generatorhybrid_response_handlerhybrid_pdf_generatortraceback
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)
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class ChartGenerator 72.9% similar
-
class GraphicsGenerator 68.0% similar
-
function main_v59 65.2% similar
-
class IllustrationGenerator 61.8% similar
-
function demo_improvement_comparison 60.2% similar