File size: 2,142 Bytes
0070fce
25a6abd
0070fce
a2e084c
0070fce
25a6abd
 
 
0070fce
a2e084c
 
 
 
 
 
 
 
0070fce
 
 
 
 
 
 
 
 
 
 
25a6abd
0070fce
 
 
a2e084c
 
 
 
0070fce
 
 
 
 
 
25a6abd
 
 
 
 
 
 
0070fce
 
 
a2e084c
 
 
 
 
 
 
 
 
 
 
 
 
 
0070fce
 
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
import re
from typing import TYPE_CHECKING

from modules import scripts, shared

if TYPE_CHECKING:
    from modules.processing import StableDiffusionProcessing


def strip_comments(text: str) -> str:
    if not shared.opts.enable_prompt_comments:
        return text

    # multi line comment (/* */)
    text = re.sub(r"\/\*.*?\*\/", "", text, flags=re.DOTALL)
    # single line comment (# | //)
    text = re.sub(r"[^\S\n]*(\#|\/\/).*", "", text)

    return text


class ScriptComments(scripts.Script):
    def title(self):
        return "Comments"

    def show(self, is_img2img):
        return scripts.AlwaysVisible

    def process(self, p: "StableDiffusionProcessing", *args):
        if not shared.opts.enable_prompt_comments:
            return

        if shared.opts.save_prompt_comments:
            p._all_prompts_c = p.all_prompts.copy()
            p._all_negative_prompts_c = p.all_negative_prompts.copy()

        p.all_prompts = [strip_comments(x) for x in p.all_prompts]
        p.all_negative_prompts = [strip_comments(x) for x in p.all_negative_prompts]

        p.main_prompt = strip_comments(p.main_prompt)
        p.main_negative_prompt = strip_comments(p.main_negative_prompt)

        if getattr(p, "enable_hr", False):
            p.all_hr_prompts = [strip_comments(x) for x in p.all_hr_prompts]
            p.all_hr_negative_prompts = [strip_comments(x) for x in p.all_hr_negative_prompts]

            p.hr_prompt = strip_comments(p.hr_prompt)
            p.hr_negative_prompt = strip_comments(p.hr_negative_prompt)


shared.options_templates.update(
    shared.options_section(
        ("ui_comments", "Comments", "ui"),
        {
            "enable_prompt_comments": shared.OptionInfo(True, "Remove Comments from Prompts").html(
                """

<b>Comment Syntax:</b><br>

<ul style='margin-left: 2em'>

<li># ...</li>

<li>// ...</li>

<li>/* ... */</li>

</ul>

                """
            ),
            "save_prompt_comments": shared.OptionInfo(False, "Save Raw Comments").info("include the comments in Infotext"),
        },
    )
)