-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathrun.py
301 lines (258 loc) · 10.2 KB
/
run.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
import argparse
import json
import time
import requests
import logging
import string
import glob
from nltk.tokenize import sent_tokenize, word_tokenize
import torch
import torch.multiprocessing as torch_mp
from stable_diffusion import load_model as load_sd, run_model as run_sd, add_prompt_modifiers
from dump_docx import dump_images_captions_docx
OPENAI_TOKEN = os.environ['OPENAI_TOKEN']
SECTIONS = ["start", "middle", "end"]
EXTRACT_LENGTH = 100
EXTRACT_PROMPT_POSTPROCESS = False
logger = logging.getLogger('run_sd')
logging.basicConfig(level=logging.DEBUG)
with torch.no_grad():
torch.cuda.empty_cache()
torch_mp.set_start_method("spawn", force=True)
SD_MODEL = load_sd()
def query_gpt3(prompt):
response = requests.post(
"https://api.openai.com/v1/completions",
headers={
'authorization': "Bearer " + OPENAI_TOKEN,
"content-type": "application/json",
},
json={
"model": "text-davinci-002",
"prompt": prompt,
"max_tokens": 150,
"temperature": 0.8,
}
)
text = response.text
logger.debug(text)
try:
result = json.loads(text)
except:
raise Exception(f'Cannot load: {text}, {response}')
result = result['choices'][0]['text']
return result
def generate_image_prompts_with_sections(text):
suffix_template = "\n\nRecommend five different detailed, logo-free, sign-free images to accompany the previous text that illustrate the {} of this text: 1)"
image_prompts = { s: [] for s in SECTIONS }
for section in SECTIONS:
suffix = suffix_template.format(section)
prompt = text + suffix
result = query_gpt3(prompt)
result_list = result.strip().split(")") # removes space and number
clean_result_list = []
for i, r in enumerate(result_list):
res = r.strip()
if not res:
continue
if i < len(result_list) - 1:
res = res[:-2]
# Remove punctuation
res = res.translate(str.maketrans('', '', string.punctuation))
clean_result_list.append(res)
image_prompts[section].extend(clean_result_list)
return image_prompts
def generate_image_prompts_with_extracts(text):
image_prompts = []
with open('extracts_prompt_prefix.txt','r') as f:
promt_prefix = f.read()
extracts = []
text_sentences = sent_tokenize(text)
current_extract = None
current_extract_num_words = 0
for sentence in text_sentences:
if current_extract is None:
current_extract = sentence
else:
current_extract+=' '+sentence
current_extract_num_words+=len(word_tokenize(sentence))
if current_extract_num_words > EXTRACT_LENGTH:
extracts.append(current_extract)
current_extract = None
current_extract_num_words = 0
if current_extract is not None:
extracts.append(current_extract)
for extract in extracts:
prompt = promt_prefix+extract+'\n\ncontent:'
result = query_gpt3(prompt)
if 'style:' not in result:
continue
content, style = result.split('style:')
image_prompts.append(content.strip() + ', ' + style.strip())
return {'EXTRACTS': image_prompts}
def generate_image_prompts(method, text, filename):
if method == "sections":
image_prompts = generate_image_prompts_with_sections(text)
if method == "extracts":
image_prompts = generate_image_prompts_with_extracts(text)
# Store image prompts
filepath = f'image_prompts/{filename}_{method}.json'
logger.debug(f'Writing image prompts to {filepath}...')
with open(filepath, 'w') as f:
f.write(json.dumps(image_prompts, indent=4))
filepath_all = f'image_prompts/{filename}_{method}-all.txt'
logger.debug(f'Writing image prompts to {filepath_all}...')
with open(filepath_all, 'a') as f:
f.write(json.dumps(image_prompts, indent=4))
f.write('\n')
f.write(json.dumps(image_prompts, indent=4))
f.write('\n')
logger.debug(image_prompts)
return image_prompts
def make_image_prompts(method, filename, text, overwrite_prompts):
filename = filename.split('.')[0]
engineered_filepath = f"engineered_image_prompts/{filename}_{method}.json"
engineered_filepath_all = f"engineered_image_prompts/{filename}_{method}-all.txt"
if os.path.exists(engineered_filepath) and not overwrite_prompts:
logger.debug(f'Reading from existing {engineered_filepath}...')
with open(engineered_filepath) as f:
engineered_prompts = json.load(f)
else:
image_prompts = generate_image_prompts(method, text, filename)
# TODO: extractive summarization from long-form text as additional prompts to engineer and input into Stable Diffusion
# Engineer prompts (add modifiers to image prompts)
if method == 'sections':
engineered_prompts = { s: [] for s in SECTIONS }
for section, prompts in image_prompts.items():
for prompt in prompts:
engineered_prompt = add_prompt_modifiers(prompt)
engineered_prompts[section].append(engineered_prompt)
else:
if EXTRACT_PROMPT_POSTPROCESS:
engineered_prompts = []
for prompt in image_prompts['EXTRACTS']:
engineered_prompt = add_prompt_modifiers(prompt)
engineered_prompts.append(engineered_prompt)
engineered_prompts = {'EXTRACTS': engineered_prompts}
else:
engineered_prompts = image_prompts
if not os.path.isfile(engineered_filepath_all,):
with open(engineered_filepath_all, 'w') as fp:
pass
# Store engineered image prompts
logger.debug(f'Writing engineered image prompts to {engineered_filepath_all}...')
with open(engineered_filepath_all, 'a') as f:
f.write(json.dumps(engineered_prompts, indent=4))
f.write('\n')
f.write(json.dumps(engineered_prompts, indent=4))
f.write('\n')
logger.debug(f'Writing engineered image prompts to {engineered_filepath}...')
with open(engineered_filepath, 'w') as f:
f.write(json.dumps(engineered_prompts, indent=4))
logger.debug(engineered_prompts)
return engineered_prompts
def run_text_to_image(args):
prompt = args['prompt']
section = args['section']
save_folder = args['save_folder']
image = run_sd(SD_MODEL, prompt) # PIL output
save_prompt_name = prompt[:100].replace(' ', '_')
image_name = f'{section}-{save_prompt_name}-{str(int(time.time()))}'
image_path = f'{save_folder}/{image_name}.png'
image.save(image_path)
return (prompt, image_path)
def generate_images(sd_inputs, num_processes):
if num_processes == 1:
prompts_and_image_paths = [run_text_to_image(sd_input) for sd_input in sd_inputs]
else:
pool = torch_mp.Pool(processes=num_processes)
prompts_and_image_paths = pool.map(run_text_to_image, sd_inputs)
pool.close()
pool.join()
return prompts_and_image_paths
def setup(file):
save_folder = 'images/' + file.split('.')[0].replace(' ', '-')
os.makedirs(save_folder, exist_ok=True)
logger.debug(f'Using folder to save: {save_folder}')
filepath = f'texts/{file}' if '.' in file else f'texts/{file}.txt'
with open(filepath, 'r') as f:
text = f.read()
return text, save_folder
def prepare_sd_inputs(method, image_prompts, save_folder):
sd_inputs = []
if method == 'sections':
section_counts = {}
for s in SECTIONS:
section_counts[s] = len(glob.glob(f'{save_folder}/{s}-*.png'))
# Generate, sorted by the section that has the least images generated
for section in sorted(section_counts, key=lambda k: section_counts[k]):
prompts = image_prompts[section]
for prompt in prompts:
sd_input = {
'prompt': prompt,
'section': section,
'save_folder': save_folder,
}
sd_inputs.append(sd_input)
elif method == 'extracts':
for prompt in image_prompts['EXTRACTS']:
sd_input = {
'prompt': prompt,
'section': 'EXTRACTS',
'save_folder': save_folder,
}
sd_inputs.append(sd_input)
logger.debug(sd_inputs)
return sd_inputs
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--files",
"-f",
default=["three_little_pigs.txt"],
type=str,
nargs='+',
help="File for text"
)
parser.add_argument(
"--overwrite_prompts",
"-op",
action='store_true',
help="Overwrite json file image prompts"
)
parser.add_argument(
"--num_gpu_processes",
"-n",
default=3,
type=int,
help="Num processes for gpu multiprocessing"
)
parser.add_argument(
"--method",
"-m",
choices=["sections", "summary", "extracts", "summary+extracts"],
default="sections",
help="How to generate image prompts (see README)")
parser.add_argument(
"--extract_length",
"-el",
default=200,#TODO not using right now
type=int,
help="If using extract method, how many words to include in each extract.")
parser.add_argument(
"--output",
"-o", #TODO
choices=["txt", "images", "html", "docx", "markdown", "latex", "pdf"],
default="images",
help="Where to put resulting images (see README)")
args = parser.parse_args()
for file_name in args.files:
text, save_folder = setup(file_name)
image_prompts = make_image_prompts(args.method, file_name, text, overwrite_prompts=args.overwrite_prompts)
sd_inputs = prepare_sd_inputs(args.method, image_prompts, save_folder)
prompts_and_image_paths = generate_images(sd_inputs, args.num_gpu_processes)
dump_images_captions_docx(file_name, prompts_and_image_paths)
logger.info('All complete')
if __name__ == "__main__":
main()