Skip to content

Commit cd4ce6e

Browse files
committed
Code formatting
1 parent d92ab90 commit cd4ce6e

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/diffusers/pipelines/pipeline_utils.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,23 +1939,33 @@ def unfuse_qkv_projections(self, unet: bool = True, vae: bool = True):
19391939

19401940
def safety_checker_level(self, level):
19411941
"""
1942-
Adjust the filter intensity.
1943-
1942+
Adjust the safety checker level.
1943+
19441944
Args:
1945-
level (`int` or `float` or one of the following [`WEAK`], [`MEDIUM`], [`NOMAL`], [`STRONG`], [`MAX`])
1945+
Level (`int` or `float` or one of the following [`WEAK`], [`MEDIUM`], [`NOMAL`], [`STRONG`], [`MAX`]):
1946+
The level of safety checker adjustment, either as an integer, a float, or one of the predefined levels.
1947+
Negative values decrease the filtering strength, while positive values increase it.
19461948
"""
1949+
# Retrieve the safety_checker attribute from the instance
19471950
_safety_checker = getattr(self, "safety_checker", None)
1951+
1952+
# Check if the safety_checker exists
19481953
if _safety_checker is not None:
1954+
# Check if the safety_checker has the update_safety_checker_Level method
19491955
if hasattr(_safety_checker, "update_safety_checker_Level"):
1956+
# Update the safety checker level using the provided method
19501957
self.safety_checker.update_safety_checker_Level(level)
19511958
else:
1959+
# Log a warning if the method is not found in safety_checker
19521960
logger.warning("`safety_checker_level` is ignored because `update_safety_checker_Level` is not in `safety_checker`.")
19531961
else:
1962+
# Log a warning if safety_checker is not present
19541963
logger.warning("Since there is no `safety_checker`, `safety_checker_level` is ignored.")
19551964

1965+
@property
19561966
def filter_level(self):
19571967
"""
19581968
Return:
19591969
`int` ,`float` or None
19601970
"""
1961-
return getattr(getattr(self,"safety_checker",None), "adjustment", None)
1971+
return getattr(getattr(self, "safety_checker", None), "adjustment", None)

src/diffusers/pipelines/stable_diffusion/safety_checker.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ def __init__(self, config: CLIPConfig):
5151

5252
def update_safety_checker_Level(self, Level):
5353
"""
54-
Args:
55-
Level (`int` or `float` or one of the following [`WEAK`], [`MEDIUM`], [`NOMAL`], [`STRONG`], [`MAX`])
54+
Adjust the safety checker level.
55+
56+
Parameters:
57+
Level (`int` or `float` or one of the following [`WEAK`], [`MEDIUM`], [`NOMAL`], [`STRONG`], [`MAX`]):
58+
The level of safety checker adjustment, either as an integer, a float, or one of the predefined levels.
59+
Negative values decrease the filtering strength, while positive values increase it.
5660
"""
5761
Level_dict = {
5862
"WEAK": -0.10,
@@ -61,16 +65,22 @@ def update_safety_checker_Level(self, Level):
6165
"STRONG": 0.01,
6266
"MAX": 0.10,
6367
}
68+
69+
# If the provided Level is a predefined string, convert it to the corresponding float value
6470
if Level in Level_dict:
6571
Level = Level_dict[Level]
72+
73+
# Check if the Level is a float or an integer
6674
if isinstance(Level, (float, int)):
67-
setattr(self,"adjustment",Level)
75+
setattr(self, "adjustment", Level) # Set the adjustment attribute to the Level value
6876
else:
77+
# Raise an error if Level is not a valid type or predefined string
6978
raise ValueError("`int` or `float` or one of the following ['WEAK'], ['MEDIUM'], ['NOMAL'], ['STRONG'], ['MAX']")
70-
71-
if self.adjustment<0:
79+
80+
# Log a warning if the adjustment level is weakened (negative value)
81+
if self.adjustment < 0:
7282
logger.warning(
73-
f"You have weakened the filtering strength of safety checker. Ensure"
83+
"You have weakened the filtering strength of safety checker. Ensure"
7484
" that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered"
7585
" results in services or applications open to the public. Both the diffusers team and Hugging Face"
7686
" strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling"

0 commit comments

Comments
 (0)