import openai
# Setup
openai.api_key = 'xxxxxxxxxxxxxxxxxxxxxx'
openai.api_base = "xxxxxxxxxxxxxxxxxxxxxx"
openai.api_version = '2024-08-20' # Ensure this is correct
def test_openai():
try:
response = openai.Image.create(
prompt="A dog in rain image",
model="dall-e-3", # Try with a different model ifneeded
n=1,
size="1024x1024"
)
print(response)
except Exception as e:
print(f"Error: {e}")
test_openai()
Error: Resource not found
I have created azure open ai model dall-e-3
the api key and api base both worked with model gpt-35-turbo
The error you are getting because you need to use the Deployment name instead of the Model name.
You need to create client to connect to generate result.
Below code worked for me.
I am using a Flask app to use the openai code and view the generated image in index.html
file.
app.py
:
from flask import Flask,render_template
import os
from openai import AzureOpenAI
import json
app = Flask(__name__)
@app.route('/', methods=['GET'])
def fetch_image_url():
try:
client = AzureOpenAI(
api_version="2024-05-01-preview",
azure_endpoint="https://xxxxxxxxxx.openai.azure.com/",
api_key="xxxxxxxxxxxxxxxxxx"
)
result = client.images.generate(
model="dallechatgpt",
prompt="A dog in rain image",
n=1
)
image_url = json.loads(result.model_dump_json())['data'][0]['url']
return render_template('index.html',image_url=image_url)
except Exception as e:
print(f"error{e}")
if __name__== "__main__":
app.run()
templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{% if image %}
<h2>Generated Image:</h2>
<img src="{{ image }}" alt="Generated Image" style="width: auto; height: 50vh;">
{% endif %}
</body>
</html>
requirements.txt
:
flask
openai
OUTPUT
: