Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions build_scripts/keyword_only_baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ pyrit.converter.pdf_converter:PDFConverter._serialize_pdf_async
pyrit.converter.random_capital_letters_converter:RandomCapitalLettersConverter.generate_random_positions
pyrit.converter.random_capital_letters_converter:RandomCapitalLettersConverter.string_to_upper_case_by_percentage
pyrit.converter.token_smuggling.variation_selector_smuggler_converter:VariationSelectorSmugglerConverter.encode_visible_hidden
pyrit.converter.transparency_attack_converter:TransparencyAttackConverter._compute_mse_loss
pyrit.converter.transparency_attack_converter:TransparencyAttackConverter._create_blended_image
pyrit.converter.transparency_attack_converter:TransparencyAttackConverter._save_blended_image_async
pyrit.converter.word_doc_converter:WordDocConverter._replace_placeholder_in_paragraph
pyrit.datasets.seed_datasets.remote.categorical_harmful_qa_dataset:_CategoricalHarmfulQADataset._fetch_dataset_async.<locals>.resolve_harm_categories
pyrit.datasets.seed_datasets.remote.garak_leak_replay_dataset:_write_cloze_tsv
Expand Down
12 changes: 6 additions & 6 deletions pyrit/converter/transparency_attack_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def _load_and_preprocess_image(self, path: str) -> np.ndarray:
except Exception as e:
raise ValueError(f"Failed to load and preprocess image from {path}: {e}") from e

def _compute_mse_loss(self, blended_image: np.ndarray, target_tensor: np.ndarray) -> float:
def _compute_mse_loss(self, blended_image: np.ndarray, *, target_tensor: np.ndarray) -> float:
"""
Compute Mean Squared Error (MSE) loss between blended and target images.

Expand All @@ -237,7 +237,7 @@ def _compute_mse_loss(self, blended_image: np.ndarray, target_tensor: np.ndarray
"""
return float(np.mean(np.square(blended_image - target_tensor)))

def _create_blended_image(self, attack_image: np.ndarray, alpha: np.ndarray) -> np.ndarray:
def _create_blended_image(self, attack_image: np.ndarray, *, alpha: np.ndarray) -> np.ndarray:
"""
Create a blended image using the attack image and alpha transparency.

Expand All @@ -259,7 +259,7 @@ def _create_blended_image(self, attack_image: np.ndarray, alpha: np.ndarray) ->

return la_image

async def _save_blended_image_async(self, attack_image: np.ndarray, alpha: np.ndarray) -> str:
async def _save_blended_image_async(self, attack_image: np.ndarray, *, alpha: np.ndarray) -> str:
"""
Save the blended image with transparency as a PNG file.

Expand All @@ -277,7 +277,7 @@ async def _save_blended_image_async(self, attack_image: np.ndarray, alpha: np.nd
img_serializer = data_serializer_factory(category="prompt-memory-entries", data_type="image_path")
img_serializer.file_extension = "png"

la_image = self._create_blended_image(attack_image, alpha)
la_image = self._create_blended_image(attack_image, alpha=alpha)
# Pillow 10+ can infer the mode from the array dtype and shape
la_pil = Image.fromarray(la_image)
image_buffer = BytesIO()
Expand Down Expand Up @@ -325,7 +325,7 @@ async def convert_async(self, *, prompt: str, input_type: PromptDataType = "imag
# Simulate blending: alpha=1 uses darkened attack image, alpha=0 uses white
blended_image = alpha * background_tensor + (1 - alpha) * white_background

current_loss = self._compute_mse_loss(blended_image, self._cached_benign_image)
current_loss = self._compute_mse_loss(blended_image, target_tensor=self._cached_benign_image)
if step % 100 == 0:
logger.debug(f"Step {step}/{self.steps}, Loss: {current_loss:.6f}")

Expand All @@ -345,5 +345,5 @@ async def convert_async(self, *, prompt: str, input_type: PromptDataType = "imag
alpha = optimizer.update(params=alpha, grads=grad_alpha)
alpha = np.clip(alpha, 0.0, 1.0)

image_path = await self._save_blended_image_async(background_tensor, alpha)
image_path = await self._save_blended_image_async(background_tensor, alpha=alpha)
return ConverterResult(output_text=image_path, output_type="image_path")
6 changes: 3 additions & 3 deletions tests/unit/converter/test_transparency_attack_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_compute_mse_loss(self, sample_benign_image):
target = np.array([[2.0, 3.0], [4.0, 5.0]])
expected_loss = 1.0

loss = converter._compute_mse_loss(blended, target)
loss = converter._compute_mse_loss(blended, target_tensor=target)
assert loss == expected_loss
assert isinstance(loss, float)

Expand All @@ -149,7 +149,7 @@ def test_create_blended_image(self, sample_benign_image):
attack_image = np.array([[0.2]], dtype=np.float32) # 1x1 grayscale image
alpha = np.array([[0.8]], dtype=np.float32) # 1x1 alpha

la_image = converter._create_blended_image(attack_image, alpha)
la_image = converter._create_blended_image(attack_image, alpha=alpha)

assert la_image.shape == (1, 1, 2) # LA (Luminance + Alpha)
assert la_image.dtype == np.uint8
Expand All @@ -169,7 +169,7 @@ async def test_save_blended_image(self, sample_benign_image):
attack_image = np.ones((10, 10), dtype=np.float32) * 0.5
alpha = np.ones((10, 10), dtype=np.float32) * 0.7

result_path = await converter._save_blended_image_async(attack_image, alpha)
result_path = await converter._save_blended_image_async(attack_image, alpha=alpha)

assert result_path == "mock_image_path.png"
mock_factory.assert_called_once_with(category="prompt-memory-entries", data_type="image_path")
Expand Down
Loading