File size: 9,559 Bytes
649ee03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { useEffect, useState } from 'react'
import {
  anonymizeText,
  deidentifyText,
  detectText,
  redactText,
  type DeidStrategy,
  type PseudonymMapping,
  type TextPIISpan,
} from '../lib/api'
import { l1Rgb, l1Tint } from '../lib/colors'
import Legend from './Legend'
import MappingTable from './MappingTable'

type Action = 'detect' | 'redact' | 'deidentify' | 'anonymize'

const ACTION_BUTTON_LABELS: Record<Action, string> = {
  detect: 'Detect PII',
  redact: 'Redact',
  deidentify: 'De-identify',
  anonymize: 'Anonymize',
}

const ACTION_HINTS: Record<Action, string> = {
  detect: 'Finds PII spans and highlights them in place — nothing is changed.',
  redact: 'Replaces each span with a [category] token. One-way, nothing kept.',
  deidentify:
    'Replaces each entity with a consistent pseudonym (Person_1) and returns the mapping for authorized re-linking.',
  anonymize:
    'One-way: generalizes ages/dates/geography, collapses names and IDs to unnumbered tokens. No mapping exists.',
}

const EXAMPLE_TEXT = `DISCHARGE SUMMARY
Patient: Mr. John Doe (Male, 45 yrs), DOB 12-03-1979, Blood group O+.
Address: 14 MG Road, Indiranagar, Bangalore, Karnataka 560038.
Aadhaar: 1234 5678 9012  |  PAN: ABCDE1234F  |  ABHA: 14-1234-5678-9012.
MRN/UHID: UH00219834. Insurance policy: TPA-IND-99213.
Contact: +91 98765 43210, email john.doe@example.com.
Treating physician: Dr. Asha Menon (NMC Reg. 2011/04/1123).
Payment received to UPI johndoe@okhdfcbank, A/C 50100123456789.`

function renderHighlighted(text: string, spans: TextPIISpan[]) {
  const sorted = [...spans].sort((a, b) => a.start - b.start)
  const parts: React.ReactNode[] = []
  let prev = 0
  for (const [i, sp] of sorted.entries()) {
    if (sp.start < prev) continue
    parts.push(text.slice(prev, sp.start))
    parts.push(
      <mark
        key={i}
        className="pii-mark"
        style={{ background: l1Tint(sp.l1), borderColor: l1Rgb(sp.l1) }}
        title={`${sp.category} · ${sp.score != null ? sp.score.toFixed(3) : '?'}`}
      >
        {text.slice(sp.start, sp.end)}
        <sup className="pii-tag" style={{ color: l1Rgb(sp.l1) }}>
          {sp.category}
        </sup>
      </mark>,
    )
    prev = sp.end
  }
  parts.push(text.slice(prev))
  return parts
}

