### Installation Source: https://microsoft.github.io/presidio/tutorial/00_getting_started Install Presidio Analyzer and Anonymizer, and download the English language model for spaCy. ```shell pip install presidio_analyzer pip install presidio_anonymizer python -m spacy download en_core_web_lg ``` -------------------------------- ### Start a Specific Service with Docker Compose Source: https://microsoft.github.io/presidio/development Example of starting only the analyzer service using Docker Compose. ```shell docker-compose up --build -d presidio-analyzer ``` -------------------------------- ### Simple flow Source: https://microsoft.github.io/presidio/tutorial/00_getting_started A basic example of using Presidio Analyzer to find PII in a text. ```python from presidio_analyzer import AnalyzerEngine text = "His name is Mr. Jones and his phone number is 212-555-5555" analyzer = AnalyzerEngine() analyzer_results = analyzer.analyze(text=text, language="en") print(analyzer_results) ``` -------------------------------- ### Install client requirements Source: https://microsoft.github.io/presidio/samples/deployments/openai-anonymaztion-and-deanonymaztion-best-practices/src/client_app Install the necessary Python dependencies for the client application. ```bash cd src/client_app pip install -r requirements.txt ``` -------------------------------- ### Install dependencies Source: https://microsoft.github.io/presidio/samples/python/example_dicom_image_redactor Install the required Presidio packages and the spaCy language model. ```bash !pip install presidio_analyzer presidio_anonymizer presidio_image_redactor !python -m spacy download en_core_web_lg ``` -------------------------------- ### Start Streamlit App Source: https://microsoft.github.io/presidio/samples/python/streamlit Launches the Presidio demo website using the Streamlit framework. Ensure all dependencies are installed before running this command. ```bash streamlit run presidio_streamlit.py ``` -------------------------------- ### Install Poetry using Homebrew Source: https://microsoft.github.io/presidio/development Instructions for installing Poetry on macOS using Homebrew. ```shell brew install poetry ``` -------------------------------- ### AnalyzerEngine initialization example Source: https://microsoft.github.io/presidio/api/analyzer_python Example of how to initialize the AnalyzerEngine. ```python from presidio_analyzer import AnalyzerEngine # Set up the engine, loads the NLP module (spaCy model by default) # and other PII recognizers analyzer = AnalyzerEngine() ``` -------------------------------- ### Pre-commit hook execution example Source: https://microsoft.github.io/presidio/development Example output showing ruff and ruff-format hooks running during a git commit. ```text [INFO] Initializing environment for https://github.com/astral-sh/ruff-pre-commit. [INFO] Installing environment for https://github.com/astral-sh/ruff-pre-commit. [INFO] Once installed this environment will be reused. [INFO] This may take a few minutes... ruff.....................................................................Passed ruff-format..............................................................Failed - hook id: ruff-format - files were modified by this hook 5 files reformatted, 4 files left unchanged ``` -------------------------------- ### Install Presidio with GLiNER support Source: https://microsoft.github.io/presidio/samples/python/gliner Install the required dependencies using pip. ```bash pip install 'presidio-analyzer[gliner]' ``` -------------------------------- ### Install Poetry using Pip Source: https://microsoft.github.io/presidio/development Instructions for installing Poetry, a Python package manager, using Pip. ```shell pip install poetry ``` -------------------------------- ### Install presidio-structured Source: https://microsoft.github.io/presidio/structured To install the `presidio-structured` package, run the following command: ```bash pip install presidio-structured ``` -------------------------------- ### Install Presidio Dependencies Source: https://microsoft.github.io/presidio/samples/python/encrypt_decrypt Install the necessary Python packages and download the Spacy language model. ```bash !pip install presidio_analyzer presidio_anonymizer !python -m spacy download en_core_web_lg ``` -------------------------------- ### Install required dependencies Source: https://microsoft.github.io/presidio/samples/python/example_pdf_annotation Install the necessary libraries for Presidio, PDF processing, and language models. ```bash !pip install presidio_analyzer !pip install presidio_anonymizer !python -m spacy download en_core_web_lg !pip install pdfminer.six !pip install pikepdf ``` -------------------------------- ### Install Presidio with LangExtract support Source: https://microsoft.github.io/presidio/samples/python/langextract Install the necessary dependencies to enable language model-based detection. ```bash pip install presidio-analyzer[langextract] ``` -------------------------------- ### Install Presidio and Dependencies Source: https://microsoft.github.io/presidio/samples/python/synth_data_with_openai Installs the necessary Presidio libraries, OpenAI client, pandas, and downloads the English language model for spaCy. ```bash # download presidio !pip install presidio_analyzer presidio_anonymizer !pip install openai pandas !python -m spacy download en_core_web_lg ``` -------------------------------- ### Install Python Dependencies Source: https://microsoft.github.io/presidio/samples/deployments/openai-anonymaztion-and-deanonymaztion-best-practices/src/api Navigate to the API source directory and install required Python packages using pip. ```bash cd src/api pip install -r requirements.txt ``` -------------------------------- ### Install Presidio and spaCy Source: https://microsoft.github.io/presidio/samples/python/Anonymizing%20known%20values Installs the necessary Presidio libraries and downloads the English language model for spaCy. ```bash # download presidio !pip install presidio_analyzer presidio_anonymizer !python -m spacy download en_core_web_lg ``` -------------------------------- ### Install Presidio and Download spaCy Model Source: https://microsoft.github.io/presidio/samples/python/getting_entity_values Installs the necessary Presidio libraries and downloads the English language model for spaCy. ```bash # download presidio !pip install presidio_analyzer presidio_anonymizer !python -m spacy download en_core_web_lg ``` -------------------------------- ### Install Dependencies Source: https://microsoft.github.io/presidio/samples/python/streamlit Installs the necessary Python packages for the Presidio demo website. It's recommended to do this within a virtual environment. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install Presidio and Dependencies Source: https://microsoft.github.io/presidio/samples/python/batch_processing Installs the necessary Presidio libraries and the spaCy language model. Use this to set up your environment before running Presidio. ```python # download presidio #!pip install presidio_analyzer presidio_anonymizer #!python -m spacy download en_core_web_lg #!pip install pandas ``` -------------------------------- ### Installing pre-commit git hooks Source: https://microsoft.github.io/presidio/development Command to install pre-commit hooks for automatic code formatting and linting. ```bash pre-commit install ``` -------------------------------- ### Initialize environment configuration Source: https://microsoft.github.io/presidio/samples/deployments/openai-anonymaztion-and-deanonymaztion-best-practices/src/client_app Create the .env file from the provided sample template. ```bash cp .env.sample .env ``` -------------------------------- ### Set Up Environment Variables Source: https://microsoft.github.io/presidio/samples/deployments/openai-anonymaztion-and-deanonymaztion-best-practices/src/api Copy the sample environment file and edit it to configure your local setup. ```bash cp .env.sample .env ``` -------------------------------- ### Run web interface Source: https://microsoft.github.io/presidio/samples/deployments/openai-anonymaztion-and-deanonymaztion-best-practices/src/client_app Start the web server for the chat interface. ```bash python serve.py ``` -------------------------------- ### PhoneRecognizer Usage Example Source: https://microsoft.github.io/presidio/api/analyzer_python This snippet shows how to use the PhoneRecognizer to analyze text and get results. ```python from presidio_analyzer import AnalyzerEngine, RecognizerResult from presidio_analyzer.recognizer import PhoneRecognizer # Initialize the analyzer engine analyzer = AnalyzerEngine() # Example text containing a phone number text = "My phone number is 123-456-7890." # Analyze the text results = analyzer.analyze(text=text, language='en') # Print the results for result in results: print(result) ``` -------------------------------- ### Initialize AnalyzerEngine with multiple configuration files Source: https://microsoft.github.io/presidio/analyzer/analyzer_engine_provider Split configuration into separate files for the analyzer, NLP engine, and recognizer registry. ```python from presidio_analyzer import AnalyzerEngine, AnalyzerEngineProvider analyzer_conf_file = "./analyzer/analyzer-config.yml" nlp_engine_conf_file = "./analyzer/nlp-config.yml" recognizer_registry_conf_file = "./analyzer/recognizers-config.yml" provider = AnalyzerEngineProvider( analyzer_engine_conf_file=analyzer_conf_file, nlp_engine_conf_file=nlp_engine_conf_file, recognizer_registry_conf_file=recognizer_registry_conf_file, ) analyzer = provider.create_engine() results = analyzer.analyze(text="My name is Morris", language="en") print(results) ``` -------------------------------- ### Basic Presidio Analyzer and Anonymizer Configuration Source: https://microsoft.github.io/presidio/recipes/template This snippet shows the basic setup for using Presidio's AnalyzerEngine and AnonymizerEngine to detect and anonymize PII in text. Ensure you have the necessary Presidio libraries installed. ```python from presidio_analyzer import AnalyzerEngine from presidio_anonymizer import AnonymizerEngine # Your Presidio configuration analyzer = AnalyzerEngine() anonymizer = AnonymizerEngine() # Analyze and anonymize results = analyzer.analyze(text=sample_text, language="en") anonymized = anonymizer.anonymize(text=sample_text, analyzer_results=results) print(anonymized.text) ``` -------------------------------- ### Install spaCy CUDA Dependencies Source: https://microsoft.github.io/presidio/analyzer/nlp_engines/gpu_usage Install the specific spaCy package variant that matches your installed CUDA toolkit version. ```bash pip install "spacy[cuda12x]" # For CUDA 12.x # or pip install "spacy[cuda11x]" # For CUDA 11.x ``` -------------------------------- ### Initialize AnalyzerEngine with a single configuration file Source: https://microsoft.github.io/presidio/analyzer/analyzer_engine_provider Use the AnalyzerEngineProvider to load all settings from a single YAML file. ```python from presidio_analyzer import AnalyzerEngine, AnalyzerEngineProvider analyzer_conf_file = "./analyzer/analyzer-config-all.yml" provider = AnalyzerEngineProvider( analyzer_engine_conf_file=analyzer_conf_file ) analyzer = provider.create_engine() results = analyzer.analyze(text="My name is Morris", language="en") print(results) ``` -------------------------------- ### Run Arbitrary Scripts with Poetry Source: https://microsoft.github.io/presidio/development Examples of running arbitrary scripts within the virtual environment managed by Poetry. ```shell poetry run ruff check ``` ```shell poetry run pip freeze ``` ```shell poetry run python -m spacy download en_core_web_lg ``` -------------------------------- ### Initialize AnalyzerEngine with default configuration Source: https://microsoft.github.io/presidio/analyzer/analyzer_engine_provider Create an engine instance without providing specific configuration files to use defaults. ```python from presidio_analyzer import AnalyzerEngine, AnalyzerEngineProvider provider = AnalyzerEngineProvider().create_engine() results = provider.analyze(text="My name is Morris", language="en") print(results) ``` -------------------------------- ### Install AHDS Recognizer Source: https://microsoft.github.io/presidio/ahds_integration Install the required package for AHDS entity recognition. ```bash pip install presidio-analyzer[ahds] ``` -------------------------------- ### Initialize and Run FlairRecognizer Source: https://microsoft.github.io/presidio/samples/python/flair_recognizer Demonstrates how to instantiate the FlairRecognizer, add it to the registry, and analyze text. Note that the model download is large (over 2GB) on the first execution. ```python if __name__ == "__main__": from presidio_analyzer import AnalyzerEngine, RecognizerRegistry flair_recognizer = ( FlairRecognizer() ) # This would download a very large (+2GB) model on the first run registry = RecognizerRegistry() registry.add_recognizer(flair_recognizer) analyzer = AnalyzerEngine(registry=registry) results = analyzer.analyze( "My name is Christopher and I live in Irbid.", language="en", return_decision_process=True, ) for result in results: print(result) print(result.analysis_explanation) ``` ```python if __name__ == "__main__": from presidio_analyzer import AnalyzerEngine, RecognizerRegistry flair_recognizer = ( FlairRecognizer() ) # This would download a very large (+2GB) model on the first run registry = RecognizerRegistry() registry.add_recognizer(flair_recognizer) analyzer = AnalyzerEngine(registry=registry) results = analyzer.analyze( "My name is Christopher and I live in Irbid.", language="en", return_decision_process=True, ) for result in results: print(result) print(result.analysis_explanation) ``` -------------------------------- ### Example configuration file structure Source: https://microsoft.github.io/presidio/analyzer/analyzer_engine_provider A sample YAML configuration file defining supported languages, NLP settings, and recognizer registry. ```yaml supported_languages: - en default_score_threshold: 0 nlp_configuration: nlp_engine_name: spacy models: - lang_code: en model_name: en_core_web_lg - lang_code: es model_name: es_core_news_md ner_model_configuration: model_to_presidio_entity_mapping: PER: PERSON PERSON: PERSON LOC: LOCATION LOCATION: LOCATION GPE: LOCATION ORG: ORGANIZATION DATE: DATE_TIME TIME: DATE_TIME NORP: NRP low_confidence_score_multiplier: 0.4 low_score_entity_names: - ORGANIZATION - ORG default_score: 0.85 recognizer_registry: global_regex_flags: 26 recognizers: - name: CreditCardRecognizer supported_languages: - en supported_entity: IT_FISCAL_CODE type: predefined - name: ItFiscalCodeRecognizer type: predefined ``` -------------------------------- ### Deanonymize Payload Example Source: https://microsoft.github.io/presidio/anonymizer Example JSON payload for the deanonymize HTTP endpoint. ```json { "text": "My name is S184CMt9Drj7QaKQ21JTrpYzghnboTF9pn/neN8JME0=", "deanonymizers": { "PERSON": { "type": "decrypt", "key": "WmZq4t7w!z%C&F)J" } }, "anonymizer_results": [ { "start": 11, "end": 55, "entity_type": "PERSON" } ]} ``` -------------------------------- ### Running e2e tests on Mac/Linux/WSL Source: https://microsoft.github.io/presidio/development Steps to set up and run end-to-end tests using pytest on Unix-like systems. ```bash # Create a virtualenv named presidio-e2e (needs to be done only on the first run) python -m venv presidio-e2e # Activate the virtualenv source presidio-e2e/bin/activate # Install e2e-tests requirements using pip pip install -r requirements.txt # Run pytest pytest # Deactivate the virtualenv deactivate ``` -------------------------------- ### Anonymize Payload Example Source: https://microsoft.github.io/presidio/anonymizer Example JSON payload for the anonymize HTTP endpoint. ```json { "text": "hello world, my name is Jane Doe. My number is: 034453334", "anonymizers": { "PHONE_NUMBER": { "type": "mask", "masking_char": "*", "chars_to_mask": 4, "from_end": true } }, "analyzer_results": [ { "start": 24, "end": 32, "score": 0.8, "entity_type": "NAME" }, { "start": 24, "end": 28, "score": 0.8, "entity_type": "FIRST_NAME" }, { "start": 29, "end": 32, "score": 0.6, "entity_type": "LAST_NAME" }, { "start": 48, "end": 57, "score": 0.95, "entity_type": "PHONE_NUMBER" } ]} ``` -------------------------------- ### Install Presidio Packages Source: https://microsoft.github.io/presidio/samples/python/ner_model_configuration Use pip to install the necessary analyzer and anonymizer packages. ```bash # download presidio !pip install presidio_analyzer presidio_anonymizer ``` -------------------------------- ### Install Presidio AHDS Package Source: https://microsoft.github.io/presidio/samples/python/ahds Install the required Presidio package with AHDS support. ```bash pip install "presidio-analyzer[ahds]" ``` -------------------------------- ### Install Presidio Image Redactor Source: https://microsoft.github.io/presidio/image-redactor Install the package and the required spaCy language model. ```bash pip install presidio-image-redactor python -m spacy download en_core_web_lg ``` -------------------------------- ### Checking for linting issues locally Source: https://microsoft.github.io/presidio/development Command to check for PEP8 compliance and style guide issues using ruff. ```bash poetry run ruff check ``` -------------------------------- ### Set up Ollama via Docker Source: https://microsoft.github.io/presidio/samples/python/langextract Commands to start the Ollama service and pull a model using Docker Compose. ```bash docker compose up -d ollama docker exec presidio-ollama-1 ollama pull qwen2.5:1.5b docker exec presidio-ollama-1 ollama list ``` -------------------------------- ### Install Presidio Image Redactor Source: https://microsoft.github.io/presidio/getting_started/getting_started_images Install the required Python package for image redaction. ```bash pip install presidio-image-redactor ``` -------------------------------- ### Verify CUDA Installation Source: https://microsoft.github.io/presidio/analyzer/nlp_engines/gpu_usage Check if the NVIDIA driver and compiler are correctly installed on the system. ```bash nvidia-smi nvcc --version ``` -------------------------------- ### Install AHDS Surrogate Operator Source: https://microsoft.github.io/presidio/ahds_integration Install the required package for AHDS surrogate generation. ```bash pip install presidio-anonymizer[ahds] ``` -------------------------------- ### Set up Ollama natively Source: https://microsoft.github.io/presidio/samples/python/langextract Commands to pull and run a model directly on the host system. ```bash ollama pull qwen2.5:1.5b ollama run qwen2.5:1.5b ``` -------------------------------- ### RecognizerRegistryProvider Example Source: https://microsoft.github.io/presidio/api/analyzer_python Example of a recognizer registry configuration. ```json { "supported_languages": ["de", "es"], "recognizers": [ { "name": "Zip code Recognizer", "supported_language": "en", "patterns": [ { "name": "zip code (weak)", "regex": "(\b\d{5}(?:\\-\d{4})?\b)", "score": 0.01 } ], "context": ["zip", "code"], "supported_entity": "ZIP" } ] } ``` -------------------------------- ### Load and Display Standard Example Image Source: https://microsoft.github.io/presidio/samples/python/image_redaction_allow_list_approach Loads a standard image file and displays it. ```python image = Image.open("../../image-redactor/ocr_text.png") display(image) ``` -------------------------------- ### Configure NLP engine via file Source: https://microsoft.github.io/presidio/analyzer/customizing_nlp_models Pass the path of a configuration file to the NlpEngineProvider to initialize the engine. ```python from presidio_analyzer import AnalyzerEngine, RecognizerRegistry from presidio_analyzer.nlp_engine import NlpEngineProvider LANGUAGES_CONFIG_FILE = "./docs/analyzer/languages-config.yml" # Create NLP engine based on configuration file provider = NlpEngineProvider(conf_file=LANGUAGES_CONFIG_FILE) nlp_engine_with_spanish = provider.create_engine() # Pass created NLP engine and supported_languages to the AnalyzerEngine analyzer = AnalyzerEngine( nlp_engine=nlp_engine_with_spanish, supported_languages=["en", "es"] ) # Analyze in different languages results_spanish = analyzer.analyze(text="Mi nombre es David", language="es") print(results_spanish) results_english = analyzer.analyze(text="My name is David", language="en") print(results_english) ``` -------------------------------- ### Install Project Dependencies with Poetry Source: https://microsoft.github.io/presidio/development Command to create a virtual environment for a Presidio service and install all requirements, including development dependencies, using Poetry. ```shell poetry install --all-extras ``` -------------------------------- ### Display template samples Source: https://microsoft.github.io/presidio/samples/python/synth_data_with_openai Prints the first five templates from the loaded list. ```python print("Example templates:") templates[:5] ``` -------------------------------- ### Reinstall CuPy for CUDA Source: https://microsoft.github.io/presidio/analyzer/nlp_engines/gpu_usage Update the CuPy installation to match the specific CUDA version installed on the host. ```bash pip uninstall cupy cupy-cuda11x cupy-cuda12x pip install cupy-cuda12x # Match your CUDA version ``` -------------------------------- ### Initialize and use FlairRecognizer Source: https://microsoft.github.io/presidio/samples/python/flair_recognizer Example showing how to register the FlairRecognizer with the AnalyzerEngine and perform text analysis. ```python from presidio_analyzer import AnalyzerEngine, RecognizerRegistry flair_recognizer = FlairRecognizer() registry = RecognizerRegistry() registry.add_recognizer(flair_recognizer) analyzer = AnalyzerEngine(registry=registry) results = analyzer.analyze( "My name is Christopher and I live in Irbid.", language="en", return_decision_process=True, ) for result in results: print(result) print(result.analysis_explanation) ``` -------------------------------- ### Configure Demo Parameters Source: https://microsoft.github.io/presidio/samples/fabric/artifacts/presidio_and_spark Define configuration variables for data scaling, file paths, and Delta table output settings. ```python num_duplicates = 5000 # for the scale part csv_path = "Files/presidio/fabric_sample_data.csv" is_write_to_delta = True table_namne = "presidio_demo_table" partitions_number = 100 ``` -------------------------------- ### SpacyRecognizer Example Source: https://microsoft.github.io/presidio/api/analyzer_python Example of how SpacyRecognizer processes entities and builds results. ```python logger.debug( f"Skipping entity {ner_entity.label_} " f"as it is not in the supported entities list" ) continue textual_explanation = self.DEFAULT_EXPLANATION.format(ner_entity.label_) explanation = self.build_explanation(ner_score, textual_explanation) spacy_result = RecognizerResult( entity_type=ner_entity.label_, start=ner_entity.start_char, end=ner_entity.end_char, score=ner_score, analysis_explanation=explanation, recognition_metadata={ RecognizerResult.RECOGNIZER_NAME_KEY: self.name, RecognizerResult.RECOGNIZER_IDENTIFIER_KEY: self.id, }, ) results.append(spacy_result) return results ```