Skip to content

Commit 82f1f00

Browse files
committed
PII and HayStack
1 parent a565e84 commit 82f1f00

1 file changed

Lines changed: 270 additions & 0 deletions

File tree

ai-apps/haystack_tonic.ipynb

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"authorship_tag": "ABX9TyPTApHdeA109Za10Eckvgnh",
8+
"include_colab_link": true
9+
},
10+
"kernelspec": {
11+
"name": "python3",
12+
"display_name": "Python 3"
13+
},
14+
"language_info": {
15+
"name": "python"
16+
}
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {
22+
"id": "view-in-github",
23+
"colab_type": "text"
24+
},
25+
"source": [
26+
"<a href=\"https://colab.research.google.com/github/afizs/python-notes/blob/main/ai-apps/haystack_tonic.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"source": [
32+
"## PII detection and extraction in Haystack pipelines"
33+
],
34+
"metadata": {
35+
"id": "skFZ8NKLZR08"
36+
}
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"source": [
41+
"## Installation"
42+
],
43+
"metadata": {
44+
"id": "KIpZyP_GcQNr"
45+
}
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 11,
50+
"metadata": {
51+
"id": "OaRdp0C9Yxsy"
52+
},
53+
"outputs": [],
54+
"source": [
55+
"!pip install textual-haystack -q"
56+
]
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"source": [
61+
"## Setup\n",
62+
"First thing first get your textual FREE API Key.\n",
63+
"https://docs.tonic.ai/textual/tonic-textual-getting-started"
64+
],
65+
"metadata": {
66+
"id": "Q9q6XTh_btGr"
67+
}
68+
},
69+
{
70+
"cell_type": "code",
71+
"source": [
72+
"import os\n",
73+
"from google.colab import userdata\n",
74+
"\n",
75+
"# Load the TONIC_TEXTUAL_API_KEY from Colab secrets\n",
76+
"os.environ[\"TONIC_TEXTUAL_API_KEY\"] = userdata.get('TONIC_TEXTUAL_API_KEY')"
77+
],
78+
"metadata": {
79+
"id": "eyqkUKPfaCDG"
80+
},
81+
"execution_count": 12,
82+
"outputs": []
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"source": [
87+
"## 1. Document cleaning\n",
88+
"\n",
89+
"**Clean up documents before replacing PII with realistic fake data**"
90+
],
91+
"metadata": {
92+
"id": "_gDSEoRQaTzQ"
93+
}
94+
},
95+
{
96+
"cell_type": "code",
97+
"source": [
98+
"from haystack.dataclasses import Document\n",
99+
"from haystack_integrations.components.tonic_textual import TonicTextualDocumentCleaner\n",
100+
"\n",
101+
"cleaner = TonicTextualDocumentCleaner(generator_default=\"Synthesis\")\n",
102+
"result = cleaner.run(documents=[\n",
103+
" Document(content=\"Patient John Smith, DOB 03/15/1982, was admitted for chest pain.\")\n",
104+
"])\n",
105+
"print(result[\"documents\"][0].content)\n"
106+
],
107+
"metadata": {
108+
"colab": {
109+
"base_uri": "https://localhost:8080/"
110+
},
111+
"id": "o_gGC5o6Zosb",
112+
"outputId": "0bb4b1b6-ee17-4203-ed95-51b6a0b70d40"
113+
},
114+
"execution_count": 13,
115+
"outputs": [
116+
{
117+
"output_type": "stream",
118+
"name": "stdout",
119+
"text": [
120+
"Patient Alfonzo Uva, DOB 03/20/1982, was admitted for chest pain.\n"
121+
]
122+
}
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"source": [
128+
"**Tokenize PII**"
129+
],
130+
"metadata": {
131+
"id": "HuX870z_a9HF"
132+
}
133+
},
134+
{
135+
"cell_type": "code",
136+
"source": [
137+
"cleaner = TonicTextualDocumentCleaner(generator_default=\"Redaction\")\n",
138+
"result = cleaner.run(documents=[\n",
139+
" Document(content=\"Contact Jane Doe at jane@example.com.\")\n",
140+
"])\n",
141+
"print(result[\"documents\"][0].content)\n"
142+
],
143+
"metadata": {
144+
"colab": {
145+
"base_uri": "https://localhost:8080/"
146+
},
147+
"id": "SuBT0lKla8z4",
148+
"outputId": "6776a34f-f711-4338-d71c-32d641872057"
149+
},
150+
"execution_count": 14,
151+
"outputs": [
152+
{
153+
"output_type": "stream",
154+
"name": "stdout",
155+
"text": [
156+
"Contact [NAME_GIVEN_iKpB3] [NAME_FAMILY_Z3W2] at [EMAIL_ADDRESS_QEgToEQ8FO6hC16fJG].\n"
157+
]
158+
}
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"source": [
164+
"## Entity Extraction"
165+
],
166+
"metadata": {
167+
"id": "W_6L_KoccBGc"
168+
}
169+
},
170+
{
171+
"cell_type": "code",
172+
"source": [
173+
"from haystack.dataclasses import Document\n",
174+
"from haystack_integrations.components.tonic_textual import TonicTextualEntityExtractor\n",
175+
"\n",
176+
"extractor = TonicTextualEntityExtractor()\n",
177+
"result = extractor.run(documents=[\n",
178+
" Document(content=\"My name is Afiz Shaik and my email is afiz@example.com.\")\n",
179+
"])\n",
180+
"\n",
181+
"for entity in TonicTextualEntityExtractor.get_stored_annotations(result[\"documents\"][0]):\n",
182+
" print(f\"{entity.entity}: {entity.text} (confidence: {entity.score:.2f})\")\n"
183+
],
184+
"metadata": {
185+
"colab": {
186+
"base_uri": "https://localhost:8080/"
187+
},
188+
"id": "yZ6I4B9obCYa",
189+
"outputId": "c0ecec1f-f1b6-48a1-df1d-4ceed020e1ff"
190+
},
191+
"execution_count": 15,
192+
"outputs": [
193+
{
194+
"output_type": "stream",
195+
"name": "stdout",
196+
"text": [
197+
"NAME_GIVEN: Afiz (confidence: 0.95)\n",
198+
"NAME_FAMILY: Shaik (confidence: 0.93)\n",
199+
"EMAIL_ADDRESS: afiz@example.com (confidence: 0.99)\n"
200+
]
201+
}
202+
]
203+
},
204+
{
205+
"cell_type": "markdown",
206+
"source": [
207+
"## Complete Haystack Pipeline"
208+
],
209+
"metadata": {
210+
"id": "Jduqp5y1cy-p"
211+
}
212+
},
213+
{
214+
"cell_type": "code",
215+
"source": [
216+
"from haystack import Pipeline\n",
217+
"from haystack.dataclasses import Document\n",
218+
"from haystack_integrations.components.tonic_textual import (\n",
219+
" TonicTextualDocumentCleaner,\n",
220+
" TonicTextualEntityExtractor,\n",
221+
")\n",
222+
"\n",
223+
"pipeline = Pipeline()\n",
224+
"pipeline.add_component(\"cleaner\", TonicTextualDocumentCleaner(generator_default=\"Synthesis\"))\n",
225+
"pipeline.add_component(\"extractor\", TonicTextualEntityExtractor())\n",
226+
"pipeline.connect(\"cleaner\", \"extractor\")\n",
227+
"\n",
228+
"result = pipeline.run({\n",
229+
" \"cleaner\": {\n",
230+
" \"documents\": [\n",
231+
" Document(content=\"Contact Jane Doe at jane@example.com or (555) 867-5309.\"),\n",
232+
" ]\n",
233+
" }\n",
234+
"})\n",
235+
"\n",
236+
"for doc in result[\"extractor\"][\"documents\"]:\n",
237+
" entities = TonicTextualEntityExtractor.get_stored_annotations(doc)\n",
238+
" print(f\"Cleaned: {doc.content}\")\n",
239+
" print(f\"Entities: {[(e.entity, e.text) for e in entities]}\")\n"
240+
],
241+
"metadata": {
242+
"colab": {
243+
"base_uri": "https://localhost:8080/"
244+
},
245+
"id": "CtmPwcwscLqo",
246+
"outputId": "20e05741-2dba-459d-d80f-fb0b1dae0135"
247+
},
248+
"execution_count": 16,
249+
"outputs": [
250+
{
251+
"output_type": "stream",
252+
"name": "stdout",
253+
"text": [
254+
"Cleaned: Contact Antonina Crummitt at pacv@pvjivyj.lfq or (851) 452-9397.\n",
255+
"Entities: [('NAME_GIVEN', 'Antonina'), ('NAME_FAMILY', 'Crummitt'), ('EMAIL_ADDRESS', 'pacv@pvjivyj.lfq'), ('PHONE_NUMBER', '(851) 452-9397')]\n"
256+
]
257+
}
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"source": [],
263+
"metadata": {
264+
"id": "TBgcSEt-dc_n"
265+
},
266+
"execution_count": null,
267+
"outputs": []
268+
}
269+
]
270+
}

0 commit comments

Comments
 (0)