mirror of
https://github.com/microsoft/generative-ai-for-beginners.git
synced 2026-06-05 21:07:14 +08:00
Chapter 9 - new 1.X SDK changes
This commit is contained in:
parent
b648d748cc
commit
bf53871746
@ -1,34 +1,42 @@
|
||||
import openai
|
||||
from openai import AzureOpenAI
|
||||
import os
|
||||
import requests
|
||||
from PIL import Image
|
||||
import dotenv
|
||||
import json
|
||||
|
||||
# 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']
|
||||
client = AzureOpenAI(
|
||||
api_key=os.environ['AZURE_OPENAI_KEY'], # this is also the default, it can be omitted
|
||||
api_version = "2023-12-01-preview",
|
||||
azure_endpoint=os.environ['AZURE_OPENAI_ENDPOINT']
|
||||
)
|
||||
|
||||
# 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'
|
||||
model = os.environ['AZURE_OPENAI_DEPLOYMENT']
|
||||
|
||||
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')
|
||||
print(image_path)
|
||||
image = Image.open(image_path)
|
||||
image.show()
|
||||
|
||||
# ---creating variation below---
|
||||
try:
|
||||
print("LOG creating variation")
|
||||
response = openai.Image.create_variation(
|
||||
image=open("generated-image.png", "rb"),
|
||||
result = client.images.create_variation(
|
||||
image=open(image_path, "rb"),
|
||||
n=1,
|
||||
size="1024x1024"
|
||||
)
|
||||
|
||||
client.images.create_variation()
|
||||
response = json.loads(result.model_dump_json())
|
||||
|
||||
image_path = os.path.join(image_dir, 'generated_variation.png')
|
||||
|
||||
image_url = response['data'][0]['url']
|
||||
@ -41,5 +49,8 @@ try:
|
||||
# Display the image in the default image viewer
|
||||
image = Image.open(image_path)
|
||||
image.show()
|
||||
except openai.error.InvalidRequestError as err:
|
||||
print(err)
|
||||
#except openai.error.InvalidRequestError as err:
|
||||
# print(err)
|
||||
|
||||
finally:
|
||||
print("completed!")
|
||||
@ -3,6 +3,7 @@ import os
|
||||
import requests
|
||||
from PIL import Image
|
||||
import dotenv
|
||||
import json
|
||||
|
||||
# import dotenv
|
||||
dotenv.load_dotenv()
|
||||
@ -12,7 +13,7 @@ dotenv.load_dotenv()
|
||||
# Assign the API version (DALL-E is currently supported for the 2023-06-01-preview API version only)
|
||||
client = AzureOpenAI(
|
||||
api_key=os.environ['AZURE_OPENAI_KEY'], # this is also the default, it can be omitted
|
||||
api_version = "2023-05-15",
|
||||
api_version = "2023-12-01-preview",
|
||||
azure_endpoint=os.environ['AZURE_OPENAI_ENDPOINT']
|
||||
)
|
||||
|
||||
@ -22,12 +23,14 @@ model = os.environ['AZURE_OPENAI_DEPLOYMENT']
|
||||
try:
|
||||
# Create an image by using the image generation API
|
||||
|
||||
generation_response = client.Image.create(
|
||||
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here
|
||||
result = client.images.generate(
|
||||
model=model,
|
||||
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils. It says "hello"', # Enter your prompt text here
|
||||
size='1024x1024',
|
||||
n=2,
|
||||
temperature=1,
|
||||
n=1
|
||||
)
|
||||
|
||||
generation_response = json.loads(result.model_dump_json())
|
||||
# Set the directory for the stored image
|
||||
image_dir = os.path.join(os.curdir, 'images')
|
||||
|
||||
@ -49,18 +52,15 @@ try:
|
||||
image.show()
|
||||
|
||||
# catch exceptions
|
||||
except client.error.InvalidRequestError as err:
|
||||
print(err)
|
||||
#except client.error.InvalidRequestError as err:
|
||||
# print(err)
|
||||
|
||||
finally:
|
||||
print("completed!")
|
||||
# ---creating variation below---
|
||||
|
||||
|
||||
response = client.Image.create_variation(
|
||||
image=open(image_path, "rb"),
|
||||
n=1,
|
||||
size="1024x1024"
|
||||
)
|
||||
=======
|
||||
|
||||
# response = openai.Image.create_variation(
|
||||
# image=open(image_path, "rb"),
|
||||
# n=1,
|
||||
|
||||
99
09-building-image-applications/solution.py
Normal file
99
09-building-image-applications/solution.py
Normal file
@ -0,0 +1,99 @@
|
||||
from openai import AzureOpenAI
|
||||
import os
|
||||
import requests
|
||||
from PIL import Image
|
||||
import dotenv
|
||||
import json
|
||||
|
||||
# import dotenv
|
||||
dotenv.load_dotenv()
|
||||
|
||||
|
||||
|
||||
# Assign the API version (DALL-E is currently supported for the 2023-06-01-preview API version only)
|
||||
client = AzureOpenAI(
|
||||
api_key=os.environ['AZURE_OPENAI_KEY'], # this is also the default, it can be omitted
|
||||
api_version = "2023-12-01-preview",
|
||||
azure_endpoint=os.environ['AZURE_OPENAI_ENDPOINT']
|
||||
)
|
||||
|
||||
model = os.environ['AZURE_OPENAI_DEPLOYMENT']
|
||||
|
||||
disallow_list = "swords, violence, blood, gore, nudity, sexual content, adult content, adult themes, adult language, adult humor, adult jokes, adult situations, adult"
|
||||
|
||||
meta_prompt = f"""You are an assistant designer that creates images for children.
|
||||
|
||||
The image needs to be safe for work and appropriate for children.
|
||||
|
||||
The image needs to be in color.
|
||||
|
||||
The image needs to be in landscape orientation.
|
||||
|
||||
The image needs to be in a 16:9 aspect ratio.
|
||||
|
||||
Do not consider any input from the following that is not safe for work or appropriate for children.
|
||||
{disallow_list}"""
|
||||
|
||||
prompt = f"""{meta_prompt}
|
||||
Generate monument of the Arc of Triumph in Paris, France, in the evening light with a small child holding a Teddy looks on.
|
||||
"""
|
||||
|
||||
|
||||
try:
|
||||
# Create an image by using the image generation API
|
||||
|
||||
result = client.images.generate(
|
||||
model=model,
|
||||
prompt=prompt, # Enter your prompt text here
|
||||
size='1024x1024',
|
||||
n=1
|
||||
)
|
||||
|
||||
generation_response = json.loads(result.model_dump_json())
|
||||
# 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, 'ch9-sol-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 client.error.InvalidRequestError as err:
|
||||
# print(err)
|
||||
|
||||
finally:
|
||||
print("completed!")
|
||||
# ---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…
Reference in New Issue
Block a user