If needed this is my code that works perfectly 100% of the time now:
import os
import requests
import base64
import shutil
def generate_gpt_images_copy(image_prompt: str):
“”"
Allows your Pickaxe to generate GPT Images.
Args:
image_prompt (string): Prompt for the image to be generated
Envs:
OPENAI_API_KEY (string): Your OpenAI API key, required for generating images with GPT Image
"""
# Insert your PYTHON code below. You can access environment variables using os.environ[].
# Currently, only the requests library is supported, but more libraries will be available soon.
# Use print statements or return values to display results to the user.
# If you save a png, pdf, csv, jpg, webp, gif, or html file in the root directory, it will be automatically displayed to the user.
# You do not have to call this function as the bot will automatically call and fill in the parameters.
api_key = os.environ["OPENAI_API_KEY"]
api_url = "https://api.openai.com/v1/images/generations"
headers = {
"Content-Type": "application/json",
}
headers["Authorization"] = f"Bearer {api_key}"
data = {"prompt": image_prompt, "model": "gpt-image-1", "size": "auto"}
response = requests.post(api_url, headers=headers, json=data) # <- how long to wait (in seconds) before aborting the HTTP request
if response.status_code == 200:
image_base64 = response.json()["data"][0]["b64_json"]
image_bytes = base64.b64decode(image_base64)
output_file = "gpt-image.png"
with open(output_file, "wb") as f:
f.write(image_bytes)
#shutil.copy(output_file, "generated_image.png")
else:
print("Failed to generate image")