-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_json.py
executable file
·30 lines (25 loc) · 953 Bytes
/
generate_json.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
#!/usr/bin/env python3
import gpt_2_simple as gpt2
import json
pregenerated = open('pregenerated.json', 'r')
entries = json.load(pregenerated)
pregenerated.close()
sess = gpt2.start_tf_sess()
gpt2.load_gpt2(sess)
petitions = gpt2.generate(sess, prefix="<|startofpetition|>", truncate="<|endofpetition|>", include_prefix=False, return_as_list=True, batch_size=10, nsamples=10)
for petition in petitions:
petition = petition.replace("<|startofpetition|>", "") # Sometimes this gets duplicated
petition = petition.replace("Reason for rejection:", "<h2>Why was this petition rejected?</h2>")
lines = petition.split("\n")
if lines[0].strip() == "":
start = 1
else:
start = 0
entry = {
'title': lines[start],
'body': "<br />".join(lines[start+2:])
}
entries.append(entry)
pregenerated = open('pregenerated.json', 'w')
json.dump(entries, pregenerated, indent=4)
pregenerated.close()