Skip to main content

generate_images

dataflow

To get started:

Dynamically pull and run

from hamilton import dataflows, driver
# downloads into ~/.hamilton/dataflows and loads the module -- WARNING: ensure you know what code you're importing!
generate_images = dataflows.import_module("generate_images", "elijahbenizzy")
dr = (
driver.Builder()
.with_config({}) # replace with configuration as appropriate
.with_modules(generate_images)
.build()
)
# If you have sf-hamilton[visualization] installed, you can see the dataflow graph
# In a notebook this will show an image, else pass in arguments to save to a file
# dr.display_all_functions()
# Execute the dataflow, specifying what you want back. Will return a dictionary.
result = dr.execute(
[generate_images.CHANGE_ME, ...], # this specifies what you want back
inputs={...} # pass in inputs as appropriate
)

Use published library version

pip install sf-hamilton-contrib --upgrade  # make sure you have the latest
from hamilton import dataflows, driver
# Make sure you've done - `pip install sf-hamilton-contrib --upgrade`
from hamilton.contrib.user.elijahbenizzy import generate_images
dr = (
driver.Builder()
.with_config({}) # replace with configuration as appropriate
.with_modules(generate_images)
.build()
)
# If you have sf-hamilton[visualization] installed, you can see the dataflow graph
# In a notebook this will show an image, else pass in arguments to save to a file
# dr.display_all_functions()
# Execute the dataflow, specifying what you want back. Will return a dictionary.
result = dr.execute(
[generate_images.CHANGE_ME, ...], # this specifies what you want back
inputs={...} # pass in inputs as appropriate
)

Modify for your needs

Now if you want to modify the dataflow, you can copy it to a new folder (renaming is possible), and modify it there.

dataflows.copy(generate_images, "path/to/save/to")

Purpose of this module

This module provides a simple dataflow to generate an image using openAI DallE generation capabilities.

Configuration Options

This module can be configured with the following options: [list options]

  • base_prompt -- prompt to generate from a string
  • style -- string for the style of the image to generate, defaults to "realist"
  • size -- string for the size of the image to generate, defaults to "1024x1024"
  • hd -- Whether to use high definition, defaults to False

Limitations

Pretty simple -- only exposes the above parameters.

Source code

__init__.py
import logging

logger = logging.getLogger(__name__)

from hamilton import contrib

with contrib.catch_import_errors(__name__, __file__, logger):
import openai


def image_prompt(
image_generation_prompt: str, image_style: str = None, additional_image_prompt: str = None
) -> str:
"""Returns the prompt used to generate an image"""
prompt_out = image_generation_prompt
if image_style is not None:
prompt_out += f" The image should be in the {image_style} style."
if additional_image_prompt is not None:
prompt_out += f" {additional_image_prompt}"
return prompt_out


def generated_image(image_prompt: str, size: str = "1024x1024", hd: bool = False) -> str:
"""Returns the generated image"""
client = openai.OpenAI()

response = client.images.generate(
model="dall-e-3",
prompt=image_prompt,
size=size,
quality="standard" if not hd else "hd",
n=1,
)
image_url = response.data[0].url
return image_url


if __name__ == "__main__":
import __init__ as generate_images

from hamilton import base, driver

dr = driver.Driver(
{},
generate_images,
adapter=base.DefaultAdapter(),
)
# saves to current working directory creating dag.png.
dr.display_all_functions("dag", {"format": "png", "view": False}, show_legend=False)

Requirements

# put non-hamilton requirements here
openai