Spaces:
Running
Running
File size: 21,870 Bytes
da7e220 | 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 | # AI-output / document reinsert workflow review
Status: completed recommendation.
Workpackage: WP9 — AI-output / document reinsert workflow UX and architecture review.
Repository: `solidprivacy-nl/scrub`.
Date: 2026-06-08.
This review decides what Scrub should do next for AI-output and document-level reinsert before implementation starts.
No code, UI, export behavior, Scrub Key behavior, AI calls or cloud processing are changed by this document.
---
## 1. Decision summary
Pasted-text reinsert is useful and should remain available, but it is not sufficient as the final legal-document workflow.
Recommended direction:
```text
Use a two-mode interface:
1. Anonimiseren
2. Originele waarden terugzetten
Keep pasted-text reinsert as the simplest path.
Add document-level reinsert in controlled phases.
Prioritize TXT and DOCX before PDF.
Do not implement full PDF reinsert yet.
Do not add AI calls.
Do not add cloud processing.
```
The next implementation should not be a broad UI refactor or full document rehydration. It should be helper-first and test-first.
Recommended next implementation workpackage:
```text
WP10 — v13.4 TXT/DOCX reinsert foundation helper and tests
```
Goal of WP10:
```text
Prepare document-level reinsert without changing UI yet:
- add pure TXT reinsert input/output helper behavior;
- add a pure DOCX placeholder replacement helper design/implementation if feasible;
- add synthetic tests;
- keep PDF out of scope;
- keep AI/cloud out of scope;
- keep export semantics unchanged.
```
---
## 2. Current baseline
The implemented v13.3 reinsert flow is:
```text
User pastes text into the reinsert area.
User loads or provides a Scrub Key.
Scrub restores mapped placeholders locally.
User can download restored text as TXT.
```
This is a safe baseline because it is deterministic, local and simple. It also avoids prematurely claiming that Scrub can reliably rehydrate every Word or PDF document.
However, the current baseline is only a text-level workflow. It does not yet match the natural workflow of a legal professional who receives or produces AI output as DOCX or PDF.
---
## 3. The first three obvious ideas challenged
### Idea 1 — Keep only pasted-text reinsert
Question:
```text
Is copying AI-output text from a PDF/DOCX and pasting it into Scrub good enough?
```
Decision:
```text
Limit this idea. Keep pasted-text reinsert, but do not treat it as the final product workflow.
```
Critique:
- Usability burden: legal users should not have to manually copy text from a generated document, paste it into Scrub, then manually reconstruct the output.
- Loss of formatting: copy/paste loses headings, tables, numbering, footnotes, page structure and sometimes line breaks.
- Legal-document workflow friction: lawyers and legal support staff often work in Word/PDF outputs, not plain text boxes.
- Risk of incomplete copying: a user may accidentally omit a header, footer, table cell, footnote, exhibit label or final paragraph.
- Poor fit for DOCX/PDF outputs: if the AI result is returned as a Word document or PDF, a plain text reinsert result creates extra manual work and extra error risk.
Why it still matters:
Pasted text is still the safest fallback and should remain available. It is useful for quick AI chat output, email snippets, short summaries and debugging. It should be positioned as the simple path, not the only path.
---
### Idea 2 — Add direct PDF/DOCX reinsert immediately
Question:
```text
Should Scrub immediately support upload of any AI-output PDF or Word document and return a de-anonymized document?
```
Decision:
```text
Reject as an immediate broad implementation. Split DOCX and PDF, and start with DOCX helper work only after TXT helper behavior is stable.
```
Critique:
- DOCX is feasible, but needs careful placeholder replacement across paragraphs, runs, tables, headers and footers.
- Word placeholders may be split across runs. A naïve string replacement can miss placeholders or damage formatting.
- PDF is harder than DOCX because it may require text extraction, layout reconstruction, font handling, coordinates or OCR.
- Scanned PDFs are high-risk because text may not exist as selectable text. OCR would add accuracy, privacy and dependency questions.
- Preserving formatting and metadata hygiene is non-trivial.
- Scrub must not silently claim reliable document restoration if only text extraction/replacement is actually supported.
Recommended interpretation:
Document-level reinsert should be built, but not as one big feature. DOCX should be handled before PDF. PDF should first be investigated as text extraction only, with explicit limitation warnings.
---
### Idea 3 — Add one combined screen for everything
Question:
```text
Should anonymize and de-anonymize live in the same long workflow?
```
Decision:
```text
Reject as the main UX direction. Use explicit modes instead.
```
Critique:
- Cognitive overload: the existing workflow already includes upload, recognition, review, Scrub Key export/import, reinsert and downloads.
- Users may confuse scrub/anonymize with reinsert/de-anonymize.
- The reversible Scrub Key flow has different risks from anonymization. It can restore sensitive values and therefore needs its own mental model.
- Privacy state must be explicit: `opgeschoond`, `omkeerbaar met sleutel`, and `hersteld met originele waarden` are different states.
- A single-scroll Streamlit flow makes it too easy to miss where the user is in the privacy lifecycle.
Recommended interpretation:
The UI should start with a mode choice. The current single-scroll flow can continue temporarily while helpers mature, but the product direction should be two visible modes.
---
## 4. Model architecture perspective
### 4.1 Scrub Key as reversible mapping layer
The Scrub Key is the reversible mapping layer between original values and placeholders.
Conceptually:
```text
original sensitive value <-> placeholder
```
The Scrub Key must remain a separate security object from the scrubbed document. The scrubbed document may be safe enough for AI or external processing after review. The Scrub Key can restore sensitive values and must stay local and protected.
This means Scrub Key support is pseudonymization, not full anonymization.
### 4.2 Text-level reinsert versus document-level reinsert
Text-level reinsert is the simplest architecture:
```text
text + Scrub Key -> restored text + audit summary
```
It is already implemented and should remain the core deterministic operation.
Document-level reinsert should wrap the same core operation rather than create a second replacement engine.
Preferred architecture:
```text
TXT/DOCX/PDF intake
-> extract replaceable text units
-> call deterministic placeholder replacement logic
-> rebuild output where safe
-> return restored output + audit summary
```
The pure text replacement logic should remain authoritative.
### 4.3 DOCX placeholder replacement helper as likely next pure helper
DOCX is the most realistic next document format because Word documents are common in legal drafting and review.
The next DOCX helper should be pure and testable. It should not start in Streamlit UI.
The helper should consider:
- paragraphs;
- tables;
- headers and footers where feasible;
- run-splitting risk;
- preservation of basic formatting;
- output as a new restored DOCX file, not overwrite input;
- audit summary: replaced placeholders, unknown placeholders, missing placeholders, unsupported areas.
A first DOCX helper may explicitly limit scope if needed. It is better to support a smaller reliable DOCX subset than claim full document fidelity.
### 4.4 PDF reinsert as later/high-risk
PDF should not be implemented as full document reinsert yet.
Recommended PDF path:
1. Investigate text-based PDF extraction only.
2. Report whether placeholders are found.
3. Provide restored plain text if reliable enough.
4. Do not reconstruct a restored PDF until a reliability review proves that layout, metadata and scanning limitations are acceptable.
Scanned PDFs and OCR should remain out of scope until explicitly approved.
### 4.5 Separate anonymization and reinsert pipelines
Scrub should maintain two separate pipelines:
```text
Pipeline 1 — Anonymization
Input document/text -> recognition -> review -> scrubbed output -> Scrub Key -> export
Pipeline 2 — Reinsert
AI output / scrubbed output + Scrub Key -> restored output -> audit -> export
```
The pipelines can share the Scrub Key model and deterministic replacement helper, but they should not share UI state in a way that silently mutates review rows or export behavior.
### 4.6 No AI calls inside Scrub
Reinsert must not call AI.
Scrub’s role is:
```text
prepare safe text/documents for AI
restore original values locally after AI use
```
The AI step is external to Scrub. Scrub should never need the user’s AI provider credentials for this workflow.
### 4.7 Local-only processing
All reinsert processing should remain local-first. No Scrub Key, original values or restored output should be sent to cloud services by Scrub.
The Hugging Face demo may still run as a hosted prototype, but the product architecture must be compatible with the long-term desktop/offline direction.
---
## 5. Strategic perspective
### 5.1 Product promise
Scrub’s product promise is:
```text
Sensitive information stays local.
The user remains in control.
The document stays readable.
```
Document-level reinsert supports the third part of that promise. A restored plain text box is not enough when the professional deliverable is a formatted legal document.
### 5.2 Legal-professional workflow
A realistic legal workflow is:
1. A user uploads or pastes a source document.
2. Scrub anonymizes/pseudonymizes the document.
3. The user reviews replacements.
4. The user exports scrubbed content and a Scrub Key.
5. The user uses scrubbed content in an AI tool or external process.
6. The user receives AI output, often as text, DOCX or PDF.
7. The user returns to Scrub to restore original values locally.
8. The user reviews the restored output before sharing or filing.
This makes reinsert a separate product capability, not just a small extra textbox.
### 5.3 Positioning: Anonimiseren, Terugzetten, Controle/Audit
The product should eventually present three clear concepts:
```text
Anonimiseren
Originele waarden terugzetten
Controle / Audit
```
For the next UI direction, the first two should be primary modes:
```text
1. Anonimiseren
2. Originele waarden terugzetten
```
`Controle / Audit` can be a supporting layer inside both modes at first. It does not need to be a separate top-level mode yet.
### 5.4 When document-level reinsert becomes strategically important
Document-level reinsert becomes strategically important when Scrub is used for real legal AI workflows. Professionals will expect to upload an AI-output file and receive a restored file, not manually copy/paste text and rebuild formatting.
However, trust is more important than breadth. A reliable DOCX-first workflow is strategically better than a broad but unreliable DOCX/PDF promise.
---
## 6. Tactical perspective — phased implementation sequence
Recommended sequence:
### Phase A — Keep current text-paste reinsert, improve guidance
Status:
```text
Keep as current baseline.
```
Purpose:
- Keep simple pasted-text reinsert available.
- Clarify that this is best for plain text / AI chat output.
- Explain that restored text may again contain confidential information.
Implementation risk:
- Low.
### Phase B — Add TXT upload/download reinsert
Status:
```text
Recommended next small implementation layer.
```
Purpose:
- Let users upload a `.txt` AI-output file plus a Scrub Key.
- Use the same deterministic reinsert helper.
- Return restored text as `.txt`.
Why before DOCX:
- Very testable.
- No layout complexity.
- Validates document-upload flow without changing reinsert semantics.
### Phase C — Add DOCX reinsert helper, pure helper + tests
Status:
```text
Recommended after TXT helper/path is stable.
```
Purpose:
- Build a helper that opens DOCX, replaces placeholders, and writes a new restored DOCX.
- Keep it outside Streamlit at first.
- Use synthetic DOCX fixtures only.
Required guardrails:
- Do not overwrite uploaded files.
- Report unsupported document parts.
- Preserve basic formatting where feasible.
- Test paragraphs, runs, tables and simple headers/footers if feasible.
- Document limitations clearly.
### Phase D — Add DOCX reinsert UI
Status:
```text
Only after Phase C tests are stable.
```
Purpose:
- Add `.docx` upload in `Originele waarden terugzetten` mode.
- Return restored `.docx` and audit summary.
- Ask for app verification because UI/download behavior changes.
### Phase E — Investigate PDF text extraction only
Status:
```text
Research/contract phase only.
```
Purpose:
- Determine whether text-based PDFs with placeholders can be extracted reliably.
- Report scanned/image-only PDFs as unsupported.
- Avoid OCR initially.
### Phase F — Consider PDF output only after reliability review
Status:
```text
Do not build yet.
```
Purpose:
- Only consider restored PDF output if layout, metadata, text extraction and limitation messaging are reliable enough.
---
## 7. Operational perspective
### 7.1 Testability
All document-level reinsert behavior should start with pure helpers and synthetic tests.
Recommended tests:
- TXT input with placeholders;
- TXT input with unknown placeholders;
- TXT input with missing key placeholders;
- DOCX paragraph replacement;
- DOCX table replacement;
- DOCX split-run placeholder behavior;
- DOCX no-placeholder behavior;
- invalid Scrub Key behavior;
- duplicate placeholder behavior;
- no AI/cloud markers remain true/false as expected.
### 7.2 Helper-first implementation
Do not start by editing the Streamlit UI.
Recommended order:
```text
pure helper -> tests -> specification update -> UI wiring -> app verification
```
This avoids fragile parallel edits to `fix_streamlit_nested_expanders.py`.
### 7.3 Synthetic data only
Use only synthetic Dutch legal placeholders and values. Do not commit real client names, real case data, real BSNs, real addresses or real Scrub Keys.
### 7.4 Metadata risks
Document-level reinsert can reintroduce sensitive values and can also carry document metadata.
DOCX/PDF output should eventually align with document hygiene work:
- remove or warn about metadata;
- avoid hidden content leakage;
- avoid tracked changes surprises;
- avoid comments containing sensitive values.
This review does not implement metadata cleaning. It flags the risk so future document-level work does not silently expand the product promise.
### 7.5 Export/download semantics
Existing scrubbed TXT, CSV, DOCX and PDF downloads must not change.
Reinsert downloads should be separate from anonymized/scrubbed downloads and clearly labelled as restored output.
Suggested naming distinction:
```text
Download opgeschoonde tekst/document
Download herstelde tekst/document
```
### 7.6 App verification requirements
No app verification is required for this WP9 review because no UI behavior changed.
Future UI work must request app verification when:
- mode choice is added;
- TXT upload is added;
- DOCX restored download is added;
- any export/download UI is changed.
### 7.7 Avoiding parallel edits
Future UI work should not run in parallel with other work that edits:
- `fix_streamlit_nested_expanders.py`;
- `presidio_streamlit.py`;
- review table flow;
- export/download flow;
- shared session state.
### 7.8 No cloud processing
Document reinsert must not introduce cloud processing. OCR, external converters or AI-based document reconstruction must not be added without explicit approval.
### 7.9 Audit summary requirements
Every reinsert result should include an audit summary:
- file type;
- mapping item count;
- active mapping count;
- replacement count;
- unknown placeholders;
- missing placeholders;
- duplicate placeholders;
- validation issues;
- local-only status;
- AI processing status;
- cloud processing status;
- unsupported document areas if applicable.
---
## 8. Visual / UX perspective
### 8.1 Interface options compared
#### Option A — Tabs
Pros:
- Simple in Streamlit.
- Clear separation between `Anonimiseren` and `Originele waarden terugzetten`.
- Lower implementation complexity than separate pages.
Cons:
- Tabs can still share too much session state if not carefully handled.
- Users may not notice mode-level warnings if tabs are visually too light.
Assessment:
```text
Good near-term direction.
```
#### Option B — Landing page with two large choices
Pros:
- Best mental model.
- Strong privacy-state separation.
- Works well for future desktop product.
- Reduces confusion between scrubbing and restoring.
Cons:
- Larger UI change.
- Needs careful implementation in current patch-based Streamlit setup.
Assessment:
```text
Best target direction, but implement carefully after helper foundation.
```
#### Option C — Accordion/stepper
Pros:
- Fits current single-page Streamlit style.
- Can show workflow sequence.
Cons:
- Still risks becoming one long mixed workflow.
- Reinsert may feel like a sub-step of anonymization rather than a separate capability.
Assessment:
```text
Acceptable temporary bridge, not the ideal long-term structure.
```
#### Option D — Separate pages
Pros:
- Strong separation.
- Good for mature product.
Cons:
- More refactor work.
- Risky while UI is still patch-based.
Assessment:
```text
Good later, not the next small step.
```
#### Option E — Current single-scroll Streamlit flow
Pros:
- Already implemented.
- Lowest short-term risk.
Cons:
- Poor scalability.
- Hard to explain privacy state.
- Reinsert is buried after export/Scrub Key controls.
- Does not clearly answer: am I anonymizing or restoring?
Assessment:
```text
Keep temporarily, but do not continue expanding it indefinitely.
```
### 8.2 Recommended visual direction
Yes, the UI should start with a mode choice.
Recommended target:
```text
Welkom bij Scrub
Wat wilt u doen?
[Anonimiseren]
Maak tekst of documenten veilig voor AI of delen.
[Originele waarden terugzetten]
Gebruik een Scrub Key om placeholders lokaal terug te zetten in AI-output of scrubbed output.
```
Near-term Streamlit implementation can be tabs:
```text
Tab 1: Anonimiseren
Tab 2: Originele waarden terugzetten
```
Longer-term product implementation can become a landing page or separate pages.
### 8.3 Ideal user journey — Anonimiseren
1. User chooses `Anonimiseren`.
2. User uploads DOCX/PDF/TXT or pastes text.
3. Scrub detects sensitive values locally.
4. User reviews replacements.
5. User downloads scrubbed output.
6. User downloads Scrub Key if reversible workflow is needed.
7. UI warns that the Scrub Key must remain protected.
### 8.4 Ideal user journey — Originele waarden terugzetten
1. User chooses `Originele waarden terugzetten`.
2. User uploads or pastes a Scrub Key.
3. User provides AI-output or scrubbed output:
- paste text now;
- TXT upload next;
- DOCX upload later;
- PDF later only if supported.
4. Scrub validates the key and detects placeholders.
5. User clicks an explicit restore action.
6. Scrub restores original values locally.
7. UI shows restored output and audit summary.
8. User downloads restored output.
9. UI warns that restored output may again contain confidential information.
---
## 9. Required recommendation answers
### 1. Is pasted-text reinsert enough for now?
Yes, it is enough as the current safe baseline and fallback.
No, it is not enough as the final legal-document workflow.
### 2. Should document upload reinsert be supported?
Yes, but phased and helper-first.
Start with TXT, then DOCX, then only investigate PDF.
### 3. Should DOCX be prioritized before PDF?
Yes.
DOCX is closer to normal legal drafting workflows and is more feasible to handle with controlled helper logic. PDF should remain later and high-risk.
### 4. Should anonymization and de-anonymization be separated visually?
Yes.
The UI should move toward a two-mode structure:
```text
1. Anonimiseren
2. Originele waarden terugzetten
```
### 5. What should be the next implementation workpackage?
```text
WP10 — v13.4 TXT/DOCX reinsert foundation helper and tests
```
Recommended scope:
- no UI first;
- add pure TXT document-level reinsert helper behavior;
- add or design DOCX placeholder replacement helper;
- add synthetic regression tests;
- keep PDF out of scope;
- keep AI/cloud out of scope;
- do not change existing export/download behavior.
### 6. What should explicitly not be built yet?
Do not build yet:
- full PDF reinsert;
- OCR for scanned PDFs;
- AI-based document reconstruction;
- cloud conversion;
- automatic AI calls;
- server-side Scrub Key storage;
- automatic document rehydration without explicit user action;
- a broad UI refactor in parallel with other UI work;
- metadata-clean document promises until document hygiene work is explicitly scoped;
- changes to existing scrubbed export/download semantics.
---
## 10. Final recommendation
Scrub should keep pasted-text reinsert, but treat it as the simple/manual path.
The product should move toward a two-mode UX:
```text
Anonimiseren
Originele waarden terugzetten
```
Document-level reinsert should be supported, because legal professionals work with documents, not only copied text. But the implementation should be cautious:
```text
TXT first.
DOCX second.
PDF later only after reliability review.
```
The next workpackage should build the helper/test foundation, not immediately change the UI.
This keeps the product aligned with the core promise:
```text
local-first
user control
readable documents
safe auditability
```
|