forked from TencentQQGYLab/ComfyUI-ELLA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
159 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from contextlib import suppress | ||
|
||
|
||
def escape_important(text): | ||
return text.replace("\\)", "\0\1").replace("\\(", "\0\2") | ||
|
||
|
||
def unescape_important(text): | ||
return text.replace("\0\1", ")").replace("\0\2", "(") | ||
|
||
|
||
def parse_parentheses(string): | ||
result = [] | ||
current_item = "" | ||
nesting_level = 0 | ||
for char in string: | ||
if char == "(": | ||
if nesting_level == 0: | ||
if current_item: | ||
result.append(current_item) | ||
current_item = "(" | ||
else: | ||
current_item = "(" | ||
else: | ||
current_item += char | ||
nesting_level += 1 | ||
elif char == ")": | ||
nesting_level -= 1 | ||
if nesting_level == 0: | ||
result.append(current_item + ")") | ||
current_item = "" | ||
else: | ||
current_item += char | ||
else: | ||
current_item += char | ||
if current_item: | ||
result.append(current_item) | ||
return result | ||
|
||
|
||
def _remove_weights(string): | ||
a = parse_parentheses(string) | ||
out = [] | ||
for x in a: | ||
if len(x) >= 2 and x[-1] == ")" and x[0] == "(": | ||
x = x[1:-1] | ||
xx = x.rfind(":") | ||
if xx > 0: | ||
with suppress(Exception): | ||
x = x[:xx] | ||
out += _remove_weights(x) | ||
else: | ||
out += [x] | ||
return out | ||
|
||
|
||
def remove_weights(text: str): | ||
text = escape_important(text) | ||
parsed_weights = _remove_weights(text) | ||
return "".join([unescape_important(segment) for segment in parsed_weights]) |