generated from kyegomez/Python-Package-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kye
committed
Oct 15, 2023
1 parent
6837aa5
commit 3528871
Showing
9 changed files
with
116 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
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 | ||
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. | ||
- Frame your photographic prompts like camera position, lighting, film type, year, usage context. This implicitly suggests image qualities. | ||
- For illustrations, you can borrow photographic terms like "close up" and prompt for media, style, artist, animation style, etc. | ||
- Prompt hack: name a film/TV show genre + year to "steal the look" for costumes, lighting, etc without knowing technical details. | ||
- Try variations of a prompt, make edits, and do recursive uncropping to create interesting journeys and zoom-out effects. | ||
- Use an image editor like Photopea to uncrop DALL-E outputs and prompt again to extend the image. | ||
- Combine separate DALL-E outputs into panoramas and murals with careful positioning/editing. | ||
- Browse communities like Reddit r/dalle2 to get inspired and share your creations. See tools, free image resources, articles. | ||
- 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.