Spaces:
Running
Running
Commit
·
c5fbe61
1
Parent(s):
22b5485
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pii_transform.api.e2e import PiiTextProcessor
|
| 3 |
+
from pii_extract.defs import FMT_CONFIG_PLUGIN
|
| 4 |
+
|
| 5 |
+
examples = []
|
| 6 |
+
with open("examples.txt", "r") as f:
|
| 7 |
+
examples = f.readlines()
|
| 8 |
+
examples_truncated = [example[:50] + "..." for example in examples]
|
| 9 |
+
language_choices = {
|
| 10 |
+
"English": "en",
|
| 11 |
+
"Italian": "it",
|
| 12 |
+
"Spanish": "es",
|
| 13 |
+
"Portugese": "pt",
|
| 14 |
+
"Deutsche": "de",
|
| 15 |
+
"French": "fr",
|
| 16 |
+
}
|
| 17 |
+
language_code = "en"
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def change_language(language_selection):
|
| 21 |
+
global language_code
|
| 22 |
+
language_code = language_choices[language_selection]
|
| 23 |
+
gr.Info(f"{language_selection} selected")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def process(text, policy):
|
| 27 |
+
# Create the object, defining the language to use and the policy
|
| 28 |
+
# Further customization is possible by providing a config
|
| 29 |
+
if text == "":
|
| 30 |
+
print("Empty text field")
|
| 31 |
+
gr.Warning("No text present")
|
| 32 |
+
return ""
|
| 33 |
+
|
| 34 |
+
# Custom config to prevent loading of the Presidio plugin
|
| 35 |
+
# config = {FMT_CONFIG_PLUGIN: {"piisa-detectors-presidio": {"load": False}}}
|
| 36 |
+
proc = PiiTextProcessor(
|
| 37 |
+
lang=language_code, default_policy=policy, config="config.json"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Process a text buffer and get the transformed buffer
|
| 41 |
+
outbuf = proc(text)
|
| 42 |
+
return outbuf
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def get_full_example(idx):
|
| 46 |
+
return examples[idx]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
with gr.Row():
|
| 51 |
+
with gr.Column(scale=0, min_width=75):
|
| 52 |
+
logo = gr.Image("image.jpeg", show_label=False, show_download_button=False)
|
| 53 |
+
with gr.Column():
|
| 54 |
+
pass
|
| 55 |
+
with gr.Column(scale=0, min_width=200):
|
| 56 |
+
lang_picker = gr.Dropdown(
|
| 57 |
+
choices=list(language_choices.keys()),
|
| 58 |
+
label="Select Language",
|
| 59 |
+
value=list(language_choices.keys())[0],
|
| 60 |
+
type="value",
|
| 61 |
+
)
|
| 62 |
+
lang_picker.select(change_language, inputs=lang_picker, outputs=None)
|
| 63 |
+
with gr.Row():
|
| 64 |
+
with gr.Column(scale=2, min_width=400):
|
| 65 |
+
text_original = gr.Textbox(
|
| 66 |
+
label="Original Text",
|
| 67 |
+
lines=10,
|
| 68 |
+
placeholder="Enter the text you would like to analyze, or select from one of the examples below",
|
| 69 |
+
)
|
| 70 |
+
with gr.Column(scale=0, min_width=25):
|
| 71 |
+
pass
|
| 72 |
+
with gr.Column(scale=0, min_width=100):
|
| 73 |
+
for i in range(3):
|
| 74 |
+
with gr.Row():
|
| 75 |
+
pass
|
| 76 |
+
redact_btn = gr.Button(value="Redact", variant="primary", size="sm")
|
| 77 |
+
anonymize_btn = gr.Button(value="Anonymize", variant="primary", size="sm")
|
| 78 |
+
placeholder_btn = gr.Button(
|
| 79 |
+
value="Placeholder", variant="primary", size="sm"
|
| 80 |
+
)
|
| 81 |
+
with gr.Column(scale=0, min_width=25):
|
| 82 |
+
pass
|
| 83 |
+
with gr.Column(
|
| 84 |
+
scale=2,
|
| 85 |
+
min_width=400,
|
| 86 |
+
):
|
| 87 |
+
text_redacted = gr.TextArea(
|
| 88 |
+
label="Transformed Text",
|
| 89 |
+
lines=10,
|
| 90 |
+
show_copy_button=True,
|
| 91 |
+
interactive=False,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
redact_btn.click(
|
| 95 |
+
fn=process,
|
| 96 |
+
inputs=[
|
| 97 |
+
text_original,
|
| 98 |
+
gr.Text(value="redact", visible=False),
|
| 99 |
+
],
|
| 100 |
+
outputs=text_redacted,
|
| 101 |
+
)
|
| 102 |
+
anonymize_btn.click(
|
| 103 |
+
fn=process,
|
| 104 |
+
inputs=[
|
| 105 |
+
text_original,
|
| 106 |
+
gr.Text(value="synthetic", visible=False),
|
| 107 |
+
],
|
| 108 |
+
outputs=text_redacted,
|
| 109 |
+
)
|
| 110 |
+
placeholder_btn.click(
|
| 111 |
+
fn=process,
|
| 112 |
+
inputs=[
|
| 113 |
+
text_original,
|
| 114 |
+
gr.Text(value="label", visible=False),
|
| 115 |
+
],
|
| 116 |
+
outputs=text_redacted,
|
| 117 |
+
)
|
| 118 |
+
with gr.Row():
|
| 119 |
+
example_selector = gr.Dropdown(
|
| 120 |
+
examples_truncated, type="index", label="Examples"
|
| 121 |
+
)
|
| 122 |
+
example_selector.select(
|
| 123 |
+
get_full_example, inputs=example_selector, outputs=text_original
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
demo.queue().launch()
|