Repository navigation
Expand file tree
/
Copy pathdata.py
More file actions
244 lines (221 loc) · 12.9 KB
/
Copy pathdata.py
File metadata and controls
244 lines (221 loc) · 12.9 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import openai, asyncio, requests
def run_async_as_sync(async_function):
return asyncio.get_event_loop().run_until_complete(async_function)
def verify_openai(api_key):
client = openai.OpenAI(api_key=api_key)
try: client.embeddings.create(input="", model="text-embedding-3-small")
except openai.APIConnectionError as e: return "network"
except: return "invalid"
else: return "valid"
def generate_dalle3(prompt, batches, additional_parameters):
response_format = "b64_json"
client = openai.AsyncOpenAI(api_key=SERVICES["OPENAI"]["api_key"])
if additional_parameters['aspect_ratio']['value'] == "square": additional_parameters['aspect_ratio']['value'] = "1024x1024"
if additional_parameters['aspect_ratio']['value'] == "landscape": additional_parameters['aspect_ratio']['value'] = "1792x1024"
if additional_parameters['aspect_ratio']['value'] == "portrait": additional_parameters['aspect_ratio']['value'] = "1024x1792"
for i, batch in enumerate(batches):
yield {"message": f"Generating batch #{i+1} with {batch} image(s)...", "value":None, "type":"log", "level":None}
async def get_image(prompt, size, quality, style, response_format = "b64_json"):
response = await client.images.generate(
model="dall-e-3",
prompt=prompt,
size=size,
quality=quality,
style=style,
response_format=response_format,
n=1
)
if response_format == "url": image = response.data[0].url
else: image = response.data[0].b64_json
return image
tasks = [get_image(prompt, additional_parameters['aspect_ratio']['value'], additional_parameters['quality']['value'], additional_parameters['style']['value'], response_format=response_format) for _ in range(batch)]
try: images = run_async_as_sync(asyncio.gather(*tasks))
except openai.AuthenticationError as e:
yield {"message": f"The API key for OpenAI is incorrect.", "value":e, "type":"error", "level":"critical"}
except openai.BadRequestError as e:
yield {"message": f"Provided prompt was rejected by OpenAI.", "value":e, "type":"error", "level":"critical"}
except openai.RateLimitError as e:
if "quota" in e.message:
yield {"message": f"Your OpenAI account does not have sufficient credits/balance.", "value":e, "type":"error", "level":"critical"}
else:
yield {"message": f"Too many requests sent to OpenAI. Please wait and try again later.", "value":e, "type":"error", "level":"warn"}
except Exception as e:
yield {"message": "Unknown error.", "value":e, "type":"error", "level":"warn"}
else:
yield {"message": f"Successfully generated batch #{i+1}!", "value": images, "type":response_format, "level": None}
def generate_dalle2(prompt, batches, additional_parameters):
response_format = "b64_json"
client = openai.AsyncOpenAI(api_key=SERVICES["OPENAI"]["api_key"])
for i, batch in enumerate(batches):
yield {"message": f"Generating batch #{i+1} with {batch} image(s)...", "value":None, "type":"log", "level":None}
async def get_image(prompt, response_format = "b64_json"):
response = await client.images.generate(
model="dall-e-2",
prompt=prompt,
size="1024x1024",
response_format=response_format
)
if response_format == "url": image = response.data[0].url
else: image = response.data[0].b64_json
return image
tasks = [get_image(prompt, response_format=response_format) for _ in range(batch)]
try: images = run_async_as_sync(asyncio.gather(*tasks))
except openai.AuthenticationError as e:
yield {"message": f"The API key for OpenAI is incorrect.", "value":e, "type":"error", "level":"critical"}
except openai.BadRequestError as e:
yield {"message": f"Provided prompt was rejected by OpenAI.", "value":e, "type":"error", "level":"critical"}
except openai.RateLimitError as e:
if "quota" in e.message:
yield {"message": f"Your OpenAI account does not have sufficient credits/balance.", "value":e, "type":"error", "level":"critical"}
else:
yield {"message": f"Too many requests sent to OpenAI. Please wait and try again later.", "value":e, "type":"error", "level":"warn"}
except Exception as e:
yield {"message": "Unknown error.", "value":e, "type":"error", "level":"warn"}
else:
yield {"message": f"Successfully generated batch #{i+1}!", "value": images, "type":response_format, "level": None}
def generate_sd3(prompt, batches, additional_parameters):
def generate_image(prompt, api_key, aspect_ratio):
response = requests.post(
f"https://api.stability.ai/v2beta/stable-image/generate/sd3",
headers={
"authorization": f"Bearer {api_key}",
"accept": "application/json"
},
files={"none": ''},
data={
"prompt": prompt,
"model": "sd3",
"output_format": "png",
"aspect_ratio": aspect_ratio
},
)
if response.status_code == 200: return response.json()["image"]
else: raise Exception(response.status_code)
async def get_image(prompt, api_key, aspect_ratio):
loop = asyncio.get_event_loop()
image = loop.run_in_executor(None, generate_image, prompt, api_key, aspect_ratio)
return await image
for i, batch in enumerate(batches):
yield {"message": f"Generating batch #{i+1} with {batch} image(s)...", "value":None, "type":"log", "level":None}
tasks = [get_image(prompt, SERVICES["STABILITYAI"]["api_key"], additional_parameters['aspect_ratio']['value'] ) for _ in range(batch)]
try:
images = run_async_as_sync(asyncio.gather(*tasks))
except Exception as e:
status_code = int(str(e))
if status_code == 401:
yield {"message": f"The API key for StabilityAI is incorrect.", "value":e, "type":"error", "level":"critical"}
elif status_code == 402:
yield {"message": f"Your OpenAI account does not have sufficient credits/balance.", "value":e, "type":"error", "level":"critical"}
elif status_code == 403:
yield {"message": f"Provided prompt was rejected by StabilityAI.", "value":e, "type":"error", "level":"critical"}
elif status_code == 429:
yield {"message": f"Too many requests sent to StabilityAI. Please wait and try again later.", "value":e, "type":"error", "level":"warn"}
elif status_code == 500:
yield {"message": f"StabilityAI faced an unexpected error.", "value":e, "type":"error", "level":"warn"}
else:
yield {"message": f"Unknown error.", "value":e, "type":"error", "level":"warn"}
else:
yield {"message": f"Successfully generated batch #{i+1}!", "value": images, "type":"b64_json", "level": None}
def generate_sd3_turbo(prompt, batches, additional_parameters):
def generate_image(prompt, api_key, aspect_ratio):
response = requests.post(
f"https://api.stability.ai/v2beta/stable-image/generate/sd3",
headers={
"authorization": f"Bearer {api_key}",
"accept": "application/json"
},
files={"none": ''},
data={
"prompt": prompt,
"model": "sd3-turbo",
"output_format": "png",
"aspect_ratio": aspect_ratio
},
)
if response.status_code == 200: return response.json()["image"]
else: raise Exception(response.status_code)
async def get_image(prompt, api_key, aspect_ratio):
loop = asyncio.get_event_loop()
image = loop.run_in_executor(None, generate_image, prompt, api_key, aspect_ratio)
return await image
for i, batch in enumerate(batches):
yield {"message": f"Generating batch #{i+1} with {batch} image(s)...", "value":None, "type":"log", "level":None}
tasks = [get_image(prompt, SERVICES["STABILITYAI"]["api_key"], additional_parameters['aspect_ratio']['value'] ) for _ in range(batch)]
try:
images = run_async_as_sync(asyncio.gather(*tasks))
except Exception as e:
status_code = int(str(e))
if status_code == 401:
yield {"message": f"The API key for StabilityAI is incorrect.", "value":e, "type":"error", "level":"critical"}
elif status_code == 402:
yield {"message": f"Your OpenAI account does not have sufficient credits/balance.", "value":e, "type":"error", "level":"critical"}
elif status_code == 403:
yield {"message": f"Provided prompt was rejected by StabilityAI.", "value":e, "type":"error", "level":"critical"}
elif status_code == 429:
yield {"message": f"Too many requests sent to StabilityAI. Please wait and try again later.", "value":e, "type":"error", "level":"warn"}
elif status_code == 500:
yield {"message": f"StabilityAI faced an unexpected error.", "value":e, "type":"error", "level":"warn"}
else:
yield {"message": f"Unknown error.", "value":e, "type":"error", "level":"warn"}
else:
yield {"message": f"Successfully generated batch #{i+1}!", "value": images, "type":"b64_json", "level": None}
SERVICES = {
"OPENAI" : {
"api_key" : None,
"required" : False,
"alias" : "OpenAI",
"description" : "The [bold]OpenAI[/bold] platform providing Dall-E 2 and 3.",
"link" : "https://platform.openai.com/api-keys",
"models" : [
{
"name" : "dalle-3",
"alias" : "Dall-E 3",
"online_only" : True,
"description" : "(Recommended) OpenAI's flagship image generator that trades affordability for a wide array of generation features.",
"additional_parameters" : [
{"name" : "aspect_ratio", "alias" : "Aspect Ratio", "description" : "The aspect ratio/format of the generated image(s).", "default" : "square", "options" : ["square", "landscape", "portrait"]},
{"name" : "quality", "alias" : "Quality", "description" : "The quality of the image. Affects attention to detail, not resolution.", "default" : "standard", "options" : ["hd", "standard"]},
{"name" : "style", "alias" : "Style", "description" : "The style of the image. Vivid images are more dramatic and hyper-real than natural images.", "default" : "vivid", "options" : ["vivid", "natural"]}
],
"generate_function" : generate_dalle3
},
{
"name" : "dalle-2",
"alias" : "Dall-E 2",
"online_only" : True,
"description" : "An older image generator that provides faster and cheaper but lower quality image generation.",
"additional_parameters" : None,
"generate_function" : generate_dalle2
},
],
"always_verify" : False,
"verification_function" : verify_openai
},
"STABILITYAI" : {
"api_key" : None,
"required" : False,
"alias" : "StabilityAI",
"description" : "The [bold]StabilityAI[/bold] platform providing Stable Diffusion 3 and 3 Turbo.",
"link" : "https://platform.stability.ai/account/keys",
"models" : [
{
"name" : "sd3",
"alias" : "Stable Diffusion 3",
"online_only" : True,
"description" : "(Recommended) The newest image generator from StabilityAI.",
"additional_parameters" : [{"name" : "aspect_ratio", "alias" : "Aspect Ratio", "description" : "The aspect ratio/format of the generated image(s).", "default" : "1:1", "options" : ["16:9","1:1","21:9","2:3","3:2", "4:5", "5:4", "9:16", "9:21"]}],
"generate_function" : generate_sd3
},
{
"name" : "sd3-turbo",
"alias" : "Stable Diffusion 3 Turbo",
"online_only" : True,
"description" : "A faster and cheaper version of Stable Diffusion 3",
"additional_parameters" : [{"name" : "aspect_ratio", "alias" : "Aspect Ratio", "description" : "The aspect ratio/format of the generated image(s).", "default" : "1:1", "options" : ["16:9","1:1","21:9","2:3","3:2", "4:5", "5:4", "9:16", "9:21"]}],
"generate_function" : generate_sd3_turbo
},
],
"always_verify" : False,
"verification_function" : None
}
}