Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions gpt_2_simple/gpt_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def generate_samples():
with open(
os.path.join(SAMPLE_DIR, run_name,
'samples-{}').format(counter), 'w') as fp:
fp.write('\n'.join(all_text))
fp.write('\n'.join(all_text))

def sample_batch():
return [data_sampler.sample(1024) for _ in range(batch_size)]
Expand Down Expand Up @@ -473,25 +473,42 @@ def generate(sess,
for i in range(batch_size):
generated += 1
gen_text = enc.decode(out[i])
# process the text to strip out the prefix and/or end at the truncate text
if prefix:
gen_text = enc.decode(context_tokens[:1]) + gen_text
if truncate:
truncate_esc = re.escape(truncate)
if prefix and not include_prefix:
prefix_esc = re.escape(prefix)
pattern = '(?:{})(.*?)(?:{})'.format(prefix_esc,
truncate_esc)
pattern = '(?:{})(.*?)(?:{})'.format(prefix_esc, truncate_esc)
else:
pattern = '(.*?)(?:{})'.format(truncate_esc)

trunc_text = re.search(pattern, gen_text, re.S)
if trunc_text:
gen_text = trunc_text.group(1)
gen_text = gen_text.lstrip('\n')
if destination_path:
f.write("{}\n{}".format(gen_text, sample_delim))
if not return_as_list and not destination_path:
print("{}\n{}".format(gen_text, sample_delim), end='')
# the generated text sometimes doesn't include the truncate text, but we can try again to just strip out the prefix (if it exists), leaving only the new gen_text
elif not trunc_text and prefix:
pattern = '(?:{})(.*)'.format(prefix_esc)
trunc_text = re.search(pattern, gen_text, re.S)
if trunc_text:
gen_text = trunc_text.group(1)

# truncate is not set, but we should still strip the prefix, if set and requested
else:
if prefix and not include_prefix:
prefix_esc = re.escape(prefix)
pattern = '(?:{})(.*)'.format(prefix_esc)
trunc_text = re.search(pattern, gen_text, re.S)
if trunc_text:
gen_text = trunc_text.group(1)
try:
if destination_path:
f.write("{}\n{}".format(gen_text, sample_delim))
if not return_as_list and not destination_path:
print("{}\n{}".format(gen_text, sample_delim), end='')
except Exception as e:
pass
gen_texts.append(gen_text)

if destination_path:
Expand Down