Tutorial

How to Redact Documents (PDF, JPG, PNG) using Python

Learn how to implement document redaction in Python using Eden AI's OCR anonymization API. Launch an async redaction job and retrieve results using two simple API calls to protect sensitive data.

How to Redact Documents (PDF, JPG, PNG) using Python

Document redaction, or anonymization, is crucial in industries like healthcare, law, and finance where sensitive data must be protected.

With Eden AI’s asynchronous OCR anonymization API, you can automate this process using Python in just a few steps.

This tutorial walks you through how to redact documents using Eden AI’s API via Python, using a two-step asynchronous process.

What is Document Redaction?

Document redaction, also known as anonymization, is the process of hiding or removing sensitive information, such as names, dates, or identification numbers from documents.

This helps protect privacy and ensures compliance with data protection regulations by making confidential data unreadable or invisible.

How to Redact Documents

Set Up Your Eden AI Account

1. Create an account: If you don't have an Eden AI account, create one for free using the following link. Then you can obtain your API key in the API Keys section, which you can use to access various AI services.

2. Access OCR Tools: Once logged in, go to the document parsing section of the platform.

3. Choose the Document Redaction Feature: Select the Document Redaction tool. You can also explore other OCR tools.

Implementing Document Redaction in Python

The process is asynchronous to allow for better performance and scalability. Redacting a document can take time depending on file size and complexity, so:

  • The POST request initiates the job and returns immediately.
  • The GET request lets you check when the job is complete and fetch the result.

This two-step system is ideal for batch processing or workflows where real-time response isn’t required.

Step 1: Install the Python Requests Module

If you haven’t already, install the requests module which allows Python to send HTTP requests:


pip install requests

Step 2: Launch a Redaction Job (POST Request)

The first step is to initiate the redaction process by launching a job. You’ll need to send the image URL to the API for processing.

Code Example:


import requests

headers = {"Authorization": "Bearer {API_TOKEN}"}
url = "https://api.edenai.run/v2/ocr/anonymization_async"
json_payload = {
    "providers": "readyredact",
    "file_url": "🔗 URL of your image"
}

response = requests.post(url, json=json_payload, headers=headers)

result = response.json()
print(result)

Code Explanation:

  • headers: You must include your Bearer API token for authentication.
  • file_url: This should be a direct link to your document or image file (PDF, JPG, etc.).
  • POST to /anonymization_async: This tells Eden AI to start the redaction job asynchronously.

Output Example:


{
  "public_id": "1234567890abcdef",
  "status": "started"
}

  • public_id: This is your job ID, used to check the result later.
  • status: Indicates that the redaction job has been successfully initiated.
  • Step 3: Get the Redaction Results (GET Request)

    Once the job is launched, you need to fetch the results using the public_id.

    Code Example:

    
    import requests
    
    headers = {
        "Authorization": "Bearer {API_TOKEN}"
    }
    url = f'https://api.edenai.run/v2/ocr/anonymization_async/{public_id}/'
    
    response = requests.get(url, headers=headers)
    
    result = response.json()
    print(result)
    
    

    Code Explanation:

    • Replace {public_id} with the ID received in the POST request.
    • This GET request queries the API to retrieve the anonymized result once the job is complete.

    Output Example:

    
    {
      "status": "finished",
      "results": {
        "readyredact": {
          "status": "success",
          "redacted_document_url": "https://url_to_redacted_file.pdf"
        }
      }
    }
    
    
    • status: Shows the job has finished.
    • redacted_document_url: A direct link to the redacted version of the document.

    Managing Redaction Jobs

    In addition to launching and retrieving results from redaction jobs, Eden AI provides optional endpoints to help you manage your anonymization tasks more efficiently:

    1. List All Redaction Jobs
      Endpoint: GET https://api.edenai.run/v2/ocr/anonymization_async/
      Use this to retrieve a list of all launched redaction jobs. It’s helpful for tracking job statuses or fetching results later using their job IDs.
      Note: A job's status only updates after a GET request is made. You can check the documentation here.

    2. Delete Redaction Jobs
      Endpoint: DELETE https://api.edenai.run/v2/ocr/anonymization_async/
      This allows you to delete redaction jobs from your account once you’ve retrieved the results, helping you keep your job history clean and organized.
      You can check the documentation here.

    Why Eden AI is the Best Tool for Document Redaction

    Eden AI provides several advantages.

    Multiple AI Providers

    You can choose between different AI services and providers, helping you compare results for the best performance.

    Easy Integration

    Streamline development with one API key that gives access to multiple AI service. Skip the complexity of separate integrations and launch faster.

    Cost Efficiency

    It’s a pay-as-you-go model—pay only for what you use. There are no upfront costs, offering flexible access to various AI services with just one API key.

    Conclusion

    With just a few lines of code, you can automate document redaction using Python and Eden AI.

    The two-step process ensures accuracy and flexibility, especially for larger workloads.

    Whether you’re processing legal files, patient records, or financial data, Eden AI provides a scalable and secure solution for anonymization.

    Start redacting smarter, not harder, with Eden AI.

    You can access the full documentation for Document Redaction here.

    Start Your AI Journey Today

    • Access 100+ AI APIs in a single platform.
    • Compare and deploy AI models effortlessly.
    • Pay-as-you-go with no upfront fees.
    Start building FREE

    Related Posts

    Try Eden AI for free.

    You can directly start building now. If you have any questions, feel free to chat with us!

    Get startedContact sales
    X

    Start Your AI Journey Today

    Sign up now with free credits to explore 100+ AI APIs.
    Get my FREE credits now