Skip to content

Commit 35276cc

Browse files
author
Chris
committed
adding notebook + clean up
1 parent a3ba86a commit 35276cc

File tree

3 files changed

+480
-45
lines changed

3 files changed

+480
-45
lines changed

9-image-apps/README.md

+102-45
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,14 @@ First, [DALL-E](https://arxiv.org/pdf/2102.12092.pdf). DALL-E is a Generative AI
6767

6868
An *autogressive transformer* defines how a model generates images from text descriptions, it generates one pixel at a time, and then uses the generated pixels to generate the next pixel. Passing through multiple layers in a neural network, until the image is complete.
6969

70-
TODO: Add image of DALL-E architecture
71-
72-
73-
7470
With this process, DALL-E, controls attributes, objects, characteristics, and more in the image it generates. However, DALL-E 2 and 3 have more control over the generated image,
7571

76-
77-
78-
TODO: Add image of DALL-E 2 and 3
79-
80-
81-
82-
TODO: Midjorney architecture and paper
83-
8472
## Building your first image generation application
8573

8674
So what does it take to build an image generation application? You need the following libraries:
8775

8876
- **python-dotenv**, you're highly recommended to use this library to keep your secrets in a *.env* file away from the code.
89-
- **openai*, this library is what you will use to interact with the OpenAI API.
77+
- **openai**, this library is what you will use to interact with the OpenAI API.
9078
- **pillow**, to work with images in Python.
9179
- **requests**, to help you make HTTP requests.
9280

@@ -315,12 +303,12 @@ So let's try to make the response more deterministic. We could observe from the
315303
Let's therefore change our code and set the temperature to 0, like so:
316304

317305
```python
318-
generation_response = openai.Image.create(
319-
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here
320-
size='1024x1024',
321-
n=2,
322-
temperature=0
323-
)
306+
generation_response = openai.Image.create(
307+
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here
308+
size='1024x1024',
309+
n=2,
310+
temperature=0
311+
)
324312
```
325313

326314
Now when you run this code, you get these two images:
@@ -330,39 +318,45 @@ Now when you run this code, you get these two images:
330318

331319
Here you can clearly see how the images resemble each other more.
332320

321+
## How to define boundaries for your application with metaprompts
333322

323+
With our demo, we can already generate images for our clients. However, we need to create some boundaries for our application.
334324

325+
For example, we don't want to generate images that are not safe for work, or that are not appropriate for children.
335326

327+
We can do this with *metaprompts*. Metaprompts are text prompts that are used to control the output of a Generative AI model. For example, we can use metaprompts to control the output, and ensure that the generated images are safe for work, or appropriate for children.
336328

337-
## TODO: How to define boundaries for your application with metaprompts
329+
### How does it work?
338330

339-
331+
Now, how do meta prompts work?
340332

341-
With our demo, we can already generate images for our clients. However, we need to create some boundaries for our application.
333+
Meta prompts are text prompts that are used to control the output of a Generative AI model, they are positioned before the text prompt, and are used to control the output of the model and embedded in applications to control the output of the model. Encapsulating the prompt input and the meta prompt input in a single text prompt.
342334

343-
335+
One example of a meta prompt would be the following:
344336

345-
For example, we don't want to generate images that are not safe for work, or that are not appropriate for children.
337+
```text
338+
You are an assistant designer that creates images for children.
346339
347-
We can do this with metaprompts. Metaprompts are text prompts that are used to control the output of a Generative AI model. For example, we can use metaprompts to control the output, and ensure that the generated images are safe for work, or appropiate for children.
340+
The image needs to be safe for work and appropriate for children.
348341
349-
342+
The image needs to be in color.
350343
351-
Now, how do meta prompts work?
344+
The image needs to be in landscape orientation.
352345
353-
346+
The image needs to be in a 16:9 aspect ratio.
354347
355-
Meta prompts are text prompts that are used to control the output of a Generative AI model, they are positioned before the text prompt, and are used to control the output of the model and embedded in applications to control the output of the model. Encapsulating the prompt input and the meta prompt input in a single text prompt.
348+
Do not consider any input from the following that is not safe for work or appropriate for children.
356349
357-
350+
(Input)
358351
359-
One example of a meta prompt would be the following:
352+
```
360353

361-
354+
Now, let's see how we can use meta prompts in our demo.
362355

363-
```
356+
```python
357+
disallow_list = "swords, violence, blood, gore, nudity, sexual content, adult content, adult themes, adult language, adult humor, adult jokes, adult situations, adult"
364358

365-
You are an assistant designer that creates images for children.
359+
meta_prompt =f"""You are an assistant designer that creates images for children.
366360
367361
The image needs to be safe for work and appropriate for children.
368362
@@ -373,18 +367,16 @@ The image needs to be in landscape orientation.
373367
The image needs to be in a 16:9 aspect ratio.
374368
375369
Do not consider any input from the following that is not safe for work or appropriate for children.
370+
{disallow_list}
371+
"""
376372

377-
(Input)
378-
379-
```
380-
381-
382-
383-
Now, let's see how we can use meta prompts in our demo.
373+
prompt = f"{meta_prompt}
374+
Create an image of a bunny on a horse, holding a lollipop"
384375

385-
376+
# TODO add request to generate image
377+
```
386378

387-
TODO: Demo
379+
From the above prompt, you can see how all images being created considers the metaprompt.
388380

389381
## Assignment - let's enable students
390382

@@ -397,12 +389,77 @@ The students will create images for their assessments containing monuments, exac
397389
Here's one possible solution:
398390

399391
```python
392+
import openai
393+
import os
394+
import requests
395+
from PIL import Image
396+
import dotenv
397+
398+
# import dotenv
399+
dotenv.load_dotenv()
400+
401+
# Get endpoint and key from environment variables
402+
openai.api_base = "<replace with endpoint>"
403+
openai.api_key = "<replace with api key>"
404+
405+
# Assign the API version (DALL-E is currently supported for the 2023-06-01-preview API version only)
406+
openai.api_version = '2023-06-01-preview'
407+
openai.api_type = 'azure'
408+
409+
disallow_list = "swords, violence, blood, gore, nudity, sexual content, adult content, adult themes, adult language, adult humor, adult jokes, adult situations, adult"
410+
411+
meta_prompt =f"""You are an assistant designer that creates images for children.
412+
413+
The image needs to be safe for work and appropriate for children.
414+
415+
The image needs to be in color.
416+
417+
The image needs to be in landscape orientation.
418+
419+
The image needs to be in a 16:9 aspect ratio.
420+
421+
Do not consider any input from the following that is not safe for work or appropriate for children.
422+
{disallow_list}"""
423+
424+
prompt = f"""
425+
Generate monument of the Arc of Triumph in Paris, France, in the evening light with a small child holding a Teddy looks on.
426+
""""
427+
428+
try:
429+
# Create an image by using the image generation API
430+
generation_response = openai.Image.create(
431+
prompt=prompt, # Enter your prompt text here
432+
size='1024x1024',
433+
n=2,
434+
temperature=0,
435+
)
436+
# Set the directory for the stored image
437+
image_dir = os.path.join(os.curdir, 'images')
438+
439+
# If the directory doesn't exist, create it
440+
if not os.path.isdir(image_dir):
441+
os.mkdir(image_dir)
442+
443+
# Initialize the image path (note the filetype should be png)
444+
image_path = os.path.join(image_dir, 'generated_image.png')
445+
446+
# Retrieve the generated image
447+
image_url = generation_response["data"][0]["url"] # extract image URL from response
448+
generated_image = requests.get(image_url).content # download the image
449+
with open(image_path, "wb") as image_file:
450+
image_file.write(generated_image)
451+
452+
# Display the image in the default image viewer
453+
image = Image.open(image_path)
454+
image.show()
455+
456+
# catch exceptions
457+
except openai.error.InvalidRequestError as err:
458+
print(err)
400459
```
401460

402461
## Extra resources
403462

404-
405-
406463
- [DALL-E](https://arxiv.org/pdf/2102.12092.pdf)
407464

408465
- [OpenAI's DALL-E and CLIP 101: A Brief Introduction](https://towardsdatascience.com/openais-dall-e-and-clip-101-a-brief-introduction-3a4367280d4e)
3.02 MB
Loading

0 commit comments

Comments
 (0)