Dall-E Image Generator
OpenAI Dall-E are text-to-image models developed by
OpenAI
using deep learning methodologies to generate digital images from natural language descriptions, called "prompts".
This notebook shows how you can generate images from a prompt synthesized using an OpenAI LLM. The images are generated usingDall-E
, which uses the same OpenAI API key as the LLM.
# Needed if you would like to display images in the notebook
%pip install--upgrade--quiet opencv-python scikit-image langchain-community
import os
from langchain_openaiimport OpenAI
os.environ["OPENAI_API_KEY"]="insertapikey"
API Reference:OpenAI
Run as a chain
from langchain.chainsimport LLMChain
from langchain_community.utilities.dalle_image_generatorimport DallEAPIWrapper
from langchain_core.promptsimport PromptTemplate
from langchain_openaiimport OpenAI
llm= OpenAI(temperature=0.9)
prompt= PromptTemplate(
input_variables=["image_desc"],
template="Generate a detailed prompt to generate an image based on the following description: {image_desc}",
)
chain= LLMChain(llm=llm, prompt=prompt)
image_url= DallEAPIWrapper().run(chain.run("halloween night at a haunted museum"))
image_url
# You can click on the link above to display the image
# Or you can try the options below to display the image inline in this notebook
try:
import google.colab
IN_COLAB=True
except ImportError:
IN_COLAB=False
if IN_COLAB:
from google.colab.patchesimport cv2_imshow# for image display
from skimageimport io
image= io.imread(image_url)
cv2_imshow(image)
else:
import cv2
from skimageimport io
image= io.imread(image_url)
cv2.imshow("image", image)
cv2.waitKey(0)# wait for a keyboard input
cv2.destroyAllWindows()
Run as a tool with an agent
from langchain_community.tools.openai_dalle_image_generationimport(
OpenAIDALLEImageGenerationTool,
)
from langchain_community.utilities.dalle_image_generatorimport DallEAPIWrapper
from langchain_openaiimport ChatOpenAI
from langgraph.prebuiltimport create_react_agent
llm= ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
api_wrapper= DallEAPIWrapper()
dalle_tool= OpenAIDALLEImageGenerationTool(api_wrapper=api_wrapper)
tools=[dalle_tool]
agent= create_react_agent(llm, tools, debug=True)
# User prompt
prompt="Create an image of a halloween night at a haunted museum"
messages=[
# "role": "user" Indicates message is coming from user
# "content": prompt is where the user's input is placed
{"role":"user","content": prompt}
]
# Sending the message to be processed and adjusted by ChatGPT, after which is sent through DALL-E
response= agent.invoke({"messages": messages})
print(response)
Related
- Toolconceptual guide
- Toolhow-to guides