Skip to content

Commit 728000b

Browse files
add bulk generating
1 parent a8d8ed9 commit 728000b

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,31 @@ Download to directory: C:\Users\pc\dalle2
8181
```
8282

8383

84+
or generate a specific amount
85+
86+
```python
87+
from dalle2 import Dalle2
88+
89+
dalle = Dalle2("sess-xxxxxxxxxxxxxxxxxxxxxxxxxxxx")
90+
generations = dalle.generate_amount("portal to another dimension", 12) # Every generation has batch size 6 -> amount % 6 == 0 works best
91+
92+
print(generations)
93+
```
94+
95+
```
96+
✔️ Task created with ID: task-lm0V4nZasgAFasd7AsStE67 and PROMT: portal to another dimension OVERALL: 1/ 2
97+
⌛ Waiting for task to finish ..
98+
➕ Appended new generations to all_generations
99+
✔️ Task created with ID: task-WcetZOHt8asdvHb433gi and PROMT: portal to another dimension OVERALL: 2/ 2
100+
⌛ Waiting for task to finish ..
101+
➕ Appended new generations to all_generations
102+
🙌 Task completed!
103+
104+
```
105+
```
106+
-> [list]
107+
```
108+
84109
[![Test In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1EEgZNAI58V_OiEfRJQSsQV_xkhHzQeRB?usp=sharing)
85110

86111

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = dalle2
3-
version = 1.0.1
3+
version = 1.0.4
44
author = ezzcodeezzlife
55
author_email = [email protected]
66
description = Use DALL·E 2 in Python

src/dalle2/dalle2.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from tokenize import String
12
import requests
23
import json
34
import time
@@ -58,6 +59,47 @@ def generate_and_download(self, promt):
5859

5960
urllib.request.urlretrieve(imageurl, id +".jpg")
6061
print("✔️ Downloaded: " , id + ".jpg")
62+
63+
def generate_amount(self, promt, amount):
64+
url = "https://labs.openai.com/api/labs/tasks"
65+
headers = {
66+
'Authorization': "Bearer " + self.bearer,
67+
'Content-Type': "application/json"
68+
}
69+
body = {
70+
"task_type": "text2im",
71+
"prompt": {
72+
"caption": promt,
73+
"batch_size": self.batch_size,
74+
}
75+
}
76+
6177

78+
all_generations = []
79+
for i in range(1,int(amount / self.batch_size +1)):
80+
url = "https://labs.openai.com/api/labs/tasks"
81+
response = requests.post(url, headers=headers, data=json.dumps(body))
82+
if response.status_code != 200:
83+
print(response.text)
84+
return None
85+
data = response.json()
86+
print("✔️ Task created with ID:", data["id"], "and PROMT:", promt, "OVERALL:" , str(i) + "/" , int(amount / self.batch_size ))
87+
print("⌛ Waiting for task to finish .. ")
6288

63-
89+
while True:
90+
url = "https://labs.openai.com/api/labs/tasks/" + data["id"]
91+
response = requests.get(url, headers=headers)
92+
data = response.json()
93+
if data["status"] == "succeeded":
94+
95+
generations = data["generations"]["data"]
96+
print("➕ Appended new generations to all_generations")
97+
all_generations.append(generations)
98+
break
99+
else:
100+
# print("Task not completed yet")
101+
time.sleep(3)
102+
continue
103+
print("🙌 Task completed!")
104+
print(all_generations)
105+
return all_generations

0 commit comments

Comments
 (0)