File size: 3,668 Bytes
90730f5 4945c56 90730f5 4945c56 90730f5 4945c56 90730f5 4945c56 90730f5 4945c56 90730f5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
"""Streamlit app for Presidio."""
import json
from json import JSONEncoder
import pandas as pd
import streamlit as st
from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine
from transformers_recognizer import TransformersRecognizer
import spacy
spacy.cli.download("en_core_web_lg")
# Helper methods
@st.cache(allow_output_mutation=True)
def analyzer_engine():
"""Return AnalyzerEngine."""
transformers_recognizer = TransformersRecognizer()
registry = RecognizerRegistry()
registry.add_recognizer(transformers_recognizer)
analyzer = AnalyzerEngine(registry=registry)
return analyzer
@return AnalyzerEngine()
@st.cache(allow_output_mutation=True)
def anonymizer_engine():
"""Return AnonymizerEngine."""
return AnonymizerEngine()
def get_supported_entities():
"""Return supported entities from the Analyzer Engine."""
return analyzer_engine().get_supported_entities()
def analyze(**kwargs):
"""Analyze input using Analyzer engine and input arguments (kwargs)."""
if "entities" not in kwargs or "All" in kwargs["entities"]:
kwargs["entities"] = None
return analyzer_engine().analyze(**kwargs)
def anonymize(text, analyze_results):
"""Anonymize identified input using Presidio Abonymizer."""
res = anonymizer_engine().anonymize(text, analyze_results)
return res.text
st.set_page_config(page_title="Presidio demo", layout="wide")
# Side bar
st.sidebar.markdown(
"""
Anonymize PII entities with [presidio](https://aka.ms/presidio).
"""
)
st_entities = st.sidebar.multiselect(
label="Which entities to look for?",
options=get_supported_entities(),
default=list(get_supported_entities()),
)
st_threhsold = st.sidebar.slider(
label="Acceptance threshold", min_value=0.0, max_value=1.0, value=0.35
)
st_return_decision_process = st.sidebar.checkbox("Add analysis explanations in json")
st.sidebar.info(
"Presidio is an open source framework for PII detection and anonymization. "
"For more info visit [aka.ms/presidio](https://aka.ms/presidio)"
)
# Main panel
analyzer_load_state = st.info("Starting Presidio analyzer...")
engine = analyzer_engine()
analyzer_load_state.empty()
# Create two columns for before and after
col1, col2 = st.columns(2)
# Before:
col1.subheader("Input string:")
st_text = col1.text_area(
label="Enter text",
value="Type in some text, "
"like a phone number (212-141-4544) "
"or a name (Lebron James).",
height=400,
)
# After
col2.subheader("Output:")
st_analyze_results = analyze(
text=st_text,
entities=st_entities,
language="en",
score_threshold=st_threhsold,
return_decision_process=st_return_decision_process,
)
st_anonymize_results = anonymize(st_text, st_analyze_results)
col2.text_area(label="", value=st_anonymize_results, height=400)
# table result
st.subheader("Findings")
if st_analyze_results:
df = pd.DataFrame.from_records([r.to_dict() for r in st_analyze_results])
df = df[["entity_type", "start", "end", "score"]].rename(
{
"entity_type": "Entity type",
"start": "Start",
"end": "End",
"score": "Confidence",
},
axis=1,
)
st.dataframe(df, width=1000)
else:
st.text("No findings")
# json result
class ToDictEncoder(JSONEncoder):
"""Encode dict to json."""
def default(self, o):
"""Encode to JSON using to_dict."""
return o.to_dict()
st.json(json.dumps(st_analyze_results, cls=ToDictEncoder))
import gradio as gr
from presidio_analyzer import AnalyzerEngine, RecognizerRegistry
|