SVG Instruction Logic
SVG Instruction Logic
Udility Diffuser employs a dual-stage pipeline to transform natural language prompts into precise, labeled SVG illustrations. This process separates the conceptual planning of an image from the technical code generation, ensuring higher accuracy for educational and scientific visualizations.
The Two-Stage Generation Pipeline
The transformation process is handled by two core functions within the diffuser module:
- Instruction Generation: Translates a user prompt into a detailed visual blueprint.
- SVG Scripting: Converts the visual blueprint into valid SVG code.
Instruction Generation (get_detailed_instructions)
This function acts as the "conceptual designer." It takes a raw text description and expands it into a set of comprehensive instructions that define layout, labeling, and visual hierarchy.
API Reference:
diffuser.get_detailed_instructions(text_description)
- Parameters:
text_description(str): The user's prompt (e.g., "The water cycle").
- Returns:
str: A detailed descriptive list of visual elements, coordinates, and labels required to build the image.
Usage Example:
from Udility import diffuser
# Step 1: Generate the visual blueprint
blueprint = diffuser.get_detailed_instructions("Diagram of a plant cell")
print(blueprint)
SVG Scripting (generate_svg_from_instructions)
This function acts as the "programmer." It interprets the detailed instructions generated in the previous step and produces raw SVG code. It is specifically constrained to return only valid SVG tags to ensure compatibility with rendering engines.
API Reference:
diffuser.generate_svg_from_instructions(detailed_instructions)
- Parameters:
detailed_instructions(str): The output fromget_detailed_instructions.
- Returns:
str: A string containing the XML-based SVG code starting with<svg>and ending with</svg>.
Usage Example:
# Step 2: Convert the blueprint into SVG code
svg_code = diffuser.generate_svg_from_instructions(blueprint)
with open("cell_diagram.svg", "w") as f:
f.write(svg_code)
Modular Workflow
While generate_image_from_text() handles the entire pipeline automatically, you can use these functions independently to intercept or modify the logic. This is particularly useful for:
- Prompt Engineering: Refining the instructions before the SVG is generated.
- Custom Rendering: Exporting the raw SVG code for use in web applications instead of converting to PNG.
- Debugging: Inspecting the logic used by the model to represent complex mathematical or biological concepts.
from Udility import diffuser
prompt = "Phases of the moon"
# 1. Get the plan
instructions = diffuser.get_detailed_instructions(prompt)
# 2. Get the code
svg_script = diffuser.generate_svg_from_instructions(instructions)
# 3. Final Conversion
diffuser.svg_to_png(svg_script, "moon_phases.png")