Skip to main content

PHI Anonymizer

PHI (Protected Health Information) Anonymizer is a tool designed to help you anonymize sensitive information in your text data. It uses advanced natural language processing techniques to identify and redact sensitive information such as names, addresses, and medical terms.

Disclaimer

This tool is best-effort and could still miss sensitive information, it also depends on which LLM is used on the quality of results. All feedback we receive will go towards improving it. Use at your own risk.

Usage

Install

pip install phianonymizer

Example usage

from phianonymizer import anonymize_text, deanonymize_text

original_text = "Hi! My name is Bobby Smith, I was born on 01/04/1970 in New York City. My phone number is 626-433-7890 and my email is bobby.smith@gmail.com. You probably need my social security number too, it's 213-45-6919. So what have you learned about me?"
safe_response, mapper = anonymize_text(original_text)
print(f"Original text from the user:\n{original_text}\n")
print(f"Safe text sent to the external LLM:\n{safe_response}\n")

# Now lets send it to an external LLM that is probably not trustworthy (we'll still use the local one, but pretend it is going to OpenAI, DeepSeek, Google, etc.)
response = mapper.llm.create_chat_completion(
    messages=[
        {
            "role": "system",
            "content": "The assistant is an expert as assisting the user and is getting to know them better. The assistant is very friendly and helpful.",
        },
        {
            "role": "user",
            "content": f"{safe_response}",
        },
    ],
    temperature=0.4,
    max_tokens=2048,
)

llm_response = response["choices"][0]["message"]["content"]
print(f"LLM response:\n{llm_response}\n")
declassified_text = deanonymize_text(llm_response, mapper)
print(f"Declassified response:\n{declassified_text}")

Replace the response section with using something like your OpenAI API or other LLM provider to anonymize the text before sending it to the LLM, and then deanonymize the response after receiving it.

Example output

Original text from the user:
Hi! My name is Bobby Smith, I was born on 01/04/1970 in New York City. My phone number is 626-433-7890 and my email is bobby.smith@gmail.com. You probably need my social security number too, it's 213-45-6919.

Safe text sent to the external LLM:
Hi! My name is John, I was born on 01/04/1970 in New York City. My phone number is 378-602-8003 and my email is bobby.smith@gmail.com. You probably need my social security number too, it's 123456789.

LLM response:
Hello John! Thank you for sharing your information. It's great that you've provided your contact details. If you have any questions or need assistance with something related to your information, feel free to ask. I'm here to help.

Declassified response:
Hello Bobby Smith! Thank you for sharing your information. It's great that you've provided your contact details. If you have any questions or need assistance with something related to your information, feel free to ask. I'm here to help.

Technical Details

We're using a combination of llama-cpp-python and a quantized version of the Qwen 0.5B model.

If you have the CUDA version of llama-cpp-python installed, this will run on GPU. Otherwise, it will run on CPU.

The Qwen 0.5B model runs on very little RAM and can be run on CPU very quickly still. Putting a more powerful model in would greatly improve the quality of the anonymization, but would also increase the cost and complexity of running this tool. To change which model is used, change the model_repo and model_filename parameters in the anonymize_text function. Here's an example of how to do that:

from phianonymizer import anonymize_text

anonymized_text, mapper = anonymize_text(
    text="My name is Bobby Smith, I was born on 01/04/1970 in New York City. My phone number is 626-433-7890 and my email is bobby@smith.org. You probably need my social security number too, it's 213-45-6919. What have you learned about me?",
    model_repo="bartowski/Qwen2.5-0.5B-Instruct-GGUF",
    model_filename="*Q4_K_M.gguf",
)

Contributing

We welcome contributions to this project! If you have an idea for a new feature or improvement, please open an issue or submit a pull request.

License

MIT License, see LICENSE for details.

Metadata

Release files for phianonymizer 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for phianonymizer 0.0.6
File Size Uploaded
phianonymizer-0.0.6.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for phianonymizer 0.0.6
File Interpreter ABI Platform
phianonymizer-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 16.6 kB

Release files / phianonymizer-0.0.6.tar.gz

Download URL phianonymizer-0.0.6.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
73b8fb396d16fe82072e3b4749ff8973827e942b4122012bed62822d4d0309e0
BLAKE2b-256 checksum
How to use checksums
d19ab7899d956ceb70565cf7148041e550fd1b4718a26e53f8ed92f5667d54f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.9

Release files / phianonymizer-0.0.6-py3-none-any.whl

Download URL phianonymizer-0.0.6-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d9f4486c56911a0547e9dbb91e004e8636555de8f63ef62c5362a5db4e7323b
BLAKE2b-256 checksum
How to use checksums
071c6f7da559b7f87525724744c221135ee033d8c8ca7b09de63d3009fa3e7a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page