Skip to content

Commit

Permalink
fix AND linebreaks without replacing newlines with normal space (#33)
Browse files Browse the repository at this point in the history
* fix and linebreaks

* fix token counter

* comment

* renamee

---------

Co-authored-by: ljleb <set>
  • Loading branch information
ljleb authored Jul 22, 2023
1 parent 6d574a4 commit 9615a3c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
16 changes: 10 additions & 6 deletions lib_neutral_prompt/prompt_parser_hijack.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
)


@prompt_parser_hijacker.hijack('get_multicond_learned_conditioning')
def get_multicond_learned_conditioning_hijack(model, prompts, steps, original_function):
# the only difference with the original `re_weight` is the prefix `\s*` in `^(\s*.*?)` (originally `^(.*?)`)
# this makes it possible to line break AND prompts without replacing all newlines with normal spaces
prompt_parser.re_weight = re.compile(r"^(\s*.*?)(?:\s*:\s*([-+]?(?:\d+\.?|\d*\.\d+)))?\s*$")


@prompt_parser_hijacker.hijack('get_multicond_prompt_list')
def get_multicond_prompt_list_hijack(prompts, original_function):
if not global_state.is_enabled:
return original_function(model, prompts, steps)
return original_function(prompts)

global_state.prompt_exprs = parse_prompts(prompts)
return original_function(model, transpile_exprs(global_state.prompt_exprs), steps)
return original_function(transpile_exprs(global_state.prompt_exprs))


def parse_prompts(prompts: List[str]) -> neutral_prompt_parser.PromptExpr:
Expand All @@ -40,8 +45,7 @@ def transpile_exprs(exprs: neutral_prompt_parser.PromptExpr):

class WebuiPromptVisitor:
def visit_leaf_prompt(self, that: neutral_prompt_parser.LeafPrompt) -> str:
prompt = re.sub(r'\s+', ' ', that.prompt).strip()
return f'{prompt} :{that.weight}'
return f'{that.prompt} :{that.weight}'

def visit_composite_prompt(self, that: neutral_prompt_parser.CompositePrompt) -> str:
return ' AND '.join(child.accept(self) for child in that.children)
1 change: 1 addition & 0 deletions lib_neutral_prompt/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def unpack_processing_args(
def on_ui_settings():
section = ('neutral_prompt', 'Neutral Prompt')
shared.opts.add_option('neutral_prompt_enabled', shared.OptionInfo(True, 'Enable neutral-prompt extension', section=section))
global_state.is_enabled = shared.opts.data.get('neutral_prompt_enabled', True)
shared.opts.add_option('neutral_prompt_verbose', shared.OptionInfo(False, 'Enable verbose debugging for neutral-prompt', section=section))
shared.opts.onchange('neutral_prompt_verbose', update_verbose)

Expand Down
7 changes: 0 additions & 7 deletions scripts/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
from lib_neutral_prompt import global_state, hijacker, neutral_prompt_parser, prompt_parser_hijack, cfg_denoiser_hijack, ui
import importlib
importlib.reload(global_state)
importlib.reload(hijacker)
importlib.reload(neutral_prompt_parser)
importlib.reload(prompt_parser_hijack)
importlib.reload(cfg_denoiser_hijack)
importlib.reload(ui)
from modules import scripts, processing, shared
from typing import Dict
import functools
Expand Down

0 comments on commit 9615a3c

Please sign in to comment.