Core Functions & Parameters
Core Functions & Parameters
The Udility library provides a streamlined interface for generating high-quality, labeled educational illustrations. The primary entry point for users is the diffuser module.
Main Generation Interface
generate_image_from_text
The primary function for creating and displaying an illustration in a single call. It orchestrates the process of generating instructions, creating SVG code, converting it to a PNG file, and rendering it to the screen.
Parameters:
text_description(str): A detailed or simple prompt describing the educational concept or diagram you wish to generate (e.g., "The water cycle").output_filename(str, optional): The path and filename where the resulting PNG image will be saved. Defaults to'output.png'.
Example:
from Udility import diffuser
# Generate and display a diagram of a plant cell
diffuser.generate_image_from_text(
text_description="Diagram of a plant cell with labels for nucleus, cell wall, and chloroplasts",
output_filename="plant_cell.png"
)
Utility Functions
These functions are used by the main interface but can be called independently for more granular control over the image generation and display process.
svg_to_png
Converts raw SVG string data into a PNG file. This is useful if you have custom SVG code you wish to render using the Udility backend.
Parameters:
svg_code(str): The raw string containing valid<svg>...</svg>markup.output_filename(str, optional): The destination path for the converted PNG file. Defaults to'output.png'.
Example:
svg_data = '<svg height="100" width="100"><circle cx="50" cy="50" r="40" fill="red" /></svg>'
diffuser.svg_to_png(svg_data, output_filename="circle.png")
display_image
A helper function to render a generated PNG file using matplotlib. This is particularly useful in Jupyter Notebooks or Google Colab environments.
Parameters:
image_path(str): The file path to the PNG image you want to display.
Example:
diffuser.display_image("output.png")
Internal Workflow Functions
The following functions are used internally by generate_image_from_text. While they are accessible, they are primarily responsible for the multi-stage LLM reasoning process.
| Function | Role | Input | Output |
| :--- | :--- | :--- | :--- |
| get_detailed_instructions | Contextualizes the prompt for SVG generation using Meta Llama. | text_description (str) | Instructional Prompt (str) |
| generate_svg_from_instructions | Converts natural language instructions into valid SVG code. | detailed_instructions (str) | SVG Markup (str) |
Note: These internal functions require a valid
OPENROUTER_API_KEYto be set in your environment variables to communicate with the Llama 3.1 405B model.