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()
Ошибка: Ресурс не найден.
Я создал модель Azure Open AI Dall-e-3.
ключ API и база API работали с моделью gpt-35-turbo
🤔 А знаете ли вы, что...
В Python есть инструменты для создания графиков и визуализации данных, такие как библиотеки Matplotlib и Seaborn.
Поскольку название модели зависит от развертывания, обратитесь к последней документации: документация Azure
Ошибка, которую вы получаете, потому что вам нужно использовать имя развертывания вместо имени модели.
Вам необходимо создать клиент для подключения для получения результата.
Ниже код работал у меня.
Я использую приложение Flask, чтобы использовать код openai и просматривать сгенерированное изображение в файле index.html
.
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
: