SHOW:
|
|
- or go back to the newest paste.
1 | ##### A guide on how to: Modify the game to write the save files to your computer | |
2 | # and add an autosave feature (Plus other tweaks). [Offline users only] | |
3 | # | |
4 | ### Prerequisite: | |
5 | # | |
6 | # First, familiarize yourself with the following two sections | |
7 | # of the code. I'll need to refer to them frequently | |
8 | # | |
9 | # [help texts] | |
10 | # link - https://github.com/aidungeon99999/AIDungeon-Uncensored/blob/7989620d2e400a92d76e432daf26ec0415c3abdf/play.py#L59-L74 | |
11 | # | |
12 | # Begins with: | |
13 | ||
14 | def instructions(): | |
15 | ||
16 | # This is what gets printed when you type 'help'. Not essential | |
17 | # but probably still a good idea to modify. | |
18 | # | |
19 | # | |
20 | # [command block] | |
21 | # link - https://github.com/aidungeon99999/AIDungeon-Uncensored/blob/7989620d2e400a92d76e432daf26ec0415c3abdf/play.py#L138-L195 | |
22 | # | |
23 | # The section starting with | |
24 | ||
25 | if action == "restart": | |
26 | rating = input("Please rate the story quality from 1-10: ") | |
27 | rating_float = float(rating) | |
28 | story_manager.story.rating = rating_float | |
29 | break | |
30 | ||
31 | # this is the section that handles the actual input command logics. | |
32 | ||
33 | ||
34 | ||
35 | ||
36 | ||
37 | ### To add autosaving to local directory: | |
38 | ||
39 | # 1. In your AIDungeon directory, create a new directory named "saves" | |
40 | ||
41 | # 2. Open "./story/story_manager.py" and add | |
42 | ||
43 | import copy | |
44 | ||
45 | # to the top of the file | |
46 | ||
47 | # then replace: | |
48 | ||
49 | def save_to_storage(self): | |
50 | self.uuid = str(uuid.uuid1()) | |
51 | ||
52 | ||
53 | story_json = self.to_json() | |
54 | file_name = "story" + str(self.uuid) + ".json" | |
55 | f = open(file_name, "w") | |
56 | f.write(story_json) | |
57 | f.close() | |
58 | ||
59 | FNULL = open(os.devnull, 'w') | |
60 | p = Popen(['gsutil', 'cp', file_name, 'gs://aidungeonstories'], stdout=FNULL, stderr=subprocess.STDOUT) | |
61 | return self.uuid | |
62 | ||
63 | def load_from_storage(self, story_id): | |
64 | ||
65 | file_name = "story" + story_id + ".json" | |
66 | cmd = "gsutil cp gs://aidungeonstories/" + file_name + " ." | |
67 | os.system(cmd) | |
68 | exists = os.path.isfile(file_name) | |
69 | ||
70 | if exists: | |
71 | with open(file_name, 'r') as fp: | |
72 | game = json.load(fp) | |
73 | self.init_from_dict(game) | |
74 | return str(self) | |
75 | else: | |
76 | return "Error save not found." | |
77 | ||
78 | # with | |
79 | # | |
80 | def save_to_storage(self, overwrite=False): | |
81 | ref = self if overwrite else copy.copy(self) | |
82 | ||
83 | if overwrite == False: | |
84 | ref.uuid = str(uuid.uuid1()) | |
85 | ||
86 | story_json = ref.to_json() | |
87 | file_name = "saves\\" + str(ref.uuid) + ".json" | |
88 | f = open(file_name, "w") | |
89 | f.write(story_json) | |
90 | f.close() | |
91 | ||
92 | return ref.uuid | |
93 | ||
94 | def load_from_storage(self, story_id): | |
95 | ||
96 | file_name = "saves\\" + story_id + ".json" | |
97 | with open(file_name, 'r') as fp: | |
98 | game = json.load(fp) | |
99 | self.init_from_dict(game) | |
100 | return str(self) | |
101 | ||
102 | # then replace: | |
103 | ||
104 | def load_new_story(self, story_id): | |
105 | file_name = "story" + story_id + ".json" | |
106 | cmd = "gsutil cp gs://aidungeonstories/" + file_name + " ." | |
107 | os.system(cmd) | |
108 | exists = os.path.isfile(file_name) | |
109 | ||
110 | if exists: | |
111 | with open(file_name, 'r') as fp: | |
112 | game = json.load(fp) | |
113 | self.story = Story("") | |
114 | self.story.init_from_dict(game) | |
115 | return str(self.story) | |
116 | else: | |
117 | return "Error: save not found." | |
118 | ||
119 | # with | |
120 | ||
121 | def load_new_story(self, story_id): | |
122 | file_name = "saves\\" + story_id + ".json" | |
123 | with open(file_name, 'r') as fp: | |
124 | game = json.load(fp) | |
125 | self.story = Story("") | |
126 | self.story.init_from_dict(game) | |
127 | return str(self.story) | |
128 | ||
129 | # 3. In "./play.py", go to the [help texts] section and add | |
130 | text += '\n "autosave" Toggle autosave on and off. Default is off' | |
131 | ||
132 | # 4. In [command block], replace the following: | |
133 | ||
134 | elif action == "save": | |
135 | if upload_story: | |
136 | id = story_manager.story.save_to_storage() | |
137 | console_print("Game saved.") | |
138 | console_print("To load the game, type 'load' and enter the following ID: " + id) | |
139 | else: | |
140 | console_print("Saving has been turned off. Cannot save.") | |
141 | ||
142 | # with | |
143 | ||
144 | elif action == "autosave": | |
145 | autosave = not autosave | |
146 | console_print("Autosaving is now turned " + ("on" if autosave else "off")) | |
147 | ||
148 | elif action == "save": | |
149 | if upload_story: | |
150 | print("Save to new file, or overwrite the current file?") | |
151 | print("0) Save as new\n1) Save to current file\n") | |
152 | choice = get_num_options(2) | |
153 | id = story_manager.story.save_to_storage(overwrite=(choice == 1)) | |
154 | console_print("Game saved.") | |
155 | console_print( | |
156 | "To load the game, type 'load' and enter the following ID: " | |
157 | + id | |
158 | ) | |
159 | else: | |
160 | console_print("Saving has been turned off. Cannot save.") | |
161 | ||
162 | # 5. Below the line: | |
163 | upload_story = True | |
164 | # add | |
165 | autosave = False | |
166 | ||
167 | # 6. Below | |
168 | while True: | |
169 | if story_manager.story != None: | |
170 | del story_manager.story | |
171 | # add in | |
172 | if autosave: | |
173 | story_manager.story.save_to_storage(overwrite=True) | |
174 | ||
175 | ||
176 | ### Change 'temperature' and 'top_k' while in progress | |
177 | # !! Warning: Requires a lot of RAM, make sure you have ~6GB of free memory !! | |
178 | ||
179 | # 1. In [help texts], add | |
180 | text += '\n "settemp" Changes the generator temperature' | |
181 | text += '\n "settopk" Changes the generator top_k' | |
182 | ||
183 | # 2. In [command block] add in | |
184 | ||
185 | elif action == "settemp": | |
186 | temp = float(input("Set a new temperature\n> ")) | |
187 | story_manager.generator = GPT2Generator(temperature=temp, top_k=top_k) | |
188 | ||
189 | elif action == "settopk": | |
190 | top_k = int(input("Set a new top_k\n>")) | |
191 | story_manager.generator = GPT2Generator(temperature=temp, top_k=top_k) | |
192 | ||
193 | ### Change 'memory' while in progress, this is how much of the current story | |
194 | # you want to feed to the generator | |
195 | ||
196 | # 1. In [help texts], add | |
197 | ||
198 | text += '\n "setmem" Changes the memory value. Default is 20, try lowering it if your game crashes after playing for a while' | |
199 | ||
200 | # 2. In [command block] add | |
201 | ||
202 | elif action == "setmem": | |
203 | new_mem = input("Enter a new memory value\n> ") | |
204 | story_manager.story.memory = int(new_mem) | |
205 | ||
206 | ### Print out the current settings | |
207 | ||
208 | # 1. In [help texts] | |
209 | ||
210 | text += '\n "showsetting" Display current settings and generator parameters' | |
211 | ||
212 | # 2. In [command block] | |
213 | ||
214 | elif action == "showsetting": | |
215 | text = "nosaving is set to: " + str(not upload_story) | |
216 | text += "\nautosave is set to: " + str(autosave) | |
217 | text += "\nmemory is set to: " + str(story_manager.story.memory) | |
218 | text += "\ntemperature is set to: " + str(temp) | |
219 | text += "\ntop_k is set to: " + str(top_k) | |
220 | print(text) | |
221 | ||
222 | ||
223 | ||
224 | # (English is not my primary language and this is my first | |
225 | # time writing a guide, so if you feel that any part of | |
226 | # this document is confusing or poorly worded, feel free | |
227 | # to rewrite it and redistribute your own version) |