### Install ai4privacy Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/ai4privacy/README.md Use pip to install the ai4privacy package. ```bash pip install ai4privacy ``` -------------------------------- ### Retrieve Detailed Replacement Information Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/ai4privacy/README.md Set verbose=True to receive a dictionary containing replacement details for the detected PII. ```python from ai4privacy import protect text = "Senden Sie es an Eva Schmidt." details = protect(text, classify_pii=True, verbose=True) print(details['replacements']) # Expected Output: [{'label': 'GIVENNAME', 'start': 18, 'end': 22, ...}, {'label': 'SURNAME', 'start': 23, 'end': 30, ...}] ``` -------------------------------- ### Batch Process and Reidentify PII Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Use batch_protect to process multiple strings simultaneously and batch_reidentify to restore the original text from the results. ```python from ai4privacy import batch_protect, batch_reidentify texts = [ "Email: alice@example.com", "Call me at +41794567890", "My name is Pierre Dupont" ] # Mask all texts results = batch_protect(texts, verbose=True, classify_pii=True) for r in results: print(r["masked_text"]) # Restore all texts restored = batch_reidentify(results) print(restored) ``` -------------------------------- ### Switch Models and Classify PII Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/ai4privacy/README.md Use the multilingual flag for non-English text and classify_pii with verbose mode to retrieve categorical labels. ```python from ai4privacy import protect text = "Je m'appelle Pierre et j'habite à Paris." # Use the multilingual model for non-English text masked_multilingual = protect(text, multilingual=True) print(f"Multilingual: {masked_multilingual}") # Expected Output: Multilingual: Je m'appelle [PII_1] et j'habite à [PII_2] # Use the categorical model to see the PII types details = protect(text, classify_pii=True, verbose=True) print(f"Categorical Labels: {[r['label'] for r in details['replacements']]}") # Expected Output: Categorical Labels: ['GIVENNAME', 'CITY'] ``` -------------------------------- ### observe(texts, score_threshold=0.01, batch_size=32, multilingual=False, classify_pii=False, developer_verbose=False) Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Analyzes text(s) for PII without modification. Returns statistics and a privacy mask. ```APIDOC ## observe(texts, score_threshold=0.01, batch_size=32, multilingual=False, classify_pii=False, developer_verbose=False) ### Description Analyze text(s) for PII without modification. Returns statistics and a privacy mask. ### Parameters - **texts** (str/list) - Required - The text or list of texts to analyze. - **score_threshold** (float) - Optional - Threshold for PII detection. - **batch_size** (int) - Optional - Number of texts to process in a batch. - **multilingual** (bool) - Optional - Enable multilingual support. - **classify_pii** (bool) - Optional - Enable PII classification. - **developer_verbose** (bool) - Optional - Enable developer-level logging. ``` -------------------------------- ### Protect PII with protect() Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/ai4privacy/README.md The protect function masks PII in a string with placeholders. ```python from ai4privacy import protect text = "Email me at developers@ai4privacy.com or call me at +41763223001." masked_text = protect(text) print(masked_text) # Expected Output: Email me at [PII_1] or call me at [PII_2] ``` -------------------------------- ### Analyze Text with observe() Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/ai4privacy/README.md The observe function returns statistics and a detailed privacy mask without altering the input text. ```python from ai4privacy import observe import json text = "My name is Alice and I live in Berlin." report = observe(text, classify_pii=True) print(json.dumps(report, indent=2)) ``` ```json { "num_texts_processed": 1, "num_texts_with_pii": 1, "pii_entity_counts": { "GIVENNAME": 1, "CITY": 1 }, "total_pii_entities_found": 2, "privacy_mask": [ { "label": "GIVENNAME", "start": 11, "end": 16, "activation": 0.98, "value": "Alice" }, { "label": "CITY", "start": 30, "end": 36, "activation": 0.99, "value": "Berlin" } ] } ``` -------------------------------- ### batch_protect(texts, verbose=False, score_threshold=0.01, batch_size=32, multilingual=False, classify_pii=False, developer_verbose=False) Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Masks PII in a list of texts. Returns a list of results. ```APIDOC ## batch_protect(texts, verbose=False, score_threshold=0.01, batch_size=32, multilingual=False, classify_pii=False, developer_verbose=False) ### Description Masks PII in a list of texts. Returns a list of results. ### Parameters - **texts** (list) - Required - A list of text strings to process. - **verbose** (bool) - Optional - If True, returns a list of dictionaries with details. - **score_threshold** (float) - Optional - Threshold for PII detection. - **batch_size** (int) - Optional - Number of texts to process in a batch. - **multilingual** (bool) - Optional - Enable multilingual support. - **classify_pii** (bool) - Optional - Enable PII classification. - **developer_verbose** (bool) - Optional - Enable developer-level logging. ``` -------------------------------- ### Anonymize Text with Multilingual and Categorical Models Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Demonstrates how to use the protect function with multilingual and categorical flags to detect and classify PII in non-English text. ```python from ai4privacy import protect text = "Je m'appelle Pierre et j'habite a Paris." # Multilingual model for non-English text masked = protect(text, multilingual=True) print(masked) # Output: Je m'appelle [PII_1] et j'habite a [PII_2] # Categorical model to see PII types details = protect(text, classify_pii=True, verbose=True) print([r['label'] for r in details['replacements']]) # Output: ['GIVENNAME', 'CITY'] ``` -------------------------------- ### protect(text, multilingual=False, classify_pii=False, verbose=False, developer_verbose=False, score_threshold=0.01) Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Masks PII in a single string of text. Supports multilingual models, categorical PII classification, and configurable sensitivity thresholds. ```APIDOC ## protect(text, ...) ### Description Masks PII in a single string of text. Returns the masked string or a dictionary of details if verbose is enabled. ### Parameters - **text** (str) - Required - The input text to process. - **multilingual** (bool) - Optional - Use the multilingual model for non-English text. - **classify_pii** (bool) - Optional - Use the categorical model to identify specific PII types. - **verbose** (bool) - Optional - Returns a dictionary with original text, masked text, and replacement details. - **developer_verbose** (bool) - Optional - Adds token-by-token breakdown of model predictions. - **score_threshold** (float) - Optional - Confidence threshold for flagging PII (default: 0.01). ``` -------------------------------- ### batch_reidentify(results) Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Restores a list of masked results. Each item must have masked_text and replacements keys. ```APIDOC ## batch_reidentify(results) ### Description Restore a list of masked results. Each item must have `masked_text` and `replacements` keys. ### Parameters - **results** (list) - Required - A list of dictionaries containing masked_text and replacements. ``` -------------------------------- ### batch_protect(texts, ...) Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Processes a list of texts efficiently to mask PII. ```APIDOC ## batch_protect(texts, ...) ### Description Processes multiple texts efficiently to mask PII. ### Parameters - **texts** (list) - Required - A list of strings to process. - **verbose** (bool) - Optional - Returns detailed replacement information for each text. - **classify_pii** (bool) - Optional - Use the categorical model for PII classification. ``` -------------------------------- ### Adjust PII Detection Sensitivity Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Configure the score_threshold parameter to balance between high precision and high sensitivity when flagging PII tokens. ```python from ai4privacy import protect text = "Maybe this is a name, maybe not. Contact John." # High precision masked = protect(text, score_threshold=0.5) print(f"High Precision: {masked}") # Output: High Precision: Maybe this is a name, maybe not. Contact [PII_1] # High sensitivity masked = protect(text, score_threshold=0.001) print(f"High Sensitivity: {masked}") ``` -------------------------------- ### protect(text, verbose=False, score_threshold=0.01, multilingual=False, classify_pii=False, developer_verbose=False) Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Masks PII in a single text string. Returns a masked string by default, or a dictionary with details when verbose is set to True. ```APIDOC ## protect(text, verbose=False, score_threshold=0.01, multilingual=False, classify_pii=False, developer_verbose=False) ### Description Masks PII in a single text string. Returns a masked string by default, or a dictionary with details when `verbose=True`. ### Parameters - **text** (str) - Required - The input text to process. - **verbose** (bool) - Optional - If True, returns a dictionary with details. - **score_threshold** (float) - Optional - Threshold for PII detection. - **multilingual** (bool) - Optional - Enable multilingual support. - **classify_pii** (bool) - Optional - Enable PII classification. - **developer_verbose** (bool) - Optional - Enable developer-level logging. ``` -------------------------------- ### Adjust Sensitivity with score_threshold Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/ai4privacy/README.md Control the model's confidence threshold to balance precision and sensitivity. ```python from ai4privacy import protect text = "Maybe this is a name, maybe not. Contact John." # High precision (less likely to flag "Maybe") masked_high_prec = protect(text, score_threshold=0.5) print(f"High Precision: {masked_high_prec}") # Expected Output: High Precision: Maybe this is a name, maybe not. Contact [PII_1] # High sensitivity (more likely to flag "Maybe" if the model is unsure) masked_high_sens = protect(text, score_threshold=0.001) print(f"High Sensitivity: {masked_high_sens}") ``` -------------------------------- ### Reidentify masked text Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Use reidentify() to reverse the masking process by providing the masked text and the replacement map generated during the protection step. ```python from ai4privacy import protect, reidentify # Step 1: Mask text (with verbose=True to get the replacement map) result = protect("My email is alice@example.com", verbose=True) print(result["masked_text"]) # Output: My email is [PII_1] # Step 2: Restore original text restored = reidentify(result["masked_text"], result["replacements"]) print(restored) # Output: My email is alice@example.com ``` -------------------------------- ### batch_reidentify(results) Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Restores original text from masked results. ```APIDOC ## batch_reidentify(results) ### Description Restores original text from the results generated by batch_protect. ### Parameters - **results** (list) - Required - The output list from batch_protect. ``` -------------------------------- ### reidentify(masked_text, replacements) Source: https://github.com/ai4privacy/ai4privacy-python-module/blob/main/README.md Restores a masked text to its original form using the replacements list from protect(verbose=True). ```APIDOC ## reidentify(masked_text, replacements) ### Description Restore a masked text to its original form using the replacements list from `protect(verbose=True)`. ### Parameters - **masked_text** (str) - Required - The masked text string. - **replacements** (list) - Required - The list of replacements obtained from the protect method. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.