1
from napkin import response, request
2
import os
3
import openai
4
from klaviyo_api import KlaviyoAPI
5
6
def openai_generate_images(input):
7
openai.api_key = os.getenv("OPENAI_API_KEY")
8
9
openai_response = openai.Image.create(
10
prompt=input.get("prompt") or "an avocado, in kawaii art style",
11
n=input.get("count") or 1,
12
size=input.get("size") or "1024x1024"
13
)
14
15
return openai_response
16
17
def klaviyo_create_image(image_src, image_name):
18
# cute cat photo
19
default_photo_url = "https://t4.ftcdn.net/jpg/05/62/99/31/360_F_562993122_e7pGkeY8yMfXJcRmclsoIjtOoVDDgIlh.jpg"
20
image_url = image_src or default_photo_url
21
22
klaviyo = KlaviyoAPI(os.getenv("KLAVIYO_API_KEY"), max_delay=60, max_retries=3, test_host=None)
23
klaviyo_response = klaviyo.Images.upload_image_from_url({
24
"data": {
25
"type": "image",
26
"attributes": {
27
"import_from_url": image_url,
28
"name": image_name,
29
"hidden": False
30
}
31
}
32
})
33
34
return klaviyo_response
35
36
def full_image_example(request, response):
37
input = request.body or {}
38
39
openai_response = openai_generate_images(input)
40
images = openai_response.get("data")
41
image_name = input.get("name") or input.get("prompt") or "Dall-E Generated Image"
42
43
print(images)
44
45
klaviyo_responses = []
46
for image in images:
47
klaviyo_response = klaviyo_create_image(image.get("url"), image_name)
48
klaviyo_responses.append(klaviyo_response)
49
print(klaviyo_response)
50
51
response.body = {
52
"responses": klaviyo_responses
53
}
54
response.status_code = 200
55
56
full_image_example(request, response)