mayzyo commited on
Commit
185bb55
·
1 Parent(s): 91b0724

:tada: Initial commit

Browse files
.gitignore ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ data/
2
+ vad_chunks/
3
+ uploads/
4
+ output/
5
+ flagged/
6
+
7
+ # Byte-compiled / optimized / DLL files
8
+ __pycache__/
9
+ *.py[cod]
10
+ *$py.class
11
+
12
+ # C extensions
13
+ *.so
14
+
15
+ # Distribution / packaging
16
+ .Python
17
+ build/
18
+ develop-eggs/
19
+ dist/
20
+ downloads/
21
+ eggs/
22
+ .eggs/
23
+ lib/
24
+ lib64/
25
+ parts/
26
+ sdist/
27
+ var/
28
+ wheels/
29
+ share/python-wheels/
30
+ *.egg-info/
31
+ .installed.cfg
32
+ *.egg
33
+ MANIFEST
34
+ Include/
35
+ Scripts/
36
+ share/
37
+ pyvenv.cfg
38
+
39
+ # PyInstaller
40
+ # Usually these files are written by a python script from a template
41
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
42
+ *.manifest
43
+ *.spec
44
+
45
+ # Installer logs
46
+ pip-log.txt
47
+ pip-delete-this-directory.txt
48
+
49
+ # Unit test / coverage reports
50
+ htmlcov/
51
+ .tox/
52
+ .nox/
53
+ .coverage
54
+ .coverage.*
55
+ .cache
56
+ nosetests.xml
57
+ coverage.xml
58
+ *.cover
59
+ *.py,cover
60
+ .hypothesis/
61
+ .pytest_cache/
62
+ cover/
63
+
64
+ # Translations
65
+ *.mo
66
+ *.pot
67
+
68
+ # Django stuff:
69
+ *.log
70
+ local_settings.py
71
+ db.sqlite3
72
+ db.sqlite3-journal
73
+
74
+ # Flask stuff:
75
+ instance/
76
+ .webassets-cache
77
+
78
+ # Scrapy stuff:
79
+ .scrapy
80
+
81
+ # Sphinx documentation
82
+ docs/_build/
83
+
84
+ # PyBuilder
85
+ .pybuilder/
86
+ target/
87
+
88
+ # Jupyter Notebook
89
+ .ipynb_checkpoints
90
+
91
+ # IPython
92
+ profile_default/
93
+ ipython_config.py
94
+
95
+ # pyenv
96
+ # For a library or package, you might want to ignore these files since the code is
97
+ # intended to run in multiple environments; otherwise, check them in:
98
+ # .python-version
99
+
100
+ # pipenv
101
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
102
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
103
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
104
+ # install all needed dependencies.
105
+ #Pipfile.lock
106
+
107
+ # poetry
108
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
109
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
110
+ # commonly ignored for libraries.
111
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
112
+ #poetry.lock
113
+
114
+ # pdm
115
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
116
+ #pdm.lock
117
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
118
+ # in version control.
119
+ # https://pdm.fming.dev/#use-with-ide
120
+ .pdm.toml
121
+
122
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
123
+ __pypackages__/
124
+
125
+ # Celery stuff
126
+ celerybeat-schedule
127
+ celerybeat.pid
128
+
129
+ # SageMath parsed files
130
+ *.sage.py
131
+
132
+ # Environments
133
+ .env
134
+ .venv
135
+ env/
136
+ venv/
137
+ ENV/
138
+ env.bak/
139
+ venv.bak/
140
+ envs/
141
+
142
+ # Spyder project settings
143
+ .spyderproject
144
+ .spyproject
145
+
146
+ # Rope project settings
147
+ .ropeproject
148
+
149
+ # mkdocs documentation
150
+ /site
151
+
152
+ # mypy
153
+ .mypy_cache/
154
+ .dmypy.json
155
+ dmypy.json
156
+
157
+ # Pyre type checker
158
+ .pyre/
159
+
160
+ # pytype static type analyzer
161
+ .pytype/
162
+
163
+ # Cython debug symbols
164
+ cython_debug/
165
+
166
+ # PyCharm
167
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
168
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
169
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
170
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
171
+ #.idea/
README.md CHANGED
@@ -11,3 +11,69 @@ license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
+
15
+ # Data Faker
16
+
17
+ Data Faker is a tool designed to protect privacy and maintain confidentiality by anonymizing sensitive information in text data before passing it to language models like GPT-4. It provides a user-friendly interface for anonymizing and deanonymizing text using the Presidio Reversible Anonymizer.
18
+
19
+ ## Features
20
+
21
+ - Anonymize sensitive information such as names, email addresses, phone numbers, and more
22
+ - Deanonymize previously anonymized text to recover the original information
23
+ - Maintain a separate anonymizer instance for each user session
24
+ - Simple and intuitive web-based interface using Gradio
25
+
26
+ ## Installation
27
+
28
+ 1. Clone the repository:
29
+
30
+ ```
31
+ git clone https://github.com/mayzyo/data-faker.git
32
+ ```
33
+
34
+ 2. Navigate to the project directory:
35
+
36
+ ```
37
+ cd data-faker
38
+ ```
39
+
40
+ 3. Install the required dependencies:
41
+
42
+ ```
43
+ pip install -r requirements.txt
44
+ ```
45
+
46
+ ## Usage
47
+
48
+ 1. Run the application:
49
+
50
+ ```
51
+ python app.py
52
+ ```
53
+
54
+ 2. Open your web browser and navigate to the provided URL (e.g., `http://localhost:7860`).
55
+
56
+ 3. Enter the text you want to anonymize in the "Text to Convert" input box.
57
+
58
+ 4. Click the "Generate Fake" button to anonymize the text. The anonymized text will appear in the "Output Text" box.
59
+
60
+ 5. To deanonymize the text, click the "Revert Fake" button. The original text will be restored in the "Output Text" box.
61
+
62
+ 6. To reset the anonymizer instance and clear the input and output boxes, click the "Reset" button.
63
+
64
+ ## Configuration
65
+
66
+ The `AnonymiserManager` class in `anonymiser_manager.py` uses the Presidio Reversible Anonymizer to handle the anonymization and deanonymization process. You can customize the `analyzed_fields` parameter to specify the types of sensitive information you want to anonymize.
67
+
68
+ ## Contributing
69
+
70
+ Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
71
+
72
+ ## License
73
+
74
+ This project is licensed under the [MIT License](LICENSE).
75
+
76
+ ## Acknowledgements
77
+
78
+ - [Presidio](https://github.com/microsoft/presidio) - Data anonymization library
79
+ - [Gradio](https://gradio.app/) - Web-based interface library
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from uuid import UUID
4
+ from data_faker.anonymiser_manager import AnonymiserManager
5
+
6
+ # device = 0 if torch.cuda.is_available() else "cpu"
7
+
8
+ theme = gr.themes.Base(
9
+ font=[gr.themes.GoogleFont('Libre Franklin'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
10
+ )
11
+
12
+ manager = AnonymiserManager()
13
+
14
+ def anonymise_text(id: UUID, input_text: str):
15
+ if input_text is None:
16
+ raise gr.Error("No text found! Please write something in the input box first.")
17
+
18
+ if id == None:
19
+ gr.Info('Using a new anonymiser instance')
20
+ else:
21
+ gr.Info(f'Running anonymise on existing instance ({id})')
22
+
23
+ anon_text, id = manager.add(id, input_text)
24
+ return id, anon_text
25
+
26
+ def deanonymise_text(id: UUID, input_text: str):
27
+ if input_text is None:
28
+ raise gr.Error("No text found! Please write something in the input box first.")
29
+
30
+ if id == None:
31
+ raise gr.Error("Unable to deanonymise without anonymising something first.")
32
+
33
+ deanon_text = manager.revert(id, input_text)
34
+ return deanon_text
35
+
36
+ def clear_instance(id: UUID):
37
+ if id is None:
38
+ raise gr.Error("No anonymising recorded yet, nothing to clear.")
39
+
40
+ if manager.clear(id) == True:
41
+ gr.Info(f'Anonymiser instance remove successfully ({id})')
42
+ else:
43
+ gr.Info(f'Anonymiser instance ({id}) cannot be found')
44
+
45
+ return None, '', ''
46
+
47
+ with gr.Blocks() as text_to_text:
48
+ state = gr.State()
49
+ with gr.Row():
50
+ with gr.Column(scale=1):
51
+ input_text = gr.TextArea(label="Text to Convert", show_copy_button=True)
52
+ with gr.Row():
53
+ anon_btn = gr.Button("Generate Fake", variant="primary")
54
+ denon_btn = gr.Button("Revert Fake", variant="primary")
55
+ with gr.Column(scale=1):
56
+ output_text = gr.TextArea(label="Output Text", show_copy_button=True)
57
+ clear_btn = gr.Button("Reset")
58
+ anon_btn.click(anonymise_text, inputs=[state, input_text], outputs=[state, output_text], concurrency_limit=4)
59
+ denon_btn.click(deanonymise_text, inputs=[state, input_text], outputs=[output_text], concurrency_limit=4)
60
+ clear_btn.click(clear_instance, inputs=[state], outputs=[state, input_text, output_text], concurrency_limit=4)
61
+
62
+ with gr.Blocks(title="Data Faker", theme=theme) as demo:
63
+ gr.Markdown("""# Data Faker
64
+ A crucial data faking tool before passing information to a language model like GPT-4 to help protect privacy and maintain confidentiality""")
65
+ gr.TabbedInterface(
66
+ [text_to_text],
67
+ ["Text to Text"]
68
+ )
69
+
70
+ if __name__ == "__main__":
71
+ demo.queue(api_open=True).launch(show_api=True)
data_faker/__init__.py ADDED
File without changes
data_faker/anonymiser_manager.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer
2
+ from uuid import uuid5, UUID, NAMESPACE_DNS
3
+
4
+ class AnonymiserManager:
5
+ mapping = {}
6
+
7
+ def add(self, id: UUID, text: str):
8
+ if id in self.mapping.keys():
9
+ anonymiser = self.mapping[id]
10
+ else:
11
+ anonymiser = PresidioReversibleAnonymizer(
12
+ analyzed_fields=['PERSON', 'EMAIL_ADDRESS', 'PHONE_NUMBER', 'IBAN_CODE', 'CREDIT_CARD', 'CRYPTO', 'IP_ADDRESS', 'LOCATION', 'NRP', 'MEDICAL_LICENSE', 'URL', 'US_BANK_NUMBER', 'US_DRIVER_LICENSE', 'US_ITIN', 'US_PASSPORT', 'US_SSN']
13
+ )
14
+ id = uuid5(NAMESPACE_DNS, 'destinesiastudio.com.au')
15
+ self.mapping[id] = anonymiser
16
+
17
+ anon_text = anonymiser.anonymize(text, language="en")
18
+
19
+ return anon_text, id
20
+
21
+ def revert(self, id: UUID, text: str) -> str:
22
+ if id in self.mapping.keys():
23
+ anonymiser: PresidioReversibleAnonymizer = self.mapping[id]
24
+ deanon_text = anonymiser.deanonymize(text)
25
+
26
+ return deanon_text
27
+
28
+ def clear(self, id: UUID) -> bool:
29
+ if id in self.mapping.keys():
30
+ del self.mapping[id]
31
+ return True
32
+
33
+ return False
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio==4.20.1
2
+ langchain-experimental==0.0.53
3
+ presidio-analyzer==2.2.353
4
+ presidio-anonymizer==2.2.353
5
+ spacy==3.7.4
6
+ faker==24.0.0