export default function TextTab({ entities, ready }: { entities: string[] | null; ready: boolean }) {
  const [text, setText] = useState(EXAMPLE_TEXT)
  const [action, setAction] = useState<Action>('detect')
  const [strategy, setStrategy] = useState<DeidStrategy>('counter')
  const [selected, setSelected] = useState<Set<string>>(new Set())
  const [spans, setSpans] = useState<TextPIISpan[]>([])
  const [outputText, setOutputText] = useState<string | null>(null)
  const [outputTitle, setOutputTitle] = useState('')
  const [mapping, setMapping] = useState<PseudonymMapping | null>(null)
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)
  const [ranOnce, setRanOnce] = useState(false)

  // All categories selected by default, once the taxonomy arrives.
  useEffect(() => {
    if (entities) setSelected(new Set(entities))
  }, [entities])

  function toggle(value: string) {
    const next = new Set(selected)
    if (next.has(value)) next.delete(value)
    else next.add(value)
    setSelected(next)
  }

  const allSelected = entities != null && selected.size === entities.length
  // Omit the param entirely when everything is selected (= server default).
  const categories = allSelected ? undefined : [...selected]

  async function run() {
    if (selected.size === 0) return
    setLoading(true)
    setError(null)
    setMapping(null)
    setOutputText(null)
    try {
      if (action === 'detect') {
        setSpans(await detectText(text, categories))
      } else {
        setSpans([])
        if (action === 'redact') {
          setOutputText(await redactText(text, categories))
          setOutputTitle('Redacted')
        } else if (action === 'deidentify') {
          const result = await deidentifyText(text, categories, strategy)
          setOutputText(result.text)
          setMapping(result.mapping)
          setOutputTitle('De-identified')
        } else {
          setOutputText(await anonymizeText(text, categories))
          setOutputTitle('Anonymized')
        }
      }
      setRanOnce(true)
    } catch (err) {
      setError(err instanceof Error ? err.message : String(err))
    } finally {
      setLoading(false)
    }
  }

  const groups = [...new Set(spans.map((s) => s.l1))].sort()

  return (
    <div className="tab-panel">
      <p className="tab-caption">Detects PII in raw text and highlights each span, color-coded by group.</p>

      <section className="card">
        <h2 className="card-title">Input</h2>

        {entities && (
          <details className="exclude-panel">
            <summary>
              Categories to detect ({selected.size}/{entities.length} selected)
            </summary>
            <div className="select-buttons">
              <button
                type="button"
                className="btn-secondary btn-small"
                onClick={() => setSelected(new Set(entities))}
              >
                Select all
              </button>
              <button
                type="button"
                className="btn-secondary btn-small"
                onClick={() => setSelected(new Set())}
              >
                Deselect all
              </button>
            </div>
            <div className="exclude-columns">
              <div>
                {entities.map((c) => (
                  <label key={c} className="checkbox-row">
                    <input type="checkbox" checked={selected.has(c)} onChange={() => toggle(c)} />
                    {c}
                  </label>
                ))}
              </div>
            </div>
          </details>
        )}

        <textarea
          className="text-input"
          value={text}
          onChange={(e) => setText(e.target.value)}
          rows={10}
        />

        <div className="controls-row">
          <label className="field">
            <span className="field-label">Action</span>
            <select value={action} onChange={(e) => setAction(e.target.value as Action)}>
              <option value="detect">Detect (highlight)</option>
              <option value="redact">Redact</option>
              <option value="anonymize">Anonymize</option>
              <option value="deidentify">De-identify</option>
            </select>
          </label>
          {action === 'deidentify' && (
            <label className="field">
              <span className="field-label">Token style</span>
              <select
                value={strategy}
                onChange={(e) => setStrategy(e.target.value as DeidStrategy)}
              >
                <option value="counter">Sequential (Person_1)</option>
                <option value="hash">Hash — global (Person_a3f9c1)</option>
              </select>
            </label>
          )}
          <button
            type="button"
            className="primary-btn"
            onClick={run}
            disabled={!text.trim() || !ready || loading || selected.size === 0}
          >
            {loading ? 'Working…' : ACTION_BUTTON_LABELS[action]}
          </button>
        </div>
        <p className="tab-caption action-hint">{ACTION_HINTS[action]}</p>
      </section>

      {error && <div className="error-banner">{error}</div>}

      {outputText != null && (
        <section className="card">
          <h2 className="card-title">{outputTitle}</h2>
          <div className="output-text">{outputText}</div>
        </section>
      )}

      {mapping && <MappingTable mapping={mapping} />}

      {ranOnce && action === 'detect' && (
        <>
          {spans.length > 0 ? (
            <>
              <section className="card">
                <h2 className="card-title">Highlighted</h2>
                <Legend groups={groups} />
                <div className="highlighted-text">{renderHighlighted(text, spans)}</div>
              </section>

              <section className="card">
                <h2 className="card-title">Detected spans</h2>
                <div className="table-scroll">
                  <table className="entity-table">
                    <thead>
                      <tr>
                        <th>category</th>
                        <th>l1</th>
                        <th>text</th>
                        <th>start</th>
                        <th>end</th>
                        <th>score</th>
                      </tr>
                    </thead>
                    <tbody>
                      {spans.map((s, i) => (
                        <tr key={i}>
                          <td>{s.category}</td>
                          <td>
                            <span className="l1-dot" style={{ background: l1Rgb(s.l1) }} />
                            {s.l1}
                          </td>
                          <td>{s.text}</td>
                          <td>{s.start}</td>
                          <td>{s.end}</td>
                          <td>{s.score != null ? s.score.toFixed(3) : ''}</td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              </section>
            </>
          ) : (
            <p className="tab-caption">No PII detected.</p>
          )}
        </>
      )}
    </div>
  )
}