-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.py
48 lines (37 loc) · 1.33 KB
/
sync.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
43
44
45
46
47
48
import os
import openai
import requests
import secret
import time
openai.api_key = secret.api_key
# CODIO SOLUTION BEGIN
# Set the prompts
prompts = ["robot dog in a lab", "robot dog exploring the city", "robot dog watching the sunset"]
start_time = time.time() # Start measuring execution time
# Generate and save the images
for i, prompt in enumerate(prompts):
try:
response = openai.Image.create(
prompt=prompt,
n=1,
size="256x256"
)
# Get the image URL from the response
image_url = response['data'][0]['url']
# Download and save the image
img_data = requests.get(image_url).content
with open(f"robot_dog_journey_{i+1}.jpg", 'wb') as handler:
handler.write(img_data)
except requests.exceptions.RequestException as e:
# This will catch any general network error
print(f"Network error: {e}")
except openai.api_errors.APIError as e:
# This will catch any error returned by the OpenAI API
print(f"API error: {e}")
except Exception as e:
# This is a catch-all for any other exceptions
print(f"Unexpected error: {e}")
end_time = time.time() # End measuring execution time
execution_time = end_time - start_time
print(f"Execution time: {execution_time} seconds")
# CODIO SOLUTION END