-
-
Notifications
You must be signed in to change notification settings - Fork 221
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
Add NER PII Masking module #319
Draft
bwook00
wants to merge
5
commits into
main
Choose a base branch
from
Feature/#314
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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,3 +1,4 @@ | ||
from .percentile_cutoff import similarity_percentile_cutoff | ||
from .ner_pii_masking import ner_pii_masking | ||
from .pass_passage_filter import pass_passage_filter | ||
from .threshold_cutoff import similarity_threshold_cutoff |
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,41 @@ | ||
import asyncio | ||
from typing import List, Tuple | ||
|
||
from transformers import pipeline | ||
|
||
from autorag.nodes.passagefilter.base import passage_filter_node | ||
|
||
|
||
@passage_filter_node | ||
def ner_pii_masking(contents_list: List[List[str]], | ||
scores_list: List[List[float]], ids_list: List[List[str]], | ||
) -> Tuple[List[List[str]], List[List[str]], List[List[float]]]: | ||
""" | ||
Mask PII in the contents using NER. | ||
Use a Hugging Face NER model for PII Masking | ||
|
||
:param contents_list: The list of lists of contents to filter | ||
:param scores_list: The list of lists of scores retrieved | ||
:param ids_list: The list of lists of ids retrieved | ||
:return: Tuple of lists containing the filtered contents, ids, and scores | ||
""" | ||
model = pipeline("ner", grouped_entities=True) | ||
|
||
tasks = [mask_pii(model, contents) for contents in contents_list] | ||
loop = asyncio.get_event_loop() | ||
results = loop.run_until_complete(asyncio.gather(*tasks)) | ||
masked_contents_list = list(results) | ||
|
||
return masked_contents_list, ids_list, scores_list | ||
|
||
|
||
async def mask_pii(model, contents: List[str]) -> List[str]: | ||
new_contents_list = [] | ||
for content in contents: | ||
new_contents = content | ||
response = model(content) | ||
for entry in response: | ||
entity_group_tag = f"[{entry['entity_group']}_{entry['start']}]" | ||
new_contents = new_contents.replace(entry["word"], entity_group_tag).strip() | ||
new_contents_list.append(new_contents) | ||
return new_contents_list |
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,23 @@ | ||
# NER PII Masking | ||
|
||
This module is inspired by | ||
LlamaIndex ['PII Masking'](https://docs.llamaindex.ai/en/stable/examples/node_postprocessor/PII/#option-1-use-ner-model-for-pii-masking). | ||
|
||
Use a Hugging Face NER model for PII Masking | ||
|
||
## What is PII Masking? | ||
|
||
PII(Personally Identifiable Information) Masking is a data protection method that obscures sensitive personal | ||
information to prevent unauthorized access while retaining data utility for analysis or development. Techniques include | ||
encryption, substitution, and scrambling, ensuring compliance and minimizing breach risks. | ||
|
||
## **Module Parameters** | ||
|
||
- **Not Applicable (N/A):** There are no direct module parameters specified for the `ner_pii_masking` module. | ||
|
||
## **Example config.yaml** | ||
|
||
```yaml | ||
modules: | ||
- module_type: ner_pii_masking | ||
``` |
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 |
---|---|---|
|
@@ -51,4 +51,5 @@ maxdepth: 1 | |
--- | ||
similarity_threshold_cutoff.md | ||
similarity_percentile_cutoff.md | ||
ner_pii_masking.md | ||
``` |
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,15 @@ | ||
from autorag.nodes.passagefilter import ner_pii_masking | ||
from tests.autorag.nodes.passagefilter.test_passage_filter_base import base_passage_filter_test, contents_example, \ | ||
ids_example, scores_example, project_dir, previous_result, base_passage_filter_node_test | ||
|
||
|
||
def test_ner_pii_masking(): | ||
original_ner = ner_pii_masking.__wrapped__ | ||
contents, ids, scores = original_ner(contents_example, scores_example, ids_example) | ||
assert contents[1][3] == "[PER_0] is one of the members of [ORG_34]." | ||
base_passage_filter_test(contents, ids, scores) | ||
|
||
|
||
def test_ner_pii_masking_node(): | ||
result_df = ner_pii_masking(project_dir=project_dir, previous_result=previous_result) | ||
base_passage_filter_node_test(result_df) |
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.
Actually, this is useless async operation.
I'll make a PR for resolving this kind of issue in the model rerankers...