Skip to content

Commit 1e52f87

Browse files
authored
Merge pull request #854 from Bergvca/patch-1
Update session 9 to latest version of openai
2 parents 3743ec1 + 21bd560 commit 1e52f87

File tree

4 files changed

+114
-103
lines changed

4 files changed

+114
-103
lines changed

09-building-image-applications/README.md

Lines changed: 114 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,22 @@ So what does it take to build an image generation application? You need the foll
7878
- **pillow**, to work with images in Python.
7979
- **requests**, to help you make HTTP requests.
8080

81+
## Create and deploy an Azure OpenAI model
82+
83+
If not done already, follow the instructions on the [Microsoft Learn](https://learn.microsoft.com/azure/ai-foundry/openai/how-to/create-resource?pivots=web-portal) page
84+
to create an Azure OpenAI resource and model. Select DALL-E 3 as model.
85+
86+
## Create the app
87+
8188
1. Create a file _.env_ with the following content:
8289

8390
```text
8491
AZURE_OPENAI_ENDPOINT=<your endpoint>
8592
AZURE_OPENAI_API_KEY=<your key>
93+
AZURE_OPENAI_DEPLOYMENT="dall-e-3"
8694
```
8795

88-
Locate this information in Azure Portal for your resource in the "Keys and Endpoint" section.
96+
Locate this information in Azure OpenAI Foundry Portal for your resource in the "Deployments" section.
8997

9098
1. Collect the above libraries in a file called _requirements.txt_ like so:
9199

@@ -113,57 +121,54 @@ So what does it take to build an image generation application? You need the foll
113121

114122
1. Add the following code in file called _app.py_:
115123

116-
```python
117-
import openai
118-
import os
119-
import requests
120-
from PIL import Image
121-
import dotenv
122-
123-
# import dotenv
124-
dotenv.load_dotenv()
125-
126-
# Get endpoint and key from environment variables
127-
openai.api_base = os.environ['AZURE_OPENAI_ENDPOINT']
128-
openai.api_key = os.environ['AZURE_OPENAI_API_KEY']
129-
130-
# Assign the API version (DALL-E is currently supported for the 2023-06-01-preview API version only)
131-
openai.api_version = '2023-06-01-preview'
132-
openai.api_type = 'azure'
133-
134-
135-
try:
136-
# Create an image by using the image generation API
137-
generation_response = openai.Image.create(
138-
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here
139-
size='1024x1024',
140-
n=2,
141-
temperature=0,
142-
)
143-
# Set the directory for the stored image
144-
image_dir = os.path.join(os.curdir, 'images')
145-
146-
# If the directory doesn't exist, create it
147-
if not os.path.isdir(image_dir):
148-
os.mkdir(image_dir)
149-
150-
# Initialize the image path (note the filetype should be png)
151-
image_path = os.path.join(image_dir, 'generated-image.png')
152-
153-
# Retrieve the generated image
154-
image_url = generation_response["data"][0]["url"] # extract image URL from response
155-
generated_image = requests.get(image_url).content # download the image
156-
with open(image_path, "wb") as image_file:
157-
image_file.write(generated_image)
158-
159-
# Display the image in the default image viewer
160-
image = Image.open(image_path)
161-
image.show()
162-
163-
# catch exceptions
164-
except openai.InvalidRequestError as err:
165-
print(err)
166-
124+
```python
125+
import openai
126+
import os
127+
import requests
128+
from PIL import Image
129+
import dotenv
130+
from openai import OpenAI, AzureOpenAI
131+
132+
# import dotenv
133+
dotenv.load_dotenv()
134+
135+
# configure Azure OpenAI service client
136+
client = AzureOpenAI(
137+
azure_endpoint = os.environ["AZURE_OPENAI_ENDPOINT"],
138+
api_key=os.environ['AZURE_OPENAI_API_KEY'],
139+
api_version = "2024-02-01"
140+
)
141+
try:
142+
# Create an image by using the image generation API
143+
generation_response = client.images.generate(
144+
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils',
145+
size='1024x1024', n=1,
146+
model=os.environ['AZURE_OPENAI_DEPLOYMENT']
147+
)
148+
149+
# Set the directory for the stored image
150+
image_dir = os.path.join(os.curdir, 'images')
151+
152+
# If the directory doesn't exist, create it
153+
if not os.path.isdir(image_dir):
154+
os.mkdir(image_dir)
155+
156+
# Initialize the image path (note the filetype should be png)
157+
image_path = os.path.join(image_dir, 'generated-image.png')
158+
159+
# Retrieve the generated image
160+
image_url = generation_response.data[0].url # extract image URL from response
161+
generated_image = requests.get(image_url).content # download the image
162+
with open(image_path, "wb") as image_file:
163+
image_file.write(generated_image)
164+
165+
# Display the image in the default image viewer
166+
image = Image.open(image_path)
167+
image.show()
168+
169+
# catch exceptions
170+
except openai.InvalidRequestError as err:
171+
print(err)
167172
```
168173

169174
Let's explain this code:
@@ -185,28 +190,26 @@ Let's explain this code:
185190
dotenv.load_dotenv()
186191
```
187192

188-
- After that, we set the endpoint, key for the OpenAI API, version and type.
193+
- After that, we configure Azure OpenAI service client
189194

190195
```python
191196
# Get endpoint and key from environment variables
192-
openai.api_base = os.environ['AZURE_OPENAI_ENDPOINT']
193-
openai.api_key = os.environ['AZURE_OPENAI_API_KEY']
194-
195-
# add version and type, Azure specific
196-
openai.api_version = '2023-06-01-preview'
197-
openai.api_type = 'azure'
197+
client = AzureOpenAI(
198+
azure_endpoint = os.environ["AZURE_OPENAI_ENDPOINT"],
199+
api_key=os.environ['AZURE_OPENAI_API_KEY'],
200+
api_version = "2024-02-01"
201+
)
198202
```
199203

200204
- Next, we generate the image:
201205

202206
```python
203207
# Create an image by using the image generation API
204-
generation_response = openai.Image.create(
205-
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here
206-
size='1024x1024',
207-
n=2,
208-
temperature=0,
209-
)
208+
generation_response = client.images.generate(
209+
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils',
210+
size='1024x1024', n=1,
211+
model=os.environ['AZURE_OPENAI_DEPLOYMENT']
212+
)
210213
```
211214

212215
The above code responds with a JSON object that contains the URL of the generated image. We can use the URL to download the image and save it to a file.
@@ -222,14 +225,13 @@ Let's explain this code:
222225

223226
Let's look at the code that generates the image in more detail:
224227

225-
```python
226-
generation_response = openai.Image.create(
227-
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here
228-
size='1024x1024',
229-
n=2,
230-
temperature=0,
231-
)
232-
```
228+
```python
229+
generation_response = client.images.generate(
230+
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils',
231+
size='1024x1024', n=1,
232+
model=os.environ['AZURE_OPENAI_DEPLOYMENT']
233+
)
234+
```
233235

234236
- **prompt**, is the text prompt that is used to generate the image. In this case, we're using the prompt "Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils".
235237
- **size**, is the size of the image that is generated. In this case, we're generating an image that is 1024x1024 pixels.
@@ -244,20 +246,29 @@ You've seen so far how we were able to generate an image using a few lines in Py
244246

245247
You can also do the following:
246248

247-
- **Perform edits**. By providing an existing image a mask and a prompt, you can alter an image. For example, you can add something to a portion of an image. Imagine our bunny image, you can add a hat to the bunny. How you would do that is by providing the image, a mask (identifying the part of the area for the change) and a text prompt to say what should be done.
249+
- **Perform edits**. By providing an existing image a mask and a prompt, you can alter an image. For example, you can add something to a portion of an image. Imagine our bunny image, you can add a hat to the bunny. How you would do that is by providing the image, a mask (identifying the part of the area for the change) and a text prompt to say what should be done.
250+
> Note: this is not supported in DALL-E 3.
251+
252+
Here is an example using GPT Image:
253+
254+
```python
255+
response = client.images.edit(
256+
model="gpt-image-1",
257+
image=open("sunlit_lounge.png", "rb"),
258+
mask=open("mask.png", "rb"),
259+
prompt="A sunlit indoor lounge area with a pool containing a flamingo"
260+
)
261+
image_url = response.data[0].url
262+
```
248263

249-
```python
250-
response = openai.Image.create_edit(
251-
image=open("base_image.png", "rb"),
252-
mask=open("mask.png", "rb"),
253-
prompt="An image of a rabbit with a hat on its head.",
254-
n=1,
255-
size="1024x1024"
256-
)
257-
image_url = response['data'][0]['url']
258-
```
264+
The base image would only contain the lounge with pool but the final image would have a flamingo:
265+
266+
<div style="display: flex; justify-content: space-between; align-items: center; margin: 20px 0;">
267+
<img src="./images/sunlit_lounge.png" style="width: 30%; max-width: 200px; height: auto;">
268+
<img src="./images/mask.png" style="width: 30%; max-width: 200px; height: auto;">
269+
<img src="./images/sunlit_lounge_result.png" style="width: 30%; max-width: 200px; height: auto;">
270+
</div>
259271

260-
The base image would only contain the rabbit but the final image would have the hat on the rabbit.
261272

262273
- **Create variations**. The idea is that you take an existing image and ask that variations are created. To create a variation, you provide an image and a text prompt and code like so:
263274

@@ -280,16 +291,16 @@ Let's look at an example of how temperature works, by running this prompt twice:
280291

281292
> Prompt : "Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils"
282293
283-
![Bunny on a horse holding a lollipop, version 1](./images/v1-generated-image.png?WT.mc_id=academic-105485-koreyst)
294+
![Bunny on a horse holding a lollipop, version 1](./images/v1-generated-image.png)
284295

285296
Now let's run that same prompt just to see that we won't get the same image twice:
286297

287-
![Generated image of bunny on horse](./images/v2-generated-image.png?WT.mc_id=academic-105485-koreyst)
298+
![Generated image of bunny on horse](./images/v2-generated-image.png)
288299

289300
As you can see, the images are similar, but not the same. Let's try changing the temperature value to 0.1 and see what happens:
290301

291302
```python
292-
generation_response = openai.Image.create(
303+
generation_response = client.images.create(
293304
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here
294305
size='1024x1024',
295306
n=2
@@ -303,7 +314,7 @@ So let's try to make the response more deterministic. We could observe from the
303314
Let's therefore change our code and set the temperature to 0, like so:
304315

305316
```python
306-
generation_response = openai.Image.create(
317+
generation_response = client.images.create(
307318
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here
308319
size='1024x1024',
309320
n=2,
@@ -313,8 +324,8 @@ generation_response = openai.Image.create(
313324

314325
Now when you run this code, you get these two images:
315326

316-
- ![Temperature 0, v1](./images/v1-temp-generated-image.png?WT.mc_id=academic-105485-koreyst)
317-
- ![Temperature 0 , v2](./images/v2-temp-generated-image.png?WT.mc_id=academic-105485-koreyst)
327+
- ![Temperature 0, v1](./images/v1-temp-generated-image.png)
328+
- ![Temperature 0 , v2](./images/v2-temp-generated-image.png)
318329

319330
Here you can clearly see how the images resemble each other more.
320331

@@ -394,17 +405,17 @@ import os
394405
import requests
395406
from PIL import Image
396407
import dotenv
397-
408+
from openai import AzureOpenAI
398409
# import dotenv
399410
dotenv.load_dotenv()
400411

401412
# Get endpoint and key from environment variables
402-
openai.api_base = "<replace with endpoint>"
403-
openai.api_key = "<replace with api key>"
413+
client = AzureOpenAI(
414+
azure_endpoint = os.environ["AZURE_OPENAI_ENDPOINT"],
415+
api_key=os.environ['AZURE_OPENAI_API_KEY'],
416+
api_version = "2024-02-01"
417+
)
404418

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'
408419

409420
disallow_list = "swords, violence, blood, gore, nudity, sexual content, adult content, adult themes, adult language, adult humor, adult jokes, adult situations, adult"
410421

@@ -419,19 +430,19 @@ The image needs to be in landscape orientation.
419430
The image needs to be in a 16:9 aspect ratio.
420431
421432
Do not consider any input from the following that is not safe for work or appropriate for children.
422-
{disallow_list}"""
433+
{disallow_list}
434+
"""
423435

424436
prompt = f"""{meta_prompt}
425437
Generate monument of the Arc of Triumph in Paris, France, in the evening light with a small child holding a Teddy looks on.
426438
""""
427439

428440
try:
429441
# Create an image by using the image generation API
430-
generation_response = openai.Image.create(
442+
generation_response = client.images.generate(
431443
prompt=prompt, # Enter your prompt text here
432444
size='1024x1024',
433-
n=2,
434-
temperature=0,
445+
n=1,
435446
)
436447
# Set the directory for the stored image
437448
image_dir = os.path.join(os.curdir, 'images')
@@ -444,7 +455,7 @@ try:
444455
image_path = os.path.join(image_dir, 'generated-image.png')
445456

446457
# Retrieve the generated image
447-
image_url = generation_response["data"][0]["url"] # extract image URL from response
458+
image_url = generation_response.data[0].url # extract image URL from response
448459
generated_image = requests.get(image_url).content # download the image
449460
with open(image_path, "wb") as image_file:
450461
image_file.write(generated_image)
@@ -454,7 +465,7 @@ try:
454465
image.show()
455466

456467
# catch exceptions
457-
except openai.InvalidRequestError as err:
468+
except openai.BadRequestError as err:
458469
print(err)
459470
```
460471

532 KB
Loading
557 KB
Loading
533 KB
Loading

0 commit comments

Comments
 (0)