Skip to content

Commit

Permalink
Refactoring and regenerate name
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaGusev committed Feb 9, 2024
1 parent 762bd8b commit 0b3a91a
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 123 deletions.
43 changes: 29 additions & 14 deletions gradio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,24 @@ def validate_inputs(model_state):
raise gr.Error("Please set the API key!")


def generate_plan(novel_type, description, model_state):
def generate_name(state, model_state):
writer = RecurrentGPT(model_state)
state.name = writer.generate_name(state)
return (state, state.name)


def generate_meta(novel_type, description, model_state):
validate_inputs(model_state)
writer = RecurrentGPT(model_state)
state = writer.generate_plan(novel_type=novel_type, description=description)
return (state, state.name, state.language, state.synopsis, state.plan)
state = writer.generate_meta(novel_type=novel_type, description=description)
return (state, state.name, state.language, state.synopsis, state.outline)


def generate_first_paragraphs(state, model_state):
def generate_first_step(state, model_state):
assert state is not None
validate_inputs(model_state)
writer = RecurrentGPT(model_state)
state = writer.generate_first_paragraphs(state)
state = writer.generate_first_step(state)
return (
state,
state.short_memory,
Expand Down Expand Up @@ -96,7 +102,7 @@ def load(file_name):
state,
state.name,
state.synopsis,
state.plan,
state.outline,
state.short_memory,
"\n\n".join(state.paragraphs),
state.next_instructions[0],
Expand Down Expand Up @@ -193,6 +199,10 @@ def on_prompt_template_name_select(model_state, prompt_template, evt: gr.SelectD
max_lines=1,
lines=1
)
btn_gen_name = gr.Button(
"Regenerate",
variant="primary"
)
language = gr.Textbox(
label="Language (editable)",
max_lines=1,
Expand All @@ -204,7 +214,7 @@ def on_prompt_template_name_select(model_state, prompt_template, evt: gr.SelectD
lines=8
)
with gr.Column(scale=9):
plan = gr.Textbox(
outline = gr.Textbox(
label="Outline (editable)",
lines=17,
max_lines=17
Expand Down Expand Up @@ -336,7 +346,7 @@ def on_prompt_template_name_select(model_state, prompt_template, evt: gr.SelectD
value=model_state.value.generation_params.repetition_penalty,
step=0.05,
interactive=True,
label="Rep"
label="Repetition penalty"
)
with gr.Column(scale=1, min_width=200):
top_p = gr.Slider(
Expand Down Expand Up @@ -367,7 +377,7 @@ def set_field(state, field, value):
"description": description,
"novel_type": novel_type,
"synopsis": synopsis,
"plan": plan,
"outline": outline,
"instruction": instruction,
"short_memory": short_memory
}
Expand Down Expand Up @@ -404,11 +414,11 @@ def set_param(state, field, value):

# Main events
btn_init.click(
generate_plan,
generate_meta,
inputs=[novel_type, description, model_state],
outputs=[state, name, language, synopsis, plan]
outputs=[state, name, language, synopsis, outline]
).success(
generate_first_paragraphs,
generate_first_step,
inputs=[state, model_state],
outputs=[state, short_memory, paragraphs, instruction1, instruction2, instruction3]
)
Expand All @@ -426,6 +436,11 @@ def set_param(state, field, value):
instruction
]
)
btn_gen_name.click(
generate_name,
inputs=[state, model_state],
outputs=[state, name]
)

# Save/Load
btn_save.click(
Expand Down Expand Up @@ -461,7 +476,7 @@ def set_param(state, field, value):
state,
name,
synopsis,
plan,
outline,
short_memory,
paragraphs,
instruction1,
Expand All @@ -480,7 +495,7 @@ def set_param(state, field, value):
state,
name,
synopsis,
plan,
outline,
short_memory,
paragraphs,
instruction1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Synopsis:
{{synopsis}}

Outline:
{{plan}}
{{outline}}

Remember, you are an author narrating events. Events should be narrated in the third person limited perspective and contain dialogues between the characters present. Do not conclude your output, leave it open for continuation. Write in a novelistic style and take your time to set the scene.
Write 3 paragraphs below:
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Write a summary that captures the critical information of the paragraphs.

Then write 3 different instructions for what to write next, each containing around 5 sentences.

Each instruction should present a possible, exciting story continuation that fits the global plan presented below.
Each instruction should present a possible, exciting story continuation that fits the global outline presented below.

The output should be in JSON with the following fields:
{
Expand All @@ -17,7 +17,7 @@ Synopsis:
{{synopsis}}

Outline:
{{plan}}
{{outline}}

Paragraphs:
{{paragraphs}}
4 changes: 2 additions & 2 deletions tale_studio/prompts/instruct.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
You should output 3 different instructions, each is a possible interesting continuation of the story.
Each output instruction should contain around 5 sentences and fit the global plan.
Each output instruction should contain around 5 sentences and fit the global outline.
Think about what plot can be attractive for common readers when writing output instructions.

Write and organize your output by strictly following the format below using JSON.
Expand All @@ -14,7 +14,7 @@ Use strictly this language: {{language}}

Here is the outline:
####
{{plan}}
{{outline}}
####

Previous short summary:
Expand Down
20 changes: 14 additions & 6 deletions tale_studio/prompts/plan.jinja → tale_studio/prompts/meta.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Please write a detailed outline for a {% if not novel_type %}Science Fiction{% e

Begin with a catchy name of the novel and determine its main language.
Then, generate a summary of the whole novel containing its main idea.
Then, plan all 6 or 7 chapters of the novel.
The story should have an ending.
Then, write an outline with all 6 or 7 chapters of the novel.
Chapter summaries should contain at least 2 sentences.
The story should have an ending.
Write in a novelistic style.
Write all the fields in the selected langauge.

Expand All @@ -14,9 +14,17 @@ The output should be in JSON with the following fields:
"language": <language of the novel>,
"name": <name of the novel>,
"synopsis": <synopsis of the whole novel>,
"chapter_summaries": [
"Chapter 1. <summary of chapter 1>",
...,
"Chapter 6. <summary of chapter 6>"
"outline": [
{
"index": 1,
"chapter_name": <chapter_name>,
"chapter_summary": <chapter_summary>,
},
...
{
"index": 6,
"chapter_name": <chapter_name>,
"chapter_summary": <chapter_summary>,
}
]
}
12 changes: 12 additions & 0 deletions tale_studio/prompts/name.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Generate a name for the novel.

Idea: {{description}}
Novel type: {{novel_type}}
Synopsis: {{synopsis}}
Outline: {{outline}}
Language: {{language}}

Format output as a JSON with one key, "name":
{
"name": "..."
}
2 changes: 1 addition & 1 deletion tale_studio/prompts/output.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Use strictly this language: {{language}}. Do not switch to another language.
Do not write any chapter numbers.

Outline of possible past and future events:
{{plan}}
{{outline}}

Summary of previous events:
{{short_memory}}
Expand Down
Loading

0 comments on commit 0b3a91a

Please sign in to comment.