forked from microsoft/generative-ai-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request microsoft#30 from softchris/Chapter9
Adding Chapter 9
- Loading branch information
Showing
14 changed files
with
969 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import openai | ||
import os | ||
import requests | ||
from PIL import Image | ||
import dotenv | ||
|
||
# import dotenv | ||
dotenv.load_dotenv() | ||
|
||
# Get endpoint and key from environment variables | ||
openai.api_base = os.environ['AZURE_OPENAI_ENDPOINT'] | ||
openai.api_key = os.environ['AZURE_OPENAI_KEY'] | ||
|
||
# Assign the API version (DALL-E is currently supported for the 2023-06-01-preview API version only) | ||
openai.api_version = '2023-06-01-preview' | ||
openai.api_type = 'azure' | ||
|
||
image_dir = os.path.join(os.curdir, 'images') | ||
|
||
# Initialize the image path (note the filetype should be png) | ||
image_path = os.path.join(image_dir, 'generated_image.png') | ||
|
||
# ---creating variation below--- | ||
try: | ||
print("LOG creating variation") | ||
response = openai.Image.create_variation( | ||
image=open("generated_image.png", "rb"), | ||
n=1, | ||
size="1024x1024" | ||
) | ||
|
||
image_path = os.path.join(image_dir, 'generated_variation.png') | ||
|
||
image_url = response['data'][0]['url'] | ||
|
||
print("LOG downloading image") | ||
generated_image = requests.get(image_url).content # download the image | ||
with open(image_path, "wb") as image_file: | ||
image_file.write(generated_image) | ||
|
||
# Display the image in the default image viewer | ||
image = Image.open(image_path) | ||
image.show() | ||
except openai.error.InvalidRequestError as err: | ||
print(err) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import openai | ||
import os | ||
import requests | ||
from PIL import Image | ||
import dotenv | ||
|
||
# import dotenv | ||
dotenv.load_dotenv() | ||
|
||
# Get endpoint and key from environment variables | ||
openai.api_base = os.environ['AZURE_OPENAI_ENDPOINT'] | ||
openai.api_key = os.environ['AZURE_OPENAI_KEY'] | ||
|
||
# Assign the API version (DALL-E is currently supported for the 2023-06-01-preview API version only) | ||
openai.api_version = '2023-06-01-preview' | ||
openai.api_type = 'azure' | ||
|
||
|
||
try: | ||
# Create an image by using the image generation API | ||
generation_response = openai.Image.create( | ||
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here | ||
size='1024x1024', | ||
n=2, | ||
temperature=0, | ||
) | ||
# Set the directory for the stored image | ||
image_dir = os.path.join(os.curdir, 'images') | ||
|
||
# If the directory doesn't exist, create it | ||
if not os.path.isdir(image_dir): | ||
os.mkdir(image_dir) | ||
|
||
# Initialize the image path (note the filetype should be png) | ||
image_path = os.path.join(image_dir, 'generated_image.png') | ||
|
||
# Retrieve the generated image | ||
image_url = generation_response["data"][0]["url"] # extract image URL from response | ||
generated_image = requests.get(image_url).content # download the image | ||
with open(image_path, "wb") as image_file: | ||
image_file.write(generated_image) | ||
|
||
# Display the image in the default image viewer | ||
image = Image.open(image_path) | ||
image.show() | ||
|
||
# catch exceptions | ||
except openai.error.InvalidRequestError as err: | ||
print(err) | ||
|
||
# ---creating variation below--- | ||
|
||
response = openai.Image.create_variation( | ||
image=open(image_path, "rb"), | ||
n=1, | ||
size="1024x1024" | ||
) | ||
|
||
image_path = os.path.join(image_dir, 'generated_variation.png') | ||
|
||
image_url = response['data'][0]['url'] | ||
|
||
generated_image = requests.get(image_url).content # download the image | ||
with open(image_path, "wb") as image_file: | ||
image_file.write(generated_image) | ||
|
||
# Display the image in the default image viewer | ||
image = Image.open(image_path) | ||
image.show() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.