-
Notifications
You must be signed in to change notification settings - Fork 1
/
text_to_image_with_upload.py
75 lines (57 loc) · 1.92 KB
/
text_to_image_with_upload.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# This script demos using reference images
import os
import requests
import json
import sys
from slugify import slugify
CLIENT_ID = os.environ.get('CLIENT_ID')
CLIENT_SECRET = os.environ.get('CLIENT_SECRET')
def getAccessToken(id, secret):
response = requests.post(f"https://ims-na1.adobelogin.com/ims/token/v3?client_id={id}&client_secret={secret}&grant_type=client_credentials&scope=openid,AdobeID,firefly_enterprise,firefly_api")
return response.json()
def uploadImage(path, id, token):
with open(path,'rb') as file:
response = requests.post("https://firefly-beta.adobe.io/v2/storage/image", data=file, headers = {
"X-API-Key":id,
"Authorization":f"Bearer {token}",
"Content-Type": "image/jpeg"
})
return response.json()
def textToImage(text, imageId, id, token):
data = {
"n":3,
"prompt":text,
"contentClass":"photo",
"size":{
"width":2048,
"height":2048
},
"styles":{
"referenceImage":{
"id":imageId
}
}
}
response = requests.post("https://firefly-beta.adobe.io/v2/images/generate", json=data, headers = {
"X-API-Key":id,
"Authorization":f"Bearer {token}",
"Content-Type":"application/json"
})
return response.json()
accessToken = getAccessToken(CLIENT_ID, CLIENT_SECRET)['access_token']
#print(accessToken)
image = uploadImage("input/cat_godzilla.jpg", CLIENT_ID, accessToken)
imageId = image["images"][0]["id"]
prompt = "cats on unicorns under a rainbow"
response = textToImage(prompt, imageId , CLIENT_ID, accessToken)
#print(json.dumps(response,indent=1))
# So, assume a good response, and loop over response.outputs
for resp in response["outputs"]:
# todo, make new file based on slug of prompt + seed
newName = "output/" + slugify(prompt) + "-" + str(resp["seed"]) + ".jpg"
imgUrl = resp["image"]["presignedUrl"]
print(f"Saving {newName}")
with open(newName,'wb') as output:
bits = requests.get(imgUrl, stream=True).content
output.write(bits)
print("\nDone")