Implementation of DALL·E Using OpenAI API
What is DALL·E?
DALL·E is an AI model by OpenAI that generates images from text prompts. DALL·E 2 and 3 can create:
- Original digital art
- Realistic scenes
- Variations of uploaded images
- Edits with inpainting
In this build, we’ll replicate basic DALL·E functionality using:
- OpenAI’s DALL·E API or Stable Diffusion API
- Gradio for a beautiful UI
Step 1: Install Required Libraries
pip install openai gradio
Step 2: Set OpenAI API Key
import openai
openai.api_key = "YOUR_OPENAI_API_KEY"
Step 3: Text-to-Image Function Using DALL·E API
def generate_image(prompt):
response = openai.Image.create(
prompt=prompt,
n=1,
size="512x512"
)
return response['data'][0]['url']
✅ Example Call:
print(generate_image("A futuristic city skyline at night with neon lights, digital art"))
🖼️ Output:
Returns a URL of the generated image like:
https://oaidalleapiprodscus.blob.core.windows.net/.../image.png
You can download or display the image using Python or UI.
Step 4: Create a Gradio Interface (DALL·E Clone)
import gradio as gr
def generate_and_display(prompt):
image_url = generate_image(prompt)
return image_url
gr.Interface(
fn=generate_and_display,
inputs=gr.Textbox(label="Enter your image prompt"),
outputs=gr.Image(label="Generated Image"),
title="DALL·E Clone - AI Image Generator",
description="Type a description and get an AI-generated image using DALL·E"
).launch()
Example Prompts You Can Try
Prompt Example | Output Style |
---|---|
"A robot walking a dog in a park, Pixar style" | 3D cartoon image |
"Mona Lisa wearing sunglasses in 2050" | Surreal/Modern art |
"An astronaut lounging on Mars drinking tea" | Sci-fi realism |
"A palace made of ice cream under a rainbow" | Fantasy, kids-style |
Bonus: Image Variations (like DALL·E’s “Generate more”)
def generate_variations(image_path):
with open(image_path, "rb") as image_file:
response = openai.Image.create_variation(
image=image_file,
n=1,
size="512x512"
)
return response['data'][0]['url']
Use this to generate new versions of an image (user uploads image, gets variations).
Optional Advanced Features
Feature | Tools |
---|---|
Inpainting / Edits | DALL·E 2 Edit API |
Drag-to-edit UI | Streamlit + Canvas plugin |
Local model option | Use Stable Diffusion + HuggingFace |
Prompt enhancement | GPT prompt optimization |
Image download/save | Python or browser link |
Folder Structure
dalle-clone/
│
├── dalle_app.py # main app file
├── image_gen.py # logic to call OpenAI
├── requirements.txt
Full Workflow Summary
Step | Input | Output |
---|---|---|
User prompt | “A dragon made of clouds” | 🖼️ DALL·E-generated image (URL) |
Variation upload | Upload an image | New variation of it |
UI | Gradio textbox + image preview | Live generation experience |
Sample Output (Real)
Prompt:
“A cyberpunk cat DJing in a neon-lit club”
Output:
(Displayed inside your Gradio app)
✅ Final Notes
Your DALL·E-like image generator can:
- Generate images from text
- Handle user input via a clean UI
- Output images dynamically
- Be extended into an AI art app, design helper, or marketing visual generator