-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.py
42 lines (36 loc) · 1.2 KB
/
basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
from dotenv import load_dotenv
from openai import OpenAI
import traceback
# Load the API key from the .env file
load_dotenv()
API_KEY = os.getenv('OPENAI_API_KEY')
if not API_KEY:
print("Failed to load API KEY. Check your .env file.")
exit(1)
# Initialize the OpenAI client with the API key
client = OpenAI(api_key=API_KEY)
def generate_image(prompt):
try:
# Attempting to generate a single image with minimal parameters
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1792",
quality="standard",
n=1
)
print("Image generation successful!")
return response
except Exception as e:
print(f"Failed to generate image: {str(e)}")
traceback.print_exc() # Print detailed traceback
return None
# Generate a simple image
prompt = "A simple, beautiful landscape painting."
image_response = generate_image(prompt)
# If image generation was successful, print the image URL
if image_response and 'data' in image_response and image_response['data']:
print("Image URL:", image_response['data'][0]['url'])
else:
print("No image data received.")