Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 6,797 Bytes
646bd9e df6182e d812385 646bd9e df6182e 646bd9e d812385 646bd9e 2b591f4 646bd9e 2b591f4 646bd9e df6182e 628fe8f df6182e 628fe8f df6182e b160148 646bd9e b160148 646bd9e df6182e 646bd9e b160148 646bd9e b160148 646bd9e df6182e 646bd9e d812385 646bd9e d812385 2b591f4 646bd9e d812385 b160148 d0b1031 646bd9e b160148 646bd9e 8bad0f5 646bd9e df6182e 646bd9e 2b591f4 |
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
"""A Gradio app for anonymizing text data using FHE."""
import gradio as gr
from fhe_anonymizer import FHEAnonymizer
import pandas as pd
from openai import OpenAI
import os
import json
import re
from utils_demo import *
from typing import List, Dict, Tuple
anonymizer = FHEAnonymizer()
client = OpenAI(
api_key=os.environ.get("openaikey"),
)
def check_user_query_fn(user_query: str) -> Dict:
if is_user_query_valid(user_query):
# TODO: check if the query is related to our context
error_msg = ("Unable to process β: The request exceeds the length limit or falls "
"outside the scope of this document. Please refine your query.")
print(error_msg)
return {input_text: gr.update(value=error_msg)}
else:
# Collapsing Multiple Spaces
return {input_text: gr.update(value=re.sub(" +", " ", user_query))}
def deidentify_text(input_text):
anonymized_text, identified_words_with_prob = anonymizer(input_text)
# Convert the list of identified words and probabilities into a DataFrame
if identified_words_with_prob:
identified_df = pd.DataFrame(
identified_words_with_prob, columns=["Identified Words", "Probability"]
)
else:
identified_df = pd.DataFrame(columns=["Identified Words", "Probability"])
return anonymized_text, identified_df
def query_chatgpt(anonymized_query):
with open("files/anonymized_document.txt", "r") as file:
anonymized_document = file.read()
with open("files/chatgpt_prompt.txt", "r") as file:
prompt = file.read()
# Prepare prompt
full_prompt = (
prompt + "\n"
)
query = "Document content:\n```\n" + anonymized_document + "\n\n```" + "Query:\n```\n" + anonymized_query + "\n```"
print(full_prompt)
completion = client.chat.completions.create(
model="gpt-4-1106-preview", # Replace with "gpt-4" if available
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": query},
],
)
anonymized_response = completion.choices[0].message.content
with open("original_document_uuid_mapping.json", "r") as file:
uuid_map = json.load(file)
inverse_uuid_map = {v: k for k, v in uuid_map.items()} # TODO load the inverse mapping from disk for efficiency
# Pattern to identify words and non-words (including punctuation, spaces, etc.)
token_pattern = r"(\b[\w\.\/\-@]+\b|[\s,.!?;:'\"-]+)"
tokens = re.findall(token_pattern, anonymized_response)
processed_tokens = []
for token in tokens:
# Directly append non-word tokens or whitespace to processed_tokens
if not token.strip() or not re.match(r"\w+", token):
processed_tokens.append(token)
continue
if token in inverse_uuid_map:
processed_tokens.append(inverse_uuid_map[token])
else:
processed_tokens.append(token)
deanonymized_response = "".join(processed_tokens)
return anonymized_response, deanonymized_response
with open("files/original_document.txt", "r") as file:
original_document = file.read()
with open("files/anonymized_document.txt", "r") as file:
anonymized_document = file.read()
demo = gr.Blocks(css=".markdown-body { font-size: 18px; }")
with demo:
gr.Markdown(
"""
<p align="center">
<img width=200 src="file/images/logos/zama.jpg">
</p>
<h1 style="text-align: center;">Encrypted Anonymization Using Fully Homomorphic Encryption</h1>
<p align="center">
<a href="https://github.com/zama-ai/concrete-ml"> <img style="vertical-align: middle; display:inline-block; margin-right: 3px;" width=15 src="file/images/logos/github.png">Concrete-ML</a>
β
<a href="https://docs.zama.ai/concrete-ml"> <img style="vertical-align: middle; display:inline-block; margin-right: 3px;" width=15 src="file/images/logos/documentation.png">Documentation</a>
β
<a href="https://zama.ai/community"> <img style="vertical-align: middle; display:inline-block; margin-right: 3px;" width=15 src="file/images/logos/community.png">Community</a>
β
<a href="https://twitter.com/zama_fhe"> <img style="vertical-align: middle; display:inline-block; margin-right: 3px;" width=15 src="file/images/logos/x.png">@zama_fhe</a>
</p>
"""
)
with gr.Accordion("What is Encrypted Anonymization?", open=False):
gr.Markdown(
"""
Encrypted Anonymization leverages Fully Homomorphic Encryption (FHE) to protect sensitive information during data processing. This approach allows for the anonymization of text data, such as personal identifiers, while ensuring that the data remains encrypted throughout the entire process.
"""
)
with gr.Row():
with gr.Accordion("Original Document", open=True):
gr.Markdown(original_document)
with gr.Accordion("Anonymized Document", open=True):
gr.Markdown(anonymized_document)
# gr.Markdown(
# """
# <p align="center">
# <img src="file/images/banner.png">
# </p>
# """
# )
########################## User Query Part ##########################
with gr.Row():
input_text = gr.Textbox(value="Who lives in Maine?", label="User query", interactive=True)
default_query_box = gr.Radio(choices=list(DEFAULT_QUERIES.keys()), label="Example Queries")
default_query_box.change(
fn=lambda default_query_box: DEFAULT_QUERIES[default_query_box],
inputs=[default_query_box],
outputs=[input_text]
)
input_text.change(
check_user_query_fn,
inputs=[input_text],
outputs=[input_text],
)
anonymized_text_output = gr.Textbox(label="Anonymized Text with FHE", lines=1, interactive=True)
identified_words_output = gr.Dataframe(label="Identified Words", visible=False)
submit_button = gr.Button("Anonymize with FHE")
submit_button.click(
deidentify_text,
inputs=[input_text],
outputs=[anonymized_text_output, identified_words_output],
)
with gr.Row():
chatgpt_response_anonymized = gr.Textbox(label="ChatGPT Anonymized Response", lines=13)
chatgpt_response_deanonymized = gr.Textbox(label="ChatGPT Deanonymized Response", lines=13)
chatgpt_button = gr.Button("Query ChatGPT")
chatgpt_button.click(
query_chatgpt,
inputs=[anonymized_text_output],
outputs=[chatgpt_response_anonymized, chatgpt_response_deanonymized],
)
# Launch the app
demo.launch(share=False)
|