Files
wehub-resource-sync 3fbbd7970c
Code Quality / Python Lint & Format (push) Has been cancelled
Code Quality / Python Tests (push) Has been cancelled
Code Quality / JavaScript/TypeScript Lint (advisory) (push) Has been cancelled
Security Scan / CodeQL Analysis (python) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript-typescript) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:43:57 +08:00

99 lines
2.8 KiB
Python

from openai import AzureOpenAI, BadRequestError
import os
import requests
from PIL import Image
import dotenv
import json
# import dotenv
dotenv.load_dotenv()
# Assign the API version (check the Microsoft Foundry docs for the current API version required by your model)
client = AzureOpenAI(
api_key=os.environ['AZURE_OPENAI_API_KEY'], # this is also the default, it can be omitted
api_version = "2024-10-21",
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 BadRequestError as err:
# print(err)
finally:
print("completed!")
# ---creating variation below---
# response = client.images.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()