Spaces:
Build error
Build error
File size: 6,053 Bytes
a1ca2de |
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 |
import re
import gradio as gr
from interfaces import utils
from modules import palmchat
def _add_side_character(
enable, prompt, cur_side_chars,
name, age, mbti, personality, job
):
if enable:
prompt = prompt + f"""
side character #{cur_side_chars}
- name: {name},
- job: {job},
- age: {age},
- mbti: {mbti},
- personality: {personality}
"""
cur_side_chars = cur_side_chars + 1
return prompt, cur_side_chars
async def plot_gen(
temperature,
genre, place, mood,
side_char_enable1, side_char_enable2, side_char_enable3,
name1, age1, mbti1, personality1, job1,
name2, age2, mbti2, personality2, job2,
name3, age3, mbti3, personality3, job3,
name4, age4, mbti4, personality4, job4,
):
cur_side_chars = 1
prompt = f"""Write a title and an outline of a novel based on the background information below in Ronald Tobias's plot theory. The outline should follow the "rising action", "crisis", "climax", "falling action", and "denouement" plot types. Each should be filled with a VERY detailed and descriptive at least two paragraphs of string. Randomly choose if the story goes optimistic or tragic.
background information:
- genre: string
- where: string
- mood: string
main character
- name: string
- job: string
- age: string
- mbti: string
- personality: string
JSON output:
{{
"title": "string",
"outline": {{
"rising action": "paragraphs of string",
"crisis": "paragraphs of string",
"climax": "paragraphs of string",
"falling action": "paragraphs of string",
"denouement": "paragraphs of string"
}}
}}
background information:
- genre: {genre}
- where: {place}
- mood: {mood}
main character
- name: {name1}
- job: {job1}
- age: {age1}
- mbti: {mbti1}
- personality: {personality1}
"""
prompt, cur_side_chars = _add_side_character(
side_char_enable1, prompt, cur_side_chars,
name2, job2, age2, mbti2, personality2
)
prompt, cur_side_chars = _add_side_character(
side_char_enable2, prompt, cur_side_chars,
name3, job3, age3, mbti3, personality3
)
prompt, cur_side_chars = _add_side_character(
side_char_enable3, prompt, cur_side_chars,
name4, job4, age4, mbti4, personality4
)
prompt = prompt + "JSON output:\n"
print(f"generated prompt:\n{prompt}")
parameters = {
'model': 'models/text-bison-001',
'candidate_count': 1,
'temperature': temperature,
'top_k': 40,
'top_p': 1,
'max_output_tokens': 4096,
}
response_json = await utils.retry_until_valid_json(prompt, parameters=parameters)
return (
response_json['title'],
f"## {response_json['title']}",
response_json['outline']['rising action'],
response_json['outline']['crisis'],
response_json['outline']['climax'],
response_json['outline']['falling action'],
response_json['outline']['denouement'],
)
async def first_story_gen(
title,
rising_action, crisis, climax, falling_action, denouement,
genre, place, mood,
side_char_enable1, side_char_enable2, side_char_enable3,
name1, age1, mbti1, personality1, job1,
name2, age2, mbti2, personality2, job2,
name3, age3, mbti3, personality3, job3,
name4, age4, mbti4, personality4, job4,
cursors, cur_cursor
):
cur_side_chars = 1
prompt = f"""Write the chapter title and the first few paragraphs of the "rising action" plot based on the background information below in Ronald Tobias's plot theory. Also, suggest three choosable actions to drive current story in different directions. The first few paragraphs should be filled with a VERY MUCH detailed and descriptive at least two paragraphs of string.
REMEMBER the first few paragraphs should not end the whole story and allow leaway for the next paragraphs to come.
The whole story SHOULD stick to the "rising action -> crisis -> climax -> falling action -> denouement" flow, so REMEMBER not to write anything mentioned from the next plots of crisis, climax, falling action, and denouement yet.
background information:
- genre: string
- where: string
- mood: string
main character
- name: string
- job: string
- age: string
- mbti: string
- personality: string
overall outline
- title: string
- rising action: string
- crisis: string
- climax: string
- falling action: string
- denouement: string
JSON output:
{{
"chapter_title": "string",
"paragraphs": ["string", "string", ...],
"actions": ["string", "string", "string"]
}}
background information:
- genre: {genre}
- where: {place}
- mood: {mood}
main character
- name: {name1}
- job: {job1},
- age: {age1},
- mbti: {mbti1},
- personality: {personality1}
"""
prompt, cur_side_chars = _add_side_character(
side_char_enable1, prompt, cur_side_chars,
name2, job2, age2, mbti2, personality2
)
prompt, cur_side_chars = _add_side_character(
side_char_enable2, prompt, cur_side_chars,
name3, job3, age3, mbti3, personality3
)
prompt, cur_side_chars = _add_side_character(
side_char_enable3, prompt, cur_side_chars,
name4, job4, age4, mbti4, personality4
)
prompt = prompt + f"""
overall outline
- title: {title}
- rising action: {rising_action}
- crisis: {crisis}
- climax: {climax}
- falling action: {falling_action}
- denouement: {denouement}
JSON output:
"""
print(f"generated prompt:\n{prompt}")
parameters = {
'model': 'models/text-bison-001',
'candidate_count': 1,
'temperature': 1,
'top_k': 40,
'top_p': 1,
'max_output_tokens': 4096,
}
response_json = await utils.retry_until_valid_json(prompt, parameters=parameters)
chapter_title = response_json["chapter_title"]
pattern = r"Chapter\s+\d+\s*[:.]"
chapter_title = re.sub(pattern, "", chapter_title)
cursors.append({
"title": chapter_title,
"plot_type": "rising action",
"story": "\n\n".join(response_json["paragraphs"])
})
return (
f"### {chapter_title} (\"rising action\")",
"\n\n".join(response_json["paragraphs"]),
cursors,
cur_cursor,
gr.update(interactive=True),
gr.update(interactive=True),
gr.update(value=response_json["actions"][0], interactive=True),
gr.update(value=response_json["actions"][1], interactive=True),
gr.update(value=response_json["actions"][2], interactive=True),
) |