Upload 10 files
Browse files- presidio_streamlit.py +35 -34
presidio_streamlit.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
"""Streamlit app for Presidio."""
|
2 |
import logging
|
3 |
import os
|
|
|
4 |
|
|
|
5 |
import pandas as pd
|
6 |
import streamlit as st
|
7 |
import streamlit.components.v1 as components
|
8 |
-
import dotenv
|
9 |
from annotated_text import annotated_text
|
10 |
from streamlit_tags import st_tags
|
11 |
|
@@ -17,7 +18,6 @@ from presidio_helpers import (
|
|
17 |
annotate,
|
18 |
create_fake_data,
|
19 |
analyzer_engine,
|
20 |
-
nlp_engine_and_registry,
|
21 |
)
|
22 |
|
23 |
st.set_page_config(
|
@@ -35,7 +35,6 @@ logger = logging.getLogger("presidio-streamlit")
|
|
35 |
|
36 |
allow_other_models = os.getenv("ALLOW_OTHER_MODELS", False)
|
37 |
|
38 |
-
can_present_results = True
|
39 |
|
40 |
# Sidebar
|
41 |
st.sidebar.header(
|
@@ -172,7 +171,7 @@ elif st_operator == "synthesize":
|
|
172 |
api_type=openai_api_type,
|
173 |
)
|
174 |
|
175 |
-
|
176 |
|
177 |
st_threshold = st.sidebar.slider(
|
178 |
label="Acceptance threshold",
|
@@ -241,27 +240,9 @@ with st.expander("About this demo", expanded=False):
|
|
241 |
)
|
242 |
|
243 |
analyzer_load_state = st.info("Starting Presidio analyzer...")
|
244 |
-
nlp_engine, registry = nlp_engine_and_registry(*analyzer_params)
|
245 |
|
246 |
analyzer_load_state.empty()
|
247 |
|
248 |
-
# Choose entities
|
249 |
-
st_entities_expander = st.sidebar.expander("Choose entities to look for")
|
250 |
-
st_entities = st_entities_expander.multiselect(
|
251 |
-
label="Which entities to look for?",
|
252 |
-
options=get_supported_entities(*analyzer_params),
|
253 |
-
default=list(get_supported_entities(*analyzer_params)),
|
254 |
-
help="Limit the list of PII entities detected. "
|
255 |
-
"This list is dynamic and based on the NER model and registered recognizers. "
|
256 |
-
"More information can be found here: https://microsoft.github.io/presidio/analyzer/adding_recognizers/",
|
257 |
-
)
|
258 |
-
|
259 |
-
|
260 |
-
analyzer_load_state = st.info("Starting Presidio analyzer...")
|
261 |
-
analyzer = analyzer_engine(*analyzer_params)
|
262 |
-
analyzer_load_state.empty()
|
263 |
-
|
264 |
-
|
265 |
# Read default text
|
266 |
with open("demo_text.txt") as f:
|
267 |
demo_text = f.readlines()
|
@@ -275,20 +256,35 @@ st_text = col1.text_area(
|
|
275 |
label="Enter text", value="".join(demo_text), height=400, key="text_input"
|
276 |
)
|
277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
|
|
|
|
|
|
|
|
|
|
289 |
|
290 |
-
# After
|
291 |
-
if can_present_results:
|
292 |
if st_operator not in ("highlight", "synthesize"):
|
293 |
with col2:
|
294 |
st.subheader(f"Output")
|
@@ -348,6 +344,11 @@ if can_present_results:
|
|
348 |
else:
|
349 |
st.text("No findings")
|
350 |
|
|
|
|
|
|
|
|
|
|
|
351 |
components.html(
|
352 |
"""
|
353 |
<script type="text/javascript">
|
|
|
1 |
"""Streamlit app for Presidio."""
|
2 |
import logging
|
3 |
import os
|
4 |
+
import traceback
|
5 |
|
6 |
+
import dotenv
|
7 |
import pandas as pd
|
8 |
import streamlit as st
|
9 |
import streamlit.components.v1 as components
|
|
|
10 |
from annotated_text import annotated_text
|
11 |
from streamlit_tags import st_tags
|
12 |
|
|
|
18 |
annotate,
|
19 |
create_fake_data,
|
20 |
analyzer_engine,
|
|
|
21 |
)
|
22 |
|
23 |
st.set_page_config(
|
|
|
35 |
|
36 |
allow_other_models = os.getenv("ALLOW_OTHER_MODELS", False)
|
37 |
|
|
|
38 |
|
39 |
# Sidebar
|
40 |
st.sidebar.header(
|
|
|
171 |
api_type=openai_api_type,
|
172 |
)
|
173 |
|
174 |
+
input_is_valid = True if st_openai_key else False
|
175 |
|
176 |
st_threshold = st.sidebar.slider(
|
177 |
label="Acceptance threshold",
|
|
|
240 |
)
|
241 |
|
242 |
analyzer_load_state = st.info("Starting Presidio analyzer...")
|
|
|
243 |
|
244 |
analyzer_load_state.empty()
|
245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
246 |
# Read default text
|
247 |
with open("demo_text.txt") as f:
|
248 |
demo_text = f.readlines()
|
|
|
256 |
label="Enter text", value="".join(demo_text), height=400, key="text_input"
|
257 |
)
|
258 |
|
259 |
+
try:
|
260 |
+
# Choose entities
|
261 |
+
st_entities_expander = st.sidebar.expander("Choose entities to look for")
|
262 |
+
st_entities = st_entities_expander.multiselect(
|
263 |
+
label="Which entities to look for?",
|
264 |
+
options=get_supported_entities(*analyzer_params),
|
265 |
+
default=list(get_supported_entities(*analyzer_params)),
|
266 |
+
help="Limit the list of PII entities detected. "
|
267 |
+
"This list is dynamic and based on the NER model and registered recognizers. "
|
268 |
+
"More information can be found here: https://microsoft.github.io/presidio/analyzer/adding_recognizers/",
|
269 |
+
)
|
270 |
|
271 |
+
# Before
|
272 |
+
analyzer_load_state = st.info("Starting Presidio analyzer...")
|
273 |
+
analyzer = analyzer_engine(*analyzer_params)
|
274 |
+
analyzer_load_state.empty()
|
275 |
+
|
276 |
+
st_analyze_results = analyze(
|
277 |
+
*analyzer_params,
|
278 |
+
text=st_text,
|
279 |
+
entities=st_entities,
|
280 |
+
language="en",
|
281 |
+
score_threshold=st_threshold,
|
282 |
+
return_decision_process=st_return_decision_process,
|
283 |
+
allow_list=st_allow_list,
|
284 |
+
deny_list=st_deny_list,
|
285 |
+
)
|
286 |
|
287 |
+
# After
|
|
|
288 |
if st_operator not in ("highlight", "synthesize"):
|
289 |
with col2:
|
290 |
st.subheader(f"Output")
|
|
|
344 |
else:
|
345 |
st.text("No findings")
|
346 |
|
347 |
+
except Exception as e:
|
348 |
+
print(e)
|
349 |
+
traceback.print_exc()
|
350 |
+
st.error(e)
|
351 |
+
|
352 |
components.html(
|
353 |
"""
|
354 |
<script type="text/javascript">
|