Skip to content

Commit 06f37bf

Browse files
Merge pull request #6 from charlesjlee/fix_typo
fix typo and lint
2 parents 9e7fd00 + 3949fb8 commit 06f37bf

File tree

2 files changed

+38
-44
lines changed

2 files changed

+38
-44
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- Go to https://labs.openai.com/
66
- Open Network Tab in Developer Tools
7-
- Type a promt and press "Generate"
7+
- Type a prompt and press "Generate"
88
- Look for fetch to https://labs.openai.com/api/labs/tasks
99
- In the request header look for authorization then get the Bearer Token
1010

@@ -23,8 +23,8 @@ print(generations)
2323
```
2424

2525
```
26-
✔️ Task created with ID: task-f77yxcsdf3OEm and PROMT: portal to another dimension, digital art
27-
⌛ Waiting for task to finish ..
26+
✔️ Task created with ID: task-f77yxcsdf3OEm and PROMPT: portal to another dimension, digital art
27+
⌛ Waiting for task to finish...
2828
🙌 Task completed!
2929
3030
[
@@ -67,8 +67,8 @@ generations = dalle.generate_and_download("portal to another dimension, digital
6767
```
6868

6969
```
70-
✔️ Task created with ID: task-f77sayxcSGdfOEm and PROMT: portal to another dimension, digital art
71-
⌛ Waiting for task to finish ..
70+
✔️ Task created with ID: task-f77sayxcSGdfOEm and PROMPT: portal to another dimension, digital art
71+
⌛ Waiting for task to finish...
7272
🙌 Task completed!
7373
Download to directory: C:\Users\pc\dalle2
7474
✔️ Downloaded: generation-fAq4Lyxcm7pQVDBQEWJ.jpg
@@ -90,10 +90,10 @@ print(generations)
9090
```
9191

9292
```
93-
✔️ Task created with ID: task-lm0V4nZasgAFasd7AsStE67 and PROMT: portal to another dimension OVERALL: 1/ 2
94-
⌛ Waiting for task to finish ..
93+
✔️ Task created with ID: task-lm0V4nZasgAFasd7AsStE67 and PROMPT: portal to another dimension OVERALL: 1/ 2
94+
⌛ Waiting for task to finish...
9595
➕ Appended new generations to all_generations
96-
✔️ Task created with ID: task-WcetZOHt8asdvHb433gi and PROMT: portal to another dimension OVERALL: 2/ 2
96+
✔️ Task created with ID: task-WcetZOHt8asdvHb433gi and PROMPT: portal to another dimension OVERALL: 2/ 2
9797
⌛ Waiting for task to finish ..
9898
➕ Appended new generations to all_generations
9999
🙌 Task completed!

src/dalle2/dalle2.py

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
from tokenize import String
2-
import requests
31
import json
4-
import time
2+
import os
3+
import requests
4+
import time
55
import urllib
66
import urllib.request
7-
import os
87

98
class Dalle2():
109
def __init__(self, bearer):
1110
self.bearer = bearer
1211
self.batch_size = 4
1312

14-
def generate(self, promt):
13+
def generate(self, prompt):
1514
url = "https://labs.openai.com/api/labs/tasks"
1615
headers = {
1716
'Authorization': "Bearer " + self.bearer,
18-
'Content-Type': "application/json"
17+
'Content-Type': "application/json",
1918
}
2019
body = {
2120
"task_type": "text2im",
2221
"prompt": {
23-
"caption": promt,
22+
"caption": prompt,
2423
"batch_size": self.batch_size,
2524
}
2625
}
@@ -30,8 +29,8 @@ def generate(self, promt):
3029
print(response.text)
3130
return None
3231
data = response.json()
33-
print("✔️ Task created with ID:", data["id"], "and PROMT:", promt)
34-
print("⌛ Waiting for task to finish .. ")
32+
print("✔️ Task created with ID:", data["id"], "and PROMPT:", prompt)
33+
print("⌛ Waiting for task to finish...")
3534

3635
while True:
3736
url = "https://labs.openai.com/api/labs/tasks/" + data["id"]
@@ -40,66 +39,61 @@ def generate(self, promt):
4039
if data["status"] == "succeeded":
4140
print("🙌 Task completed!")
4241
generations = data["generations"]["data"]
43-
return(generations)
44-
else:
45-
# print("Task not completed yet")
46-
time.sleep(3)
47-
continue
42+
return generations
43+
44+
time.sleep(3)
4845

49-
def generate_and_download(self, promt):
50-
generations = self.generate(promt)
51-
if generations is None:
46+
def generate_and_download(self, prompt):
47+
generations = self.generate(prompt)
48+
if not generations:
5249
return None
53-
50+
5451
print("Download to directory: " + os.getcwd())
55-
#print(generations)
5652
for generation in generations:
57-
imageurl = generation["generation"]["image_path"]
58-
id = generation["id"]
53+
image_url = generation["generation"]["image_path"]
54+
image_id = generation["id"]
55+
56+
urllib.request.urlretrieve(image_url, image_id +".jpg")
57+
print("✔️ Downloaded: ", image_id + ".jpg")
5958

60-
urllib.request.urlretrieve(imageurl, id +".jpg")
61-
print("✔️ Downloaded: " , id + ".jpg")
59+
return generations
6260

63-
def generate_amount(self, promt, amount):
61+
def generate_amount(self, prompt, amount):
6462
url = "https://labs.openai.com/api/labs/tasks"
6563
headers = {
6664
'Authorization': "Bearer " + self.bearer,
67-
'Content-Type': "application/json"
65+
'Content-Type': "application/json",
6866
}
6967
body = {
7068
"task_type": "text2im",
7169
"prompt": {
72-
"caption": promt,
70+
"caption": prompt,
7371
"batch_size": self.batch_size,
7472
}
7573
}
76-
77-
74+
7875
all_generations = []
79-
for i in range(1,int(amount / self.batch_size +1)):
76+
for i in range(1, int(amount / self.batch_size +1)):
8077
url = "https://labs.openai.com/api/labs/tasks"
8178
response = requests.post(url, headers=headers, data=json.dumps(body))
8279
if response.status_code != 200:
8380
print(response.text)
8481
return None
8582
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 .. ")
83+
print("✔️ Task created with ID:", data["id"], "and PROMPT:", prompt, "OVERALL:", str(i) + "/", int(amount / self.batch_size))
84+
print("⌛ Waiting for task to finish...")
8885

8986
while True:
9087
url = "https://labs.openai.com/api/labs/tasks/" + data["id"]
9188
response = requests.get(url, headers=headers)
9289
data = response.json()
9390
if data["status"] == "succeeded":
94-
9591
generations = data["generations"]["data"]
9692
print("➕ Appended new generations to all_generations")
9793
all_generations.append(generations)
9894
break
99-
else:
100-
# print("Task not completed yet")
101-
time.sleep(3)
102-
continue
95+
96+
time.sleep(3)
10397
print("🙌 Task completed!")
10498
print(all_generations)
10599
return all_generations

0 commit comments

Comments
 (0)