File size: 6,995 Bytes
de242d3
787638b
 
 
de242d3
787638b
 
 
 
 
 
de242d3
787638b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d3370c
787638b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2449837
787638b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1315b1b
787638b
 
1315b1b
787638b
3b5c651
787638b
 
 
 
1315b1b
787638b
 
 
 
1315b1b
787638b
 
1315b1b
787638b
3b5c651
787638b
2d3370c
18fb5f7
 
21f08ab
2d3370c
 
 
 
2449837
2d3370c
787638b
 
 
 
 
 
 
 
 
3b5c651
787638b
3b5c651
787638b
 
 
 
 
 
 
 
 
 
2d3370c
 
2449837
2d3370c
 
2449837
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
import gradio as gr
import requests
import json
from numbers import Real

# 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 validate_key(self, key, ty):
        if key not in self.obj:
            self.valid = False
            return False
        if not isinstance(self.obj[key], ty):
            self.valid = False
            return False
        return True
    
    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", bool)
        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):
        self.validate_key("character_id", str)
        if self.validate_key("order", list):
            for s in self.obj["order"]:
                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()
        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):
            for order in self.obj["prompt_order"]:
                if Validator.is_valid_prompt_order(order) and order["character_id"] == "100000" and all(lambda o: 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_ordering(cls, ordering):
        v = cls(ordering)
        v.validate_ordering()
        return v.valid

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
    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, False

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
    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, False

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)")
            gr.Code(prompt["content"], container=False)

with gr.Blocks() as demo:
    preset_error = gr.State(False)
    preset = 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, preset_error])
            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, preset_error])
        with gr.TabItem("Viewer", id=1):
            @gr.render(inputs=[preset_error, preset])
            def render_preset(preset_error, preset):
                if preset_error:
                    gr.Markdown("Error loading preset")
                    return
                if preset is None:
                    gr.Markdown("No preset loaded, enter a URL or upload a file")
                else:
                    gr.Markdown("Preset loaded and validated")
                    prompt_map = {p["identifier"]: p for p in preset["prompts"]}
                    gr.Markdown("# Preset")
                    for order in next(o for o in preset["prompt_order"] if o["character_id"] == "100000")["order"]:
                        prompt = prompt_map[order["identifier"]]
                        render_prompt(prompt, order["enabled"])
                    with gr.Accordion("# All prompts", open=False):
                        for prompt in prompt_map.values():
                            render_prompt(prompt)

    
demo.launch()