-
Notifications
You must be signed in to change notification settings - Fork 746
FEAT: Add support for safe image outputs in console printer, file output in markdown printer #1659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fdubut
wants to merge
9
commits into
microsoft:main
Choose a base branch
from
fdubut:printer_updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0b5e3b9
Add template path support to Jailbreak scenario, add safe outputs to …
fdubut 0cd9acd
Add support for file output to MD printer
fdubut ac50df8
Merge branch 'main' of https://github.com/fdubut/PyRIT into jailbreak…
fdubut 7810090
Fix bug in MD printer to file
fdubut 82d05df
Merge branch 'main' of https://github.com/fdubut/PyRIT into printer_u…
fdubut d464382
Separate jailbreak updates, fix MD printer, add test
fdubut 3bf9858
Merge branch 'main' of https://github.com/fdubut/PyRIT into printer_u…
fdubut a83f355
Fix MD printer edge case, add tests to display_response
fdubut bbe4836
Fix edge case in test
fdubut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,56 +1,65 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import io | ||
| import logging | ||
|
|
||
| from PIL import Image | ||
|
|
||
| from pyrit.common.notebook_utils import is_in_ipython_session | ||
| from pyrit.memory import CentralMemory | ||
| from pyrit.models import AzureBlobStorageIO, DiskStorageIO, MessagePiece | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def display_image_response(response_piece: MessagePiece) -> None: | ||
| """ | ||
| Display response images if running in notebook environment. | ||
|
|
||
| Args: | ||
| response_piece (MessagePiece): The response piece to display. | ||
|
|
||
| Raises: | ||
| RuntimeError: If storage IO is not initialized. | ||
| """ | ||
| memory = CentralMemory.get_memory_instance() | ||
| if ( | ||
| response_piece.response_error == "none" | ||
| and response_piece.converted_value_data_type == "image_path" | ||
| and is_in_ipython_session() | ||
| ): | ||
| image_location = response_piece.converted_value | ||
|
|
||
| try: | ||
| if memory.results_storage_io is None: | ||
| raise RuntimeError("Storage IO not initialized") | ||
| image_bytes = await memory.results_storage_io.read_file(image_location) | ||
| except Exception as e: | ||
| if isinstance(memory.results_storage_io, AzureBlobStorageIO): | ||
| try: | ||
| # Fallback to reading from disk if the storage IO fails | ||
| image_bytes = await DiskStorageIO().read_file(image_location) | ||
| except Exception as exc: | ||
| logger.error(f"Failed to read image from {image_location}. Full exception: {str(exc)}") | ||
| return | ||
| else: | ||
| logger.error(f"Failed to read image from {image_location}. Full exception: {str(e)}") | ||
| return | ||
|
|
||
| image_stream = io.BytesIO(image_bytes) | ||
| image = Image.open(image_stream) | ||
|
|
||
| # Jupyter built-in display function only works in notebooks. | ||
| display(image) # type: ignore[name-defined] # noqa: F821 | ||
| if response_piece.response_error == "blocked": | ||
| logger.info("---\nContent blocked, cannot show a response.\n---") | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import io | ||
| import logging | ||
|
|
||
| from PIL import Image, ImageEnhance | ||
|
|
||
| from pyrit.common.notebook_utils import is_in_ipython_session | ||
| from pyrit.memory import CentralMemory | ||
| from pyrit.models import AzureBlobStorageIO, DiskStorageIO, MessagePiece | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def display_image_response(response_piece: MessagePiece, safe_outputs: bool = False) -> None: | ||
| """ | ||
| Display response images if running in notebook environment. | ||
|
|
||
| Args: | ||
| response_piece (MessagePiece): The response piece to display. | ||
| safe_outputs (bool): Whether to sanitize image outputs before displaying them. | ||
|
|
||
| Raises: | ||
| RuntimeError: If storage IO is not initialized. | ||
| """ | ||
| memory = CentralMemory.get_memory_instance() | ||
| if ( | ||
| response_piece.response_error == "none" | ||
| and response_piece.converted_value_data_type == "image_path" | ||
| and is_in_ipython_session() | ||
| ): | ||
| image_location = response_piece.converted_value | ||
|
|
||
| try: | ||
| if memory.results_storage_io is None: | ||
| raise RuntimeError("Storage IO not initialized") | ||
| image_bytes = await memory.results_storage_io.read_file(image_location) | ||
| except Exception as e: | ||
| if isinstance(memory.results_storage_io, AzureBlobStorageIO): | ||
| try: | ||
| # Fallback to reading from disk if the storage IO fails | ||
| image_bytes = await DiskStorageIO().read_file(image_location) | ||
| except Exception as exc: | ||
| logger.error(f"Failed to read image from {image_location}. Full exception: {str(exc)}") | ||
| return | ||
| else: | ||
| logger.error(f"Failed to read image from {image_location}. Full exception: {str(e)}") | ||
| return | ||
|
|
||
| image_stream = io.BytesIO(image_bytes) | ||
| image: Image.Image = Image.open(image_stream) | ||
|
|
||
| if safe_outputs: | ||
| new_width = int(image.width * 0.5) | ||
| new_height = int(image.height * 0.5) | ||
| image = image.resize((new_width, new_height), Image.Resampling.LANCZOS) | ||
|
|
||
| image = ImageEnhance.Color(image).enhance(0.0) | ||
| image = image.rotate(90.0, expand=True, fillcolor=(255, 255, 255)) | ||
|
Comment on lines
+54
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the rotation and the other changes? Blurring seems more appropriate? |
||
|
|
||
| # Jupyter built-in display function only works in notebooks. | ||
| display(image) # type: ignore[name-defined] # noqa: F821 | ||
| if response_piece.response_error == "blocked": | ||
| logger.info("---\nContent blocked, cannot show a response.\n---") | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"safe" implies that we hide only unsafe ones. Maybe hide_outputs or blur_outputs or similar would be preferable? Also, this only applies to images, not all outputs.
I also don't want to turn this on in our example notebooks because the entire point is to illustrate how the package works. We can show it in one of them, of course.