Skip to content

Commit bd5d1d4

Browse files
Updated instructions and code for DALL-E 3 (MicrosoftLearning#55)
1 parent 6266487 commit bd5d1d4

File tree

3 files changed

+32
-62
lines changed

3 files changed

+32
-62
lines changed

Instructions/Exercises/05-generate-images.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,32 @@ lab:
77

88
The Azure OpenAI Service includes an image-generation model named DALL-E. You can use this model to submit natural language prompts that describe a desired image, and the model will generate an original image based on the description you provide.
99

10+
In this exercise, you'll use a DALL-E version 3 model to generate images based on natural language prompts.
11+
1012
This exercise will take approximately **25** minutes.
1113

1214
## Provision an Azure OpenAI resource
1315

14-
Before you can use Azure OpenAI models, you must provision an Azure OpenAI resource in your Azure subscription.
16+
Before you can use Azure OpenAI to generate images, you must provision an Azure OpenAI resource in your Azure subscription. The resource must be in a region where DALL-E models are supported.
1517

1618
1. Sign into the **Azure portal** at `https://portal.azure.com`.
1719
2. Create an **Azure OpenAI** resource with the following settings:
1820
- **Subscription**: *Select an Azure subscription that has been approved for access to the Azure OpenAI service, including DALL-E*
1921
- **Resource group**: *Choose or create a resource group*
20-
- **Region**: **East US**\*
22+
- **Region**: *Choose either **East US** or **Sweden Central***\*
2123
- **Name**: *A unique name of your choice*
2224
- **Pricing tier**: Standard S0
2325

24-
> \* DALL-E models are only available in Azure OpenAI service resources in the **East US** region.
26+
> \* DALL-E 3 models are only available in Azure OpenAI service resources in the **East US** and **Sweden Central** regions.
2527
2628
3. Wait for deployment to complete. Then go to the deployed Azure OpenAI resource in the Azure portal.
2729

2830
## Explore image-generation in the DALL-E playground
2931

3032
You can use the DALL-E playground in **Azure OpenAI Studio** to experiment with image-generation.
3133

32-
1. In the Azure portal, on the **Overview** page for your Azure OpenAI resource, use the **Explore** button to open Azure OpenAI Studio in a new browser tab. Alternatively, navigate to [Azure OpenAI Studio](https://oai.azure.com) directly.
33-
2. In the **Playground** section, select the **DALL-E** playground.
34+
1. In the Azure portal, on the **Overview** page for your Azure OpenAI resource, use the **Explore** button to open Azure OpenAI Studio in a new browser tab. Alternatively, navigate to [Azure OpenAI Studio](https://oai.azure.com) directly at `https://oai.azure.com`.
35+
2. In the **Playground** section, select the **DALL-E** playground. A deployment of the DALL-E model named *Dalle3* will be created automatically.
3436
3. In the **Prompt** box, enter a description of an image you'd like to generate. For example, `An elephant on a skateboard` Then select **Generate** and view the image that is generated.
3537

3638
![The DALL-E Playground in Azure OpenAI Studio with a generated image.](../media/dall-e-playground.png)
@@ -61,7 +63,7 @@ Now let's explore how you could build a custom app that uses Azure OpenAI servic
6163
6264
### Configure your application
6365

64-
Applications for both C# and Python have been provided. Both apps feature the same functionality. First, you'll complete some key parts of the application to enable using your Azure OpenAI resource.
66+
Applications for both C# and Python have been provided. Both apps feature the same functionality. First, you'll add the endpoint and key for your Azure OpenAI resource to the app's configuration file.
6567

6668
1. In Visual Studio Code, in the **Explorer** pane, browse to the **Labfiles/05-image-generation** folder and expand the **CSharp** or **Python** folder depending on your language preference. Each folder contains the language-specific files for an app into which you're you're going to integrate Azure OpenAI functionality.
6769
2. In the **Explorer** pane, in the **CSharp** or **Python** folder, open the configuration file for your preferred language
@@ -82,14 +84,9 @@ Now you're ready to explore the code used to call the REST API and generate an i
8284
- Python: `generate-image.py`
8385

8486
2. Review the code that the file contains, noting the following key features:
85-
- The code makes https requests to the endpoint for your service, including the key for your service in the header. Both of these values are obtained from the configuration file.
86-
- The process consists of <u>two</u> REST requests: One to initiate the image-generation request, and another to retrieve the results.
87-
The initial request includes the following data:
88-
- The user-provided prompt that describes the image to be generated
89-
- The number of images to be generated (in this case, 1)
90-
- The resolution (size) of the image to be generated.
91-
- The response header from the initial request includes an **operation-location** value that is used for the subsequent callback to get the results.
92-
- The code polls the callback URL until the status of the image-generation task is *succeeded*, and then extracts and displays a URL for the generated image.
87+
- The code makes an https request to the endpoint for your service, including the key for your service in the header. Both of these values are obtained from the configuration file.
88+
- The request includes some parameters, including the prompt from on the image should be based, the number of images to generate, and the size of the generated image(s).
89+
- The response includes a revised prompt that the DALL-E model extrapolated from the user-provided prompt to make it more descriptive, and the URL for the generated image.
9390

9491
### Run the app
9592

@@ -112,9 +109,9 @@ Now that you've reviewed the code, it's time to run it and generate some images.
112109

113110
4. Wait for the image to be generated - a hyperlink will be displayed in the terminal pane. Then select the hyperlink to open a new browser tab and review the image that was generated.
114111

115-
> **TIP**: If the app can't find the header, wait a minute and try again. Newly deployed resources can take up to 5 minutes to become available.
112+
> **TIP**: If the app doesn't return a response, wait a minute and try again. Newly deployed resources can take up to 5 minutes to become available.
116113
117-
6. Close the browser tab containing the generated image and re-run the app to generate a new image with a different prompt.
114+
5. Close the browser tab containing the generated image and re-run the app to generate a new image with a different prompt.
118115

119116
## Clean up
120117

Labfiles/05-image-generation/CSharp/Program.cs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,33 @@ static async Task Main(string[] args)
2929
Console.WriteLine("Enter a prompt to request an image:");
3030
string prompt = Console.ReadLine() ?? "";
3131

32-
// Make the initial call to start the job
32+
// Call the DALL-E model
3333
using (var client = new HttpClient())
3434
{
3535
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
36-
var api = "openai/images/generations:submit?api-version=2023-06-01-preview";
36+
var api = "openai/deployments/dalle3/images/generations?api-version=2024-02-15-preview";
3737
client.BaseAddress = new Uri(aoaiEndpoint);
3838
client.DefaultRequestHeaders.Accept.Add(contentType);
3939
client.DefaultRequestHeaders.Add("api-key", aoaiKey);
4040
var data = new
4141
{
4242
prompt=prompt,
4343
n=1,
44-
size="512x512"
44+
size="1024x1024"
4545
};
4646

4747
var jsonData = JsonSerializer.Serialize(data);
4848
var contentData = new StringContent(jsonData, Encoding.UTF8, "application/json");
49-
var init_response = await client.PostAsync(api, contentData);
49+
var response = await client.PostAsync(api, contentData);
5050

51-
// Get the operation-location URL for the callback
52-
var callback_url = init_response.Headers.GetValues("operation-location").FirstOrDefault();
53-
54-
// Poll the callback URL until the job has succeeeded (or 100 attempts)
55-
var response = await client.GetAsync(callback_url);
51+
// Get the revised prompt and image URL from the response
5652
var stringResponse = await response.Content.ReadAsStringAsync();
57-
var status = JsonSerializer.Deserialize<Dictionary<string,object>>(stringResponse)["status"];
58-
var tries = 1;
59-
while (status.ToString() != "succeeded" && tries < 101)
60-
{
61-
Thread.Sleep (3000); // wait 3 seconds to avoid rate limit
62-
tries ++;
63-
response = await client.GetAsync(callback_url);
64-
stringResponse = await response.Content.ReadAsStringAsync();
65-
status = JsonSerializer.Deserialize<Dictionary<string,object>>(stringResponse)["status"];
66-
Console.WriteLine(tries.ToString() + ": " + status);
67-
}
68-
69-
// Get the results
70-
stringResponse = await response.Content.ReadAsStringAsync();
7153
JsonNode contentNode = JsonNode.Parse(stringResponse)!;
72-
JsonNode resultNode = contentNode!["result"];
73-
JsonNode dataNode = resultNode!["data"];
74-
JsonNode urlNode = dataNode[0]!;
75-
JsonNode url = urlNode!["url"];
76-
77-
// Display the URL for the generated image
54+
JsonNode dataCollectionNode = contentNode!["data"];
55+
JsonNode dataNode = dataCollectionNode[0]!;
56+
JsonNode revisedPrompt = dataNode!["revised_prompt"];
57+
JsonNode url = dataNode!["url"];
58+
Console.WriteLine(revisedPrompt.ToJsonString());
7859
Console.WriteLine(url.ToJsonString().Replace(@"\u0026", "&"));
7960

8061
}

Labfiles/05-image-generation/Python/generate-image.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,27 @@ def main():
1010
load_dotenv()
1111
api_base = os.getenv("AZURE_OAI_ENDPOINT")
1212
api_key = os.getenv("AZURE_OAI_KEY")
13-
api_version = '2023-06-01-preview'
13+
api_version = '2024-02-15-preview'
1414

1515
# Get prompt for image to be generated
1616
prompt = input("\nEnter a prompt to request an image: ")
1717

18-
# Make the initial call to start the job
19-
url = "{}openai/images/generations:submit?api-version={}".format(api_base, api_version)
18+
# Call the DALL-E model
19+
url = "{}openai/deployments/dalle3/images/generations?api-version={}".format(api_base, api_version)
2020
headers= { "api-key": api_key, "Content-Type": "application/json" }
2121
body = {
2222
"prompt": prompt,
2323
"n": 1,
24-
"size": "512x512"
24+
"size": "1024x1024"
2525
}
26-
submission = requests.post(url, headers=headers, json=body)
26+
response = requests.post(url, headers=headers, json=body)
2727

28-
# Get the operation-location URL for the callback
29-
operation_location = submission.headers['Operation-Location']
30-
31-
# Poll the callback URL until the job has succeeeded
32-
status = ""
33-
while (status != "succeeded"):
34-
time.sleep(3) # wait 3 seconds to avoid rate limit
35-
response = requests.get(operation_location, headers=headers)
36-
status = response.json()['status']
37-
38-
# Get the results
39-
image_url = response.json()['result']['data'][0]['url']
28+
# Get the revised prompt and image URL from the response
29+
revised_prompt = response.json()['data'][0]['revised_prompt']
30+
image_url = response.json()['data'][0]['url']
4031

4132
# Display the URL for the generated image
33+
print(revised_prompt)
4234
print(image_url)
4335

4436

0 commit comments

Comments
 (0)