File size: 1,940 Bytes
e5394d1 bb9019d e5394d1 8ce41f4 c05e03d 8ce41f4 c05e03d 8ce41f4 c05e03d 2ff0089 c05e03d 528a36c c05e03d e5394d1 2ff0089 e5394d1 7e5edc5 e5394d1 c05e03d e5394d1 528a36c |
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 |
from pathlib import Path
import gradio as gr
import pii_anonymizer
def upload_file(filepath):
name = Path(filepath).name
return [gr.UploadButton(visible=False), gr.DownloadButton(label=f"Download {name}", value=filepath, visible=True)]
def scrub_file(filepath, progress=gr.Progress()):
# Temporary list to hold stripped lines
stripped_lines = []
pii_lines = []
# Open the original file and strip each line
with open(filepath, 'r', encoding='utf-8') as file:
for line in file:
stripped_lines.append(line.strip())
progress(0, desc="Starting...")
for line in progress.tqdm(stripped_lines):
pii_lines.append(pii_anonymizer.anonymize_text_including_proper_nouns_and_addresses(line))
# Overwrite the original file with pii replaced lines
with open(filepath, 'w', encoding='utf-8') as file:
for line in pii_lines:
file.write(line + '\n') # Add '\n' to keep the file structure
# Extract the filename for the download button label
name = Path(filepath).name
# Return the Gradio interface elements
return [gr.UploadButton(visible=False), gr.DownloadButton(label=f"Download {name}", value=filepath, visible=True)]
def download_file():
return [gr.UploadButton(visible=True), gr.DownloadButton(visible=False)]
with gr.Blocks() as demo:
gr.Markdown("First upload a file and and then you'll be able download a PII stripped version of it (but only once!)")
#gr.Markdown("PII Anonymized:"+pii_anonymizer.anonymize_text_including_proper_nouns_and_addresses("Hello Kripa"))
with gr.Row():
u = gr.UploadButton("Upload a file", file_count="single", file_types=["text"])
d = gr.DownloadButton("Download the file", visible=False)
#u.upload(upload_file, u, [u, d])
u.upload(scrub_file, u, [u, d])
d.click(download_file, None, [u, d])
if __name__ == "__main__":
demo.queue().launch()
|