-
Notifications
You must be signed in to change notification settings - Fork 641
FEAT: Zalgo Converter #883
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
Merged
Merged
Changes from 22 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
5c54b2b
Initial zalgo converter
elisetreit cf885df
Added optional intensity and seed parameters
elisetreit 1ace7d3
Adding some intensity guardrails
elisetreit cb777ea
cleaning up example notebook
elisetreit 4870a69
Adding script version of notebook
elisetreit a68a62a
Adding to init
elisetreit 485d4d0
Initial zalgo converter
elisetreit a024a25
Added optional intensity and seed parameters
elisetreit c221645
Adding some intensity guardrails
elisetreit c797a46
cleaning up example notebook
elisetreit c5ce61c
Adding script version of notebook
elisetreit 2835945
Adding to init
elisetreit fffd7f3
Merge branch 'zalgo' of https://github.com/elisetreit/PyRIT into zalgo
elisetreit 800030a
removing example notebook + script
elisetreit e9c525b
Added normalizing intensity and logging warning if intensity is too high
elisetreit d5a5c0f
Adding unit tests
elisetreit 62d218b
Merge branch 'main' into zalgo
rlundeen2 101e827
fixing precommit, adding copyright
elisetreit f8a6fc4
reducing line length
elisetreit 9e203e7
Merge branch 'zalgo' of https://github.com/elisetreit/PyRIT into zalgo
elisetreit f30f78b
Formatting
elisetreit 6ad4981
Merge branch 'main' into zalgo
elisetreit ec43345
Changing to more inclusive language
elisetreit 672a9e5
Adding normalizing intensity (int) to constructor, more detail to log…
elisetreit d444c20
cleaning up logger formatting
elisetreit da5117d
More tests, plus better check if intensity was normalized
elisetreit df98e78
Automated formatting changes and removing unused declaration
elisetreit 2c6adae
Merge branch 'main' into zalgo
elisetreit df5a306
remove unnecessary comment
elisetreit 72aa7f7
Add zalgo converter to api.rst
elisetreit 96cb912
Merge branch 'zalgo' of https://github.com/elisetreit/PyRIT into zalgo
elisetreit 2f67d7c
Merge branch 'main' into zalgo
bashirpartovi 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
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import logging | ||
| import random | ||
| from typing import Optional | ||
|
|
||
| from pyrit.models import PromptDataType | ||
| from pyrit.prompt_converter import ConverterResult, PromptConverter | ||
|
|
||
| # Unicode combining characters for Zalgo effect (U+0300–U+036F) | ||
| ZALGO_MARKS = [chr(code) for code in range(0x0300, 0x036F + 1)] | ||
| # Setting a max intensity so people don't do anything insane | ||
elisetreit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| MAX_INTENSITY = 100 | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ZalgoConverter(PromptConverter): | ||
| def __init__(self, *, intensity: int = 10, seed: Optional[int] = None) -> None: | ||
| """ | ||
| Initializes the Zalgo converter. | ||
| Args: | ||
| intensity (int): Number of combining marks per character (higher = more cursed). Default is 10. | ||
| seed (Optional[int]): Optional seed for reproducible output. | ||
| """ | ||
| self._intensity = intensity | ||
| self._seed = seed | ||
|
|
||
| def _normalize_intensity(self) -> int: | ||
| if self._intensity > MAX_INTENSITY: | ||
| logger.warning("This is higher intensity than is supported (max 100)") | ||
elisetreit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return max(0, min(self._intensity, MAX_INTENSITY)) | ||
|
|
||
| async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult: | ||
| """ | ||
| Converts text into cursed Zalgo text using combining Unicode marks. | ||
| """ | ||
| intensity = self._normalize_intensity() | ||
| if not self.input_supported(input_type): | ||
| raise ValueError("Input type not supported") | ||
| if intensity <= 0: | ||
elisetreit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| output_text = prompt | ||
| else: | ||
| if self._seed is not None: | ||
| random.seed(self._seed) | ||
|
|
||
| def glitch(char: str) -> str: | ||
| return char + "".join(random.choice(ZALGO_MARKS) for _ in range(random.randint(1, intensity))) | ||
|
|
||
| output_text = "".join(glitch(c) if c.isalnum() else c for c in prompt) | ||
| return ConverterResult(output_text=output_text, output_type="text") | ||
elisetreit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def input_supported(self, input_type: PromptDataType) -> bool: | ||
| return input_type == "text" | ||
|
|
||
| def output_supported(self, output_type: PromptDataType) -> bool: | ||
| return output_type == "text" | ||
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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import pytest | ||
|
|
||
| from pyrit.prompt_converter import ZalgoConverter | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_zalgo_output_changes_text(): | ||
| prompt = "hello" | ||
| converter = ZalgoConverter(intensity=5, seed=42) | ||
| result = await converter.convert_async(prompt=prompt) | ||
| assert result.output_text != prompt | ||
| assert all(c in result.output_text for c in prompt) # should still contain all original letters | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_zalgo_reproducible_seed(): | ||
| prompt = "seed test" | ||
| converter1 = ZalgoConverter(intensity=5, seed=123) | ||
| converter2 = ZalgoConverter(intensity=5, seed=123) | ||
| result1 = await converter1.convert_async(prompt=prompt) | ||
| result2 = await converter2.convert_async(prompt=prompt) | ||
| assert result1.output_text == result2.output_text | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_zalgo_zero_intensity_returns_original(): | ||
| prompt = "no chaos please" | ||
| converter = ZalgoConverter(intensity=0) | ||
| result = await converter.convert_async(prompt=prompt) | ||
| assert result.output_text == prompt | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_zalgo_intensity_caps_at_max(): | ||
elisetreit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| prompt = "much zalgo!" | ||
| converter = ZalgoConverter(intensity=1000, seed=1) | ||
| result = await converter.convert_async(prompt=prompt) | ||
| # Should still complete successfully without crashing and adjust to max intensity | ||
| assert isinstance(result.output_text, str) | ||
| assert len(result.output_text) > len(prompt) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.