1- from tokenize import String
2- import requests
31import json
4- import time
2+ import os
3+ import requests
4+ import time
55import urllib
66import urllib .request
7- import os
87
98class 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