Skip to content

Commit

Permalink
idea 2 image
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Oct 15, 2023
1 parent 6837aa5 commit 3528871
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 8 deletions.
4 changes: 2 additions & 2 deletions dalle3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dalle3.main import Dalle
from dalle3.idea2image import Idea2Image


__all__ = ["Dalle"]
__all__ = ["Dalle", "Idea2Image"]
110 changes: 110 additions & 0 deletions dalle3/idea2image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import os
import logging
from dataclasses import dataclass
from dalle3 import Dalle
from swarms.models import OpenAIChat


@dataclass
class Idea2Image:
"""
A class used to generate images from text prompts using DALLE-3.
...
Attributes
----------
image_to_generate : str
Text prompt for the image to generate
openai_api_key : str
OpenAI API key
cookie : str
Cookie value for DALLE-3
output_folder : str
Folder to save the generated images
Methods
-------
llm_prompt():
Returns a prompt for refining the image generation
generate_image():
Generates and downloads the image based on the prompt
Usage:
------
from dalle3 import Idea2Image
idea2image = Idea2Image(
image_to_generate="Fish hivemind swarm in light blue avatar anime in zen garden pond concept art anime art, happy fish, anime scenery"

Check failure on line 39 in dalle3/idea2image.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle3/idea2image.py:39:89: E501 Line too long (142 > 88 characters)
)
idea2image.generate_image()
"""

image_to_generate: str
openai_api_key: str = os.getenv("OPENAI_API_KEY") or None
cookie: str = os.getenv("DALLE_COOKIE") or None
output_folder: str = "images/"

def __post_init__(self):
self.llm = OpenAIChat(openai_api_key=self.openai_api_key)
self.dalle = Dalle(self.cookie)

def llm_prompt(self):
LLM_PROMPT = f"""
Refine the USER prompt to create a more precise image tailored to the user's needs using

Check failure on line 55 in dalle3/idea2image.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle3/idea2image.py:55:89: E501 Line too long (96 > 88 characters)
an image generator like DALLE-3.
###### FOLLOW THE GUIDE BELOW TO REFINE THE PROMPT ######
- Use natural language prompts up to 400 characters to describe the image you want to generate. Be as specific or vague as needed.

Check failure on line 60 in dalle3/idea2image.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle3/idea2image.py:60:89: E501 Line too long (138 > 88 characters)
- Frame your photographic prompts like camera position, lighting, film type, year, usage context. This implicitly suggests image qualities.

Check failure on line 62 in dalle3/idea2image.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle3/idea2image.py:62:89: E501 Line too long (147 > 88 characters)
- For illustrations, you can borrow photographic terms like "close up" and prompt for media, style, artist, animation style, etc.

Check failure on line 64 in dalle3/idea2image.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle3/idea2image.py:64:89: E501 Line too long (139 > 88 characters)
- Prompt hack: name a film/TV show genre + year to "steal the look" for costumes, lighting, etc without knowing technical details.

Check failure on line 66 in dalle3/idea2image.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle3/idea2image.py:66:89: E501 Line too long (138 > 88 characters)
- Try variations of a prompt, make edits, and do recursive uncropping to create interesting journeys and zoom-out effects.

Check failure on line 68 in dalle3/idea2image.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle3/idea2image.py:68:89: E501 Line too long (130 > 88 characters)
- Use an image editor like Photopea to uncrop DALL-E outputs and prompt again to extend the image.

Check failure on line 70 in dalle3/idea2image.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle3/idea2image.py:70:89: E501 Line too long (106 > 88 characters)
- Combine separate DALL-E outputs into panoramas and murals with careful positioning/editing.

Check failure on line 72 in dalle3/idea2image.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle3/idea2image.py:72:89: E501 Line too long (101 > 88 characters)
- Browse communities like Reddit r/dalle2 to get inspired and share your creations. See tools, free image resources, articles.

Check failure on line 74 in dalle3/idea2image.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dalle3/idea2image.py:74:89: E501 Line too long (134 > 88 characters)
- Focus prompts on size, structure, shape, mood, aesthetics to influence the overall vibe and composition.
- Be more vague or detailed as needed - DALL-E has studied over 400M images and can riff creatively or replicate specific styles.
- Be descriptive, describe the art style at the end like fusing concept art with anime art or game art or product design art.
###### END OF GUIDE ######
Prompt to refine: {self.image_to_generate}
"""
return LLM_PROMPT

def generate_image(self):
"""
Generates and downloads the image based on the prompt.
This method refines the prompt using the llm, opens the website with the query,
gets the image URLs, and downloads the images to the specified folder.
"""
# Set up logging
logging.basicConfig(level=logging.INFO)

# Refine the prompt using the llm
image_to_generate = self.llm_prompt()
refined_prompt = self.llm(image_to_generate)
print(f"Refined prompt: {refined_prompt}")

# Open the website with your query
self.dalle.open_website(refined_prompt)

# Get the image URLs
urls = self.dalle.get_urls()

# Download the images to your specified folder
self.dalle.download_images(urls, self.output_folder)
7 changes: 3 additions & 4 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
from dalle3 import Dalle

# Define cookie using env or empty string
cookie = (
os.getenv("BING_COOKIE")
or "1Dv1qGH6TcGzEQUJqU6AfxnMfxPR4V9yCtKk8ox2kpl-_YwmKzM9CnzPmlge158c0DTSDAEqrDV5itj3Ll0GyuaRrWmXf0Gk_2FIZuL2NvCLUrCF3VALngS9qw9vESZ9EY9vqVVVvjGoIJpxf9HEz6OV3fih4dw_Abc7GrfRC7p8iRDiCmotsgy9lPKf6sSPg9yy2lMTSU9-MNmkga0CACSZsV4La9NU5V5nNowUAMhU"
)
cookie = os.getenv("BING_COOKIE")
#or
# cookie = "cookie"

# Set up logging
logging.basicConfig(level=logging.INFO)
Expand Down
3 changes: 1 addition & 2 deletions idea_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def main():
# Refine the prompt using the llm
image_to_generate = llm_prompt(args.image_to_generate)
refined_prompt = llm(image_to_generate)
print(f"Refined prompt: {refined_prompt}")

# Open the website with your query
dalle.open_website(refined_prompt)
Expand All @@ -95,5 +96,3 @@ def main():

if __name__ == "__main__":
main()


Binary file added images/.DS_Store
Binary file not shown.
Binary file added images/15-10-2023 02-37-31/image_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/15-10-2023 02-37-31/image_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/15-10-2023 02-37-31/image_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/15-10-2023 02-37-31/image_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3528871

Please sign in to comment.