pythonazurechatgpt-apiazure-openaichat-gpt-4

Unable to properly generate dalle-3 logo images using python


If I am giving prompt in azure open ai studio > playground > dall-e-3 using api version 2024-05-01-preview" .it is generating perfect image but the same prompt if I am using it in my python to generate a total different kind of logo is generated (it is not in readable lang The image was generated but not as logo) in python i am using api version 2023-06-01-preview but if i am using 2024-05-01-preview" it is showing error :resource not found how to set that

prompt is: create a circular logo on the name: python - python beginner and also Raj name on the circle border

i have changed openai==0.28 to latest openai version but still unable to get it openai = 1.42.0 then i got the error Error generating image:

You tried to access openai.Image, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run openai migrate to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. pip install openai==0.28

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

jayashankarGS code error :

Traceback (most recent call last): File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpx_transports\default.py", line 69, in map_httpcore_exceptions yield File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpx_transports\default.py", line 233, in handle_request resp = self._pool.handle_request(req) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpcore_sync\connection_pool.py", line 216, in handle_request raise exc from None File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpcore_sync\connection_pool.py", line 196, in handle_request response = connection.handle_request( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpcore_sync\connection.py", line 99, in handle_request raise exc File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpcore_sync\connection.py", line 76, in handle_request stream = self._connect(request) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpcore_sync\connection.py", line 122, in _connect stream = self._network_backend.connect_tcp(**kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpcore_backends\sync.py", line 205, in connect_tcp with map_exceptions(exc_map): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\contextlib.py", line 158, in exit self.gen.throw(typ, value, traceback) File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpcore_exceptions.py", line 14, in map_exceptions raise to_exc(exc) from exc httpcore.ConnectError: [Errno 11002] getaddrinfo failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai_base_client.py", line 973, in _request response = self._client.send( ^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpx_client.py", line 914, in send response = self._send_handling_auth( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpx_client.py", line 942, in _send_handling_auth response = self._send_handling_redirects( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpx_client.py", line 979, in _send_handling_redirects response = self._send_single_request(request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpx_client.py", line 1015, in _send_single_request response = transport.handle_request(request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpx_transports\default.py", line 232, in handle_request with map_httpcore_exceptions(): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\contextlib.py", line 158, in exit self.gen.throw(typ, value, traceback) File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\httpx_transports\default.py", line 86, in map_httpcore_exceptions raise mapped_exc(message) from exc httpx.ConnectError: [Errno 11002] getaddrinfo failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "c:\Users\Comp\Desktop\testing.py", line 12, in result = client.images.generate( ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai\resources\images.py", line 253, in generate return self._post( ^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai_base_client.py", line 1260, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai_base_client.py", line 937, in request return self._request( ^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai_base_client.py", line 997, in _request return self._retry_request( ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai_base_client.py", line 1075, in _retry_request return self._request( ^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai_base_client.py", line 997, in _request return self._retry_request( ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai_base_client.py", line 1075, in _retry_request return self._request( ^^^^^^^^^^^^^^ File "C:\Users\Comp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai_base_client.py", line 1007, in _request raise APIConnectionError(request=request) from err openai.APIConnectionError: Connection error. PS C:\Users\Comp>

Prudhvi code:

import openai
import os

# Set your Azure OpenAI credentials and endpoint
openai.api_key = os.getenv("AZURE_OPENAI_API_KEY", "xxxxxxxxxx")
openai.api_base = "xxxxxxxxxxxxxxx"
openai.api_type = "azure"             
openai.api_version = "2023-06-01-preview"  # Adjust this based on your Azure API version

def generate_image(prompt):
    try:
        # Make a request to Azure OpenAI to generate an image with DALL-E 3
        response = openai.Image.create(
            prompt=prompt,
            n=1,  # Number of images
            size="1024x1024"  # Adjust size if needed
        )

        # Extract the URL of the generated image
        image_url = response['data'][0]['url']
        print(f"Image URL: {image_url}")

        return image_url

    except Exception as e:
        print(f"Error generating image: {e}")
        return None

if __name__ == "__main__":
    # Example prompt to generate an image
    prompt = "  create a circular logo on the name: python - python beginner   "
    image_url = generate_image(prompt)

    if image_url:
        print(f"Generated image available at: {image_url}")

Solution

  • You tried to access openai.Image, but this is no longer supported in openai>=1.0.0.

    Also, according to this documentation you use Azureopenai class providing the model name.

    Make sure you have already deployed the dall-e-3 model. If you deploying the model freshly you need to wait 4-7 minutes.

    enter image description here

    Use below code.

    import os
    import httpx
    from openai import AzureOpenAI
    from PIL import Image
    
    client = AzureOpenAI(
        api_version="2024-05-01-preview",  
        api_key="131xxxxxxxxxx",  
        azure_endpoint="https://opexx.openai.azure.com/"
    )
                
    result = client.images.generate(
        model="dall-e-3",
        prompt="create a circular logo on the name: python - python beginner and also Raj name on the circle border",
        n=1
     )
                
    image_dir = os.path.join(os.curdir, 'images')
                
    
    if not os.path.isdir(image_dir):
        os.mkdir(image_dir)
                
    image_path = os.path.join(image_dir, 'generated_image.png')
                
    image_url = result.data[0].url  
    generated_image = httpx.get(image_url).content  
    with open(image_path, "wb") as image_file:
        image_file.write(generated_image)
        
    image  =  Image.open(image_path)
    image
    

    Output:

    enter image description here

    This code can be used for both dall-e-2 and dall-e-3.