I tried to use DALL-E2 with Azure Resource. Image generation is work well, but Image Edit is doesn`t work and i got some error.
Here my code.
import os
import openai
import cv2
import urllib.request
import random
import time
import numpy as np
import logging
import requests
from PIL import Image
from glob import glob
import matplotlib.pyplot as plt
# 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'
img_list = glob('./default vs custom/*.jpg')
base_image = cv2.imread(img_list[0])
inpaint_top = 100
inpaint_bottom = 300
inpaint_width = 200
image_width = base_image.shape[1]
inpaint_left = int(random.random() * (image_width - inpaint_width))
inpaint_right = inpaint_left + inpaint_width
mask = 255 * np.ones_like(base_image)
mask[inpaint_top:inpaint_bottom, inpaint_left:inpaint_right, :] = 0
# Create an image by using the image generation API
generation_response = openai.Image.create_edit(
image=cv2.imencode('.png', base_image)[1],
mask=cv2.imencode('.png', mask)[1],
prompt='A painting of a cat.', # Enter your prompt text here
size='1024x1024',
n=1
)
# Set the directory for the stored image
image_dir = os.path.join(os.curdir, 'DALL-E_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_image5.png')
image_path = os.path.join(image_dir, 'edited_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()
And i got below error.
---------------------------------------------------------------------------
InvalidRequestError Traceback (most recent call last)
Cell In[22], line 25
20 mask[inpaint_top:inpaint_bottom, inpaint_left:inpaint_right, :] = 0
24 # Create an image by using the image generation API
---> 25 generation_response = openai.Image.create_edit(
26 image=cv2.imencode('.png', base_image)[1],
27 mask=cv2.imencode('.png', mask)[1],
28 prompt='A painting of a cat.', # Enter your prompt text here
29 size='1024x1024',
30 n=1
31 )
33 # generation_response = openai.Image.create(
34 # prompt='A painting of a cat.', # Enter your prompt text here
35 # size='1024x1024',
36 # n=2
37 # )
40 print(generation_response)
File c:\Users\my\anaconda3\envs\openai_0.28\lib\site-packages\openai\api_resources\image.py:237, in Image.create_edit(cls, image, mask, api_key, api_base, api_type, api_version, organization, **params)
224 raise error.InvalidAPIType("Edits are not supported by the Azure OpenAI API yet.")
226 requestor, url, files = cls._prepare_create_edit(
227 image,
228 mask,
(...)
234 **params,
235 )
--> 237 response, _, api_key = requestor.request("post", url, files=files)
239 return util.convert_to_openai_object(
240 response, api_key, api_version, organization
241 )
File c:\Users\my\anaconda3\envs\openai_0.28\lib\site-packages\openai\api_requestor.py:298, in APIRequestor.request(self, method, url, params, headers, files, stream, request_id, request_timeout)
277 def request(
278 self,
279 method,
(...)
286 request_timeout: Optional[Union[float, Tuple[float, float]]] = None,
287 ) -> Tuple[Union[OpenAIResponse, Iterator[OpenAIResponse]], bool, str]:
288 result = self.request_raw(
289 method.lower(),
290 url,
(...)
296 request_timeout=request_timeout,
297 )
--> 298 resp, got_stream = self._interpret_response(result, stream)
299 return resp, got_stream, self.api_key
File c:\Users\my\anaconda3\envs\openai_0.28\lib\site-packages\openai\api_requestor.py:700, in APIRequestor._interpret_response(self, result, stream)
692 return (
693 self._interpret_response_line(
694 line, result.status_code, result.headers, stream=True
695 )
696 for line in parse_stream(result.iter_lines())
697 ), True
698 else:
699 return (
--> 700 self._interpret_response_line(
701 result.content.decode("utf-8"),
702 result.status_code,
703 result.headers,
704 stream=False,
705 ),
706 False,
707 )
File c:\Users\my\anaconda3\envs\openai_0.28\lib\site-packages\openai\api_requestor.py:765, in APIRequestor._interpret_response_line(self, rbody, rcode, rheaders, stream)
763 stream_error = stream and "error" in resp.data
764 if stream_error or not 200 <= rcode < 300:
--> 765 raise self.handle_error_response(
766 rbody, rcode, resp.data, rheaders, stream_error=stream_error
767 )
768 return resp
InvalidRequestError: Resource not found
Image Edit in Azure is not applied yet?