forked from daveshap/PlotGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_synopsis.py
90 lines (76 loc) · 3.07 KB
/
generate_synopsis.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
import re
import os
import openai
from time import time,sleep
from random import seed,choice
from uuid import uuid4
def open_file(filepath):
with open(filepath, 'r', encoding='utf-8') as infile:
return infile.read()
def save_file(filepath, content):
with open(filepath, 'w', encoding='utf-8') as outfile:
outfile.write(content)
openai.api_key = open_file('openaiapikey.txt')
def gpt3_completion(prompt, engine='text-davinci-002', temp=1.1, top_p=1.0, tokens=3000, freq_pen=0.0, pres_pen=0.0, stop=['asdfasdf', 'asdasdf']):
max_retry = 5
retry = 0
prompt = prompt.encode(encoding='ASCII',errors='ignore').decode() # force it to fix any unicode errors
while True:
try:
response = openai.Completion.create(
engine=engine,
prompt=prompt,
temperature=temp,
max_tokens=tokens,
top_p=top_p,
frequency_penalty=freq_pen,
presence_penalty=pres_pen,
stop=stop)
text = response['choices'][0]['text'].strip()
text = re.sub('\s+', ' ', text)
filename = '%s_gpt3.txt' % time()
save_file('gpt3_logs/%s' % filename, prompt + '\n\n==========\n\n' + text)
return text
except Exception as oops:
retry += 1
if retry >= max_retry:
return "GPT3 error: %s" % oops
print('Error communicating with OpenAI:', oops)
sleep(1)
def pick_random(filename):
seed()
lines = open_file(filename).splitlines()
return choice(lines)
def generate_synopsis():
# pick random variables
genre = pick_random('genres.txt')
tone = pick_random('tones.txt')
character = pick_random('characters.txt')
pace = pick_random('paces.txt')
storyline = pick_random('storylines.txt')
style = pick_random('styles.txt')
setting = pick_random('settings.txt')
timeperiod = pick_random('times.txt')
# instantiate prompt
prompt = open_file('prompt_synopsis.txt')
prompt = prompt.replace('<<GENRE>>', genre)
prompt = prompt.replace('<<TONE>>', tone)
prompt = prompt.replace('<<CHARACTER>>', character)
prompt = prompt.replace('<<PACE>>', pace)
prompt = prompt.replace('<<STORYLINE>>', storyline)
prompt = prompt.replace('<<STYLE>>', style)
prompt = prompt.replace('<<SETTING>>', setting)
prompt = prompt.replace('<<TIME>>', timeperiod)
prompt = prompt.replace('<<UUID>>', str(uuid4()))
print('\n\nTERMS:', genre, setting, timeperiod, tone, character, pace, storyline, style)
# generate and save synopsis
synopsis = gpt3_completion(prompt).replace('SYNOPSIS:','').replace('BEGINNING:', '').replace('MIDDLE:','').replace('END:','').replace(' ', ' ')
return synopsis
if __name__ == '__main__':
for i in list(range(0,200)):
synopsis = generate_synopsis()
print('\n\nSYNOPSIS:', synopsis)
unique_id = str(uuid4())
save_file('current_id.txt', unique_id)
filename = 'synopses/%s.txt' % unique_id
save_file(filename, synopsis)