Skip to content

Commit

Permalink
Adds basic dataflow to generate an image from a prompt
Browse files Browse the repository at this point in the history
This is a simple wrapper over openAI, but forms components that are nice
and reusable.
  • Loading branch information
elijahbenizzy committed Dec 8, 2023
1 parent 5b8035b commit e0fc792
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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
Write limitations/assumptions/known issues here.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import logging

logger = logging.getLogger(__name__)

from hamilton import contrib

with contrib.catch_import_errors(__name__, __file__, logger):
# non-hamilton imports go here
import openai

pass


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


def generated_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=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})
# out = dr.execute(
# ["generated_image"],
# inputs={
# "base_prompt": "Aerial view of a sprawling two-story residential home nestled in a verdant landscape, with clusters of mature oak trees dotting the terrain. The large house presents a "
# "mix of architectural details with a primarily beige color scheme and white trim accents. The upper level features a row of regular double-hung windows flanked by darker "
# "shutters and a prominent deck area with a wood railing, providing an intimate outdoor space. A sizable terrace extends from the lower level boasting an expansive "
# "outdoor entertaining area with a variety of patio furniture arranged for dining and relaxation, overlooking the sloping lawn.\n\nTo the left, a smaller, "
# "single-story structure with a red gable roof and similar beige siding hints at additional living quarters or perhaps a guest house, complete with its own petite wooden "
# "porch adorned with a bench. The natural and manicured elements of the landscape harmonize to create a secluded retreat atmosphere; terraced garden beds and low "
# "vegetation patches add texture to the otherwise unruly native greenery.\n\nIn the background, perched on an adjacent hill, another house peeks through the tree canopy, "
# "only partially visible, indicating a neighborhood that balances privacy with a sense of community. The tranquil blue sky above the panoramic scene suggests a serene and "
# "pleasant day. The property as a whole exudes a feeling of comfortable country living combined with luxurious outdoor leisure spaces."
# },
# )
# print(out)
16 changes: 16 additions & 0 deletions contrib/hamilton/contrib/user/elijahbenizzy/generate_images/dag
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Dependency Graph
digraph {
graph [compound=true concentrate=true rankdir=LR ranksep=0.4]
image_generated [label=<<b>image_generated</b><br /><br /><i>str</i>> fillcolor="#b4d8e4" fontname=Helvetica margin=0.15 shape=rectangle style="rounded,filled"]
prompt [label=<<b>prompt</b><br /><br /><i>str</i>> fillcolor="#b4d8e4" fontname=Helvetica margin=0.15 shape=rectangle style="rounded,filled"]
prompt -> image_generated
_image_generated_inputs [label=<<table border="0"><tr><td>size</td><td>str</td></tr><tr><td>hd</td><td>bool</td></tr></table>> fontname=Helvetica margin=0.15 shape=rectangle style=dashed]
_image_generated_inputs -> image_generated
_prompt_inputs [label=<<table border="0"><tr><td>base_prompt</td><td>str</td></tr><tr><td>style</td><td>str</td></tr></table>> fontname=Helvetica margin=0.15 shape=rectangle style=dashed]
_prompt_inputs -> prompt
subgraph cluster__legend {
graph [fontname=helvetica label=Legend rank=same]
input [fontname=Helvetica margin=0.15 shape=rectangle style=dashed]
function [fillcolor="#b4d8e4" fontname=Helvetica margin=0.15 shape=rectangle style="rounded,filled"]
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# put non-hamilton requirements here
openai
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"schema": "1.0",
"use_case_tags": ["example"],
"secondary_tags": {
"language": "English"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit e0fc792

Please sign in to comment.