aboutsummaryrefslogtreecommitdiff
path: root/modules/processing_scripts/comments.py
diff options
context:
space:
mode:
authorAUTOMATIC1111 <16777216c@gmail.com>2024-03-02 07:03:13 +0300
committerAUTOMATIC1111 <16777216c@gmail.com>2024-03-02 07:03:13 +0300
commitbef51aed032c0aaa5cfd80445bc4cf0d85b408b5 (patch)
tree42957c454a4ac8d98488f19811b60359d05d88ba /modules/processing_scripts/comments.py
parentcf2772fab0af5573da775e7437e6acdca424f26e (diff)
parent13984857890401e8605a3e53bd671e900a18d73f (diff)
Merge branch 'release_candidate'
Diffstat (limited to 'modules/processing_scripts/comments.py')
-rw-r--r--modules/processing_scripts/comments.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/modules/processing_scripts/comments.py b/modules/processing_scripts/comments.py
new file mode 100644
index 00000000..638e39f2
--- /dev/null
+++ b/modules/processing_scripts/comments.py
@@ -0,0 +1,42 @@
+from modules import scripts, shared, script_callbacks
+import re
+
+
+def strip_comments(text):
+ text = re.sub('(^|\n)#[^\n]*(\n|$)', '\n', text) # while line comment
+ text = re.sub('#[^\n]*(\n|$)', '\n', text) # in the middle of the line comment
+
+ return text
+
+
+class ScriptStripComments(scripts.Script):
+ def title(self):
+ return "Comments"
+
+ def show(self, is_img2img):
+ return scripts.AlwaysVisible
+
+ def process(self, p, *args):
+ if not shared.opts.enable_prompt_comments:
+ return
+
+ 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)
+
+
+def before_token_counter(params: script_callbacks.BeforeTokenCounterParams):
+ if not shared.opts.enable_prompt_comments:
+ return
+
+ params.prompt = strip_comments(params.prompt)
+
+
+script_callbacks.on_before_token_counter(before_token_counter)
+
+
+shared.options_templates.update(shared.options_section(('sd', "Stable Diffusion", "sd"), {
+ "enable_prompt_comments": shared.OptionInfo(True, "Enable comments").info("Use # anywhere in the prompt to hide the text between # and the end of the line from the generation."),
+}))