forked from datacrystals/AIStoryWriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Write.py
executable file
·482 lines (415 loc) · 14.7 KB
/
Write.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/bin/python3
import argparse
import time
import datetime
import os
import json
import Writer.Config
import Writer.Interface.Wrapper
import Writer.PrintUtils
import Writer.Chapter.ChapterDetector
import Writer.Scrubber
import Writer.Statistics
import Writer.OutlineGenerator
import Writer.Chapter.ChapterGenerator
import Writer.StoryInfo
import Writer.NovelEditor
import Writer.Translator
# Setup Argparser
Parser = argparse.ArgumentParser()
Parser.add_argument("-Prompt", help="Path to file containing the prompt")
Parser.add_argument(
"-Output",
default="",
type=str,
help="Optional file output path, if none is speciifed, we will autogenerate a file name based on the story title",
)
Parser.add_argument(
"-InitialOutlineModel",
default=Writer.Config.INITIAL_OUTLINE_WRITER_MODEL,
type=str,
help="Model to use for writing the base outline content",
)
Parser.add_argument(
"-ChapterOutlineModel",
default=Writer.Config.CHAPTER_OUTLINE_WRITER_MODEL,
type=str,
help="Model to use for writing the per-chapter outline content",
)
Parser.add_argument(
"-ChapterS1Model",
default=Writer.Config.CHAPTER_STAGE1_WRITER_MODEL,
type=str,
help="Model to use for writing the chapter (stage 1: plot)",
)
Parser.add_argument(
"-ChapterS2Model",
default=Writer.Config.CHAPTER_STAGE2_WRITER_MODEL,
type=str,
help="Model to use for writing the chapter (stage 2: character development)",
)
Parser.add_argument(
"-ChapterS3Model",
default=Writer.Config.CHAPTER_STAGE3_WRITER_MODEL,
type=str,
help="Model to use for writing the chapter (stage 3: dialogue)",
)
Parser.add_argument(
"-ChapterS4Model",
default=Writer.Config.CHAPTER_STAGE4_WRITER_MODEL,
type=str,
help="Model to use for writing the chapter (stage 4: final correction pass)",
)
Parser.add_argument(
"-ChapterRevisionModel",
default=Writer.Config.CHAPTER_REVISION_WRITER_MODEL,
type=str,
help="Model to use for revising the chapter until it meets criteria",
)
Parser.add_argument(
"-RevisionModel",
default=Writer.Config.REVISION_MODEL,
type=str,
help="Model to use for generating constructive criticism",
)
Parser.add_argument(
"-EvalModel",
default=Writer.Config.EVAL_MODEL,
type=str,
help="Model to use for evaluating the rating out of 100",
)
Parser.add_argument(
"-InfoModel",
default=Writer.Config.INFO_MODEL,
type=str,
help="Model to use when generating summary/info at the end",
)
Parser.add_argument(
"-ScrubModel",
default=Writer.Config.SCRUB_MODEL,
type=str,
help="Model to use when scrubbing the story at the end",
)
Parser.add_argument(
"-CheckerModel",
default=Writer.Config.CHECKER_MODEL,
type=str,
help="Model to use when checking if the LLM cheated or not",
)
Parser.add_argument(
"-TranslatorModel",
default=Writer.Config.TRANSLATOR_MODEL,
type=str,
help="Model to use if translation of the story is enabled",
)
Parser.add_argument(
"-Translate",
default="",
type=str,
help="Specify a language to translate the story to - will not translate by default. Ex: 'French'",
)
Parser.add_argument(
"-TranslatePrompt",
default="",
type=str,
help="Specify a language to translate your input prompt to. Ex: 'French'",
)
Parser.add_argument("-Seed", default=12, type=int, help="Used to seed models.")
Parser.add_argument(
"-OutlineMinRevisions",
default=0,
type=int,
help="Number of minimum revisions that the outline must be given prior to proceeding",
)
Parser.add_argument(
"-OutlineMaxRevisions",
default=3,
type=int,
help="Max number of revisions that the outline may have",
)
Parser.add_argument(
"-ChapterMinRevisions",
default=0,
type=int,
help="Number of minimum revisions that the chapter must be given prior to proceeding",
)
Parser.add_argument(
"-ChapterMaxRevisions",
default=3,
type=int,
help="Max number of revisions that the chapter may have",
)
Parser.add_argument(
"-NoChapterRevision", action="store_true", help="Disables Chapter Revisions"
)
Parser.add_argument(
"-NoScrubChapters",
action="store_true",
help="Disables a final pass over the story to remove prompt leftovers/outline tidbits",
)
Parser.add_argument(
"-ExpandOutline",
action="store_true",
default=True,
help="Disables the system from expanding the outline for the story chapter by chapter prior to writing the story's chapter content",
)
Parser.add_argument(
"-EnableFinalEditPass",
action="store_true",
help="Enable a final edit pass of the whole story prior to scrubbing",
)
Parser.add_argument(
"-Debug",
action="store_true",
help="Print system prompts to stdout during generation",
)
Parser.add_argument(
"-SceneGenerationPipeline",
action="store_true",
default=True,
help="Use the new scene-by-scene generation pipeline as an initial starting point for chapter writing",
)
Parser.add_argument(
"-SleepTime",
default=20,
type=int,
help="Time to wait between requests in seconds",
)
Args = Parser.parse_args()
# Measure Generation Time
StartTime = time.time()
# Setup Config
Writer.Config.SEED = Args.Seed
Writer.Config.INITIAL_OUTLINE_WRITER_MODEL = Args.InitialOutlineModel
Writer.Config.CHAPTER_OUTLINE_WRITER_MODEL = Args.ChapterOutlineModel
Writer.Config.CHAPTER_STAGE1_WRITER_MODEL = Args.ChapterS1Model
Writer.Config.CHAPTER_STAGE2_WRITER_MODEL = Args.ChapterS2Model
Writer.Config.CHAPTER_STAGE3_WRITER_MODEL = Args.ChapterS3Model
Writer.Config.CHAPTER_STAGE4_WRITER_MODEL = Args.ChapterS4Model
Writer.Config.CHAPTER_REVISION_WRITER_MODEL = Args.ChapterRevisionModel
Writer.Config.EVAL_MODEL = Args.EvalModel
Writer.Config.REVISION_MODEL = Args.RevisionModel
Writer.Config.INFO_MODEL = Args.InfoModel
Writer.Config.SCRUB_MODEL = Args.ScrubModel
Writer.Config.CHECKER_MODEL = Args.CheckerModel
Writer.Config.TRANSLATOR_MODEL = Args.TranslatorModel
Writer.Config.TRANSLATE_LANGUAGE = Args.Translate
Writer.Config.TRANSLATE_PROMPT_LANGUAGE = Args.TranslatePrompt
Writer.Config.OUTLINE_MIN_REVISIONS = Args.OutlineMinRevisions
Writer.Config.OUTLINE_MAX_REVISIONS = Args.OutlineMaxRevisions
Writer.Config.CHAPTER_MIN_REVISIONS = Args.ChapterMinRevisions
Writer.Config.CHAPTER_MAX_REVISIONS = Args.ChapterMaxRevisions
Writer.Config.CHAPTER_NO_REVISIONS = Args.NoChapterRevision
Writer.Config.SCRUB_NO_SCRUB = Args.NoScrubChapters
Writer.Config.EXPAND_OUTLINE = Args.ExpandOutline
Writer.Config.ENABLE_FINAL_EDIT_PASS = Args.EnableFinalEditPass
Writer.Config.OPTIONAL_OUTPUT_NAME = Args.Output
Writer.Config.SCENE_GENERATION_PIPELINE = Args.SceneGenerationPipeline
Writer.Config.DEBUG = Args.Debug
SLEEP_TIME = Args.SleepTime
# Get a list of all used providers
Models = [
Writer.Config.INITIAL_OUTLINE_WRITER_MODEL,
Writer.Config.CHAPTER_OUTLINE_WRITER_MODEL,
Writer.Config.CHAPTER_STAGE1_WRITER_MODEL,
Writer.Config.CHAPTER_STAGE2_WRITER_MODEL,
Writer.Config.CHAPTER_STAGE3_WRITER_MODEL,
Writer.Config.CHAPTER_STAGE4_WRITER_MODEL,
Writer.Config.CHAPTER_REVISION_WRITER_MODEL,
Writer.Config.EVAL_MODEL,
Writer.Config.REVISION_MODEL,
Writer.Config.INFO_MODEL,
Writer.Config.SCRUB_MODEL,
Writer.Config.CHECKER_MODEL,
Writer.Config.TRANSLATOR_MODEL,
]
Models = list(set(Models))
# Setup Logger
SysLogger = Writer.PrintUtils.Logger()
# Initialize Interface
SysLogger.Log("Created OLLAMA Interface", 5)
Interface = Writer.Interface.Wrapper.Interface(Models)
# Load User Prompt
Prompt: str = ""
if Args.Prompt is None:
raise Exception("No Prompt Provided")
with open(Args.Prompt, "r") as f:
Prompt = f.read()
# If user wants their prompt translated, do so
if Writer.Config.TRANSLATE_PROMPT_LANGUAGE != "":
Prompt = Writer.Translator.TranslatePrompt(
Interface, SysLogger, Prompt, Writer.Config.TRANSLATE_PROMPT_LANGUAGE
)
# Generate the Outline
Outline, Elements, RoughChapterOutline, BaseContext = Writer.OutlineGenerator.GenerateOutline(
Interface, SysLogger, Prompt, Writer.Config.OUTLINE_QUALITY
)
BasePrompt = Prompt
# Detect the number of chapters
SysLogger.Log("Detecting Chapters", 5)
Messages = [Interface.BuildUserQuery(Outline)]
NumChapters: int = Writer.Chapter.ChapterDetector.LLMCountChapters(
Interface, SysLogger, Interface.GetLastMessageText(Messages)
)
SysLogger.Log(f"Found {NumChapters} Chapter(s)", 5)
## Write Per-Chapter Outline
Prompt = f"""
Please help me expand upon the following outline, chapter by chapter.
```
{Outline}
```
"""
Messages = [Interface.BuildUserQuery(Prompt)]
ChapterOutlines: list = []
if Writer.Config.EXPAND_OUTLINE:
for Chapter in range(1, NumChapters + 1):
ChapterOutline, Messages = Writer.OutlineGenerator.GeneratePerChapterOutline(
Interface, SysLogger, Chapter, Outline, Messages
)
ChapterOutlines.append(ChapterOutline)
time.sleep(SLEEP_TIME)
# Create MegaOutline
DetailedOutline: str = ""
for Chapter in ChapterOutlines:
DetailedOutline += Chapter
MegaOutline: str = f"""
# Base Outline
{Elements}
# Detailed Outline
{DetailedOutline}
"""
# Setup Base Prompt For Per-Chapter Generation
UsedOutline: str = Outline
if Writer.Config.EXPAND_OUTLINE:
UsedOutline = MegaOutline
# Write the chapters
SysLogger.Log("Starting Chapter Writing", 5)
Chapters = []
for i in range(1, NumChapters + 1):
Chapter = Writer.Chapter.ChapterGenerator.GenerateChapter(
Interface,
SysLogger,
i,
NumChapters,
Outline,
Chapters,
Writer.Config.OUTLINE_QUALITY,
BaseContext,
)
time.sleep(SLEEP_TIME)
Chapter = f"### Chapter {i}\n\n{Chapter}"
Chapters.append(Chapter)
ChapterWordCount = Writer.Statistics.GetWordCount(Chapter)
SysLogger.Log(f"Chapter Word Count: {ChapterWordCount}", 2)
# Now edit the whole thing together
StoryBodyText: str = ""
StoryInfoJSON:dict = {"Outline": Outline}
StoryInfoJSON.update({"StoryElements": Elements})
StoryInfoJSON.update({"RoughChapterOutline": RoughChapterOutline})
StoryInfoJSON.update({"BaseContext": BaseContext})
if Writer.Config.ENABLE_FINAL_EDIT_PASS:
NewChapters = Writer.NovelEditor.EditNovel(
Interface, SysLogger, Chapters, Outline, NumChapters
)
NewChapters = Chapters
StoryInfoJSON.update({"UnscrubbedChapters": NewChapters})
# Now scrub it (if enabled)
if not Writer.Config.SCRUB_NO_SCRUB:
NewChapters = Writer.Scrubber.ScrubNovel(
Interface, SysLogger, NewChapters, NumChapters
)
else:
SysLogger.Log(f"Skipping Scrubbing Due To Config", 4)
StoryInfoJSON.update({"ScrubbedChapter": NewChapters})
# If enabled, translate the novel
if Writer.Config.TRANSLATE_LANGUAGE != "":
NewChapters = Writer.Translator.TranslateNovel(
Interface, SysLogger, NewChapters, NumChapters, Writer.Config.TRANSLATE_LANGUAGE
)
else:
SysLogger.Log(f"No Novel Translation Requested, Skipping Translation Step", 4)
StoryInfoJSON.update({"TranslatedChapters": NewChapters})
# Compile The Story
for Chapter in NewChapters:
StoryBodyText += Chapter + "\n\n\n"
# Now Generate Info
Messages = []
Messages.append(Interface.BuildUserQuery(Outline))
Info = Writer.StoryInfo.GetStoryInfo(Interface, SysLogger, Messages)
Title = Info["Title"]
StoryInfoJSON.update({"Title": Info["Title"]})
Summary = Info["Summary"]
StoryInfoJSON.update({"Summary": Info["Summary"]})
Tags = Info["Tags"]
StoryInfoJSON.update({"Tags": Info["Tags"]})
print("---------------------------------------------")
print(f"Story Title: {Title}")
print(f"Summary: {Summary}")
print(f"Tags: {Tags}")
print("---------------------------------------------")
ElapsedTime = time.time() - StartTime
# Calculate Total Words
TotalWords: int = Writer.Statistics.GetWordCount(StoryBodyText)
SysLogger.Log(f"Story Total Word Count: {TotalWords}", 4)
StatsString: str = "Work Statistics: \n"
StatsString += " - Total Words: " + str(TotalWords) + " \n"
StatsString += f" - Title: {Title} \n"
StatsString += f" - Summary: {Summary} \n"
StatsString += f" - Tags: {Tags} \n"
StatsString += f" - Generation Start Date: {datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')} \n"
StatsString += f" - Generation Total Time: {ElapsedTime}s \n"
StatsString += f" - Generation Average WPM: {60 * (TotalWords/ElapsedTime)} \n"
StatsString += "\n\nUser Settings: \n"
StatsString += f" - Base Prompt: {BasePrompt} \n"
StatsString += "\n\nGeneration Settings: \n"
StatsString += f" - Generator: AIStoryGenerator_2024-06-27 \n"
StatsString += (
f" - Base Outline Writer Model: {Writer.Config.INITIAL_OUTLINE_WRITER_MODEL} \n"
)
StatsString += (
f" - Chapter Outline Writer Model: {Writer.Config.CHAPTER_OUTLINE_WRITER_MODEL} \n"
)
StatsString += f" - Chapter Writer (Stage 1: Plot) Model: {Writer.Config.CHAPTER_STAGE1_WRITER_MODEL} \n"
StatsString += f" - Chapter Writer (Stage 2: Char Development) Model: {Writer.Config.CHAPTER_STAGE2_WRITER_MODEL} \n"
StatsString += f" - Chapter Writer (Stage 3: Dialogue) Model: {Writer.Config.CHAPTER_STAGE3_WRITER_MODEL} \n"
StatsString += f" - Chapter Writer (Stage 4: Final Pass) Model: {Writer.Config.CHAPTER_STAGE4_WRITER_MODEL} \n"
StatsString += f" - Chapter Writer (Revision) Model: {Writer.Config.CHAPTER_REVISION_WRITER_MODEL} \n"
StatsString += f" - Revision Model: {Writer.Config.REVISION_MODEL} \n"
StatsString += f" - Eval Model: {Writer.Config.EVAL_MODEL} \n"
StatsString += f" - Info Model: {Writer.Config.INFO_MODEL} \n"
StatsString += f" - Scrub Model: {Writer.Config.SCRUB_MODEL} \n"
StatsString += f" - Seed: {Writer.Config.SEED} \n"
# StatsString += f" - Outline Quality: {Writer.Config.OUTLINE_QUALITY} \n"
StatsString += f" - Outline Min Revisions: {Writer.Config.OUTLINE_MIN_REVISIONS} \n"
StatsString += f" - Outline Max Revisions: {Writer.Config.OUTLINE_MAX_REVISIONS} \n"
# StatsString += f" - Chapter Quality: {Writer.Config.CHAPTER_QUALITY} \n"
StatsString += f" - Chapter Min Revisions: {Writer.Config.CHAPTER_MIN_REVISIONS} \n"
StatsString += f" - Chapter Max Revisions: {Writer.Config.CHAPTER_MAX_REVISIONS} \n"
StatsString += f" - Chapter Disable Revisions: {Writer.Config.CHAPTER_NO_REVISIONS} \n"
StatsString += f" - Disable Scrubbing: {Writer.Config.SCRUB_NO_SCRUB} \n"
# Save The Story To Disk
SysLogger.Log("Saving Story To Disk", 3)
os.makedirs("Stories", exist_ok=True)
FName = f"Stories/Story_{Title.replace(' ', '_')}"
if Writer.Config.OPTIONAL_OUTPUT_NAME != "":
FName = Writer.Config.OPTIONAL_OUTPUT_NAME
with open(f"{FName}.md", "w", encoding="utf-8") as F:
Out = f"""
{StatsString}
---
Note: An outline of the story is available at the bottom of this document.
Please scroll to the bottom if you wish to read that.
---
# {Title}
{StoryBodyText}
---
# Outline
```
{Outline}
```
"""
SysLogger.SaveStory(Out)
F.write(Out)
# Save JSON
with open(f"{FName}.json", "w", encoding="utf-8") as F:
F.write(json.dumps(StoryInfoJSON, indent=4))