Spaces:
Sleeping
Sleeping
File size: 9,184 Bytes
de242d3 787638b de242d3 f44ec10 ff2d371 f44ec10 b2bd87c f44ec10 56441c0 787638b de242d3 6edb968 787638b 6edb968 945f7f7 1ba1f0c 6edb968 787638b 67edd25 2d3370c 787638b 56441c0 787638b 6edb968 de2520c 6edb968 787638b b2bd87c 787638b 1ba1f0c f2fd77c 67edd25 f2fd77c 787638b 6edb968 787638b 4f3a32c 78eea54 4f3a32c 787638b 1315b1b 787638b 4f3a32c 787638b 1315b1b 787638b 4f3a32c 787638b 1315b1b 787638b 1315b1b 787638b 4f3a32c 787638b 1315b1b 787638b 4f3a32c 787638b 2d3370c 18fb5f7 21f08ab 2d3370c b2bd87c eb181c3 e933949 2d3370c 787638b 4f3a32c 787638b 4f3a32c 787638b 4f3a32c 787638b 857f578 787638b 4f3a32c 116a522 2d3370c 0cdfdad 2d3370c 4f3a32c 2d3370c 2449837 787638b de242d3 |
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 |
import gradio as gr
import requests
import json
from numbers import Real
builtin_pairs = [
("Chat Examples", "dialogueExamples"),
("Chat History", "chatHistory"),
("World Info (after)", "worldInfoAfter"),
("World Info (before)", "worldInfoBefore"),
("Enhance Definitions", "enhanceDefinitions"),
("Char Description", "charDescription"),
("Char Personality", "charPersonality"),
("Scenario", "scenario"),
("Persona Description", "personaDescription"),
("JB", "jailbreak"),
("NSFW", "nsfw"),
("Main Prompt", "main"),
]
builtin_prompt_ids = [x[1] for x in builtin_pairs]
builtin_prompts = [{
"name": x[0],
"identifier": x[1],
"system_prompt": True,
"marker": True,
} for x in builtin_pairs]
# Not meant to 100% validate a preset. Just enough that the code
# that renders it won't crash trying to access an invalid key.
class Validator:
def __init__(self, obj):
self.valid = True
self.obj = obj
def try_validate_key(self, key, ty):
if key not in self.obj:
return False
if not isinstance(self.obj[key], ty):
return False
return True
def validate_key(self, key, ty):
r = self.try_validate_key(key, ty)
if not r:
self.valid = False
return r
def validate_key_if_present(self, key, ty):
if key in self.obj and not isinstance(self.obj[key], ty):
self.valid = False
def validate_keys_if_present(self, keys, ty):
for k in keys:
self.validate_key_if_present(k, ty)
def validate_prompt(self):
self.validate_key("identifier", str)
self.validate_key("name", str)
self.validate_keys_if_present([
"injection_position",
"injection_depth",
], int)
self.validate_keys_if_present([
"role",
"content",
], str)
self.validate_keys_if_present([
"system_prompt",
"marker",
"forbid_overrides",
], bool)
def validate_prompt_order(self):
if not self.try_validate_key("character_id", str):
self.validate_key("character_id", int)
if self.validate_key("order", list):
if not Validator.is_valid_prompt_order_list(self.obj["order"]):
self.valid = False
def validate_prompt_order_list(self):
for s in self.obj:
if not Validator.is_valid_ordering(s):
self.valid = False
def validate_ordering(self):
self.validate_key("identifier", str)
self.validate_key("enabled", bool)
def validate_preset(self):
self.validate_keys_if_present([
"impersonation_prompt",
"new_chat_prompt",
"new_group_chat_prompt",
"new_example_chat_prompt",
"continue_nudge_prompt",
"wi_format",
"scenario_format",
"personality_format",
"group_nudge_prompt",
"assistant_prefill",
"human_sysprompt_message",
"continue_postfix",
], str)
self.validate_keys_if_present([
"claude_use_sysprompt",
"squash_system_messages",
"continue_prefill",
], bool)
self.validate_keys_if_present([
"temperature",
"frequency_penalty",
"presence_penalty",
"count_penalty",
"top_p",
"top_k",
"top_a",
"min_p",
"repetition_penalty",
], Real)
self.validate_key_if_present("names_behavior", int)
known_prompt_ids = set(builtin_prompt_ids)
if self.validate_key("prompts", list):
for prompt in self.obj["prompts"]:
if not Validator.is_valid_prompt(prompt):
continue
known_prompt_ids.add(prompt["identifier"])
seen_cid0 = False
if self.validate_key("prompt_order", list) and len(self.obj["prompt_order"]) > 0 and isinstance(self.obj["prompt_order"][0], dict):
for order in self.obj["prompt_order"]:
if Validator.is_valid_prompt_order(order) and order["character_id"] == 100001 and all(o["identifier"] in known_prompt_ids for o in order["order"]):
seen_cid0 = True
if not seen_cid0:
self.valid = False
@classmethod
def is_valid_preset(cls, preset):
v = cls(preset)
v.validate_preset()
return v.valid
@classmethod
def is_valid_prompt(cls, prompt):
v = cls(prompt)
v.validate_prompt()
return v.valid
@classmethod
def is_valid_prompt_order(cls, prompt_order):
v = cls(prompt_order)
v.validate_prompt_order()
return v.valid
@classmethod
def is_valid_prompt_order_list(cls, prompt_order_list):
v = cls(prompt_order_list)
v.validate_prompt_order_list()
return v.valid
@classmethod
def is_valid_ordering(cls, ordering):
v = cls(ordering)
v.validate_ordering()
return v.valid
def maybe_convert_to_original_format(obj):
if obj.get("version") != 1:
return obj, False
# Synthesize a traditional preset
return {
"prompts": obj["data"]["prompts"] + builtin_prompts,
"prompt_order": [{
"character_id": 100001,
"order": obj["data"]["prompt_order"],
}]
}, True
CONVERT_MESSAGE = "This preset was converted from version 1 to the traditional format, there may be inconsistencies."
def load_from_file(path):
with open(path, "r") as f:
try:
obj = json.load(f)
except Exception:
raise gr.Error("File is not valid JSON")
return None, True
obj, modified = maybe_convert_to_original_format(obj)
if not Validator.is_valid_preset(obj):
raise gr.Error("File is not a valid preset")
return None, True
return gr.update(selected=1), obj, CONVERT_MESSAGE if modified else None
def load_from_url(url):
resp = requests.get(url)
if not resp.ok:
raise gr.Error("Failed to load URL")
return None, True
try:
obj = json.loads(resp.text)
except Exception:
raise gr.Error("URL is not valid JSON")
return None, True
obj, modified = maybe_convert_to_original_format(obj)
if not Validator.is_valid_preset(obj):
raise gr.Error("URL is not a valid preset")
return None, True
return gr.update(selected=1), obj, CONVERT_MESSAGE if modified else None
def render_prompt(prompt, enabled=True):
with gr.Accordion(prompt["name"] + ("" if enabled else " (DISABLED)"), open=enabled or prompt.get("marker", False)):
if prompt.get("marker", False):
gr.Markdown(f"This is a marker ({prompt['identifier']})")
else:
gr.Markdown(f"Role: {prompt['role'] or 'system'}")
if "injection_position" in prompt and prompt["injection_position"] == 1 and "injection_depth" in prompt:
gr.Markdown(f"Injection depth: {prompt['injection_depth']} (absolute)")
if prompt["identifier"] in builtin_prompt_ids:
gr.Markdown(f"This is a system prompt ({prompt['identifier']})")
if prompt["content"]:
gr.Textbox(prompt["content"], container=False, interactive=False)
with gr.Blocks() as demo:
preset = gr.State(None)
load_extra = gr.State(None)
gr.Markdown("# SillyTavern preset viewer")
with gr.Tabs() as tabs:
with gr.TabItem("Upload", id=0):
file = gr.File(label="Upload a preset (.json)", file_types=[".json"])
file.upload(fn=load_from_file, inputs=[file], outputs=[tabs, preset, load_extra])
url_input = gr.Textbox(label="Enter a URL to a preset (.json) - press Enter to submit")
url_input.submit(fn=load_from_url, inputs=[url_input], outputs=[tabs, preset, load_extra])
with gr.TabItem("Viewer", id=1):
@gr.render(inputs=[preset, load_extra])
def render_preset(preset, load_extra):
if preset is None:
gr.Markdown("No preset loaded, enter a URL or upload a file")
else:
gr.Markdown("Preset loaded and validated")
if load_extra is not None:
gr.Markdown(load_extra)
prompt_map = {p["identifier"]: p for p in builtin_prompts+preset["prompts"]}
gr.Markdown("# Preset")
for order in (next(o for o in preset["prompt_order"] if o["character_id"] == 100001)["order"] if isinstance(preset["prompt_order"], list) else preset["prompt_order"]):
prompt = prompt_map[order["identifier"]]
render_prompt(prompt, order["enabled"])
gr.Markdown("# All prompts")
with gr.Accordion("All prompts", open=False):
for prompt in prompt_map.values():
render_prompt(prompt)
demo.launch() |