Merge pull request #845 from maherp24/maherp24/llm_engineering_ed

Day 1 PR Image Generator using DALL-E
This commit is contained in:
Ed Donner
2025-11-01 20:23:39 -04:00
committed by GitHub
2 changed files with 218 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
# Image Generator
Quick image generator using OpenAI's DALL-E 3. Generates 1024x1024 images from either random themes or your own prompts.
## Setup
You'll need an OpenAI API key. Set it as an environment variable:
```bash
export OPENAI_API_KEY='your-key-here'
```
Or just enter it when the notebook prompts you.
## How to Use
1. Open `day1_random_img_generator.ipynb`
2. Run the cells in order
3. When prompted, type either:
- `random` - picks a random theme from the list
- `custom` - lets you enter your own prompt
4. Wait 10-30 seconds for the image to generate
5. Image shows up in the notebook
## What's Inside
- 15 pre-made themes (cyberpunk cities, fantasy forests, etc.)
- Uses DALL-E 3 for image generation
- Standard quality to keep costs reasonable
- Simple error handling
## Cost Warning
DALL-E 3 costs about $0.04 per image. Not huge but watch your usage.
## Example Prompts
If you go custom, try stuff like:
- "a cat astronaut floating in space"
- "steampunk coffee shop interior"
- "synthwave sunset over ocean waves"
Keep it descriptive for better results.

View File

@@ -0,0 +1,175 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e1fb2654",
"metadata": {},
"source": [
"# Simple Image Generator with OpenAI DALL-E\n",
"\n",
"Generate beautiful images using AI! Choose a random theme or create your own custom prompt.\n",
"Costs a bit more careful!\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f856bd8",
"metadata": {},
"outputs": [],
"source": [
"# Import libraries\n",
"import os\n",
"from openai import OpenAI\n",
"from PIL import Image\n",
"import requests\n",
"from io import BytesIO\n",
"\n",
"# Get OpenAI API key\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"if not api_key:\n",
" print(\"OPENAI_API_KEY not found in environment.\")\n",
" api_key = input(\"Please enter your OpenAI API key: \").strip()\n",
"\n",
"# Initialize OpenAI client\n",
"client = OpenAI(api_key=api_key)\n",
"print(\"✓ OpenAI client initialized!\")\n",
"print(\"Ready to generate images!\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c49df89",
"metadata": {},
"outputs": [],
"source": [
"# Cell 2: Define themes\n",
"import random\n",
"\n",
"# List of fun themes to choose from\n",
"themes = [\n",
" \"a cyberpunk city at night with neon lights\",\n",
" \"a magical fantasy forest with glowing mushrooms\",\n",
" \"a cute robot having tea in a garden\",\n",
" \"an astronaut riding a horse on Mars\",\n",
" \"a cozy cabin in snowy mountains at sunset\",\n",
" \"a steampunk flying machine above clouds\",\n",
" \"a peaceful Japanese garden in autumn\",\n",
" \"a dragon sleeping on a pile of books\",\n",
" \"an underwater city with colorful fish\",\n",
" \"a vintage car on a desert highway at golden hour\",\n",
" \"a cozy coffee shop on a rainy day\",\n",
" \"a majestic phoenix rising from flames\",\n",
" \"a futuristic space station orbiting Earth\",\n",
" \"a whimsical treehouse village in the clouds\",\n",
" \"a serene Buddhist temple on a mountain peak\"\n",
"]\n",
"\n",
"print(\"Available themes loaded!\")\n",
"print(f\"Total themes: {len(themes)}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31775d64",
"metadata": {},
"outputs": [],
"source": [
"# Cell 3: Get user input\n",
"print(\"IMAGE GENERATOR - Choose Your Option\")\n",
"\n",
"choice = input(\"Type 'random' for a random theme or 'custom' for your own prompt: \").strip().lower()\n",
"\n",
"if choice == 'random':\n",
" prompt = random.choice(themes)\n",
" print(f\"\\n Random theme selected!\")\n",
" print(f\"Prompt: {prompt}\")\n",
"else:\n",
" prompt = input(\"\\nEnter your custom image prompt: \").strip()\n",
" if not prompt:\n",
" prompt = random.choice(themes)\n",
" print(f\"No input provided, using random theme: {prompt}\")\n",
" else:\n",
" print(f\"Using your custom prompt: {prompt}\")\n",
"\n",
"print(\"\\n✓ Prompt ready for generation!\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3cdc5ddc",
"metadata": {},
"outputs": [],
"source": [
"# Cell 4: Generate and display image\n",
"print(\"\\n Generating image...\")\n",
"print(\"This may take 10-30 seconds...\")\n",
"\n",
"try:\n",
" # Call DALL-E API\n",
" response = client.images.generate(\n",
" model=\"dall-e-3\",\n",
" prompt=prompt,\n",
" size=\"1024x1024\",\n",
" quality=\"standard\",\n",
" n=1,\n",
" )\n",
" \n",
" # Get image URL\n",
" image_url = response.data[0].url\n",
" \n",
" print(\"✓ Image generated successfully!\")\n",
" print(f\"\\nImage URL: {image_url}\")\n",
" \n",
" # Download and display image\n",
" print(\"\\nDownloading image...\")\n",
" response = requests.get(image_url)\n",
" img = Image.open(BytesIO(response.content))\n",
" \n",
" print(\"✓ Image ready!\")\n",
" print(\"\\nDisplaying image below:\")\n",
" display(img)\n",
" \n",
" # Store for optional saving later\n",
" generated_image = img\n",
" \n",
"except Exception as e:\n",
" print(f\"❌ Error generating image: {str(e)}\")\n",
" generated_image = None\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ead2c877",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}