Spaces:
Sleeping
Sleeping
File size: 344,603 Bytes
7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7767131 28c3dd4 7114997 b0f2d27 d2f21cc 7114997 1c556e4 7114997 53be278 7114997 4df3f85 7114997 c1f2154 d2f21cc 7114997 1c556e4 d2f21cc 4398dcc d2f21cc 1c556e4 5723f0c 352429d a35ed26 352429d 4ff6d8c 352429d 6380677 352429d 4ff6d8c 352429d 2115cbd d0928d0 b24b388 f9fadef 7548667 f9fadef 352429d f9fadef 352429d 41ffe3f 7548667 2f1755c 352429d cd7c272 352429d 4ff6d8c 352429d c14d31b 352429d 4df3f85 352429d 4398dcc 1c556e4 352429d 4df3f85 352429d 4df3f85 352429d a35ed26 352429d 6795f79 352429d a35ed26 352429d a35ed26 352429d a35ed26 352429d 1dfbe74 5dd3825 2ea4810 5dd3825 352429d 6795f79 352429d 1c556e4 c4c6dbf 1c556e4 7dcd294 6ad4bf5 352429d 6ad4bf5 7dcd294 74e793e 7dcd294 1c556e4 17874df 6795f79 1c556e4 17874df 1c556e4 7dcd294 1c556e4 69bfb64 1c556e4 6380677 1c556e4 a35ed26 1c556e4 7dcd294 1c556e4 d6c2569 1c556e4 d6c2569 e7321bb 1c556e4 7dcd294 d6c2569 7dcd294 1c556e4 6380677 1c556e4 6795f79 1c556e4 17874df 6795f79 17874df 6795f79 17874df 6795f79 17874df 1c556e4 17874df 1c556e4 17874df 1c556e4 a35ed26 1c556e4 6bd099c 1c556e4 e7321bb 69bfb64 1c556e4 4ff6d8c 69bfb64 1c556e4 6380677 1c556e4 4ff6d8c 1c556e4 17874df 1c556e4 17874df 1c556e4 7dcd294 d6c2569 1c556e4 7dcd294 1c556e4 7dcd294 1c556e4 4ff6d8c a35ed26 4ff6d8c 7114997 1c556e4 c1f2154 352429d c1f2154 352429d c1f2154 352429d c1f2154 7767131 7114997 37e5c0a 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 4398dcc 7114997 6380677 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 a35ed26 1c556e4 4ff6d8c 1c556e4 4ff6d8c 1c556e4 7767131 c14d31b 7767131 1c556e4 6380677 7114997 6380677 1c556e4 7114997 1c556e4 7114997 1c556e4 4ff6d8c a35ed26 4ff6d8c 7767131 c14d31b 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 352429d 7114997 a300066 7114997 1c556e4 7114997 352429d 7114997 a300066 7114997 1c556e4 f9fadef cd7c272 f9fadef 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 352429d d2f21cc 352429d d2f21cc 352429d 1c556e4 7114997 d2f21cc 7767131 1c556e4 d2f21cc 4398dcc 1c556e4 7114997 1c556e4 d2f21cc 1c556e4 d2f21cc 1c556e4 4398dcc d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc 7767131 d2f21cc 1c556e4 d2f21cc 1c556e4 d2f21cc 1c556e4 4398dcc 1c556e4 c14d31b d0928d0 1c556e4 d2f21cc 1c556e4 d2f21cc 4398dcc d2f21cc 1c556e4 d2f21cc 4398dcc d2f21cc 1c556e4 d2f21cc 1c556e4 d2f21cc 1c556e4 7114997 d2f21cc 7114997 1c556e4 7767131 7114997 1c556e4 7114997 1c556e4 d2f21cc d0928d0 d2f21cc c14d31b d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc c14d31b d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc d0928d0 d2f21cc c14d31b d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc 4398dcc d2f21cc 1c556e4 37e5c0a 1c556e4 7114997 1c556e4 37e5c0a 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 4398dcc 7114997 4398dcc 7114997 4398dcc 7114997 4398dcc 7114997 4398dcc 7114997 4398dcc c4c6dbf 7114997 1c556e4 7114997 6380677 7114997 1c556e4 7114997 4a328b5 8d6bce6 8249518 4a328b5 7114997 4a328b5 7114997 6795f79 f95e453 7114997 f9fadef 7114997 f9fadef 7114997 cb4fa75 7114997 1c556e4 7114997 1c556e4 7114997 4df3f85 1c556e4 4df3f85 1c556e4 4df3f85 1c556e4 4df3f85 7114997 1c556e4 352429d 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 8249518 1c556e4 e5944f2 1c556e4 7114997 1c556e4 7767131 352429d 7767131 89de9a7 7767131 89de9a7 7767131 1c556e4 00e8935 1c556e4 7114997 6733d00 7114997 6733d00 352429d 00e8935 7114997 6733d00 7114997 1c556e4 7114997 6733d00 7114997 4398dcc 7114997 7767131 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 8e6bae5 7114997 1c556e4 7114997 8e6bae5 7114997 8e6bae5 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 4398dcc 7114997 4398dcc 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 4398dcc 7114997 4398dcc 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 da35fae 1c556e4 7114997 1c556e4 4ff6d8c 1c556e4 88e7f70 6380677 1c556e4 6380677 1c556e4 4ff6d8c a35ed26 1c556e4 7114997 1c556e4 da35fae 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 c4c6dbf 7114997 1c556e4 7114997 1c556e4 7114997 7a86ea0 c4c6dbf 7114997 9b0e46e 7a86ea0 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 c14d31b 7114997 c14d31b 7a86ea0 6795f79 7a86ea0 6795f79 7a86ea0 6795f79 7a86ea0 6795f79 7a86ea0 6795f79 7a86ea0 6795f79 7a86ea0 6795f79 214f477 7a86ea0 6795f79 7114997 1c556e4 4ff6d8c 1c556e4 7114997 1c556e4 7114997 352429d 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 352429d 1c556e4 352429d 7114997 1c556e4 4398dcc 7114997 1c556e4 4398dcc d2f21cc 1c556e4 4398dcc 7114997 ae472e6 7114997 ae472e6 7114997 352429d 7114997 1c556e4 4398dcc 7114997 1c556e4 4398dcc 7114997 4398dcc 7114997 4398dcc 7114997 1c556e4 4398dcc 7114997 4398dcc 6795f79 7114997 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 1c556e4 7114997 1c556e4 2849dd9 1c556e4 7114997 1c556e4 2849dd9 da35fae 1c556e4 7114997 1c556e4 da35fae 1c556e4 da35fae a35ed26 1c556e4 da35fae 1c556e4 2849dd9 1c556e4 a35ed26 1c556e4 a35ed26 2849dd9 a35ed26 da35fae a35ed26 4398dcc da35fae 7114997 da35fae 7114997 da35fae 7114997 da35fae 7114997 da35fae 7114997 da35fae 4398dcc da35fae 1293350 da35fae 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc 6795f79 4398dcc da35fae 1293350 4ff6d8c 4398dcc da35fae 1293350 da35fae 7114997 da35fae 7114997 da35fae 7114997 da35fae 7114997 da35fae 7114997 da35fae 7114997 da35fae 7114997 4398dcc 7114997 1c556e4 7114997 1c556e4 4398dcc 1c556e4 6380677 7114997 1c556e4 7114997 6380677 1c556e4 7114997 1c556e4 7114997 1c556e4 6380677 7114997 1c556e4 f9fadef 7114997 1c556e4 5c07859 4096dd7 f28d680 7114997 f9fadef da35fae 7114997 1c556e4 7114997 1c556e4 7114997 74e793e 6380677 7114997 74e793e f9fadef 74e793e f9fadef 74e793e 7114997 74e793e 6380677 74e793e f9fadef 7114997 f9fadef 74e793e 7114997 74e793e 7114997 74e793e 7114997 74e793e 1c556e4 7114997 74e793e 7114997 74e793e 6380677 74e793e d0928d0 74e793e 1c556e4 d0928d0 6380677 d0928d0 6380677 d0928d0 4096dd7 74e793e 4096dd7 74e793e d0928d0 74e793e 6380677 d0928d0 74e793e d0928d0 4096dd7 74e793e 4096dd7 74e793e 4096dd7 74e793e 4ff6d8c 74e793e 4ff6d8c 74e793e 4ff6d8c 74e793e 6380677 d0928d0 74e793e 1c556e4 6ad4bf5 6380677 6ad4bf5 4398dcc 6380677 4398dcc 7114997 6380677 1c556e4 7114997 6380677 7114997 c4c6dbf 6380677 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 6380677 7114997 4df3f85 7114997 1c556e4 6380677 1c556e4 7767131 1c556e4 7767131 d0928d0 1c556e4 7767131 1c556e4 7114997 6380677 4398dcc 7114997 cb4fa75 c14d31b 6ad4bf5 7114997 1c556e4 6380677 1c556e4 d0928d0 1c556e4 7114997 1c556e4 7114997 cb4fa75 7114997 1c556e4 c14d31b 1c556e4 da35fae 4398dcc 1c556e4 6380677 7767131 c14d31b 7767131 cb4fa75 7767131 4ff6d8c 7114997 7767131 7114997 1c556e4 4ff6d8c 1c556e4 6380677 1c556e4 4ff6d8c a35ed26 1c556e4 da35fae 7114997 d9c662a 7114997 6795f79 7114997 6795f79 7114997 6795f79 7114997 6795f79 7114997 d9c662a 7114997 6795f79 d9c662a 7114997 d9c662a 7114997 d9c662a 6795f79 d9c662a 7114997 d9c662a 6795f79 d9c662a 7114997 d9c662a 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7767131 429b384 7767131 429b384 7767131 877f07a 7767131 7114997 1c556e4 7767131 877f07a 7767131 7114997 1c556e4 877f07a 1c556e4 877f07a 1c556e4 877f07a 6380677 1c556e4 877f07a 1c556e4 877f07a 7767131 877f07a 7767131 877f07a 7767131 877f07a 7767131 1c556e4 7767131 1c556e4 7114997 d9c662a da35fae d9c662a da35fae d9c662a da35fae d9c662a da35fae 7a86ea0 da35fae d9c662a 7a86ea0 d9c662a da35fae d9c662a da35fae d9c662a da35fae 5c07859 da35fae d9c662a da35fae d9c662a da35fae 7a86ea0 da35fae d9c662a da35fae 7a86ea0 da35fae d9c662a da35fae d9c662a da35fae d9c662a da35fae d9c662a da35fae d9c662a da35fae d9c662a da35fae d9c662a da35fae d9c662a da35fae d9c662a da35fae d9c662a 4ff6d8c da35fae 7114997 da35fae 7767131 da35fae d9c662a 7114997 da35fae 7767131 d9c662a da35fae 4ff6d8c 7114997 d9c662a 7114997 1c556e4 352429d 1c556e4 4398dcc 1c556e4 17874df 1c556e4 4398dcc 1c556e4 4398dcc 1c556e4 17874df 1c556e4 17874df 1c556e4 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 1c556e4 17874df 1c556e4 17874df 1c556e4 17874df 1c556e4 17874df 1c556e4 17874df 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 17874df 1c556e4 4398dcc 17874df 4398dcc 17874df 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 17874df 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 1c556e4 4398dcc 17874df 1c556e4 4398dcc 17874df 1c556e4 17874df 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 6795f79 7114997 6795f79 1c556e4 7114997 1c556e4 6795f79 1c556e4 7114997 1c556e4 c4c6dbf 1c556e4 429b384 6795f79 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 2849dd9 1c556e4 2849dd9 1c556e4 17874df 1c556e4 17874df 1c556e4 17874df 2849dd9 4398dcc 2849dd9 1c556e4 17874df 1c556e4 17874df 1c556e4 17874df 7114997 8e6bae5 1c556e4 7114997 da35fae 7114997 4398dcc 7114997 4398dcc 7114997 da35fae 7114997 1c556e4 c4c6dbf 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 4398dcc 7114997 1c556e4 7114997 4398dcc 7114997 1c556e4 7114997 1c556e4 4398dcc 17874df 4398dcc 7114997 1c556e4 7114997 1c556e4 4398dcc 7114997 4398dcc 7114997 1c556e4 7114997 4398dcc 7114997 1c556e4 7114997 1c556e4 4398dcc 7114997 1c556e4 7114997 f9fadef 7114997 1293350 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 4398dcc 325ea48 7114997 4398dcc 325ea48 4398dcc 7114997 325ea48 7114997 4398dcc 7114997 325ea48 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 7767131 7114997 1c556e4 b0f2d27 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7767131 1c556e4 7767131 1c556e4 7114997 1c556e4 7114997 ac04e52 4ff6d8c 1c556e4 7114997 7767131 1c556e4 c3c11dd 7767131 1c556e4 7114997 877f07a 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 877f07a 7114997 1c556e4 877f07a 1c556e4 877f07a 1c556e4 d2f21cc f858159 1c556e4 d2f21cc 541d297 d2f21cc 1c556e4 877f07a 1c556e4 877f07a 1c556e4 877f07a 468b490 1c556e4 877f07a 1c556e4 c41f4e6 325ea48 4ff6d8c cd7c272 4ff6d8c 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 7767131 7114997 76503dd 325ea48 cd7c272 4ff6d8c cd7c272 4ff6d8c ac04e52 7114997 ac04e52 1c556e4 a0db324 1c556e4 7114997 c1f2154 ac04e52 c1f2154 ac04e52 c1f2154 1c556e4 b0f2d27 7767131 877f07a c1f2154 ac04e52 7114997 1c556e4 7114997 1c556e4 7767131 1c556e4 b0f2d27 a35ed26 1c556e4 7114997 1c556e4 ac04e52 7767131 7114997 1c556e4 76503dd 1c556e4 cd7c272 1c556e4 4ff6d8c 1c556e4 b0f2d27 877f07a cd7c272 877f07a 7114997 ac04e52 7114997 ac04e52 ae472e6 7114997 1c556e4 b0f2d27 1c556e4 b0f2d27 7767131 877f07a 7767131 4ff6d8c 7114997 ac04e52 7114997 7a86ea0 14cd2f3 7a86ea0 ac04e52 d04b5a2 7a86ea0 7114997 ac04e52 d04b5a2 7a86ea0 7114997 7a86ea0 7114997 ac04e52 7114997 ac04e52 1c556e4 b0f2d27 1c556e4 b0f2d27 1c556e4 37e5c0a 1c556e4 b0f2d27 1c556e4 b0f2d27 1c556e4 b0f2d27 1c556e4 b0f2d27 1c556e4 7767131 1c556e4 b0f2d27 1c556e4 b0f2d27 1c556e4 b0f2d27 1c556e4 7114997 1c556e4 7767131 1c556e4 7114997 1c556e4 7114997 b0f2d27 1c556e4 6ad4bf5 b0f2d27 7767131 877f07a 7767131 cd7c272 7767131 4ff6d8c cd7c272 4ff6d8c 877f07a cd7c272 4ff6d8c cd7c272 4ff6d8c ef0a96f 1c556e4 ac04e52 7114997 1c556e4 7767131 877f07a 4ff6d8c 7114997 ac04e52 7114997 1c556e4 7767131 877f07a 4ff6d8c 7114997 ac04e52 1c556e4 ac04e52 7767131 4ff6d8c 7767131 877f07a 7767131 877f07a 7767131 4ff6d8c 7767131 4ff6d8c 7767131 4ff6d8c 877f07a 4ff6d8c 7767131 877f07a 7767131 b0f2d27 7114997 4ff6d8c 7114997 4ff6d8c 1c556e4 877f07a 4ff6d8c ef0a96f 7767131 4ff6d8c 7767131 ef0a96f 7114997 ac04e52 7114997 b0f2d27 7767131 877f07a 4ff6d8c 7767131 7114997 ac04e52 7114997 509700a 7114997 1c556e4 7767131 00e8935 7aa364f 7114997 946d18a 662f837 946d18a 662f837 946d18a 662f837 946d18a 662f837 946d18a 662f837 946d18a 877f07a 662f837 946d18a 662f837 877f07a 662f837 b28b587 662f837 b28b587 946d18a 662f837 946d18a 662f837 946d18a 1c556e4 946d18a 662f837 946d18a 662f837 946d18a 662f837 946d18a 662f837 1c556e4 662f837 1c556e4 877f07a 7114997 1c556e4 325ea48 6380677 325ea48 4398dcc 325ea48 4398dcc 325ea48 4398dcc 7114997 1c556e4 7114997 7767131 352429d 7767131 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7114997 1c556e4 7767131 7114997 53be278 a069141 7114997 325ea48 10d94ab 1c556e4 7114997 6cff309 7114997 | 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 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 | """
Privacy Risk Assessment UI for LLM Interactions (v9 - Dual DP Mechanisms)
Key improvements over v8:
* Two complementary Differential Privacy mechanisms:
1. Retrieval DP (Exponential Mechanism on document scores) β existing,
sensitivity now computed empirically from actual utility range rather
than a naive hardcoded constant (Dwork & Roth 2014; Koga et al. 2024).
2. Input DP (RANTEXT-inspired Exponential Mechanism on word embeddings) β
new; perturbs content words in the user's message before the LLM sees
them, satisfying Ξ΅-Local DP per token (Tong et al. 2025, InferDPT).
* User sees original message in chat; a DP badge with hover tooltip on each
user bubble transparently communicates how many words were perturbed and at
what privacy level β without replacing visible text.
* Perturbed text logged to CSV as user_prompt_perturbed for research analysis.
URL parameters:
model β LLM model name
rag β 0 or 1 (enable External Data linkage)
epsilon β float (DP privacy budget, shared by both mechanisms)
show_risk β 0 or 1 (risk panel)
show_tips β 0 or 1 (PII tooltips)
show_rag_highlights β 0 or 1 (External Data link highlights)
show_pii_highlights β 0 or 1 (PII highlights)
show_settings β 0 or 1 (settings panel)
demo β 0 or 1 (demo mode, NO LLM calls)
enable_social_scraping β 0 or 1 (enable social media scraping - experimental)
token β str (access token for the session)
show_dp β int: when to show the Privacy Settings panel
0 = never
1 = after each conversation turn (default)
2 = at the beginning only (before first turn)
3 = only when the user presses "End conversation"
show_infr_attr_card β int: when to show the "How the AI sees you" card
0 = never
1 = after each conversation turn (default)
2 = only when the user presses "End conversation"
"""
import copy
import pandas as pd
import json
import math
import os
import pickle
import random
import re
import time
from dataclasses import dataclass, field
from enum import Enum
from collections import defaultdict
import threading
import csv
from concurrent.futures import ThreadPoolExecutor, as_completed
import logging
import sys
import datetime
import gradio as gr
from openai import OpenAI
from langchain_community.embeddings import HuggingFaceEmbeddings
from create_rag_retriever import load_retriever_components, DPRetriever, HybridRetriever
import numpy as np
import hashlib
from langchain_core.documents import Document
# Import Apify social media scraper
from social_scrape_via_apify import (
get_twitter_user_posts,
get_facebook_posts,
get_facebook_page_info,
get_linkedin_posts,
get_web_search_results
)
# Configure logging to write to BOTH file and console
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s | %(levelname)s | %(message)s',
datefmt='%H:%M:%S',
handlers=[
# logging.FileHandler(log_filename, mode='w', encoding='utf-8'),
logging.StreamHandler(sys.stdout) # Try console too
]
)
logger = logging.getLogger(__name__)
logger.info("="*60)
# ============================================================
# CONFIGURATION
# All tuneable constants are grouped here for easy maintenance.
# ============================================================
PROLIFIC_ID_PATTERN_REGEX = r'[A-Za-z0-9]{24}'
# ββ Access Control βββββββββββββββββββββββββββββββββββββββββββ
# Valid access tokens (store in HF Secrets in production).
# Each entry maps a token (read from an env var) to metadata.
VALID_ACCESS_TOKENS = {
os.environ.get("VALID_ACCESS_TOKEN_1", None): {"name": "", "expires": "2099-12-31"},
os.environ.get("VALID_ACCESS_TOKEN_2", None): {"name": "", "expires": "2099-12-31"},
os.environ.get("VALID_ACCESS_TOKEN_3", None): {"name": "", "expires": "2099-12-31"},
}
# ββ Logging & HF Hub βββββββββββββββββββββββββββββββββββββββββ
INTERACTION_LOG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "interaction_log.csv")
HF_LOG_REPO_ID = "michael2222222/app-logs" # HF dataset repo name
HF_LOG_REPO_TYPE = "dataset"
# Column headers for the per-participant CSV log file.
LOG_COLUMNS = [
"timestamp",
"session_source",
"turn_number",
"demo_mode",
"scenario_mode",
"persona_attributes",
"model",
"epsilon",
"rag_enabled",
"show_risk",
"show_rag_highlights",
"show_tips",
"show_pii_highlights",
"show_settings",
"show_dp",
"show_infr_attr_card",
"show_social_scraping",
"show_upload_data",
"rag_corpus_path",
"access_token",
"social_scraping_enabled",
"corpus_source",
"uploaded_file_path",
"uploaded_file_preview",
"user_prompt",
"user_prompt_perturbed",
"num_input_dp_substitutions",
"user_prompt_length",
"user_perturbed_prompt_length",
"llm_response",
"llm_response_length",
"risk_score",
"rag_user_count",
"rag_linkages_user",
"rag_llm_count",
"rag_linkages_llm",
"pii_user_count",
"pii_detected_user",
"pii_user_perturbed_count", # β ADD
"pii_detected_user_perturbed", # β ADD
"pii_llm_count",
"pii_detected_llm",
"inference_warning_shown",
"inference_panel_updated",
"num_attributes_changed",
"inference_lifts",
"inferential_score",
"inferential_score_breakdown",
"scraped_docs",
"scraped_social_summary",
]
# ββ LLM Models βββββββββββββββββββββββββββββββββββββββββββββββ
# Uncomment / comment entries to enable or disable models.
MODEL_CONFIGS = [
# ("together", "meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8", "Llama-4"),
# ("together", "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", "Llama-3.1"),
# ("openai", "gpt-4o", "GPT-4o"),
# ("openai", "gpt-5-mini-2025-08-07", "GPT-5-mini"),
# ("together", "openai/gpt-oss-120b", "GPT-OSS"),
# ("together", "meta-llama/Llama-3.3-70B-Instruct-Turbo", "Llama-3.3-70B"),
# ("together", "deepseek-ai/DeepSeek-V3.1", "DeepSeek-V3.1"),
("gemini", "gemini-2.5-flash-lite", "Gemini-2.5-Flash"),
]
DEFAULT_MODEL_PROVIDER = MODEL_CONFIGS[0][0]
DEFAULT_MODEL_ID = MODEL_CONFIGS[0][1]
DEFAULT_MODEL_NAME = MODEL_CONFIGS[0][2]
# ββ LLM Inference Parameters βββββββββββββββββββββββββββββββββ
MAX_TOKENS_PII_DETECTION = 1400
MAX_TOKENS_LLM_RESPONSE_GENERATION = 260
MAX_TOKENS_INFER_ATTRIBUTES = 1400
TEMPERATURE_LLM_RESPONSE_GENERATION = 0.3
TEMPERATURE_INTERNAL_TASKS = 0
# ββ RAG & Retrieval ββββββββββββββββββββββββββββββββββββββββββ
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
DEFAULT_RETRIEVAL_PICKLE_PATH = "./faiss_persona_sarah_chen_retriever_components.pkl" #None
# Per-scenario system corpus pickle paths.
# Set a value to None to fall back to the default RETRIEVAL_PICKLE_PATH.
SCENARIO_RETRIEVER_PATHS = {
"real": DEFAULT_RETRIEVAL_PICKLE_PATH,
# "persona": "./faiss_panorama_retriever_components.pkl",
"persona2": "./faiss_persona_sarah_chen_retriever_components.pkl",
# Add more scenarios here following the same pattern.
}
# Base directory used for relative file paths (e.g. fallback tweet JSON files).
_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# Per-scenario fallback tweet data (used when live scraping fails / returns empty).
# Each scenario_mode maps to its own JSON file of fallback tweets.
SCENARIO_FALLBACK_TWEET_PATHS = {
"real": None, # no fallback for free-style
"persona": None,
"persona2": os.path.join(_BASE_DIR, "sarah_chen_tweets_scraped.json"),
# Add more scenarios here following the same pattern.
}
# Default fallback tweet dataset (empty; populated per-scenario at runtime).
FALLBACK_TWEET_DATA = []
RAG_K = 50
RAG_FETCH_K = 10000
RAG_LINKAGE_STORED_EXCERPT_LENGTH = 500
ATTR_INFERENCE_EVIDENCE_STORED_EXCERPT_LENGTH = 500
# ββ RAG Linkage Similarity Thresholds ββββββββββββββββββββββββ
ROUGE_L_THRESHOLD = 0.16 # Minimum ROUGE-L score (0.0-1.0)
COSINE_SIM_THRESHOLD = 0.16 # Minimum cosine similarity (0.0-1.0)
MIN_COMBINED_SCORE = 0.2 # Minimum combined similarity score
ROUGE_WEIGHT = 0.3 # Weight for ROUGE-L in combined score
COSINE_WEIGHT = 0.7 # Weight for cosine similarity in combined score
MIN_NGRAM_LENGTH = 4 # Minimum characters for n-gram matching
MIN_NGRAM_WORDS = 4 # Minimum words in a phrase to highlight
MAX_NGRAM_WORDS = 7 # Maximum words in a phrase to highlight
RAG_MIN_SIMILARITY_THRESHOLD = 0.25 # baseline threshold when DP is off
RAG_DP_THRESHOLD_SCALE = 1.5 # how fast the threshold rises as Ξ΅ falls
# ββ Privacy / Differential Privacy βββββββββββββββββββββββββββ
MAX_POSSIBLE_EPS = 100.0
INFERENCE_LIFT_THRESHOLD = 0
DP_FLOOR = 0.25 # minimum retained fraction even under maximum perturbation
# ββ Tunable constants βββββββββββββββββββββββββββββββββββββββββββββββββββββ
INFER_MAX = 55.0 # hard cap on inference contribution
INFER_SCALE = 4.0 # controls how fast the curve saturates
PII_MAX = 40.0 # raised cap to accommodate new baseline
PII_BASE = 15.0 # flat baseline added whenever β₯1 PII is detected
PII_PER_HIT = 4.0 # points per detected PII entity (on top of baseline)
RAG_MAX = 15.0 # cap on RAG-linkage contribution
RAG_PER_HIT = 0.75 # points per RAG linkage
# ββ UI & App Defaults βββββββββββββββββββββββββββββββββββββββββ
DEFAULT_USER_TEXT = (
"Hi, I am Sarah Chen. I want to take a more preventive approach to my health. "
"What routine tests or screenings should I consider that I could afford, and "
"what is the nearest location where I can undergo these tests?"
)
# Human-readable label for documents retrieved from the default background corpus.
DEFAULT_CORPUS_SOURCE_LABEL = "From Social Profiles (Internet)"
# ββ Locale & Attribute Mappings βββββββββββββββββββββββββββββββ
# Note: ATTRIBUTE_VALUES_MAP references LOCALE_TO_LOCATION and must come after it.
# PII_TYPE_TO_CATEGORY and PII_COLORS are defined later (in SECTION 1 / SECTION 2)
# because they depend on the PIICategory enum.
LOCALE_TO_LOCATION = {
"en_PH": "Philippines",
"en_CA": "Canada",
"en_US": "United States",
"en_IE": "Ireland",
"en_NZ": "New Zealand",
"en_IN": "India",
"en_AU": "Australia",
"en_GB": "United Kingdom",
"en_IL": "Israel",
"en_DE": "Germany",
"en_IT": "Italy",
"en_FR": "France",
}
SENSITIVE_ATTRIBUTES = ["Age bin", "Gender", "Marital Status", "Finance Status", "Education", "Locale"]
ATTRIBUTE_VALUES_MAP = {
"Gender": ["Female", "Male"],
"Age bin": ["0-17", "18-29", "30-44", "45-59", "60+"],
"Marital Status": ["Single", "Married", "Divorced", "Widowed"],
"Finance Status": ["Low", "Medium", "High"],
"Locale": [LOCALE_TO_LOCATION["en_PH"], LOCALE_TO_LOCATION["en_CA"], LOCALE_TO_LOCATION["en_US"],
LOCALE_TO_LOCATION["en_IE"], LOCALE_TO_LOCATION["en_NZ"], LOCALE_TO_LOCATION["en_IN"],
LOCALE_TO_LOCATION["en_AU"], LOCALE_TO_LOCATION["en_GB"], LOCALE_TO_LOCATION["en_IL"],
LOCALE_TO_LOCATION["en_DE"], LOCALE_TO_LOCATION["en_IT"], LOCALE_TO_LOCATION["en_FR"]],
"Education": ["High School", "Bachelor's", "Master's", "PhD"]
}
# ββ Population Priors for Inferential Privacy ββββββββββββββββ
# Baseline population probabilities for each attribute value, used as the
# denominator in the inferential-privacy lift calculation. The lift measures
# how much the system's available information (message text and/or RAG
# context) raises the probability of inferring an attribute above this
# base rate.
#
# Values within each attribute should sum to β1.0. Calibrate these to
# the study's participant pool or the general population.
POPULATION_PRIORS = {
"Gender": {
"Female": 0.50,
"Male": 0.50,
},
"Age bin": {
"0-17": 0.15,
"18-29": 0.15,
"30-44": 0.38,
"45-59": 0.15,
"60+": 0.15,
},
"Marital Status": {
"Single": 0.39,
"Married": 0.51,
"Divorced": 0.05,
"Widowed": 0.05,
},
"Finance Status": {
"Low": 0.23,
"Medium": 0.53,
"High": 0.23,
},
"Education": {
"High School": 0.19,
"Bachelor's": 0.43,
"Master's": 0.19,
"PhD": 0.19,
},
"Locale": {
"Philippines": 0.09,
"Canada": 0.05,
"United States": 0.40,
"Ireland": 0.05,
"New Zealand": 0.05,
"India": 0.05,
"Australia": 0.05,
"United Kingdom": 0.05,
"Israel": 0.05,
"Germany": 0.05,
"Italy": 0.05,
"France": 0.05,
},
}
# Steepness parameter for the log-based inferential privacy saturation
# function: f(x; k) = kΒ·ln(1+x) / (1 + kΒ·ln(1+x)).
# Higher k β the curve reaches closer to 1 faster for the same lift.
INFERENTIAL_SCORE_STEEPNESS_K = 3.0
# ββ Demo Prompts ββββββββββββββββββββββββββββββββββββββββββββββ
DATASET_PROMPTS_PANORAMA = {
"health": (
"Hi, I am {name}. I want to take a more preventive approach to my "
"health. What routine tests or screenings should I consider that I "
"could afford, and what is the nearest location where I can undergo "
"these tests?"
),
"job": (
"I am {name}. I am searching for new job. Do you have any "
"suggestions that would work for me?"
),
"general": (
"Hi, I'm {name}. Can you tell me about hobbies I might be "
"interested?"
),
}
# ββ Personas ββββββββββββββββββββββββββββββββββββββββββββββββββ
# "real" means the participant uses their own profile (free-style).
# Each "personaX" entry contains the ground-truth attributes used in the study
# and a placeholder prompt shown in the text box when the app loads.
PERSONAS = {
"real": {
"name": "",
"description": None, # no placeholder injected
"attributes": {},
},
"persona1": {
"name": "Raymond Phillips",
"description": (
"You are Raymond Phillips, a 52-year-old widowed male from the Philippines. "
"You work as a fisherman and have a low income. "
"Write your messages as Raymond would."
),
"attributes": {
"Gender": "Male",
"Age bin": "45-59",
"Marital Status": "Widowed",
"Finance Status": "Low",
"Locale": "Philippines",
},
},
"persona2": {
"name": "Sarah Chen",
"description": (
# "You are Sarah Chen, a 38-year-old married female from the United States. "
# "You work as a nurse and have a medium income. "
"""
You will take on the role of Sarah Chen, a 38-year-old nurse. <br>
Sarah recently moved from Seattle to Austin for a new ICU nursing job at St. David's Medical Center. Her spouse, Daniel, is still in Seattle finishing up his own job.
She has been struggling with anxiety and sleep problems since the move, and her doctor back in Seattle had prescribed her Lexapro, which she has been taking for about two years. She hasn't found a new doctor in Austin yet.
With long shifts, an empty apartment, and mounting credit card debt from the move, she has decided it's time to find a therapist or psychiatrist in Austin who accepts her Blue Shield insurance. <br>
<br>
She plans to ask for recommendations in a local community forum for nurses.
"""
),
"attributes": {
"Gender": "Female",
"Age bin": "30-44",
"Marital Status": "Married",
"Finance Status": "Medium",
"Locale": "United States",
"Education": "Bachelor\'s"
},
},
}
# ============================================================
# END OF CONFIGURATION
# ============================================================
def _extract_twitter_username(url):
"""Extract Twitter/X username from a tweet URL.
e.g. https://x.com/realDonaldTrump/status/123 β 'realDonaldTrump'
"""
try:
from urllib.parse import urlparse
path = urlparse(url).path # '/realDonaldTrump/status/123'
parts = path.strip("/").split("/") # ['realDonaldTrump', 'status', '123']
if parts and parts[0] not in ("", "search", "i", "intent", "hashtag"):
return parts[0]
except Exception:
pass
return ""
def _tweet_items_to_documents(tweet_items, fallback=False):
"""Convert raw tweet JSON items to langchain Documents.
Each document's page_content is prefixed with the Twitter username so that
it surfaces naturally in RAG linkage tooltips and evidence attribution.
Args:
tweet_items: list of dicts with at least 'url' and 'text' keys
fallback: True when these come from the fallback dataset (affects log only)
Returns:
list of Document objects
"""
from langchain_core.documents import Document
documents = []
source_tag = "fallback_tweet_data" if fallback else "social_media_twitter"
for idx, item in enumerate(tweet_items):
if not isinstance(item, dict):
continue
raw_text = item.get("text", "").strip()
if not raw_text:
continue
url = item.get("url", "") or item.get("twitterUrl", "")
username = _extract_twitter_username(url)
# Prefix every post with the author so it appears in RAG context
content = f"[{username}] {raw_text}" if username else raw_text
doc = Document(
page_content=content,
metadata={
"source": source_tag,
"twitter_username": username,
"full_name": username, # used by RAG source tooltip
"platform": "twitter",
"type": "social_media_scrape",
"post_index": idx,
"tweet_url": url,
"created_at": item.get("createdAt", ""),
"is_retweet": item.get("isRetweet", False),
}
)
documents.append(doc)
label = "fallback" if fallback else "live"
logger.info(f" β Converted {len(documents)} {label} tweets to Documents")
return documents
# ============================================================
# SECTION 2.5 β INTERACTION LOGGER
# ============================================================
logger.info(f"Interaction will be recorded to file at: {INTERACTION_LOG_PATH}")
# Known API error prefixes returned by call_generate_response's except clause
_LLM_ERROR_PREFIXES = (
"Error:",
"You exceeded your current quota",
"Rate limit",
"insufficient_quota",
"Connection error",
"Timeout",
"APIError",
"AuthenticationError",
)
def _is_llm_error(response_text):
"""Return True when the LLM response string is actually an error message."""
if not response_text:
return True
t = response_text.strip()
return any(t.startswith(p) for p in _LLM_ERROR_PREFIXES)
def _participant_id(access_token, session_source):
"""Return a safe filename-compatible participant identifier.
Prefers access_token; falls back to a short hash of session_source."""
raw = (access_token or "").strip()
if not raw:
raw = "anon_" + hashlib.sha1((session_source or "").encode()).hexdigest()[:10]
# Strip anything that is not alphanumeric, dash, or underscore
return re.sub(r"[^a-zA-Z0-9_\-]", "_", raw)[:64]
def _participant_log_path(participant_id):
"""Local filesystem path for this participant's CSV."""
log_dir = os.path.dirname(INTERACTION_LOG_PATH)
return os.path.join(log_dir, f"log_{participant_id}.csv")
def _get_file_lock(participant_id):
"""Return (creating if needed) a per-participant threading lock."""
with _log_lock:
if participant_id not in _log_locks:
_log_locks[participant_id] = threading.Lock()
return _log_locks[participant_id]
# ββ Add near the top with other imports ββββββββββββββββββββββββββββββββ
from huggingface_hub import HfApi
def _push_log_to_hub(local_path, repo_filename):
"""Push a single participant CSV to the HF dataset repo in a background thread.
The actual upload is dispatched to a daemon thread so this function returns
immediately and never blocks the HTTP response path. Failures are logged
but otherwise swallowed β logging is best-effort.
"""
def _do_upload():
try:
from huggingface_hub import HfApi
token = os.environ.get("HF_TOKEN", None)
if not token or not os.path.exists(local_path):
return
HfApi(token=token).upload_file(
path_or_fileobj=local_path,
path_in_repo=f"logs/{repo_filename}",
repo_id=HF_LOG_REPO_ID,
repo_type=HF_LOG_REPO_TYPE,
commit_message=f"log update: {repo_filename}",
)
logger.info("π€ Log pushed to HF Hub: logs/%s", repo_filename)
except Exception as e:
logger.warning("Log push to Hub failed (non-fatal): %s", e)
threading.Thread(target=_do_upload, daemon=True).start()
_LOG_FIELDS = [
"timestamp",
"session_source",
"turn_number",
"demo_mode",
"scenario_mode", # β ADD: "real" | "persona1" | "persona2" | β¦
"persona_attributes", # β ADD: JSON dict of persona ground-truth attrs, or "{}"
"model",
"epsilon",
"rag_enabled",
"show_risk",
"show_rag_highlights",
"show_tips",
"show_pii_highlights",
"show_settings",
"access_token",
"social_scraping_enabled",
"corpus_source",
"uploaded_file_path",
"uploaded_file_preview", # first 100 000 chars of the uploaded CSV
"user_prompt",
"user_prompt_perturbed", # DP-perturbed version sent to LLM (same as user_prompt when DP off)
"num_input_dp_substitutions", # number of words perturbed by Input DP (0 when DP off)
"user_prompt_length", # character length of user_prompt
"llm_response",
"llm_response_length", # character length of llm_response
"risk_score",
# ββ RAG: split by user input vs LLM response ββββββββββββββ
"num_rag_links_user",
"rag_linkages_user", # JSON: [{linked_text, source, similarity, ...}]
"num_rag_links_llm",
"rag_linkages_llm", # JSON: [{linked_text, source, similarity, ...}]
# ββ PII: split by user input vs LLM response ββββββββββββββ
"num_pii_detected_user",
"pii_detected_user", # JSON: [{text, type, confidence}]
"num_pii_detected_llm",
"pii_detected_llm", # JSON: [{text, type, confidence}]
# ββ Inference βββββββββββββββββββββββββββββββββββββββββββββ
"inference_warning_shown", # bool β was the warning banner displayed?
"inference_lifts", # JSON: {attr: {top_value, confidence, lift,
# prob_rag, prob_no_rag, evidence_type}}
"inferential_score", # float [0,1): 1βexp(βmax_lift)
"inferential_score_breakdown", # JSON: {score, max_lift, max_attr, p_post, p_pop, p_no_rag}
# ββ Social scraping βββββββββββββββββββββββββββββββββββββββ
"scraped_social_data", # JSON: list of scraped post objects (if enabled)
"scraped_social_summary", # JSON: per-platform summary [{platform, post_count, posts:[{text,url}]}]
]
# _log_lock = threading.Lock()
_log_lock = threading.Lock() # guards _log_locks dict itself
_log_locks = {} # per-participant file locks
def _ensure_log_file():
"""Create the CSV with header if it does not exist yet. Safe for concurrent startup."""
if not os.path.exists(INTERACTION_LOG_PATH):
with _log_lock:
if not os.path.exists(INTERACTION_LOG_PATH): # double-check after acquiring
with open(INTERACTION_LOG_PATH, "w", newline="", encoding="utf-8") as f:
csv.writer(f).writerow(_LOG_FIELDS)
logger.info(f"π Interaction log created: {INTERACTION_LOG_PATH}")
def _sanitize_cell(text):
"""Replace raw newlines and tabs in free-text fields so they cannot
break CSV row boundaries, while remaining human-readable in Excel."""
if not text:
return text or ""
return (
str(text)
.replace("\r\n", " \\n ")
.replace("\r", " \\n ")
.replace("\n", " \\n ")
.replace("\t", " \\t ")
)
def append_interaction_log(
session_source,
turn_number,
demo_mode,
scenario_mode,
persona_attributes,
model,
epsilon,
rag_enabled,
show_risk,
show_rag_highlights,
show_tips,
show_pii_highlights,
show_settings,
access_token,
social_scraping_enabled,
corpus_source,
uploaded_file_path,
user_prompt,
llm_response,
risk_score,
u_rag, # RAGLink list for user input only
r_rag, # RAGLink list for LLM response only
u_pii, # PIIMatch list for user input only
r_pii, # PIIMatch list for LLM response only
u_pii_perturbed,
inference_metrics,
inference_warning_shown,
scraped_docs=None, # list of Document objects from social scraping
user_prompt_perturbed=None, # DP-perturbed version sent to LLM
num_input_dp_substitutions=0, # number of words perturbed by Input DP
num_attributes_changed=0,
show_dp=1,
show_infr_attr_card=1,
show_social_scraping=False,
show_upload_data=False,
rag_corpus_path="",
):
"""Append one interaction row to the persistent CSV log."""
try:
# ββ Resolve participant identity and paths ββββββββββββ
pid = _participant_id(access_token, session_source)
log_path = _participant_log_path(pid)
log_file = f"log_{pid}.csv"
file_lock = _get_file_lock(pid)
# ββ Uploaded file info ββββββββββββββββββββββββββββββββ
uploaded_file_preview = ""
if uploaded_file_path:
try:
with open(uploaded_file_path, "r", encoding="utf-8", errors="replace") as _f:
raw_preview = _f.read(100000)
# json.dumps produces a single-line string with all special
# characters escaped (\n, \r, \", \t, etc.), making it
# completely safe to embed in any CSV cell.
uploaded_file_preview = json.dumps(raw_preview)
except Exception:
uploaded_file_preview = "<unreadable>"
# Create the file with headers if it does not exist yet
if not os.path.exists(log_path):
os.makedirs(os.path.dirname(log_path), exist_ok=True)
with file_lock:
if not os.path.exists(log_path): # double-check after acquiring lock
with open(log_path, "w", newline="", encoding="utf-8") as f:
csv.writer(f, quoting=csv.QUOTE_ALL).writerow(LOG_COLUMNS) # your existing header list
# ββ RAG linkages β user input βββββββββββββββββββββββββ
def _serialise_rag(rag_list):
return json.dumps([
{
"linked_text": lk.text,
"source": getattr(lk, "source", ""),
"similarity": round(float(lk.top_similarity), 4),
"start": lk.start,
"end": lk.end,
"corpus_snippets": lk.corpus_snippets,
"top_doc_score": lk.top_doc_score,
"overlap_keywords": lk.overlap_keywords,
}
for lk in (rag_list or [])
], ensure_ascii=False)
rag_linkages_user = _serialise_rag(u_rag)
rag_linkages_llm = _serialise_rag(r_rag)
# ββ PII detected β split by source βββββββββββββββββββ
def _serialise_pii(pii_list):
rows = []
for m in (pii_list or []):
if isinstance(m, dict):
rows.append({
"text": m.get("value", m.get("text", "")),
"type": m.get("type", "unknown"),
"confidence": round(float(m.get("confidence", 0.0)), 4),
})
else:
rows.append({
"text": m.text,
"type": m.fine_type,
"confidence": round(float(m.confidence), 4),
})
return json.dumps(rows, ensure_ascii=False)
pii_detected_user = _serialise_pii(u_pii)
pii_detected_llm = _serialise_pii(r_pii)
# ββ Inference lifts βββββββββββββββββββββββββββββββββββ
lifts = {}
for attr, m in (inference_metrics or {}).items():
lift = m.get("lift", 0)
if not (math.isinf(lift) or math.isnan(lift)):
lifts[attr] = {
"top_value": m.get("top_value", ""),
"confidence": round(float(m.get("confidence", m.get("probability", 0))), 4),
"lift": round(float(lift), 4),
"prob_rag": round(float(m.get("prob_rag", 0)), 4),
"prob_no_rag": round(float(m.get("prob_no_rag", 0)), 4),
"p_pop": round(float(m.get("p_pop", 0)), 4),
"evidence_type": m.get("evidence_type", "unknown"),
}
lifts_json = json.dumps(lifts, ensure_ascii=False)
# ββ Inferential privacy score and breakdown βββββββββββ
infer_score_info = calculate_inferential_privacy_score(inference_metrics or {})
inferential_score = round(float(infer_score_info.get("mean_score", 0.0)), 4)
infer_breakdown = {
"score": inferential_score,
"max_score": round(float(infer_score_info.get("max_score", 0.0)), 4),
"max_lift": round(float(infer_score_info.get("max_lift", 0.0)), 4),
"max_attr": infer_score_info.get("max_attr", ""),
"p_post": round(float(infer_score_info.get("p_rag", 0.0)), 4),
"p_pop": round(float(infer_score_info.get("p_pop", 0.0)), 4),
"p_no_rag": round(float(infer_score_info.get("p_no_rag", 0.0)), 4),
"mean_score": round(float(infer_score_info.get("mean_score", 0.0)), 4),
"mean_lift": round(float(infer_score_info.get("mean_lift", 0.0)), 4),
"median_score": round(float(infer_score_info.get("median_score", 0.0)), 4),
"median_lift": round(float(infer_score_info.get("median_lift", 0.0)), 4),
}
inferential_score_breakdown_json = json.dumps(infer_breakdown, ensure_ascii=False)
# ββ Scraped social data βββββββββββββββββββββββββββββββ
scraped_json = "[]"
scraped_summary_json = "[]"
if scraped_docs:
try:
scraped_json = json.dumps([
{
"text": doc.page_content,
"metadata": doc.metadata if hasattr(doc, "metadata") else {},
}
for doc in scraped_docs
], ensure_ascii=False)
except Exception:
scraped_json = "[]"
# Build a human-readable per-platform summary for analysis
try:
platform_groups = {}
for doc in scraped_docs:
meta = doc.metadata if hasattr(doc, "metadata") else {}
platform = meta.get("platform", "unknown").lower()
source_url = (
meta.get("source_url") or
meta.get("tweet_url") or
meta.get("url") or
""
)
text = doc.page_content or ""
if platform not in platform_groups:
platform_groups[platform] = []
platform_groups[platform].append({
"text": text,
"url": source_url,
})
summary_entries = []
for platform, posts in platform_groups.items():
summary_entries.append({
"platform": platform,
"post_count": len(posts),
"posts": [
{
"text": p["text"],
"url": p["url"],
}
for p in posts
],
})
scraped_summary_json = json.dumps(summary_entries, ensure_ascii=False)
except Exception:
scraped_summary_json = "[]"
persona_attributes_json = json.dumps(persona_attributes or {}, ensure_ascii=False)
row = [
datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
str(session_source),
int(turn_number),
str(demo_mode),
str(scenario_mode),
persona_attributes_json,
str(model),
str(epsilon),
str(rag_enabled),
str(show_risk),
str(show_rag_highlights),
str(show_tips),
str(show_pii_highlights),
str(show_settings),
str(show_dp),
str(show_infr_attr_card),
str(show_social_scraping),
str(show_upload_data),
str(rag_corpus_path),
str(access_token),
str(social_scraping_enabled),
str(corpus_source),
str(uploaded_file_path or ""),
uploaded_file_preview,
_sanitize_cell(user_prompt),
_sanitize_cell(user_prompt_perturbed if user_prompt_perturbed is not None else user_prompt),
int(num_input_dp_substitutions),
len(user_prompt or ""),
len(user_prompt_perturbed or ""),
_sanitize_cell(llm_response),
len(llm_response or ""),
round(float(risk_score), 2),
len(u_rag or []),
rag_linkages_user,
len(r_rag or []),
rag_linkages_llm,
len(u_pii or []),
pii_detected_user,
len(u_pii_perturbed or []),
_serialise_pii(u_pii_perturbed or []),
len(r_pii or []),
pii_detected_llm,
str(bool(inference_warning_shown)),
str(num_attributes_changed > 0),
int(num_attributes_changed),
lifts_json,
inferential_score,
inferential_score_breakdown_json,
scraped_json,
scraped_summary_json,
]
with file_lock:
with open(log_path, "a", newline="", encoding="utf-8") as f:
csv.writer(f, quoting=csv.QUOTE_ALL).writerow(row)
_push_log_to_hub(log_path, log_file)
logger.info(
f"π Logged turn {turn_number} for participant '{pid}' "
f"(risk={risk_score:.0f}, "
f"rag_user={len(u_rag or [])}, rag_llm={len(r_rag or [])}, "
f"pii_user={len(u_pii or [])}, pii_llm={len(r_pii or [])}, "
f"lifts={len(lifts)}, warning_shown={inference_warning_shown})"
)
except Exception as exc:
logger.error(f"β οΈ Failed to write interaction log: {exc}")
def append_session_end_log(state):
"""Write a sentinel END row to the participant's CSV when they end the conversation."""
try:
access_token = getattr(state, "_access_token", "")
session_source = getattr(state, "_session_source", "unknown")
pid = _participant_id(access_token, session_source)
log_path = _participant_log_path(pid)
log_file = f"log_{pid}.csv"
file_lock = _get_file_lock(pid)
end_ts = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
# Columns whose values are meaningful for the END row
config_values = {
"timestamp": end_ts,
"session_source": str(session_source),
"turn_number": "END",
"demo_mode": str(getattr(state, "_demo_mode", False)),
"scenario_mode": str(getattr(state, "_scenario_mode", "real")),
"persona_attributes": json.dumps(getattr(state, "_persona_attributes", {}), ensure_ascii=False),
"model": str(getattr(state, "_last_model", "")),
"epsilon": str(getattr(state, "_last_epsilon", float("inf"))),
"rag_enabled": str(getattr(state, "_last_rag_enabled", False)),
"show_risk": str(getattr(state, "_show_risk", True)),
"show_rag_highlights": str(getattr(state, "_show_rag_highlights", False)),
"show_tips": str(getattr(state, "_show_tips", False)),
"show_pii_highlights": str(getattr(state, "_show_pii_hl", True)),
"show_settings": str(getattr(state, "_show_settings", False)),
"access_token": str(access_token),
"social_scraping_enabled": str(getattr(state, "_last_social_scraping", False)),
"corpus_source": str(getattr(state, "_last_corpus_source", "system")),
"uploaded_file_path": str(getattr(state, "_uploaded_file_path", "") or ""),
"show_dp": str(getattr(state, "_show_dp", 1)),
"show_infr_attr_card": str(getattr(state, "_show_infr_attr_card", 1)),
"show_social_scraping": str(getattr(state, "_show_social_scraping", False)),
"show_upload_data": str(getattr(state, "_show_upload_data", False)),
"rag_corpus_path": str(getattr(state, "_rag_corpus_path", "")),
}
# Build the row: config values where available, "END" for all per-turn columns
row = [
config_values.get(col, "END")
for col in LOG_COLUMNS
]
with file_lock:
with open(log_path, "a", newline="", encoding="utf-8") as f:
csv.writer(f, quoting=csv.QUOTE_ALL).writerow(row)
_push_log_to_hub(log_path, log_file)
logger.info(f"π Logged END-OF-CONVERSATION for participant '{pid}' at {end_ts}")
except Exception as exc:
logger.error(f"β οΈ Failed to write session-end log: {exc}")
os.makedirs("logs", exist_ok=True)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Apify Configuration for Social Media Scraping
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def validate_access_token(token):
"""Check if access token is valid and not expired.
A Prolific token (exactly 24 alphanumeric characters) is always accepted.
Any other token must appear in VALID_ACCESS_TOKENS and must not be expired.
"""
if not token:
return False
# Accept valid Prolific tokens (exactly 24 alphanumeric characters).
if re.fullmatch(PROLIFIC_ID_PATTERN_REGEX, token):
return True
# For non-Prolific tokens, check the VALID_ACCESS_TOKENS dictionary.
token_info = VALID_ACCESS_TOKENS.get(token)
if not token_info:
return False
# Check expiration date.
from datetime import datetime
expires = datetime.strptime(token_info["expires"], "%Y-%m-%d")
if datetime.now() > expires:
return False
return True
def _load_fallback_tweets(scenario_mode: str):
"""Load and return fallback tweet items for the given scenario_mode.
Returns [] if the file is missing, unmapped, or set to None."""
key = str(scenario_mode).strip().lower()
path = SCENARIO_FALLBACK_TWEET_PATHS.get(key)
if not path:
return []
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
logger.info(f"β Loaded {len(data)} fallback tweets for scenario '{key}' from {path}")
return data
except FileNotFoundError:
logger.warning(f"β οΈ Fallback tweet file not found for scenario '{key}': {path}")
return []
except Exception as e:
logger.error(f"β Failed to load fallback tweets for scenario '{key}': {e}")
return []
# ============================================================
# SECTION 1 β DATA STRUCTURES
# ============================================================
def _fill_missing_with_uniform(probs):
"""Fill any missing SENSITIVE_ATTRIBUTES with a uniform distribution.
This ensures the inference panel always has something to display even when
an LLM call fails or returns a partial response. A uniform distribution
signals maximum uncertainty β no strong prediction β which is honest and
safe to show.
"""
filled = dict(probs) # shallow copy β don't mutate the original
for attr in SENSITIVE_ATTRIBUTES:
if attr not in filled or not filled[attr]:
values = ATTRIBUTE_VALUES_MAP.get(attr, [])
if values:
p = round(1.0 / len(values), 4)
filled[attr] = {v: p for v in values}
return filled
def get_persona(scenario_mode: str) -> dict:
"""Return the persona dict for a given scenario_mode string (case-insensitive).
Falls back to 'real' if the key is not found."""
return PERSONAS.get(str(scenario_mode).strip().lower(), PERSONAS["real"])
class PIICategory(Enum):
IDENTITY = "identity" # IDs, names, SSN, usernames, etc.
CONTACT = "contact" # Phone, emails
LOCATION = "location" # Addresses, organizations
SENSITIVE = "sensitive" # Medical, financial, and other sensitive data
# Mapping from specific PII fine-types (as returned by the LLM) to the four
# broad UI categories. Add new fine-types here; the rest of the code adapts.
PII_TYPE_TO_CATEGORY = {
# ββ Identity βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
"name": PIICategory.IDENTITY,
"full name": PIICategory.IDENTITY,
"first name": PIICategory.IDENTITY,
"last name": PIICategory.IDENTITY,
"ssn": PIICategory.IDENTITY,
"social security number": PIICategory.IDENTITY,
"username": PIICategory.IDENTITY,
"id": PIICategory.IDENTITY,
"identifier": PIICategory.IDENTITY,
"passport": PIICategory.IDENTITY,
"license": PIICategory.IDENTITY,
"date of birth": PIICategory.IDENTITY,
"dob": PIICategory.IDENTITY,
"age": PIICategory.IDENTITY,
"ip address": PIICategory.IDENTITY,
"ip": PIICategory.IDENTITY,
# ββ Contact βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
"email": PIICategory.CONTACT,
"email address": PIICategory.CONTACT,
"phone": PIICategory.CONTACT,
"phone number": PIICategory.CONTACT,
"mobile": PIICategory.CONTACT,
"fax": PIICategory.CONTACT,
# ββ Location ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
"location": PIICategory.LOCATION,
"address": PIICategory.LOCATION,
"street address": PIICategory.LOCATION,
"city": PIICategory.LOCATION,
"state": PIICategory.LOCATION,
"country": PIICategory.LOCATION,
"zip code": PIICategory.LOCATION,
"postal code": PIICategory.LOCATION,
"organization": PIICategory.LOCATION,
"workplace": PIICategory.LOCATION,
"school": PIICategory.LOCATION,
# ββ Sensitive (medical, financial, and other sensitive data) ββββββββββββββ
"medical": PIICategory.SENSITIVE,
"medical record": PIICategory.SENSITIVE,
"diagnosis": PIICategory.SENSITIVE,
"condition": PIICategory.SENSITIVE,
"medication": PIICategory.SENSITIVE,
"prescription": PIICategory.SENSITIVE,
"treatment": PIICategory.SENSITIVE,
"health": PIICategory.SENSITIVE,
"disability": PIICategory.SENSITIVE,
"mental health": PIICategory.SENSITIVE,
"insurance": PIICategory.SENSITIVE,
"financial": PIICategory.SENSITIVE,
"bank account": PIICategory.SENSITIVE,
"credit card": PIICategory.SENSITIVE,
"credit card number": PIICategory.SENSITIVE,
"account number": PIICategory.SENSITIVE,
"salary": PIICategory.SENSITIVE,
"income": PIICategory.SENSITIVE,
"debt": PIICategory.SENSITIVE,
"loan": PIICategory.SENSITIVE,
"mortgage": PIICategory.SENSITIVE,
"tax id": PIICategory.SENSITIVE,
"political": PIICategory.SENSITIVE,
"religion": PIICategory.SENSITIVE,
"sexual orientation": PIICategory.SENSITIVE,
"ethnicity": PIICategory.SENSITIVE,
"race": PIICategory.SENSITIVE,
"biometric": PIICategory.SENSITIVE,
}
@dataclass
class PIIMatch:
text: str
start: int
end: int
fine_type: str
category: PIICategory
confidence: float
@dataclass
class RAGLink:
"""A span of user/assistant text linked to RAG corpus documents."""
text: str
start: int
end: int
corpus_snippets: list
top_similarity: float
top_doc_text: str = ""
top_doc_score: float = 0.0
overlap_keywords: list = None
source: str = "From system data" # Source of the matched text
url: str = "" # Source URL of the matched document (if available)
@dataclass
class Message:
role: str
content: str
pii_matches: list = None
pii_card_matches: list = None # PII detected on perturbed text (for the right-panel card)
rag_links: list = None
dp_metadata: dict = None # Input DP info: {epsilon, num_substitutions, substitutions}
class ConversationState:
"""Simple in-memory conversation container."""
def __init__(self):
self.messages = []
self.last_probs_rag = {}
self.last_probs_no_rag = {}
self.last_evidence_rag = {}
# Logging metadata β set once per session
self._session_source = "unknown"
self._access_token = ""
self._show_risk = True
self._show_rag_highlights = False
self._show_tips = False
self._show_pii_hl = True
self._show_settings = False
self._show_dp = 1
self._show_infr_attr_card = 1
self._show_social_scraping = False
self._show_upload_data = False
self._rag_corpus_path = ""
self._uploaded_file_path = None
self._turn_count = 0 # incremented per user message
self._peak_inference_metrics = {}
self._prev_inference_attrs = {}
self._scenario_mode = "real"
self._last_model = ""
self._last_epsilon = float("inf")
self._last_rag_enabled = False
self._last_social_scraping = False
self._last_corpus_source = "system"
self._demo_mode = False
self._persona_attributes = {}
self._last_avatar_html = "" # cached inference card HTML for end-reveal
self._last_privacy_html = "" # cached privacy settings HTML for end-reveal
self._scraped_docs = None # None = not yet scraped; [] = scraped, nothing found
self._best_probs_rag = {} # attr β best prob distribution seen so far
self._best_evidence_rag = {} # attr β evidence from that best turn
def add(self, role, content, pii_matches=None, rag_links=None, dp_metadata=None, pii_card_matches=None):
self.messages.append(Message(role=role, content=content,
pii_matches=pii_matches,
pii_card_matches=pii_card_matches,
rag_links=rag_links,
dp_metadata=dp_metadata))
def clear(self):
self.messages = []
self.last_probs_rag = {}
self.last_probs_no_rag = {}
self.last_evidence_rag = {}
self._turn_count = 0
self._peak_inference_metrics = {}
self._last_model = ""
self._last_epsilon = float("inf")
self._last_rag_enabled = False
self._last_social_scraping = False
self._last_corpus_source = "system"
self._demo_mode = False
self._show_dp = 1
self._show_infr_attr_card = 1
self._show_social_scraping = False
self._show_upload_data = False
self._rag_corpus_path = ""
self._prev_inference_attrs = {}
self._last_avatar_html = ""
self._last_privacy_html = ""
self._scraped_docs = None
self._best_probs_rag = {} # attr β best prob distribution seen so far
self._best_evidence_rag = {} # attr β evidence from that best turn
# ============================================================
# SECTION 2 β COLOUR CONFIGURATION
# ============================================================
PII_COLORS = {
PIICategory.IDENTITY: {"bg": "#FFE4E1", "border": "#E74C3C",
"label": "Identity (IDs, Names)"},
PIICategory.CONTACT: {"bg": "#E1F0FF", "border": "#2980B9",
"label": "Contact (Phone, Email)"},
PIICategory.LOCATION: {"bg": "#E8F5E9", "border": "#27AE60",
"label": "Location"},
PIICategory.SENSITIVE: {"bg": "#FFF3E0", "border": "#E65100",
"label": "Sensitive (Medical / Financial)"},
}
RAG_LINK_COLOR = {"bg": "#E8EAF6", "border": "#3F51B5",
"label": "Linked to External Data"}
# ============================================================
# SECTION 3 β LLM CLIENT INITIALIZATION
# ============================================================
# Global LLM clients dictionary
LLM_CLIENTS = {}
def initialize_llm_clients():
"""Initialize API clients for each provider."""
global LLM_CLIENTS
try:
# OpenAI
openai_key = os.environ.get("OPENAI_API_KEY", None)
if openai_key:
os.environ["OPENAI_API_KEY"] = openai_key
LLM_CLIENTS["openai"] = OpenAI(api_key=openai_key, timeout=60.0)
logger.info("β OpenAI client initialized")
# Together AI (OpenAI-compatible)
together_key = os.environ.get("TOGETHER_API_KEY", None)
if together_key:
os.environ["TOGETHER_API_KEY"] = together_key
LLM_CLIENTS["together"] = OpenAI(
api_key=together_key,
base_url="https://api.together.xyz/v1",
timeout=60.0
)
logger.info("β Together AI client initialized")
# Gemini (OpenAI-compatible)
gemini_key = os.environ.get("GEMINI_API_KEY", None)
if gemini_key:
os.environ["GEMINI_API_KEY"] = gemini_key
LLM_CLIENTS["gemini"] = OpenAI(
api_key=gemini_key,
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
timeout=60.0,
max_retries=5
)
logger.info("β Gemini client initialized")
except Exception as e:
logger.info(f"Warning: Could not initialize LLM clients: {e}")
logger.info("Demo mode will still work without LLM clients.")
# ============================================================
# SECTION 3.5 β SOCIAL MEDIA SCRAPING & CUSTOM CORPUS
# ============================================================
class SocialMediaScraper:
"""
Social media scraper for retrieving user data from public sources using Apify.
Integrates with Apify API to scrape posts from Twitter/X.
"""
def __init__(self, enabled=False):
self.enabled = enabled
self.api_token = os.environ.get("APIFY_TOKEN", None)
# Twitter/X actor (existing)
self.actor_id = os.environ.get("APIFY_X_ACTOR_ID", None)
# Facebook / LinkedIn actor IDs (read from env; the scraper module
# also has built-in defaults so these can be left unset)
self.fb_posts_actor = os.environ.get("APIFY_FB_POSTS_ACTOR", None)
self.fb_pages_actor = os.environ.get("APIFY_FB_PAGES_ACTOR", None)
self.li_posts_actor = os.environ.get("APIFY_LI_POSTS_ACTOR", None)
self.web_scraper_actor = os.environ.get("APIFY_WEB_SCRAPER_ACTOR", None)
logger.info(f"SocialMediaScraper initialized (enabled={enabled})")
if enabled and not self.api_token:
logger.warning("β οΈ Apify scraping enabled but APIFY_TOKEN not set!")
def scrape_user_data(self, user_name, platforms=None, twitter_handle=None,
facebook_handle=None, linkedin_username=None,
fallback_data=None):
"""
Scrape user data from one or more social media platforms using Apify.
All platform scrapes are executed in parallel via ThreadPoolExecutor.
"""
if not self.enabled:
logger.info("Social media scraping is disabled")
return []
if platforms is None:
platforms = ["Twitter"]
normalised = [p.strip().lower() for p in platforms]
# Build the list of (callable, args) tasks to run in parallel
tasks = []
if any(p in ("twitter", "x") for p in normalised):
tasks.append((self._scrape_twitter,
(user_name, twitter_handle),
{"fallback_data": fallback_data}))
if "facebook" in normalised:
tasks.append((self._scrape_facebook_posts,
(user_name, facebook_handle), {}))
tasks.append((self._scrape_facebook_page_info,
(user_name, facebook_handle), {}))
if "linkedin" in normalised:
tasks.append((self._scrape_linkedin_posts,
(user_name, linkedin_username), {}))
if "web" in normalised:
tasks.append((self._scrape_web, (user_name,), {}))
if not tasks:
return []
documents = []
with ThreadPoolExecutor(max_workers=len(tasks)) as executor:
futures = {
executor.submit(fn, *args, **kwargs): fn.__name__
for fn, args, kwargs in tasks
}
for future in as_completed(futures):
fn_name = futures[future]
try:
result = future.result()
logger.info(f" β {fn_name} completed: {len(result)} documents")
documents.extend(result)
except Exception as exc:
logger.error(f" β {fn_name} raised an exception: {exc}")
logger.info(f"Social media scraping completed: {len(documents)} total documents")
return documents
def _scrape_twitter(self, user_name, twitter_handle=None, fallback_data=None):
"""Scrape Twitter/X; falls back to embedded JSON on failure."""
documents = []
live_ok = False
if self.api_token:
logger.info(f" π Live scraping Twitter for '{user_name}' via Apify...")
try:
posts = get_twitter_user_posts(
full_name=user_name,
api_token=self.api_token,
twitter_handle=twitter_handle,
actor_id=self.actor_id,
general_search_actor_id=self.web_scraper_actor or None,
max_items=30,
tweet_language="en",
sort="Latest",
log_csv_path="apify_requests_log.csv",
)
if posts:
logger.info(f" β Twitter live scrape returned {len(posts)} posts")
for idx, item in enumerate(posts):
if isinstance(item, dict):
post_text = item.get("text", "").strip()
post_url = item.get("url", "") or ""
else:
post_text = str(item).strip()
post_url = ""
if not post_text:
continue
documents.append(Document(
page_content=post_text,
metadata={
"source": "social_media_twitter",
"user": user_name,
"full_name": user_name,
"scraped_at": datetime.datetime.now().isoformat(),
"platform": "twitter",
"type": "social_media_scrape",
"post_index": idx,
"twitter_handle": twitter_handle or "",
"source_url": post_url,
},
))
live_ok = True
else:
logger.warning(f" β οΈ Twitter live scrape returned no posts for '{user_name}'")
except Exception as exc:
logger.error(f" β Twitter Apify scrape failed: {exc}")
import traceback
logger.error(traceback.format_exc())
else:
logger.warning(" β οΈ APIFY_TOKEN not set β skipping Twitter live scrape")
if not live_ok:
data = fallback_data if fallback_data is not None else FALLBACK_TWEET_DATA
logger.info(f" π Using fallback tweet dataset ({len(data)} items)...")
documents = _tweet_items_to_documents(data, fallback=True)
return documents
def _scrape_facebook_posts(self, user_name, facebook_handle=None):
"""Scrape Facebook posts via Apify facebook-posts-scraper Actor."""
documents = []
if not self.api_token:
logger.warning(" β οΈ APIFY_TOKEN not set β skipping Facebook posts scrape")
return documents
logger.info(f" π Scraping Facebook posts for '{user_name}'...")
try:
posts = get_facebook_posts(
full_name=user_name,
api_token=self.api_token,
facebook_handle=facebook_handle,
max_items=30,
actor_id=self.fb_posts_actor or None,
general_search_actor_id=self.web_scraper_actor or None,
)
for idx, item in enumerate(posts):
if isinstance(item, dict):
post_text = item.get("text", "").strip()
post_url = item.get("url", "") or ""
else:
post_text = str(item).strip()
post_url = ""
if not post_text:
continue
documents.append(Document(
page_content=post_text,
metadata={
"source": "social_media_facebook",
"user": user_name,
"full_name": user_name,
"scraped_at": datetime.datetime.now().isoformat(),
"platform": "facebook",
"type": "social_media_scrape",
"post_index": idx,
"facebook_handle": facebook_handle or "",
"source_url": post_url,
},
))
logger.info(f" β Facebook posts: {len(documents)} documents")
except Exception as exc:
logger.error(f" β Facebook posts scrape failed: {exc}")
import traceback
logger.error(traceback.format_exc())
return documents
def _scrape_facebook_page_info(self, user_name, facebook_handle=None):
"""Scrape Facebook page / profile bio via Apify facebook-pages-scraper Actor."""
documents = []
if not self.api_token:
logger.warning(" β οΈ APIFY_TOKEN not set β skipping Facebook page info scrape")
return documents
logger.info(f" π Scraping Facebook page info for '{user_name}'...")
try:
texts = get_facebook_page_info(
full_name=user_name,
api_token=self.api_token,
facebook_handle=facebook_handle,
actor_id=self.fb_pages_actor or None,
general_search_actor_id=self.web_scraper_actor or None,
)
for idx, item in enumerate(texts):
if isinstance(item, dict):
page_text = item.get("text", "").strip()
page_url = item.get("url", "") or ""
else:
page_text = str(item).strip()
page_url = ""
if not page_text:
continue
documents.append(Document(
page_content=page_text,
metadata={
"source": "social_media_facebook",
"user": user_name,
"full_name": user_name,
"scraped_at": datetime.datetime.now().isoformat(),
"platform": "facebook",
"type": "social_media_scrape",
"post_index": idx,
"facebook_handle": facebook_handle or "",
"content_type": "page_info",
"source_url": page_url,
},
))
logger.info(f" β Facebook page info: {len(documents)} documents")
except Exception as exc:
logger.error(f" β Facebook page info scrape failed: {exc}")
import traceback
logger.error(traceback.format_exc())
return documents
def _scrape_linkedin_posts(self, user_name, linkedin_username=None):
"""Scrape LinkedIn posts via Apify apimaestro/linkedin-profile-posts Actor."""
documents = []
if not self.api_token:
logger.warning(" β οΈ APIFY_TOKEN not set β skipping LinkedIn posts scrape")
return documents
logger.info(f" π Scraping LinkedIn posts for '{user_name}'...")
try:
posts = get_linkedin_posts(
full_name=user_name,
api_token=self.api_token,
linkedin_username=linkedin_username,
max_items=30,
actor_id=self.li_posts_actor or None,
general_search_actor_id=self.web_scraper_actor or None,
)
for idx, item in enumerate(posts):
if isinstance(item, dict):
post_text = item.get("text", "").strip()
post_url = item.get("url", "") or ""
else:
post_text = str(item).strip()
post_url = ""
if not post_text:
continue
documents.append(Document(
page_content=post_text,
metadata={
"source": "social_media_linkedin",
"user": user_name,
"full_name": user_name,
"scraped_at": datetime.datetime.now().isoformat(),
"platform": "linkedin",
"type": "social_media_scrape",
"post_index": idx,
"linkedin_username": linkedin_username or "",
"source_url": post_url,
},
))
logger.info(f" β LinkedIn posts: {len(documents)} documents")
except Exception as exc:
logger.error(f" β LinkedIn posts scrape failed: {exc}")
import traceback
logger.error(traceback.format_exc())
return documents
def _scrape_web(self, user_name, search_query=None, extra_urls=None,
max_pages=3):
"""Scrape top web search results about the person via apify/web-scraper."""
documents = []
if not self.api_token:
logger.warning(" β οΈ APIFY_TOKEN not set β skipping web scrape")
return documents
logger.info(f" π Scraping web results for '{user_name}'...")
try:
texts = get_web_search_results(
full_name=user_name,
api_token=self.api_token,
search_query=search_query,
extra_urls=extra_urls,
max_pages=max_pages,
actor_id=self.web_scraper_actor or None,
)
for idx, item in enumerate(texts):
if isinstance(item, dict):
web_text = item.get("text", "").strip()
web_url = item.get("url", "") or ""
else:
web_text = str(item).strip()
web_url = ""
if not web_text:
continue
documents.append(Document(
page_content=web_text,
metadata={
"source": "social_media_web",
"user": user_name,
"full_name": user_name,
"scraped_at": datetime.datetime.now().isoformat(),
"platform": "web",
"type": "web_search_scrape",
"post_index": idx,
"source_url": web_url,
},
))
logger.info(f" β Web scrape: {len(documents)} documents")
except Exception as exc:
logger.error(f" β Web scrape failed: {exc}")
import traceback
logger.error(traceback.format_exc())
return documents
def build_retriever_from_csv(csv_file_path, progress=None, user_id_col="user id", text_col="text",
first_name_col="First Name", last_name_col="Last Name"):
"""
Build a new HybridRetriever from uploaded CSV data.
Args:
csv_file_path: Path to CSV file
progress: Gradio Progress object for tracking progress
user_id_col: Column name for user IDs
text_col: Column name for text content
first_name_col: Column name for first names
last_name_col: Column name for last names
Returns:
HybridRetriever instance or None if failed
"""
try:
logger.info(f"Building retriever from CSV: {csv_file_path}")
if progress:
progress(0.25, desc="Loading CSV data...")
time.sleep(0.05) # Brief pause for UI update
# Read CSV
df = pd.read_csv(csv_file_path)
logger.info(f" β CSV loaded: {len(df)} rows, {len(df.columns)} columns")
# Validate required column
if text_col not in df.columns:
available = ", ".join(f"'{c}'" for c in df.columns)
raise ValueError(
f"Required column '{text_col}' not found. "
f"Available columns: {available}. "
f"Please ensure your CSV has a column named '{text_col}'."
)
if progress:
progress(0.35, desc=f"Processing {len(df)} documents...")
time.sleep(0.05)
# Build documents
documents = []
total_rows = len(df)
last_progress_update = 0
for idx, row in df.iterrows():
# Update progress more frequently (every 5% or at least every 100 rows)
current_progress = idx / total_rows if total_rows > 0 else 0
if progress and (current_progress - last_progress_update >= 0.05 or idx % 100 == 0):
progress(0.35 + 0.35 * current_progress, desc=f"Processing documents: {idx+1}/{total_rows}")
last_progress_update = current_progress
text = str(row.get(text_col, "")).strip()
if len(text) < 10: # Skip very short texts
continue
# Build metadata
metadata = {
"user_id": str(row.get(user_id_col, f"user_{idx}")),
"source": "uploaded_csv",
"row_index": idx
}
# Add optional fields
if first_name_col in df.columns:
metadata["first_name"] = str(row.get(first_name_col, ""))
if last_name_col in df.columns:
metadata["last_name"] = str(row.get(last_name_col, ""))
# Add all other columns as metadata
for col in df.columns:
if col not in [user_id_col, text_col, first_name_col, last_name_col]:
metadata[col] = row.get(col)
doc = Document(page_content=text, metadata=metadata)
documents.append(doc)
logger.info(f" β Created {len(documents)} documents")
if progress:
progress(0.70, desc=f"Building retriever from {len(documents)} documents (this may take a while)...")
time.sleep(0.05)
# Build retriever
retriever = HybridRetriever(documents)
logger.info(" β Retriever built successfully")
if progress:
progress(0.88, desc="Retriever built successfully!")
time.sleep(0.05)
return retriever
except Exception as e:
logger.error(f" β Failed to build retriever from CSV: {str(e)}")
raise # re-raise so _upload_corpus's except block shows the message
# Global social media scraper instance
SOCIAL_SCRAPER = None
def initialize_social_scraper(enabled=False):
"""Initialize the social media scraper."""
global SOCIAL_SCRAPER
SOCIAL_SCRAPER = SocialMediaScraper(enabled=enabled)
return SOCIAL_SCRAPER
# ============================================================
# SECTION 4 β PII DETECTION (LLM-BASED ONLY)
# ============================================================
def _mk(text, start, end, ft, conf):
"""Create PIIMatch with category mapping."""
ft_lower = ft.lower().strip()
# Map to category β exact lookup first, then keyword-based fallback
if ft_lower in PII_TYPE_TO_CATEGORY:
category = PII_TYPE_TO_CATEGORY[ft_lower]
else:
if any(kw in ft_lower for kw in ["name", "id", "ssn", "username",
"passport", "license", "birth", "dob"]):
category = PIICategory.IDENTITY
elif any(kw in ft_lower for kw in ["email", "phone", "mobile", "fax"]):
category = PIICategory.CONTACT
elif any(kw in ft_lower for kw in ["address", "location", "organization",
"city", "country", "zip"]):
category = PIICategory.LOCATION
elif any(kw in ft_lower for kw in ["medical", "health", "diagnosis",
"medication", "prescription", "condition",
"financial", "bank", "credit", "salary",
"income", "insurance", "loan", "mortgage",
"political", "religion", "ethnicity",
"race", "biometric", "sexual"]):
category = PIICategory.SENSITIVE
else:
category = PIICategory.IDENTITY # safest default
return PIIMatch(text=text, start=start, end=end, fine_type=ft_lower,
category=category, confidence=conf)
def _extract_pii_spans_from_values(text, items):
"""Convert LLM-returned {value,type} items to PIIMatch spans.
Searches are case-insensitive so that minor capitalisation differences
between the LLM's extraction and the original text do not cause misses.
The span is anchored to the *original* casing in `text`.
"""
matches = []
used = [False] * (len(text) + 1)
text_lower = text.lower()
for it in items:
if not isinstance(it, dict):
continue
val = str(it.get("value", "")).strip()
ft = str(it.get("type", "")).strip().lower()
conf = float(it.get("confidence", 0.85))
conf = max(0.0, min(1.0, conf))
if not val:
continue
val_lower = val.lower()
start = 0
while True:
idx = text_lower.find(val_lower, start)
if idx == -1:
break
j = idx + len(val)
if any(used[idx:j]):
start = idx + 1
continue
for k in range(idx, j):
used[k] = True
# Use the original-case slice from `text` for the PIIMatch text
original_slice = text[idx:j]
matches.append(_mk(original_slice, idx, j, ft, conf))
# print("Detected PIIs >>>>", _mk(original_slice, idx, j, ft, conf))
start = j
matches.sort(key=lambda x: (x.start, x.end))
return matches
def detect_pii_combined(user_text, response_text, provider, model_id):
"""
Detect PII in BOTH user input and response in a SINGLE LLM call.
This is more efficient than separate calls.
Returns: (user_pii_items, response_pii_items)
"""
logger.info(" π [LLM CALL] Detecting PII in both user input and response (COMBINED)...")
start_time = time.time()
pii_cats = list(PII_TYPE_TO_CATEGORY.keys())
client = LLM_CLIENTS.get(provider)
if not client or (not user_text and not response_text):
return [], []
detection_prompt = f"""Identify PII in the two texts below. Only flag information that directly identifies or could be linked back to THIS specific user.
CATEGORIES (use only these type values):
"name" β person's name
"contact" β email, phone, username, social handle
"address" β specific street, city, postal code the user states as their own
"workplace" β specific named employer or school the user states as their own
"health" β medical conditions, medications, diagnoses
"financial" β salary, account numbers, credit cards
"sensitive" β ethnicity, religion, sexual orientation, political views
NEVER flag:
- Academic disciplines or research fields (e.g. "Machine Learning", "Industrial Engineering", "Computer Science")
- Job titles or roles (e.g. "software engineer", "data analyst")
- Generic sectors or industries (e.g. "healthcare sector", "financial institutions")
- Places mentioned in general advice or background context (only flag when user says "I live/work/study at X")
- Technology names or product names
- Pronouns (e.g., "you, "I", "we")
- Never flag any of the following strings as PII: {_DP_EMAIL_PLACEHOLDER}, {_DP_PHONE_PLACEHOLDER}, {_DP_SSN_PLACEHOLDER}, {_DP_ID_PLACEHOLDER}
EXAMPLES
--------
Text: "I am Sarah Chen. I study Industrial Engineering at MIT and I work part-time at Google."
Correct: [{{"type":"name","value":"Sarah Chen","confidence":0.99}},{{"type":"workplace","value":"MIT","confidence":0.95}},{{"type":"workplace","value":"Google","confidence":0.95}}]
Wrong: [{{"type":"workplace","value":"Industrial Engineering","confidence":0.80}}] β discipline, NOT a workplace
Text: "I have a Master's degree in Machine Learning and I'm looking for jobs in AI."
Correct: []
Wrong: [{{"type":"workplace","value":"Machine Learning","confidence":0.75}}] β field of study, never PII
Text: "I'm Raymond Phillips. I live at 42 Oak Street, Boston. I take metformin for my diabetes."
Correct: [{{"type":"name","value":"Raymond Phillips","confidence":0.99}},{{"type":"address","value":"42 Oak Street, Boston","confidence":0.97}},{{"type":"health","value":"metformin","confidence":0.93}},{{"type":"health","value":"diabetes","confidence":0.96}}]
Text: "What are the best hospitals in New York for cardiology treatment?"
Correct: []
Wrong: [{{"type":"address","value":"New York","confidence":0.80}}] β general context, not the user's location
TEXT 1 (user): {user_text[:2000]}
TEXT 2 (assistant): {response_text[:2000]}
Respond ONLY with JSON, no other text.
For example: {{"text1_pii": [{{"type": "name", "value": "Sarah Chen", "confidence": 0.98}}], "text2_pii": [{{"type": "address", "value": "Boston", "confidence": 0.99}}]}}"""
try:
# response = client.responses.create(
response = client.chat.completions.create(
model=model_id,
messages=[{"role": "user", "content": detection_prompt}],
# max_output_tokens=MAX_TOKENS_PII_DETECTION,
max_tokens=MAX_TOKENS_PII_DETECTION,
temperature=TEMPERATURE_INTERNAL_TASKS,
timeout=25
)
response_text_llm = response.choices[0].message.content.strip()
# Extract JSON
json_match = re.search(r'```json\s*(\{.*?\})\s*```', response_text_llm, re.DOTALL)
if json_match:
json_str = json_match.group(1)
else:
json_match = re.search(r'\{.*\}', response_text_llm, re.DOTALL)
if json_match:
json_str = json_match.group(0)
else:
json_str = response_text_llm
result = json.loads(json_str)
user_pii_items = result.get('text1_pii', [])
response_pii_items = result.get('text2_pii', [])
elapsed = time.time() - start_time
logger.info(f" β PII detection completed in {elapsed:.2f}s (found {len(user_pii_items)} in user, {len(response_pii_items)} in response)")
return user_pii_items, response_pii_items
except Exception as e:
logger.info(f" β Error detecting PII: {e}")
return [], []
# ============================================================
# SECTION 5 β RETRIEVER CLASSES
# ============================================================
@dataclass
class RetrievalResult:
"""Container for retrieval results."""
document: object # LangChain Document
score: float
sources: dict = field(default_factory=dict)
def strip_special_chars(text):
"""Strip digits and special characters."""
return re.sub(r'[^a-zA-Z]', '', text.lower())
def epsilon_adjusted_threshold(epsilon, base=RAG_MIN_SIMILARITY_THRESHOLD, scale=RAG_DP_THRESHOLD_SCALE):
"""
Compute a similarity threshold that tightens as epsilon falls.
At epsilon=inf (no DP) β threshold = base (e.g. 0.30)
At epsilon=1 β threshold β base + 0.12 (e.g. 0.42)
At epsilon=0.1 β threshold β base + 0.29 (e.g. 0.59)
The log1p curve means the boost is largest at very small epsilon
(strong privacy) and negligible at large epsilon (no privacy).
The cap at 0.92 prevents the threshold from becoming unreachable.
"""
if epsilon == float("inf"):
return base
boost = scale * math.log1p(1.0 / max(epsilon, 1e-6))
return min(base + boost, 0.92)
# ============================================================
# SECTION 5b β INPUT DIFFERENTIAL PRIVACY (WORD-LEVEL LDP)
# ============================================================
#
# Literature basis
# ----------------
# Primary:
# Tong et al. (2025). "InferDPT: Privacy-Preserving Inference for
# Closed-Box Large Language Models." IEEE Transactions on Dependable
# and Secure Computing 22(5), 4625-4640.
# β RANTEXT mechanism: for each content token, sample a replacement
# from the vocabulary β exp(Ρ·sim(w,w')/2Ξ) using the Exponential
# Mechanism on word embeddings. Satisfies Ξ΅-LDP per token.
#
# Supporting:
# Yue et al. (2021). "Differential Privacy for Text Analytics via
# Natural Text Sanitization." ACL Findings.
# β SanText/CusText: original LDP-on-embeddings paradigm.
#
# Novel contribution vs. Tong et al.
# ------------------------------------
# Rather than replacing the visible text shown to the user (which would
# confuse non-expert users and be unrelated to our tool's purpose), we:
# β’ Send the PERTURBED text silently to the LLM.
# β’ Display the ORIGINAL text in the chat bubble.
# β’ Attach a visible DP badge + hover tooltip to each user bubble,
# communicating (a) that perturbation occurred, (b) how many words
# were changed, and (c) the Ξ΅ guarantee it satisfied.
# This preserves transparency without exposing the noisy output, and is
# more aligned with usable-privacy principles for non-expert audiences.
# ββ DP vocabulary and stopwords: loaded from external text files βββββββββββββ
# Place dp_vocab.txt and dp_stopwords.txt in the same directory as this script.
# Each file: one word per line, lines starting with # are comments.
# Fallback to minimal built-in lists if the files are missing.
def _load_dp_word_file(filename, fallback):
"""Load a word list from a .txt file; fall back to *fallback* if missing."""
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
try:
with open(path, "r", encoding="utf-8") as f:
words = [
line.strip().lower()
for line in f
if line.strip() and not line.startswith("#")
]
words = list(dict.fromkeys(words)) # deduplicate, preserve order
logger.info("Loaded %d words from %s", len(words), filename)
return words
except FileNotFoundError:
logger.warning(
"%s not found β using built-in fallback (%d words). "
"Create the file next to the script for an extended list.",
filename, len(fallback),
)
return list(fallback)
except Exception as exc:
logger.error("Failed to load %s: %s", filename, exc)
return list(fallback)
_DP_INPUT_VOCAB = _load_dp_word_file("dp_vocab.txt", [])
_DP_STOPWORDS = set(_load_dp_word_file("dp_stopwords.txt", []))
# ββ Structured-token DP redaction βββββββββββββββββββββββββββββββββββββββββββββ
_DP_EMAIL_PLACEHOLDER = "someone@example.com"
_DP_PHONE_PLACEHOLDER = "555-000-0000"
_DP_SSN_PLACEHOLDER = "000-00-0000"
_DP_ID_PLACEHOLDER = "000000"
_DP_NUMBER_PLACEHOLDER = "some"
_RE_EMAIL = re.compile(r'[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}')
_RE_PHONE = re.compile(
r'(?<!\w)(?:\+?1[\s\-.]?)?(?:\(?\d{3}\)?[\s\-.]?)\d{3}[\s\-.]?\d{4}(?!\w)'
)
_RE_SSN = re.compile(
r'(?<!\w)'
r'(?:\d{3}-\d{2}-\d{4}' # formatted: 123-45-6789
r'|\d{9})' # unformatted: 123456789
r'(?!\w)'
)
_RE_ID_NUMBER = re.compile(
r'(?<!\w)\d{6,}(?!\w)' # 6+ consecutive digits (passport, national ID, etc.)
)
_RE_NUMBER = re.compile(r'(?<!\w)\d+(?:[.,]\d+)*(?!\w)')
class DPInputPerturber:
"""Word-level Local DP perturbation of user input using the Exponential Mechanism.
Each content word w is replaced by a vocabulary word w' sampled with
probability proportional to exp(Ξ΅ Β· cos_sim(w, w') / 2Ξ), where Ξ=2
is the global sensitivity of cosine similarity (range [-1, 1]).
This satisfies Ξ΅-Local Differential Privacy per token (Definition 1 /
Equation 2, Tong et al. 2025). Across independently perturbed tokens
the parallel composition theorem (Dwork & Roth 2014) keeps the per-token
budget constant rather than compounding.
Vocabulary embeddings are cached at the class level so they are computed
only once per process regardless of how many turns are processed.
"""
# Class-level embedding cache (computed once)
_vocab_matrix = None # np.ndarray, shape (V, d), L2-normalised
_vocab_words = None # list[str], aligned with _vocab_matrix rows
_SENSITIVITY = 2.0 # global sensitivity of cosine similarity
_STRUCTURED_TOKEN_DECAY = 0.15 # controls replacement rate for emails/numbers
def __init__(self, epsilon=1.0, embedding_model=None, min_word_len=3):
self.epsilon = epsilon
self.embedding_model = embedding_model
self.min_word_len = min_word_len
self._build_vocab_cache()
# ------------------------------------------------------------------
# Vocabulary cache β built once per process
# ------------------------------------------------------------------
def _build_vocab_cache(self):
if DPInputPerturber._vocab_matrix is not None:
return
if self.embedding_model is None:
logger.warning("DPInputPerturber: no embedding model β perturbation disabled.")
return
try:
logger.info("DPInputPerturber: embedding vocabulary (%d words)β¦", len(_DP_INPUT_VOCAB))
raw = self.embedding_model.embed_documents(_DP_INPUT_VOCAB)
mat = np.array(raw, dtype=np.float32)
norms = np.linalg.norm(mat, axis=1, keepdims=True)
DPInputPerturber._vocab_matrix = mat / np.clip(norms, 1e-10, None)
DPInputPerturber._vocab_words = list(_DP_INPUT_VOCAB)
logger.info("DPInputPerturber: vocabulary ready (%d words).", len(_DP_INPUT_VOCAB))
except Exception as exc:
logger.error("DPInputPerturber: vocabulary embedding failed: %s", exc)
def is_ready(self):
return DPInputPerturber._vocab_matrix is not None
# ------------------------------------------------------------------
# Exponential mechanism for a single word vector
# ------------------------------------------------------------------
def _sample_replacement(self, word_vec_normed, original_word):
V = DPInputPerturber._vocab_matrix
words = DPInputPerturber._vocab_words
sims_vocab = V @ word_vec_normed # (V,)
sims_all = np.append(sims_vocab, 1.0) # (V+1,) β original appended
logits = (self.epsilon * sims_all) / (2.0 * self._SENSITIVITY)
logits -= logits.max()
probs = np.exp(logits) / np.exp(logits).sum()
idx = np.random.choice(len(sims_all), p=probs)
if idx == len(words): # last slot = original
return original_word, float(probs[idx])
return words[idx], float(probs[idx])
# ------------------------------------------------------------------
# Token helpers
# ------------------------------------------------------------------
@staticmethod
def _strip_punct(word):
return word.strip(".,!?;:\"'()[]{}ββ-")
@staticmethod
def _split_affixes(token):
"""Return (prefix_punct, core, suffix_punct)."""
prefix, suffix, s = "", "", token
while s and s[0] in "\"'([{":
prefix += s[0]; s = s[1:]
while s and s[-1] in ".,!?;:\"'()[]{}ββ":
suffix = s[-1] + suffix; s = s[:-1]
return prefix, s, suffix
def _redact_structured_tokens(self, tokens):
"""Probabilistically redact emails, phone numbers, SSNs, IDs, and numbers.
Uses exponential decay: p_replace = exp(-Ξ΅ Β· DECAY), so:
- small Ξ΅ (strong privacy) β p approaches 1 (almost always replaced)
- large Ξ΅ (weak privacy) β p approaches 0 (almost always kept)
- Ξ΅ = β is already handled by the early return in perturb()
"""
p_replace = float(np.exp(-self.epsilon * self._STRUCTURED_TOKEN_DECAY))
substitutions = []
for i, tok in enumerate(tokens):
placeholder = None
if _RE_EMAIL.search(tok):
placeholder = _RE_EMAIL.sub(_DP_EMAIL_PLACEHOLDER, tok)
elif _RE_PHONE.search(tok):
placeholder = _RE_PHONE.sub(_DP_PHONE_PLACEHOLDER, tok)
elif _RE_SSN.search(tok):
placeholder = _RE_SSN.sub(_DP_SSN_PLACEHOLDER, tok)
elif _RE_ID_NUMBER.search(tok):
placeholder = _RE_ID_NUMBER.sub(_DP_ID_PLACEHOLDER, tok)
elif _RE_NUMBER.search(tok): # β was fullmatch
placeholder = _RE_NUMBER.sub(_DP_NUMBER_PLACEHOLDER, tok) # β was constant
if placeholder is None or placeholder == tok:
continue
if np.random.random() < p_replace:
substitutions.append({
"original": tok,
"replacement": placeholder,
"position": i,
"prob": p_replace,
})
tokens[i] = placeholder
return substitutions
# ------------------------------------------------------------------
# Public API
# ------------------------------------------------------------------
def perturb(self, text):
if self.epsilon == float("inf") or not self.is_ready():
return text, []
tokens = text.split()
if not tokens:
return text, []
new_tokens = list(tokens)
# ββ Step 1: structured-token redaction (emails, phones, numbers) ββββββ
substitutions = self._redact_structured_tokens(new_tokens)
# ββ Step 2: exponential-mechanism perturbation of content words ββββββββ
content_idx = [
i for i, tok in enumerate(new_tokens)
if self._strip_punct(tok).lower() not in _DP_STOPWORDS
and len(self._strip_punct(tok)) >= self.min_word_len
and self._strip_punct(tok).replace("'", "").isalpha()
]
if not content_idx:
return " ".join(new_tokens), substitutions
content_words = [self._strip_punct(new_tokens[i]) for i in content_idx]
try:
raw_embs = self.embedding_model.embed_documents(content_words)
except Exception as exc:
logger.warning("DPInputPerturber: embed call failed (%s) β skipping.", exc)
return " ".join(new_tokens), substitutions
embs = np.array(raw_embs, dtype=np.float32)
norms = np.linalg.norm(embs, axis=1, keepdims=True)
embs = embs / np.clip(norms, 1e-10, None)
for local_i, global_i in enumerate(content_idx):
original_core = content_words[local_i]
replacement, prob = self._sample_replacement(embs[local_i], original_core.lower())
if replacement.lower() == original_core.lower():
continue
prefix, _, suffix = self._split_affixes(new_tokens[global_i])
if new_tokens[global_i][len(prefix):len(prefix) + 1].isupper():
replacement = replacement.capitalize()
new_tokens[global_i] = prefix + replacement + suffix
substitutions.append({
"original": original_core,
"replacement": replacement,
"position": global_i,
"prob": prob,
})
return " ".join(new_tokens), substitutions
def _dp_badge_html(dp_metadata):
"""Build the inline DP badge appended to a user message bubble.
The badge pill is right-aligned via its wrapper div (handled in
fmt_conversation). The tooltip shows the full perturbed sentence
with substituted words highlighted (green = replacement; the
original word is shown in red strikethrough before it).
Parameters
----------
dp_metadata : dict
epsilon β float privacy budget
num_substitutions β int number of words changed
substitutions β list [{original, replacement, position, prob}]
original_text β str what the user typed
perturbed_text β str what was sent to the LLM
"""
if not dp_metadata:
return ""
eps = dp_metadata.get("epsilon", float("inf"))
n_sub = dp_metadata.get("num_substitutions", 0)
subs = dp_metadata.get("substitutions", [])
orig = dp_metadata.get("original_text", "")
pert = dp_metadata.get("perturbed_text", "")
eps_str = f"{eps:.1f}" if eps != float("inf") else "β"
# ββ Build the highlighted perturbed sentence ββββββββββββββββββββββββββ
# Map token position β substitution entry
pos_to_sub = {s["position"]: s for s in subs}
if pert and subs:
tokens = pert.split()
rendered_tokens = []
for i, tok in enumerate(tokens):
if i in pos_to_sub:
sub = pos_to_sub[i]
orig_w = _esc(sub["original"])
new_w = _esc(tok) # the replacement word (may be capitalised)
rendered_tokens.append(
f'<del style="color:#c0392b;font-family:inherit;">{orig_w}</del>'
f' '
f'<span style="background:#d4edda;color:#155724;border-radius:3px;'
f'padding:1px 4px;font-weight:600;">{new_w}</span>'
)
else:
rendered_tokens.append(_esc(tok))
sentence_html = " ".join(rendered_tokens)
elif pert:
sentence_html = _esc(pert)
else:
sentence_html = '<em style="color:#888;">β</em>'
# ββ Tooltip content βββββββββββββββββββββββββββββββββββββββββββββββββββ
tooltip_content = (
f'<div style="font-weight:700;font-size:1.05em;margin-bottom:6px;color:#111;">'
f'π Privacy Protection Applied</div>'
f'<div style="margin-bottom:8px;color:#444;font-size:0.85em;line-height:1.5;">'
f'<strong>{n_sub} word{"s" if n_sub != 1 else ""}</strong> in your message '
f'{"were" if n_sub != 1 else "was"} automatically replaced by similar words '
f'before the AI read it. ' # (Ξ΅ = {eps_str}). '
f'Your original message is shown in the chat above.'
f'</div>'
f'<div style="height:1px;background:#ddd;margin:6px 0;"></div>'
f'<div style="font-weight:600;color:#2c3e50;margin-bottom:5px;font-size:0.9em;">'
f'What the AI actually received:</div>'
f'<div style="background:#f8f8f8;border:1px solid #ddd;border-radius:6px;'
f'padding:8px 10px;font-size:0.92em;line-height:1.6;color:#222;">'
f'{sentence_html}'
f'</div>'
f'<div style="margin-top:6px;font-size:0.82em;color:#666;">'
f'<del style="color:#c0392b;">red</del> = original | '
f'<span style="background:#d4edda;color:#155724;border-radius:3px;'
f'padding:1px 4px;">green</span> = replacement'
f'</div>'
)
# ββ Badge pill + tooltip wrapper ββββββββββββββββββββββββββββββββββββββ
badge_html = (
f'<span class="tt-anchor" style="display:inline-block;">'
f'<span style="display:inline-flex;align-items:center;gap:4px;'
f'background:#e8f4fd;border:1px solid #7ec8e3;border-radius:12px;'
f'padding:2px 10px;font-size:0.78em;color:#0a5275;cursor:help;'
f'font-weight:600;user-select:none;white-space:nowrap;">'
f'π DP-protected · ' # Ξ΅={eps_str} ·
f'{n_sub} word{"s" if n_sub!=1 else ""} changed'
f'</span>'
f'<span class="tt-popup" style="width:600px;font-size:0.82em!important;">'
f'{tooltip_content}'
f'</span>'
f'</span>'
)
return badge_html
# ============================================================
# SECTION 6 β CORPUS LOADING (GENERIC)
# ============================================================
_GLOBAL_RETRIEVER = None
_DP_INPUT_PERTURBER = None # DPInputPerturber instance, initialised lazily
# Process-level cache: scenario_mode β loaded retriever object.
# Populated lazily on first request for each scenario.
_SCENARIO_RETRIEVERS = {}
_SCENARIO_RETRIEVERS_LOCK = threading.Lock()
def get_scenario_retriever(scenario_mode: str):
"""Return (and lazily load) the system retriever for the given scenario_mode.
Thread-safe. Falls back to the default RETRIEVAL_PICKLE_PATH if the
scenario has no dedicated entry or its file cannot be loaded.
"""
key = str(scenario_mode).strip().lower()
if key in _SCENARIO_RETRIEVERS:
return _SCENARIO_RETRIEVERS[key]
with _SCENARIO_RETRIEVERS_LOCK:
# Double-checked locking
if key in _SCENARIO_RETRIEVERS:
return _SCENARIO_RETRIEVERS[key]
path = SCENARIO_RETRIEVER_PATHS.get(key) or DEFAULT_RETRIEVAL_PICKLE_PATH
logger.info(f"π Loading retriever for scenario '{key}' from: {path}")
if path is None:
logger.error(f" Retriever for scenario '{key}' is None and will be ignored!")
retriever = _GLOBAL_RETRIEVER
else:
try:
retriever = load_retriever_components(path)
logger.info(f" β Retriever loaded for scenario '{key}'")
except Exception as e:
logger.error(f" β Failed to load retriever for scenario '{key}': {e}")
# Fall back to whatever the global default retriever is
retriever = _GLOBAL_RETRIEVER
_SCENARIO_RETRIEVERS[key] = retriever
return retriever
def _init_dp_input_perturber(retriever):
"""Initialise the global DPInputPerturber, reusing the retriever embedding model.
LangChain FAISS stores the embedding function as vectorstore.embeddings.
Reusing it avoids loading a second copy of the sentence-transformer weights.
Falls back to creating a fresh HuggingFaceEmbeddings if that attribute is
absent (e.g. when the retriever was pickled without it).
"""
global _DP_INPUT_PERTURBER
if _DP_INPUT_PERTURBER is not None:
return # already initialised
embed_fn = None
try:
embed_fn = retriever.vectorstore.embeddings
logger.info("DPInputPerturber: reusing vectorstore embedding model.")
except AttributeError:
pass
if embed_fn is None:
try:
embed_fn = HuggingFaceEmbeddings(
model_name=EMBEDDING_MODEL,
model_kwargs={"device": "cpu"},
encode_kwargs={"normalize_embeddings": True},
)
logger.info("DPInputPerturber: created standalone HuggingFaceEmbeddings.")
except Exception as exc:
logger.error("DPInputPerturber: could not create embedding model: %s", exc)
return
_DP_INPUT_PERTURBER = DPInputPerturber(epsilon=1.0, embedding_model=embed_fn) # epsilon is placeholder; overridden per-request
logger.info("DPInputPerturber: initialised and ready.")
def load_retriever(pickle_path=None):
"""Load retriever from component-based pickle (FAISS-compatible)."""
global _GLOBAL_RETRIEVER
path = pickle_path or DEFAULT_RETRIEVAL_PICKLE_PATH
if path is None:
_init_dp_input_perturber(None)
return None
try:
_GLOBAL_RETRIEVER = load_retriever_components(path)
logger.info(f"β Retriever loaded from {path}")
_init_dp_input_perturber(_GLOBAL_RETRIEVER) # initialise Input DP perturber
return _GLOBAL_RETRIEVER
except Exception as e:
logger.error(f"β Failed to load retriever from {path}: {e}")
logger.info(" System will work without RAG.")
# Still initialise the Input-DP perturber using a standalone embedding
# model so word-level DP perturbation works even without a RAG retriever.
_init_dp_input_perturber(None)
return None
def get_retriever_lazy():
"""Lazy load retriever on first use."""
global _GLOBAL_RETRIEVER
if _GLOBAL_RETRIEVER is None:
logger.info("π Loading retriever for first time...")
load_retriever()
logger.info("β Retriever loaded")
return _GLOBAL_RETRIEVER
def load_retriever_async():
"""Load retriever in background thread."""
thread = threading.Thread(target=load_retriever, daemon=True)
thread.start()
return thread
def get_retriever():
"""Get the global retriever instance."""
return _GLOBAL_RETRIEVER
def _set_system_retriever(new_retriever):
"""Set the global retriever instance and (re-)initialise Input DP perturber."""
global _GLOBAL_RETRIEVER, _DP_INPUT_PERTURBER
_GLOBAL_RETRIEVER = new_retriever
# Reset perturber so it is re-initialised with the new retriever's embedding model
_DP_INPUT_PERTURBER = None
if new_retriever is not None:
_init_dp_input_perturber(new_retriever)
# ============================================================
# SECTION 7 β DEMO MODE (ZERO API CALLS)
# ============================================================
def fabricate_demo_pii(user_text):
"""Fabricate PII detections for demo mode - NO LLM calls."""
# Simple heuristic: look for common name patterns
pii_list = []
# Look for "I am" or "my name is" patterns
name_patterns = [
(r"(?:I am|I'm|my name is|call me)\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)?)", "name"),
(r"\b([A-Z][a-z]+\s+[A-Z][a-z]+)\b", "name"),
]
for pattern, pii_type in name_patterns:
for match in re.finditer(pattern, user_text, re.IGNORECASE):
pii_list.append({
"text": match.group(1),
"start": match.start(1),
"end": match.end(1),
"type": pii_type
})
break # Only first match
# If no name found, fabricate one from the text
if not pii_list:
words = user_text.split()
if len(words) >= 2:
# Use first two capitalized words as fake name
capitalized = [w for w in words if w and w[0].isupper() and len(w) > 2]
if len(capitalized) >= 2:
fake_name = f"{capitalized[0]} {capitalized[1]}"
idx = user_text.find(fake_name)
if idx != -1:
pii_list.append({
"text": fake_name,
"start": idx,
"end": idx + len(fake_name),
"type": "name"
})
return pii_list
def fabricate_demo_response(user_text):
"""Fabricate a response for demo mode - NO LLM calls."""
# Generate a generic but contextual response
keywords = ["job", "work", "career", "health", "medical", "hobby", "interest"]
context = "general"
for kw in keywords:
if kw in user_text.lower():
if kw in ["job", "work", "career"]:
context = "job"
elif kw in ["health", "medical"]:
context = "health"
elif kw in ["hobby", "interest"]:
context = "hobby"
break
responses = {
"job": "Based on your background and interests, here are some suggestions:\n\n1. Consider roles in the technology sector - your skills would be valuable there.\n2. Look into positions at medium-sized companies (50-200 employees) where you can make an impact.\n3. Check job boards like LinkedIn and Indeed for opportunities in your area.\n\nWould you like me to help you refine your search based on specific criteria?",
"health": "Here are some preventive health screenings to consider:\n\n1. Annual physical exam with bloodwork (cholesterol, glucose, etc.)\n2. Blood pressure monitoring\n3. Age-appropriate cancer screenings\n4. Dental check-up every 6 months\n\nFor affordable options, check with local community health centers or your insurance provider's network. Would you like more specific recommendations?",
"hobby": "Based on common interests, here are some hobbies you might enjoy:\n\n1. Outdoor activities like hiking or cycling\n2. Creative pursuits such as photography or painting\n3. Learning new skills through online courses\n4. Community involvement through volunteering\n\nWhat type of activities do you find most appealing?",
"general": "I'd be happy to help! To provide more personalized recommendations, could you tell me a bit more about:\n\n1. Your current interests or goals\n2. How much time you have available\n3. Any specific areas you'd like to explore\n\nThis will help me give you more relevant suggestions."
}
return responses.get(context, responses["general"])
def fabricate_demo_probs(user_text, has_rag):
"""Fabricate probability distributions for demo mode - NO LLM calls."""
# Generate probabilities based on text analysis
base_probs = {
"Gender": {"Male": 0.55, "Female": 0.45},
"Age bin": {"0-17": 0.05, "18-29": 0.25, "30-44": 0.45, "45-59": 0.20, "60+": 0.05},
"Marital Status": {"Single": 0.45, "Married": 0.40, "Divorced": 0.10, "Widowed": 0.05},
"Finance Status": {"Low": 0.25, "Medium": 0.50, "High": 0.25},
"Education": {"High School": 0.15, "Bachelor's": 0.50, "Master's": 0.25, "PhD": 0.10},
"Locale": {
"United States": 0.60, "Canada": 0.15, "United Kingdom": 0.10,
"Australia": 0.05, "India": 0.04, "Ireland": 0.02,
"New Zealand": 0.02, "Philippines": 0.02, "Israel": 0.01, "Germany": 0.01, "Italy":0.01,"France":0.01,
}
}
if has_rag:
# RAG adds confidence - shift probabilities more extremely
rag_probs = {
"Gender": {"Male": 0.65, "Female": 0.35},
"Age bin": {"0-17": 0.02, "18-29": 0.18, "30-44": 0.55, "45-59": 0.20, "60+": 0.05},
"Marital Status": {"Single": 0.55, "Married": 0.30, "Divorced": 0.10, "Widowed": 0.05},
"Finance Status": {"Low": 0.15, "Medium": 0.50, "High": 0.35},
"Education": {"High School": 0.08, "Bachelor's": 0.52, "Master's": 0.30, "PhD": 0.10},
"Locale": {
"United States": 0.70, "Canada": 0.08, "United Kingdom": 0.08,
"Australia": 0.04, "India": 0.02, "Ireland": 0.01,
"New Zealand": 0.02, "Philippines": 0.01, "Israel": 0.01, "Germany": 0.01, "Italy":0.01,"France":0.01,
}
}
return rag_probs, base_probs
else:
return base_probs, base_probs
def fabricate_demo_rag_docs(user_text):
"""Fabricate RAG documents for demo mode - NO retrieval."""
# Generate fake documents that seem relevant
templates = [
"User profile indicates experience in software development with 5 years in web technologies. Previous roles include positions at tech startups in the Bay Area.",
"Background shows interest in preventive healthcare and fitness. Regular physical activity noted, with preference for outdoor activities.",
"Career history includes work at medium-sized technology companies. Skills include project management and technical leadership.",
"Education background: Bachelor's degree in Computer Science. Additional certifications in cloud computing and data analysis.",
"Location history shows residence in major metropolitan areas. Current location indicates access to urban amenities and services."
]
# Select relevant templates based on keywords
selected = random.sample(templates, min(3, len(templates)))
return [(doc, 0.85 - i*0.05) for i, doc in enumerate(selected)]
def process_demo_message(user_input, show_tips, show_rag_hl, show_pii_hl, state, use_rag):
"""Process message in demo mode - ZERO API calls, all data fabricated."""
# Step 1: Fabricate PII in user input (NO LLM call)
user_pii_data = fabricate_demo_pii(user_input)
user_pii_matches = []
for pii_item in user_pii_data:
user_pii_matches.append(PIIMatch(
text=pii_item['text'],
start=pii_item['start'],
end=pii_item['end'],
fine_type=pii_item['type'],
category=PII_TYPE_TO_CATEGORY.get(pii_item['type'], PIICategory.IDENTITY),
confidence=0.9
))
# Step 2: Fabricate RAG docs (NO retrieval)
rag_docs = fabricate_demo_rag_docs(user_input) if use_rag else []
# Step 3: Fabricate RAG links in user input
user_rag_links = []
if use_rag and rag_docs:
query_words = user_input.lower().split()
for doc_text, score in rag_docs[:1]:
doc_words = set(doc_text.lower().split())
overlap = [w for w in query_words if w in doc_words and len(w) > 3]
if overlap:
for word in overlap[:1]:
idx = user_input.lower().find(word)
if idx != -1:
user_rag_links.append(RAGLink(
text=word,
start=idx,
end=idx + len(word),
corpus_snippets=[(doc_text[:RAG_LINKAGE_STORED_EXCERPT_LENGTH], score)],
top_similarity=score,
top_doc_text=doc_text[:RAG_LINKAGE_STORED_EXCERPT_LENGTH],
top_doc_score=score,
overlap_keywords=overlap[:3]
))
break
demo_evidence = {}
for attr in SENSITIVE_ATTRIBUTES:
demo_evidence[attr] = [
f"Based on context about {attr.lower()} from user input",
f"Relevant information suggesting {attr.lower()} from documents"
]
state.last_evidence_rag = demo_evidence
# Add user message
state.add("user", user_input, user_pii_matches, user_rag_links)
# Step 4: Fabricate response (NO LLM call)
llm_response = fabricate_demo_response(user_input)
# Step 5: Fabricate PII in response (NO LLM call)
response_pii_data = [] # Typically no PII in response
# Step 6: Fabricate RAG links in response
response_rag_links = []
if use_rag and rag_docs:
response_words = llm_response.lower().split()
for doc_text, score in rag_docs:
doc_words = set(doc_text.lower().split())
overlap = [w for w in response_words if w in doc_words and len(w) > 4]
if overlap:
for word in overlap[:1]:
idx = llm_response.lower().find(word)
if idx != -1:
response_rag_links.append(RAGLink(
text=word,
start=idx,
end=idx + len(word),
corpus_snippets=[(doc_text[:RAG_LINKAGE_STORED_EXCERPT_LENGTH], score)],
top_similarity=score,
top_doc_text=doc_text[:RAG_LINKAGE_STORED_EXCERPT_LENGTH],
top_doc_score=score,
overlap_keywords=overlap[:3]
))
break
if len(response_rag_links) >= 2:
break
# Add assistant message
state.add("assistant", llm_response, [], response_rag_links)
# Step 7: Fabricate probability distributions (NO LLM call)
probs_rag, probs_no_rag = fabricate_demo_probs(user_input, use_rag)
state.last_probs_rag = probs_rag
state.last_probs_no_rag = probs_no_rag
# Step 8: Calculate risk score
all_pii = user_pii_matches
all_rag = user_rag_links + response_rag_links
risk_score, risk_breakdown = calculate_privacy_risk(all_pii, all_rag, float('inf'))
# Build outputs
conv_html = fmt_conversation(state.messages, show_tips, show_rag_hl, show_pii_hl)
warning_html, demo_inference_metrics, demo_warning_shown = build_inference_warning(
user_input, probs_rag, probs_no_rag, use_rag, demo_evidence
)
# ββ Log demo interaction ββββββββββββββββββββββββββββββββββ
state._turn_count += 1
state._last_model = "demo"
state._last_epsilon = float("inf")
state._last_rag_enabled = use_rag
state._last_social_scraping = False
state._last_corpus_source = "demo"
state._demo_mode = True
append_interaction_log(
session_source=getattr(state, "_session_source", "demo"),
turn_number=state._turn_count,
demo_mode=True,
scenario_mode=getattr(state, "_scenario_mode", "real"),
persona_attributes={},
model="demo",
epsilon=float('inf'),
rag_enabled=use_rag,
show_risk=getattr(state, "_show_risk", True),
show_rag_highlights=show_rag_hl,
show_tips=show_tips,
show_pii_highlights=show_pii_hl,
show_settings=getattr(state, "_show_settings", False),
access_token=getattr(state, "_access_token", ""),
social_scraping_enabled=False,
corpus_source="demo",
uploaded_file_path=None,
user_prompt=user_input,
llm_response=llm_response,
risk_score=risk_score,
u_rag=user_rag_links,
r_rag=response_rag_links,
u_pii=user_pii_matches,
u_pii_perturbed=0,
r_pii=[],
inference_metrics=demo_inference_metrics,
inference_warning_shown=demo_warning_shown,
scraped_docs=None,
num_attributes_changed=0,
show_dp=getattr(state, "_show_dp", 1),
show_infr_attr_card=getattr(state, "_show_infr_attr_card", 1),
show_social_scraping=getattr(state, "_show_social_scraping", False),
show_upload_data=getattr(state, "_show_upload_data", False),
rag_corpus_path=getattr(state, "_rag_corpus_path", ""),
)
analysis = "### Demo Mode (NO API Calls)\n\nThis is a demonstration with fabricated data. "
analysis += f"**Detected {len(user_pii_matches)} PII(s)** in user prompt. "
analysis += f"**{len(user_rag_links)} RAG link(s)** in user text, "
analysis += f"**{len(response_rag_links)} RAG link(s)** in response.\n\n"
analysis += "β οΈ All data is fabricated for demonstration purposes.\n\n"
analysis += "Enable live mode by removing the `demo=1` parameter from the URL."
return conv_html, create_risk_display(risk_score, risk_breakdown), "", analysis, ""
# ============================================================
# SECTION 8 β ATTRIBUTE INFERENCE & METRICS
# ============================================================
class AttributeInferenceEngine:
"""Extract probabilities from LLM inference responses."""
# def extract_probabilities_from_response(self, response_text):
# """Extract probability distributions from JSON response."""
# try:
# # Try to extract JSON from the response
# json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
# if json_match:
# result = json.loads(json_match.group(0))
# return result
# except:
# pass
# return {}
def extract_probabilities_from_response(self, response_text):
"""Extract probability distributions AND evidence from JSON response.
Handles both evidence formats:
- new: [{"quote": "...", "type": "explicit"}, ...]
- legacy: ["...", ...] (plain strings, assumed "implicit")
"""
try:
# print("In extract_probabilities_from_response", response_text)
# Try to extract JSON from the response
json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
# print("In extract_probabilities_from_response", json_match)
if json_match:
result = json.loads(json_match.group(0))
# print("********In extract_probabilities_from_response", result)
# Restructure to separate probabilities and evidence
probabilities = {}
evidence = {} # {attr: [{"quote": str, "type": "explicit"|"implicit"}]}
for attr, data in result.items():
if isinstance(data, dict):
# New format with probabilities and evidence
if 'probabilities' in data:
probabilities[attr] = data['probabilities']
raw_ev = data.get('evidence', [])
# Normalise evidence items to dicts
normalised = []
for item in raw_ev:
if isinstance(item, dict):
normalised.append({
"quote": str(item.get("quote", item.get("text", ""))).strip(),
"type": item.get("type", "implicit"),
})
else:
# Legacy plain-string evidence
normalised.append({
"quote": str(item).strip(),
"type": "implicit",
})
evidence[attr] = normalised
else:
# Old format (backward compatibility)
probabilities[attr] = data
evidence[attr] = []
else:
# Fallback for unexpected format
probabilities[attr] = {}
evidence[attr] = []
probabilities = _fill_missing_with_uniform(probabilities)
# print('probabilities', probabilities, 'evidence', evidence)
return {'probabilities': probabilities, 'evidence': evidence}
except:
pass
logger.info("Returning nothing after extracting probabilities from response.....")
return {'probabilities': _fill_missing_with_uniform({}), 'evidence': {}}
def get_predictions(self, probs):
"""Get argmax predictions."""
preds = {}
for attr, dist in probs.items():
if dist:
preds[attr] = max(dist, key=dist.get)
return preds
class InferentialPrivacyMetrics:
"""Compute privacy metrics."""
@staticmethod
def compute_z(p_rag, p_no_rag):
"""Compute log-ratio."""
if p_no_rag <= 0:
return float('inf')
return math.log(p_rag / p_no_rag) if p_rag > 0 else -float('inf')
@staticmethod
def compute_lift(z):
"""Compute lift from z."""
return math.exp(z) - 1.0 if z != float('inf') else float('inf')
@staticmethod
def compute_inferential_privacy(probs_rag, probs_no_rag, ground_truth):
"""Compute inferential privacy metrics with robust error handling."""
metrics = {}
# Validate inputs are dictionaries
if not isinstance(probs_rag, dict):
probs_rag = {}
if not isinstance(probs_no_rag, dict):
probs_no_rag = {}
if not isinstance(ground_truth, dict):
ground_truth = {}
for attr in SENSITIVE_ATTRIBUTES:
try:
# Check if attribute exists in both probability dictionaries
if attr not in probs_rag or attr not in probs_no_rag:
continue
# Validate that values are dictionaries
if not isinstance(probs_rag[attr], dict) or not isinstance(probs_no_rag[attr], dict):
continue
# Get ground truth value
gt_val = ground_truth.get(attr)
if not gt_val:
continue
# Get probabilities with default 0.0
p_rag = probs_rag[attr].get(gt_val, 0.0)
p_no_rag = probs_no_rag[attr].get(gt_val, 0.0)
# Validate probabilities are numeric
if not isinstance(p_rag, (int, float)) or not isinstance(p_no_rag, (int, float)):
continue
# Handle NaN or negative values
if math.isnan(p_rag) or math.isnan(p_no_rag) or p_rag < 0 or p_no_rag < 0:
continue
# Compute metrics
z = InferentialPrivacyMetrics.compute_z(p_rag, p_no_rag)
lift = InferentialPrivacyMetrics.compute_lift(z)
metrics[attr] = {
'z': z,
'lift': lift,
'p_rag': p_rag,
'p_no_rag': p_no_rag
}
except (TypeError, ValueError, KeyError, AttributeError):
# Silently skip this attribute if any error occurs
continue
return metrics
# ============================================================
# SECTION 9 β PROMPT BUILDERS
# ============================================================
# def build_inference_prompt(user_prompt, retrieved_docs=None):
# """Build prompt for attribute inference."""
# ctx_note = ""
# if retrieved_docs:
# doc_snippets = "\n".join(
# f" [{i}] {doc.page_content[:200]}..."
# for i, doc in enumerate(retrieved_docs[:5], 1)
# )
# ctx_note = (
# f"Note: Retrieved {len(retrieved_docs)} documents about the user:\n"
# f"{doc_snippets}\n\n"
# )
#
# return f"""As an AI safety researcher, analyze the following to estimate probability distributions for sensitive user attributes.
#
# {ctx_note}User Query: {user_prompt}
#
# Based on the {'retrieved documents and ' if retrieved_docs else ''}user query, estimate probability distributions for:
#
# {json.dumps(ATTRIBUTE_VALUES_MAP, indent=2)}
#
# Respond with ONLY a JSON object with probability estimates.
# Each attribute maps to {{value: probability}}, probabilities sum to 1.0.
#
# Example format:
# {{
# "Gender": {{"Male": 0.7, "Female": 0.3}},
# "Age bin": {{"18-29": 0.2, "30-44": 0.5, "45-59": 0.3, "60+": 0.0, "0-17": 0.0}},
# ...
# }}
#
# Your JSON response:"""
def build_inference_prompt(user_prompt, retrieved_docs=None, conversation_state=None):
"""Build prompt for attribute inference with evidence extraction.
Including prior conversation turns ensures that attribute lifts remain
stable across the session: even when the current turn is benign, the LLM
can still see sensitive information disclosed in earlier turns, so the
inferred probabilities (and therefore lifts) reflect the full accumulated
disclosure rather than only the isolated current message.
"""
ctx_note = ""
if retrieved_docs:
doc_snippets = "\n".join(
f" [{i}] {doc.page_content[:200]}..."
for i, doc in enumerate(retrieved_docs[:5], 1)
)
ctx_note = (
f"Note: Retrieved {len(retrieved_docs)} documents about the user:\n"
f"{doc_snippets}\n\n"
)
# Include prior turns so inference is cumulative across the conversation.
# Only user messages are included (assistant replies may introduce bias).
# IMPORTANT: when DP was active for a prior turn, use the PERTURBED text
# (what the LLM actually saw) rather than the original unperturbed text.
history_note = ""
if conversation_state and conversation_state.messages:
prior = []
for i, msg in enumerate(conversation_state.messages):
if msg.role != "user":
continue
dp_meta = getattr(msg, "dp_metadata", None)
if dp_meta and dp_meta.get("perturbed_text"):
turn_text = dp_meta["perturbed_text"] # what the LLM saw
else:
turn_text = msg.content # no DP, use as-is
prior.append(f" Turn {i+1}: {turn_text[:300]}")
if prior:
history_note = (
"Prior conversation turns (earlier user messages):\n"
+ "\n".join(prior)
+ "\n\n"
)
prior_best_note = ""
if conversation_state and conversation_state._best_probs_rag:
lines = []
for attr, dist in conversation_state._best_probs_rag.items():
if not dist:
continue
top_val = max(dist, key=dist.get)
top_p = dist[top_val]
evs = conversation_state._best_evidence_rag.get(attr, [])
ev_type = "explicit" if any(
(e.get("type") if isinstance(e, dict) else "implicit") == "explicit"
for e in evs
) else "implicit"
lines.append(f" {attr}: {top_val} (confidence {top_p:.0%}, {ev_type} evidence)")
if lines:
prior_best_note = (
"Previously inferred with highest confidence β treat as prior, "
"only revise if strong new contradicting evidence exists:\n"
+ "\n".join(lines)
+ "\n\n"
)
return f"""As an AI safety researcher, analyze the following to estimate probability distributions for sensitive user attributes.
{ctx_note}{history_note}{prior_best_note}Current User Query: {user_prompt}
Based on the {'retrieved documents, prior conversation, and ' if (retrieved_docs or history_note) else ''}current user query, estimate probability distributions for ALL of the following attributes:
{json.dumps(ATTRIBUTE_VALUES_MAP, indent=2)}
IMPORTANT: You MUST include every attribute listed above in your response - do not omit any.
For each attribute, also provide 1 evidence item: a short direct quotation (max 300 chars) supporting your prediction, classified as:
- "explicit": the attribute is directly stated (e.g., "I am 35 years old")
- "implicit": inferred from indirect hints (e.g., job or hobbies suggest age)
Respond with ONLY a valid JSON object. Each attribute must follow this structure:
{{
"<AttributeName>": {{
"probabilities": {{"<Value1>": <float>, "<Value2>": <float>, ...}},
"evidence": [{{"quote": "<short supporting quote>", "type": "explicit|implicit"}}]
}},
...
}}
Example (showing one attribute β repeat this structure for all {len(ATTRIBUTE_VALUES_MAP)} attributes):
{{
"Gender": {{
"probabilities": {{"Male": 0.7, "Female": 0.3}},
"evidence": [{{"quote": "my wife and I went fishing", "type": "implicit"}}]
}},
...
}}
Rules:
- Include ALL 6 attributes. Missing any is an error.
- For each attribute, "probabilities" must contain every value listed in the attribute map above, and they must sum to 1.0.
- The estimated probabilities should be accurate as possible, rather than rounded up. For example, if you deduce a value of some attribute at probability 0.38, do not round it to 0.4, but report the precise probability 0.38.
- Use only the exact value strings listed above for each attribute
- "type" must be exactly "explicit" or "implicit"
Your JSON response:"""
def build_user_response_prompt(user_prompt, retrieved_docs=None, conversation_state=None):
"""
Build messages list for generating user-visible response with conversation history.
Returns a list of message dicts suitable for OpenAI-style chat completion API.
"""
messages = []
# System message with instructions
system_content = "You are a helpful assistant. Please provide helpful, personalized responses to the user's queries."
if retrieved_docs:
system_content += " Use the relevant context provided to enhance your responses when applicable."
system_content += f" Keep your response concise (up to {MAX_TOKENS_LLM_RESPONSE_GENERATION} words) and always end with a complete sentence."
messages.append({"role": "system", "content": system_content})
# Add conversation history from state (all previous turns)
if conversation_state and conversation_state.messages:
for msg in conversation_state.messages:
# Only include role and content, strip PII/RAG metadata
messages.append({
"role": msg.role,
"content": msg.content
})
# Build context from retrieved documents
ctx = ""
if retrieved_docs:
parts = []
for i, doc in enumerate(retrieved_docs[:10], 1):
parts.append(f"[{i}] {doc.page_content[:500]}")
ctx = "\n\nRelevant Context:\n" + "\n".join(parts)
# Add current user query with optional context
current_query = user_prompt
if ctx:
current_query = f"{user_prompt}\n{ctx}"
messages.append({"role": "user", "content": current_query})
return messages
# ============================================================
# SECTION 10 β RAG LINKAGE DETECTION
# ============================================================
# def find_rag_linkage(text, retrieved_docs):
# """Find spans in text that link to retrieved documents."""
# if not retrieved_docs:
# return []
#
# links = []
# text_lower = text.lower()
#
# # Simple keyword-based matching
# for doc in retrieved_docs:
# doc_text = doc.page_content if hasattr(doc, 'page_content') else str(doc)
# doc_words = set(w.lower() for w in doc_text.split() if len(w) > 4)
#
# # Find overlapping keywords
# for word in text.split():
# if len(word) <= 4:
# continue
# word_lower = word.lower().strip('.,!?;:')
# if word_lower in doc_words:
# idx = text_lower.find(word_lower)
# if idx != -1:
# links.append(RAGLink(
# text=word,
# start=idx,
# end=idx + len(word),
# corpus_snippets=[(doc_text[:200], 0.9)],
# top_similarity=0.9,
# top_doc_text=doc_text[:200],
# top_doc_score=0.9,
# overlap_keywords=[word_lower]
# ))
# break
#
# return links[:5] # Limit to top 5
def calculate_rouge_l(text1, text2):
"""
Calculate ROUGE-L (Longest Common Subsequence) similarity between two texts.
Returns:
float: ROUGE-L F1 score (0.0-1.0)
"""
def lcs_length(s1, s2):
"""Calculate longest common subsequence length."""
m, n = len(s1), len(s2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[m][n]
# Tokenize
tokens1 = text1.lower().split()
tokens2 = text2.lower().split()
if not tokens1 or not tokens2:
return 0.0
lcs_len = lcs_length(tokens1, tokens2)
# Calculate ROUGE-L using F1 score
if lcs_len == 0:
return 0.0
precision = lcs_len / len(tokens2)
recall = lcs_len / len(tokens1)
if precision + recall == 0:
return 0.0
f1 = (2 * precision * recall) / (precision + recall)
return f1
def calculate_cosine_similarity(text1, text2, doc_embedding=None):
"""
Calculate cosine similarity between two texts.
If doc_embedding is provided (from retrieval), use it directly.
Otherwise, fall back to simple word overlap similarity.
Args:
text1: First text string
text2: Second text string
doc_embedding: Pre-computed embedding vector for text2 (optional)
Returns:
float: Similarity score (0.0-1.0)
"""
# If we have pre-computed embeddings from the retriever, use them
if doc_embedding is not None:
# This would require computing embedding for text1
# For now, we'll use the fallback method
pass
# Fallback: Use word overlap as proxy for cosine similarity
# This is faster but less accurate than actual embeddings
words1 = set(re.sub(r'[^\w\s]', '', text1.lower()).split())
words2 = set(re.sub(r'[^\w\s]', '', text2.lower()).split())
# Remove stop words
words1 = words1 - _DP_STOPWORDS
words2 = words2 - _DP_STOPWORDS
if not words1 or not words2:
return 0.0
intersection = len(words1 & words2)
union = len(words1 | words2)
# Jaccard similarity as proxy for cosine similarity
jaccard = intersection / union if union > 0 else 0.0
# Scale to approximate cosine similarity behavior
# Jaccard tends to be lower than cosine, so we adjust
return min(1.0, jaccard * 1.3)
def combine_similarity_scores(rouge_l, cosine_sim):
"""
Combine ROUGE-L and cosine similarity into a single score.
Args:
rouge_l: ROUGE-L score (0.0-1.0)
cosine_sim: Cosine similarity score (0.0-1.0)
Returns:
float: Combined score (0.0-1.0)
"""
# Weighted average
combined = (ROUGE_WEIGHT * rouge_l) + (COSINE_WEIGHT * cosine_sim)
return combined
def extract_ngrams(text, n):
"""
Extract meaningful n-grams (sequences of n words) from text.
Filters out:
- N-grams that cross sentence boundaries (. ! ?)
- N-grams with too many stop words (must have at least 50% content words)
- N-grams that are too short
Args:
text: Input text string
n: Number of words in each n-gram (1, 2, or 3)
Returns:
list: List of dicts with 'text', 'start', 'end', 'words'
"""
# Use regex to find word boundaries
words_with_positions = []
for match in re.finditer(r'\b\w+\b', text):
words_with_positions.append({
'word': match.group(0),
'start': match.start(),
'end': match.end()
})
ngrams = []
for i in range(len(words_with_positions) - n + 1):
# Get n consecutive words
ngram_parts = words_with_positions[i:i + n]
# Build ngram text
start_pos = ngram_parts[0]['start']
end_pos = ngram_parts[-1]['end']
ngram_text = text[start_pos:end_pos]
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Filter 1: Skip if too short
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if len(ngram_text) < MIN_NGRAM_LENGTH:
continue
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Filter 2: Skip if crosses sentence boundaries
# Check for period, exclamation, or question mark followed by space
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if any(punct in ngram_text for punct in ['. ', '! ', '? ', '.\n', '!\n', '?\n']):
continue
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Filter 3: Skip if ngram has too many stop words
# Require at least 50% content words (not stop words)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
words = [p['word'].lower() for p in ngram_parts]
# Count content words (non-stop words)
content_words = [w for w in words if w not in _DP_STOPWORDS]
content_ratio = len(content_words) / len(words) if words else 0
# Require at least 50% content words for n-grams with 3+ words
# For 2-word n-grams, require at least 1 content word
if n >= 3 and content_ratio < 0.5:
continue
elif n == 2 and len(content_words) < 1:
continue
# Skip if ALL words are stop words (redundant but explicit)
if all(w in _DP_STOPWORDS for w in words):
continue
ngrams.append({
'text': ngram_text,
'start': start_pos,
'end': end_pos,
'words': words
})
return ngrams
def find_rag_linkage(text, retrieved_docs):
"""
Find spans in text that link to retrieved documents using similarity metrics.
This function:
1. Extracts n-grams (1-3 word sequences) from the text
2. Compares each n-gram to retrieved documents using ROUGE-L and cosine similarity
3. Highlights n-grams that exceed similarity thresholds
4. Filters out stop words and ensures whole word matching
Args:
text: Input text to find linkages in
retrieved_docs: List of retrieved documents from RAG system
Returns:
list: List of RAGLink objects with similarity scores
"""
if not retrieved_docs or not text:
return []
links = []
seen_positions = set() # Track already highlighted positions to avoid overlaps
# Build document corpus for comparison
doc_texts = []
doc_scores = {} # Store retrieval scores if available
doc_sources = {} # Store document sources
doc_urls = {} # Store document source URLs
for idx, doc in enumerate(retrieved_docs):
doc_text = doc.page_content if hasattr(doc, 'page_content') else str(doc)
doc_texts.append(doc_text)
# Try to extract retrieval score from metadata
if hasattr(doc, 'metadata') and isinstance(doc.metadata, dict):
score = doc.metadata.get('score', doc.metadata.get('similarity', None))
if score is not None:
doc_scores[idx] = float(score)
# Extract source information from metadata
source_type = doc.metadata.get('source', '')
# print("In find_rag_linkage", source_type)
if source_type == 'uploaded_csv':
doc_sources[idx] = "From uploaded data"
# elif source_type.startswith('social_media_'):
elif "tweet" in source_type.lower() or "twitter" in source_type.lower():
doc_sources[idx] = "From Twitter (Internet)"
elif "facebook" in source_type.lower():
content_type = doc.metadata.get("content_type", "")
if content_type == "page_info":
doc_sources[idx] = "From Facebook Profile Page (Internet)"
else:
doc_sources[idx] = "From Facebook (Internet)"
elif "linkedin" in source_type.lower():
doc_sources[idx] = "From LinkedIn (Internet)"
elif "web" in source_type.lower():
doc_sources[idx] = "From the web (Internet)"
else:
# Default background corpus (e.g. PANORAMA synthetic social profiles)
doc_sources[idx] = DEFAULT_CORPUS_SOURCE_LABEL
# Extract URL from metadata (try several common field names)
raw_url = (
doc.metadata.get("source_url") or
doc.metadata.get("tweet_url") or
doc.metadata.get("url") or
""
)
# Exclude sentinel values and non-URL strings
if raw_url and raw_url not in ("unknown", ""):
doc_urls[idx] = str(raw_url)
else:
doc_urls[idx] = ""
# Extract n-grams of different lengths (prioritize longer phrases)
# Only extract X to MAX_NGRAM_WORDS word phrases (no single words)
candidates = []
# Extract from longest to shortest (MAX_NGRAM_WORDS down to X)
for n in range(min(MAX_NGRAM_WORDS, 7), MIN_NGRAM_WORDS - 1, -1):
for ngram in extract_ngrams(text, n):
candidates.append((ngram, n)) # (ngram_dict, priority=n)
# Already sorted by priority (longer phrases extracted first) and position
candidates.sort(key=lambda x: (-x[1], x[0]['start']))
# Evaluate each candidate n-gram
for ngram, priority in candidates:
ngram_text = ngram['text']
start = ngram['start']
end = ngram['end']
words = ngram['words']
# Skip if position already highlighted
if any(pos in seen_positions for pos in range(start, end)):
continue
# Skip if all words are stop words
if all(w in _DP_STOPWORDS for w in words):
continue
# Find best matching document
best_doc_idx = -1
best_doc_text = ""
best_rouge = 0.0
best_cosine = 0.0
best_combined = 0.0
best_retrieval_score = 0.0
best_source = DEFAULT_CORPUS_SOURCE_LABEL # Default: background corpus
best_url = "" # URL of the best matching document
for doc_idx, doc_text in enumerate(doc_texts):
# Calculate ROUGE-L similarity
rouge_l = calculate_rouge_l(ngram_text, doc_text)
# Calculate cosine similarity (word overlap proxy)
cosine_sim = calculate_cosine_similarity(ngram_text, doc_text)
# Combine scores
combined_score = combine_similarity_scores(rouge_l, cosine_sim)
# Track best match
if combined_score > best_combined:
best_combined = combined_score
best_rouge = rouge_l
best_cosine = cosine_sim
best_doc_idx = doc_idx
best_doc_text = doc_text
best_retrieval_score = doc_scores.get(doc_idx, 0.0)
best_source = doc_sources.get(doc_idx, DEFAULT_CORPUS_SOURCE_LABEL)
best_url = doc_urls.get(doc_idx, "")
# Check if similarity exceeds thresholds
passes_rouge = best_rouge >= ROUGE_L_THRESHOLD
passes_cosine = best_cosine >= COSINE_SIM_THRESHOLD
passes_combined = best_combined >= MIN_COMBINED_SCORE
# Accept if either individual threshold is met OR combined threshold is met
if passes_combined or (passes_rouge and passes_cosine):
# Mark positions as used
for pos in range(start, end):
seen_positions.add(pos)
# Create RAGLink with actual similarity scores
links.append(RAGLink(
text=ngram_text,
start=start,
end=end,
corpus_snippets=[(best_doc_text[:RAG_LINKAGE_STORED_EXCERPT_LENGTH], best_combined)],
top_similarity=best_combined, # Combined score
top_doc_text=best_doc_text[:RAG_LINKAGE_STORED_EXCERPT_LENGTH],
top_doc_score=best_retrieval_score if best_retrieval_score > 0 else best_combined,
overlap_keywords=words,
source=best_source,
url=best_url,
))
# Sort by position in text
links.sort(key=lambda x: x.start)
return links
# ============================================================
# SECTION 11 β PRIVACY RISK CALCULATION
# ============================================================
def calculate_inferential_privacy_score(inference_metrics):
"""Calculate the inferential privacy score based on the maximum attribute lift.
The lift for each attribute is computed as:
lift = p_post / p_pop β 1
where p_post is the posterior probability given the system's available
information and p_pop is the population base rate (from POPULATION_PRIORS).
The score applies a log-based saturation function to the maximum lift:
score = kΒ·ln(1 + max_lift) / (1 + kΒ·ln(1 + max_lift))
where k = INFERENTIAL_SCORE_STEEPNESS_K.
This produces non-zero scores for ALL experimental conditions (including
those without RAG), enabling meaningful cross-group comparison.
Args:
inference_metrics: dict {attr: {lift, prob_rag, prob_no_rag, p_pop, ...}}
as produced by build_inference_warning.
Returns:
dict with keys:
score β float in [0, 1)
max_lift β float (raw maximum lift value, where 0 = no information gain)
max_attr β str (attribute with the highest lift)
p_rag β float (posterior probability for max_attr)
p_pop β float (population prior for max_attr)
p_no_rag β float (message-only probability, kept for logging)
"""
empty = {"score": 0.0, "max_score":0.0, "max_lift": 0.0, "max_attr": "",
"p_rag": 0.0, "p_pop": 0.0, "p_no_rag": 0.0,
"mean_score": 0.0, "mean_lift": 0.0, "median_score": 0.0, "median_lift": 0.0}
if not inference_metrics:
return empty
max_lift = 0.0
max_attr = ""
max_p_rag = 0.0
max_p_pop = 0.0
max_p_no_rag = 0.0
# ββ Collect all positive lifts for mean calculation ββββββββ
positive_lifts = []
for attr, m in inference_metrics.items():
lift = m.get("lift", 0.0)
if math.isfinite(lift) and lift > 0:
positive_lifts.append(lift)
if math.isfinite(lift) and lift > max_lift:
max_lift = lift
max_attr = attr
max_p_rag = m.get("prob_rag", m.get("p_rag", 0.0))
max_p_pop = m.get("p_pop", 0.0)
max_p_no_rag = m.get("prob_no_rag", m.get("p_no_rag", 0.0))
k = INFERENTIAL_SCORE_STEEPNESS_K
# ββ Max-lift score (existing) βββββββββββββββββββββββββββββ
adjusted_max = max(0.0, max_lift)
if adjusted_max == 0:
max_score = 0.0
else:
u = k * math.log(1.0 + adjusted_max)
max_score = u / (1.0 + u)
# ββ Mean-lift score (new) βββββββββββββββββββββββββββββββββ
if positive_lifts:
mean_lift = sum(positive_lifts) / len(positive_lifts)
u_mean = k * math.log(1.0 + mean_lift)
mean_score = u_mean / (1.0 + u_mean)
else:
mean_lift = 0.0
mean_score = 0.0
# ββ Median-lift score βββββββββββββββββββββββββββββββββββββ
if positive_lifts:
sorted_lifts = sorted(positive_lifts)
n = len(sorted_lifts)
if n % 2 == 1:
median_lift = sorted_lifts[n // 2]
else:
median_lift = (sorted_lifts[n // 2 - 1] + sorted_lifts[n // 2]) / 2.0
u_med = k * math.log(1.0 + median_lift)
median_score = u_med / (1.0 + u_med)
else:
median_lift = 0.0
median_score = 0.0
return {
"score": round(mean_score, 4),
"max_score": round(max_score, 4),
"max_lift": round(max_lift, 4),
"max_attr": max_attr,
"p_rag": round(max_p_rag, 4),
"p_pop": round(max_p_pop, 4),
"p_no_rag": round(max_p_no_rag, 4),
"mean_score": round(mean_score, 4),
"mean_lift": round(mean_lift, 4),
"median_score": round(median_score, 4),
"median_lift": round(median_lift, 4),
}
def calculate_privacy_risk(pii_matches, rag_links, epsilon, inference_metrics=None, use_rag=True):
"""Calculate overall privacy risk score (0-100) with monotonic growth across turns.
Score budget:
Inference component : 0-55 pts soft-saturating (never instantly maxes out)
PII component : 0-30 pts cumulative β grows with each turn
RAG linkage : 0-15 pts cumulative β grows with each turn
DP discount : 0-15 pts subtracted when DP is enabled
βββββββββββββββββββββββββββββββββββββββββββββββββββββ
Total maximum : 100 pts (only reachable after several high-risk turns)
The inference component uses a global exponential-saturation over the
SUM of above-threshold lifts:
inference = INFER_MAX * (1 β exp(βtotal_lift / INFER_SCALE))
Returns:
(score, breakdown)
score β int 0-100
breakdown β dict with keys: inference, pii, rag, dp_discount (all floats)
"""
# ββ Inference component: global soft saturation βββββββββββββββββββββββββββ
inference_score = 0.0
if inference_metrics:
total_lift = sum(
min(m.get('lift', 0.0), 10.0)
for m in inference_metrics.values()
if (m.get('lift', 0.0) > INFERENCE_LIFT_THRESHOLD
and not math.isinf(m.get('lift', 0.0)))
)
inference_score = INFER_MAX * (1.0 - math.exp(-total_lift / INFER_SCALE))
# ββ PII component βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if pii_matches:
pii_score = min(PII_BASE + len(pii_matches) * PII_PER_HIT, PII_MAX)
else:
pii_score = 0.0
# ββ RAG linkage component βββββββββββββββββββββββββββββββββββββββββββββββββ
rag_score = min(len(rag_links) * RAG_PER_HIT, RAG_MAX)
# ββ Differential Privacy discount βββββββββββββββββββββββββββββββββββββββββ
dp_factor = 1.0
dp_discount = 0.0
if epsilon != float('inf'):
dp_factor = DP_FLOOR + (1.0 - DP_FLOOR) * min(1.0, epsilon / MAX_POSSIBLE_EPS)
# Express the discount as the points removed, for the breakdown display
dp_discount = (inference_score + rag_score) * (1.0 - dp_factor)
inference_score_adj = inference_score * dp_factor
rag_score_adj = rag_score * dp_factor
total = inference_score_adj + pii_score + rag_score_adj
score = max(0, min(100, int(round(total))))
breakdown = {
"inference": round(inference_score_adj, 1),
"pii": round(pii_score, 1),
"rag": round(rag_score_adj, 1),
"dp_discount": round(dp_discount, 1),
"inferential_score_info": calculate_inferential_privacy_score(inference_metrics or {}),
}
return score, breakdown
# def create_risk_display(risk_score):
# """Create HTML for risk meter."""
# if risk_score == 0:
# label=""
# color="#4CAF50"
# elif risk_score < 30:
# color = "#4CAF50"
# label = "Low Risk"
# elif risk_score < 70:
# color = "#FF9800"
# label = "Medium Risk"
# else:
# color = "#F44336"
# label = "High Risk"
#
# return f"""
# <div style="text-align:center;padding:20px;">
# <div style="font-size:3em;font-weight:bold;color:{color};">{risk_score}</div>
# <div style="font-size:1.2em;margin-top:10px;color:#666;">{label}</div>
# <div style="background:#eee;height:20px;border-radius:10px;margin-top:15px;overflow:hidden;">
# <div style="background:{color};height:100%;width:{risk_score}%;"></div>
# </div>
# </div>
# """
def create_risk_display(risk_score, breakdown=None):
"""Create HTML for risk meter with shield badge and Why? breakdown tooltip.
Args:
risk_score β int 0-100
breakdown β optional dict from calculate_privacy_risk:
{"inference", "pii", "rag", "dp_discount"}
When provided the Why? anchor shows a data-driven explanation.
"""
if risk_score == 0:
label = "No Risk"
color = "#4CAF50"
icon = "β"
elif risk_score < 30:
label = "Low Risk"
color = "#4CAF50"
icon = "β"
elif risk_score < 70:
label = "Medium Risk"
color = "#FF9800"
icon = "β "
else:
label = "High Risk"
color = "#F44336"
icon = "β "
# ββ Build the Why? tooltip content ββββββββββββββββββββββββββββββββββββ
if breakdown:
infer_pts = breakdown.get("inference", 0.0)
pii_pts = breakdown.get("pii", 0.0)
rag_pts = breakdown.get("rag", 0.0)
dp_pts = breakdown.get("dp_discount", 0.0)
use_rag = breakdown.get("use_rag", True)
infer_info = breakdown.get("inferential_score_info")
# Dominant driver sentence
drivers = sorted(
[("Inference risk", infer_pts),
("PII exposure", pii_pts),
("External data linkages", rag_pts)],
key=lambda x: x[1], reverse=True
)
top_driver, top_pts = drivers[0]
if top_pts > 0:
driver_sentence = (
f"The primary driver is <b>{top_driver}</b> "
f"({top_pts:.0f} pts)."
)
else:
driver_sentence = "No significant risk factors were detected yet."
# Component rows β only show non-zero values
rows = []
if infer_pts > 0:
rows.append(
f'<div class="tt-sub" style="display:flex;justify-content:space-between;margin-bottom:3px;">'
f'<span>π Inference risk</span>'
f'<span style="font-weight:600;color:#b71c1c;">+{infer_pts:.0f} pts</span>'
f'</div>'
)
if pii_pts > 0:
rows.append(
f'<div class="tt-sub" style="display:flex;justify-content:space-between;margin-bottom:3px;">'
f'<span>π· PII exposure</span>'
f'<span style="font-weight:600;color:#e65100;">+{pii_pts:.0f} pts</span>'
f'</div>'
)
if rag_pts > 0:
rows.append(
f'<div class="tt-sub" style="display:flex;justify-content:space-between;margin-bottom:3px;">'
f'<span>π External data linkages</span>'
f'<span style="font-weight:600;color:#1565c0;">+{rag_pts:.0f} pts</span>'
f'</div>'
)
if dp_pts > 0:
rows.append(
f'<div class="tt-sub" style="display:flex;justify-content:space-between;margin-bottom:3px;">'
f'<span>π Differential Privacy</span>'
f'<span style="font-weight:600;color:#2e7d32;">β{dp_pts:.0f} pts</span>'
f'</div>'
)
divider = '<div class="tt-divider"></div>'
total_row = (
f'<div style="display:flex;justify-content:space-between;font-weight:700;font-size:1.2em">'
f'<span class="tt-title">Total</span>'
f'<span class="tt-title">{risk_score}/100</span>'
f'</div>'
)
# ββ Inferential privacy score section ββββββββββββββββββββββββββββββββββ
inferential_section = ""
if infer_info:
i_score = infer_info["max_score"]
i_attr = infer_info["max_attr"]
i_p_rag = infer_info["p_rag"]
i_p_pop = infer_info.get("p_pop", infer_info.get("p_no_rag", 0.0))
if i_attr:
# We have at least one attribute with positive lift
attr_row = (
f'<div class="tt-sub" style="margin-bottom:3px;">'
f'Highest-lift attribute: <strong>{_esc(i_attr)}</strong></div>'
f'<div class="tt-sub" style="display:flex;justify-content:space-between;margin-bottom:2px;">'
f'<span>P (population prior)</span>'
f'<span style="font-weight:600;">{i_p_pop:.1%}</span>'
f'</div>'
f'<div class="tt-sub" style="display:flex;justify-content:space-between;margin-bottom:2px;">'
f'<span>P (system estimate)</span>'
f'<span style="font-weight:600;color:#b71c1c;">{i_p_rag:.1%}</span>'
f'</div>'
f'<div class="tt-sub" style="display:flex;justify-content:space-between;margin-bottom:2px;">'
f'<span>Inferential score (max)</span>'
f'<span style="font-weight:700;color:#b71c1c;">{i_score:.3f}</span>'
f'</div>'
f'<div class="tt-sub" style="display:flex;justify-content:space-between;margin-bottom:4px;">'
f'<span>Inferential score (mean)</span>'
f'<span style="font-weight:700;color:#b71c1c;">'
f'{infer_info.get("mean_score", 0.0):.3f}</span>'
f'</div>'
)
else:
# No positive lift yet (first turn or no RAG)
attr_row = (
f'<div style="font-size:0.82em;color:#888;font-style:italic;line-height:1.4;">'
f'No significant external-data boost detected yet. '
f'The score will update as more attributes are inferred.'
f'</div>'
)
inferential_section = (
f'<div class="tt-divider"></div>'
f'<div class="tt-title" style="margin-bottom:4px;">π¬ Inferential Privacy Score'
f' <span style="font-weight:700;color:{"#b71c1c" if i_score > 0 else "#888"};">'
f'{i_score:.3f}</span></div>'
f'{attr_row}'
)
tooltip_body = (
f'<div class="tt-title">Score breakdown</div>'
f'{"".join(rows)}'
f'<div class="tt-divider"></div>'
f'{total_row}'
f'<div class="tt-bottom" style="margin-top:8px;line-height:1.4;font-size:1.2em">'
# f'{driver_sentence}'
f'{inferential_section}'
f'</div>'
)
else:
tooltip_body = (
'Score is calculated from PII exposure, external data linkages, '
'differential privacy settings, and inference risk from sensitive attributes.'
)
why_anchor = (
f'<span class="tt-anchor" style="position:relative;display:inline-block;'
f'cursor:help;color:#888;border-bottom:1px dotted #aaa;font-size:1.2em;">'
f'Risk Score: {int(risk_score)}/100 β Why?'
f'<span class="tt-popup tt-above" style="width:320px;text-align:left;">'
f'{tooltip_body}'
f'</span>'
f'<span class="tt-popup tt-below" style="width:320px;text-align:left;">'
f'{tooltip_body}'
f'</span>'
f'</span>'
)
return f"""
<div style="text-align:center;padding:20px;">
<!-- Shield Badge β larger render (150Γ150) for bigger visible number -->
<div style="position:relative;display:inline-block;margin-bottom:16px;">
<svg width="150" height="150" viewBox="0 0 120 140"
style="filter:drop-shadow(0 2px 4px rgba(0,0,0,0.2));">
<!-- Shield shape -->
<path d="M60 10 L100 30 L100 70 Q100 110 60 130 Q20 110 20 70 L20 30 Z"
fill="{color}" stroke="#fff" stroke-width="3"/>
<!-- Icon -->
<text x="60" y="50" text-anchor="middle"
font-size="22" fill="#fff" font-weight="bold">{icon}</text>
<!-- Score β larger font fills the shield -->
<text x="60" y="98" text-anchor="middle"
font-size="72" fill="#fff" font-weight="bold">{risk_score}</text>
</svg>
</div>
<!-- Label -->
<div style="font-size:1.3em;font-weight:bold;margin-bottom:12px;color:{color};">
{label}
</div>
<!-- Progress bar -->
<div style="background:#eee;height:20px;border-radius:10px;overflow:hidden;
max-width:300px;margin:0 auto;">
<div style="background:{color};height:100%;width:{risk_score}%;
transition:width 0.3s ease;"></div>
</div>
<!-- Why? anchor with score breakdown tooltip -->
<div style="margin-top:14px;">
{why_anchor}
</div>
</div>
"""
# ============================================================
# SECTION 12 β CONCURRENT LLM CALLS
# ============================================================
def make_concurrent_llm_calls_optimized(user_input, provider, model_id, use_rag, retrieved, conversation_state=None, original_user_input=None):
"""
Make MAXIMUM 3 concurrent LLM calls for optimal speed:
1. Generate response (with conversation history support)
2. Infer attributes (combined - single call for all attributes)
3. Detect PII (combined - single call for both user input and response)
`user_input` is what is sent to the LLM (may be DP-perturbed).
`original_user_input`, if provided, is used for PII span anchoring so that
PII detected in the perturbed text is correctly located in the original.
Args:
conversation_state: ConversationState object containing message history
Returns: (llm_response, u_pii, u_pii_perturbed, r_pii, probs_rag, probs_no_rag, evidence_rag)
"""
logger.info("\n" + "="*60)
logger.info("β‘ STARTING CONCURRENT LLM CALLS")
logger.info("="*60)
client = LLM_CLIENTS.get(provider)
if not client:
logger.info("β No LLM client available")
return "", [], [], [], {}, {}, {}, [], []
engine = AttributeInferenceEngine()
total_start = time.time()
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CALL 1: Generate response
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def call_generate_response():
"""LLM CALL 1/3: Generate user-visible response with conversation history."""
logger.info(" π€ [LLM CALL] Generating response...")
start_time = time.time()
try:
# Build messages list including conversation history
messages = build_user_response_prompt(
user_input,
retrieved if use_rag else None,
conversation_state=conversation_state
)
# resp = client.responses.create(
resp = client.chat.completions.create(
model=model_id,
messages=messages, # Now a list of message dicts with history
# max_tokens=MAX_TOKENS_LLM_RESPONSE_GENERATION,
max_completion_tokens=MAX_TOKENS_LLM_RESPONSE_GENERATION,
temperature=TEMPERATURE_LLM_RESPONSE_GENERATION,
timeout=30,
)
result = resp.choices[0].message.content # .output_text
# If the response was cut off at the token limit, trim to last complete sentence
if resp.choices[0].finish_reason == "length":
last_end = max(result.rfind('.'), result.rfind('!'), result.rfind('?'))
if last_end > len(result) // 2: # only trim if a sentence end exists in the second half
result = result[:last_end + 1] + " [...]"
elapsed = time.time() - start_time
logger.info(f" β Response generated in {elapsed:.2f}s (with {len(messages)} messages in context)")
return result
except Exception as e:
logger.info(f" β Error generating response: {e}")
return f"Error: {e}"
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CALL 2: Attribute inference (COMBINED - single call)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# def call_attribute_inference():
# """LLM CALL 2/3: Infer ALL attributes in a single call."""
# logger.info(" π [LLM CALL 2/3] Inferring all attributes (combined)...")
# start_time = time.time()
# try:
# # With RAG if enabled
# if use_rag and retrieved:
# inf_prompt = build_inference_prompt(user_input, retrieved_docs=retrieved)
# resp = client.chat.completions.create(
# model=model_id,
# messages=[{"role": "user", "content": inf_prompt}],
# max_tokens=MAX_TOKENS_INFER_ATTRIBUTES,
# temperature=TEMPERATURE_INTERNAL_TASKS
# )
# probs_with_rag = engine.extract_probabilities_from_response(resp.choices[0].message.content)
# else:
# probs_with_rag = {}
#
# # Without RAG (baseline)
# inf_prompt_no_rag = build_inference_prompt(user_input, retrieved_docs=None)
# resp_no_rag = client.chat.completions.create(
# model=model_id,
# messages=[{"role": "user", "content": inf_prompt_no_rag}],
# max_tokens=MAX_TOKENS_INFER_ATTRIBUTES,
# temperature=TEMPERATURE_INTERNAL_TASKS
# )
# probs_without_rag = engine.extract_probabilities_from_response(resp_no_rag.choices[0].message.content)
#
# # If RAG probs empty, use no_rag probs
# if not probs_with_rag:
# probs_with_rag = probs_without_rag
#
# elapsed = time.time() - start_time
# logger.info(f" β Attribute inference completed in {elapsed:.2f}s")
# return probs_with_rag, probs_without_rag
# except Exception as e:
# logger.info(f" β Error in attribute inference: {e}")
# return {}, {}
def call_inference_with_rag():
"""LLM CALL 2a/4: Infer attributes WITH RAG context."""
# Skip immediately if RAG is not in use β saves a full LLM round-trip.
if not (use_rag and retrieved):
return {}, {}
logger.info(" π [LLM CALL] Inferring attributes WITH RAG...")
start_time = time.time()
try:
inf_prompt = build_inference_prompt(
user_input,
retrieved_docs=retrieved,
conversation_state=conversation_state,
)
# resp = client.responses.create(
resp = client.chat.completions.create(
model=model_id,
messages=[{"role": "user", "content": inf_prompt}],
max_tokens=MAX_TOKENS_INFER_ATTRIBUTES,
# temperature=TEMPERATURE_INTERNAL_TASKS,
timeout=20,
)
result = engine.extract_probabilities_from_response(resp.choices[0].message.content)
elapsed = time.time() - start_time
logger.info(f" β Inference WITH RAG completed in {elapsed:.2f}s")
return result.get("probabilities", {}), result.get("evidence", {})
except Exception as e:
logger.warning(f" β Inference WITH RAG failed: {e}")
return {}, {}
def call_inference_no_rag():
"""LLM CALL 2b/4: Infer attributes WITHOUT RAG (baseline)."""
logger.info(" π [LLM CALL] Inferring attributes WITHOUT RAG (baseline)...")
start_time = time.time()
try:
inf_prompt_no_rag = build_inference_prompt(
user_input,
retrieved_docs=None,
conversation_state=conversation_state,
)
# resp_no_rag = client.responses.create(
resp_no_rag = client.chat.completions.create(
model=model_id,
messages=[{"role": "user", "content": inf_prompt_no_rag}],
max_tokens=MAX_TOKENS_INFER_ATTRIBUTES,
# temperature=TEMPERATURE_INTERNAL_TASKS,
timeout=20,
)
result = engine.extract_probabilities_from_response(resp_no_rag.choices[0].message.content)
elapsed = time.time() - start_time
logger.info(f" β Inference WITHOUT RAG completed in {elapsed:.2f}s")
return result.get("probabilities", {}), result.get("evidence", {})
except Exception as e:
logger.warning(f" β Inference WITHOUT RAG failed: {e}")
return {}, {}
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Submit CALL 1, 2a, and 2b concurrently.
# CALL 3 (PII) requires the LLM response text, so it is submitted
# as soon as future_response resolves β still overlapping with the
# two inference futures that are still in-flight.
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
results = {}
pii_source_text = original_user_input if original_user_input is not None else user_input
pii_source_text_perturbed = user_input
with ThreadPoolExecutor(max_workers=6) as executor:
future_response = executor.submit(call_generate_response)
future_rag_inf = executor.submit(call_inference_with_rag)
future_norag_inf = executor.submit(call_inference_no_rag)
# u_rag only needs user_input and retrieved β both available now,
# so submit immediately and let it overlap with the LLM calls.
u_rag_source_text = original_user_input if original_user_input is not None else user_input
future_u_rag = (
executor.submit(find_rag_linkage, u_rag_source_text, retrieved)
if retrieved else None
)
# Collect the response first (needed to launch PII detection and r_rag).
try:
results['response'] = future_response.result(timeout=40)
except Exception as e:
logger.warning(f" β LLM response call timed out or failed: {e}")
results['response'] = "Error: request timed out. Please try again."
# As soon as the response is available, submit PII detection and r_rag
# so they overlap with any still-running inference futures.
llm_response = results['response']
if not _is_llm_error(llm_response):
future_pii = executor.submit(
detect_pii_combined, pii_source_text, llm_response, provider, model_id
)
logger.info(f"PII DETECTION PERTURBED DP: {pii_source_text_perturbed}")
future_pii_perturbed = (
executor.submit(detect_pii_combined, pii_source_text_perturbed, "", provider, model_id)
if original_user_input is not None
else None
)
future_r_rag = (
executor.submit(find_rag_linkage, llm_response, retrieved)
if retrieved else None
)
else:
future_pii = None
future_r_rag = None
# Collect inference results (may already be done by now).
try:
probs_with_rag, evidence_with_rag = future_rag_inf.result(timeout=40)
except Exception as e:
logger.warning(f" β Inference WITH RAG timed out or failed: {e}")
probs_with_rag, evidence_with_rag = {}, {}
try:
# probs_no_rag, _ = future_norag_inf.result(timeout=40)
probs_no_rag, evidence_no_rag = future_norag_inf.result(timeout=40)
except Exception as e:
logger.warning(f" β Inference WITHOUT RAG timed out or failed: {e}")
probs_no_rag = {}
evidence_no_rag = {}
# Fall back: if the with-RAG call returned nothing, copy the baseline.
if not probs_with_rag:
probs_with_rag = probs_no_rag
evidence_with_rag = evidence_no_rag
# Collect PII result.
if future_pii is not None:
try:
user_pii_items, response_pii_items = future_pii.result(timeout=40)
except Exception as e:
logger.warning(f" β PII detection timed out or failed: {e}")
user_pii_items, response_pii_items = [], []
else:
user_pii_items, response_pii_items = [], []
if future_pii_perturbed is not None:
try:
user_pii_perturbed_items, _ = future_pii_perturbed.result(timeout=40)
logger.info(f"PII DETECTION PERTURBED DP DETECTED: {user_pii_perturbed_items}")
except Exception as e:
logger.warning(f" β Perturbed PII detection failed: {e}")
user_pii_perturbed_items = []
else:
user_pii_perturbed_items = None # no DP
# Collect RAG-linkage results (likely already done by now).
try:
u_rag = future_u_rag.result(timeout=30) if future_u_rag else []
except Exception as e:
logger.warning(f" β u_rag linkage timed out or failed: {e}")
u_rag = []
try:
r_rag = future_r_rag.result(timeout=30) if future_r_rag else []
except Exception as e:
logger.warning(f" β r_rag linkage timed out or failed: {e}")
r_rag = []
probs_rag = probs_with_rag
probs_no_rag = probs_no_rag
evidence_rag = evidence_with_rag
if _is_llm_error(llm_response):
# Skip all analysis when the LLM returned an error string
logger.warning("LLM response is an error β skipping PII/inference analysis.")
return llm_response, [], [], [], {}, {}, {}, [], []
# ββ Regex fallback for high-confidence PII patterns the LLM may have missed ββ
# Only fills gaps; never overwrites items the LLM already found for the user text.
# _llm_found_values = {it.get("value", "").lower() for it in user_pii_items if isinstance(it, dict)}
# _NAME_PATTERNS = [
# r"(?:I(?:'m| am)|[Mm]y name is|[Cc]all me|[Tt]his is)\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)+)",
# r"(?:^|\.\s+)([A-Z][a-z]+\s+[A-Z][a-z]+)(?:\s+here\b|\s+speaking\b|,)",
# ]
# _EMAIL_RE = re.compile(r'\b[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}\b')
# _PHONE_RE = re.compile(r'(?<!\d)(?:\+?\d[\d\s\-().]{7,}\d)(?!\d)')
#
# for _pat in _NAME_PATTERNS:
# for _m in re.finditer(_pat, pii_source_text):
# _name = _m.group(1).strip()
# if _name.lower() not in _llm_found_values:
# user_pii_items.append({"type": "name", "value": _name, "confidence": 0.92})
# _llm_found_values.add(_name.lower())
#
# for _m in _EMAIL_RE.finditer(pii_source_text):
# _val = _m.group(0)
# if _val.lower() not in _llm_found_values:
# user_pii_items.append({"type": "email", "value": _val, "confidence": 0.99})
# _llm_found_values.add(_val.lower())
#
# for _m in _PHONE_RE.finditer(pii_source_text):
# _val = _m.group(0).strip()
# if sum(c.isdigit() for c in _val) >= 7 and _val.lower() not in _llm_found_values:
# user_pii_items.append({"type": "phone number", "value": _val, "confidence": 0.90})
# _llm_found_values.add(_val.lower())
# Convert to PIIMatch objects (anchored to original / pii_source_text)
u_pii = _extract_pii_spans_from_values(pii_source_text, user_pii_items)
r_pii = _extract_pii_spans_from_values(llm_response, response_pii_items)
# u_pii_perturbed: when DP was active use separately detected perturbed items;
# when DP was not active it is identical to u_pii by definition.
if user_pii_perturbed_items is not None:
# Always anchor perturbed PIIs to the perturbed text (user_input),
# regardless of whether original_user_input differs.
u_pii_perturbed = _extract_pii_spans_from_values(user_input, user_pii_perturbed_items)
else:
# No separate perturbed detection was run (DP was off) β reuse original.
u_pii_perturbed = u_pii
total_elapsed = time.time() - total_start
logger.info("="*60)
logger.info(f"β
ALL CONCURRENT CALLS COMPLETED in {total_elapsed:.2f}s")
logger.info(f" (LLM calls executed in parallel: response + 2x inference + PII detection)")
logger.info("="*60 + "\n")
# print(llm_response, u_pii, r_pii, probs_rag, probs_no_rag, evidence_rag)
return llm_response, u_pii, u_pii_perturbed, r_pii, probs_rag, probs_no_rag, evidence_rag, u_rag, r_rag
def extract_user_name_from_query(query):
"""
Extract a potential user name from the query for social media scraping.
This is a simple heuristic - in production, use NER or better extraction.
Args:
query: User query string
Returns:
str: Extracted name or a generic identifier
"""
# Simple pattern: look for "I am [Name]" or similar patterns
patterns = [
r"(?:I am|I'm|My name is|This is)\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)?)",
r"(?:about|for)\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)?)",
]
for pattern in patterns:
match = re.search(pattern, query)
if match:
name = match.group(1).strip()
logger.info(f" Extracted name from query: '{name}'")
return name
# If no name found, return a generic identifier
logger.info(" No specific name found in query, using generic 'user'")
return "user"
# ============================================================
# SECTION 13 β MESSAGE PROCESSING (WITH CONCURRENT CALLS)
# ============================================================
def process_message(user_input, use_rag, epsilon, model_str, show_tips, show_rag_hl, show_pii_hl, state, retriever, social_scraping_enabled=False, social_platforms=None):
"""Process a user message with OPTIMIZED concurrent LLM calls (max 3 calls) and optional social media scraping."""
if not user_input or not user_input.strip():
return (
fmt_conversation(state.messages, show_tips, show_rag_hl, show_pii_hl),
create_risk_display(0),
"",
"",
_build_privacy_settings_html(epsilon),
)
# Parse model string to get provider and model_id
provider = DEFAULT_MODEL_PROVIDER
model_id = DEFAULT_MODEL_ID
for p, mid, mname in MODEL_CONFIGS:
if mname in model_str or mid in model_str:
provider = p
model_id = mid
break
logger.info("\n" + "="*60)
logger.info(f"π PROCESSING MESSAGE (Model: {model_str})")
logger.info("="*60)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# STEP: Input Differential Privacy (RANTEXT-inspired, word-level LDP)
# Perturbs content words in the user message BEFORE the LLM sees it.
# The conversation panel always displays the ORIGINAL message; a DP badge
# on each user bubble transparently communicates the perturbation.
# Literature: Tong et al. (2025) InferDPT/RANTEXT, IEEE TDSC 22(5).
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
llm_input = user_input # default: send original if DP off
input_dp_substitutions = []
# Lazy-init the perturber here if it is still None (e.g. retriever failed
# to load at startup but an embedding model may still be available).
if epsilon != float("inf") and _DP_INPUT_PERTURBER is None:
_init_dp_input_perturber(retriever)
if epsilon != float("inf") and _DP_INPUT_PERTURBER is not None:
try:
# Use a shallow copy so the shared singleton's epsilon is never
# mutated. Two concurrent requests with different epsilon values
# would otherwise race between the assignment and perturb().
_local_perturber = copy.copy(_DP_INPUT_PERTURBER)
_local_perturber.epsilon = epsilon
perturbed_input, input_dp_substitutions = _local_perturber.perturb(user_input)
llm_input = perturbed_input
logger.info(
" Input DP: %d word(s) perturbed (Ξ΅=%.2f) % s",
len(input_dp_substitutions), epsilon, perturbed_input
)
except Exception as _dp_exc:
logger.warning(" Input DP perturbation failed (%s) β using original.", _dp_exc)
llm_input = user_input
logger.info(f">>>>>>> LLM INPUT: {llm_input}")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# STEP: RAG retrieval (LOCAL - no LLM call)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
logger.info("π [LOCAL] RAG retrieval...")
retrieval_start = time.time()
retrieved = []
u_rag = []
logger.info(f"[LOCAL] RAG retrieval use_rag={use_rag}; retriever={retriever}")
if use_rag and retriever is not None:
try:
min_sim = epsilon_adjusted_threshold(epsilon)
logger.info(" Using threshold-filtered retrieval (min_sim=%.3f, Ξ΅=%s)", min_sim, epsilon)
if epsilon != float('inf'):
# Layer 1: threshold filter
# Layer 2: exponential-mechanism sampling (Retrieval DP)
pre_filtered = retriever.retrieve(llm_input, k=RAG_FETCH_K, fetch_k=RAG_FETCH_K, min_similarity=min_sim)
logger.info(f">>>>>>> RAG RETRIEVED pre_filtered: {retrieved}")
if pre_filtered:
dp_retriever = DPRetriever(retriever, epsilon=epsilon)
# Pass pre-filtered docs only β DPRetriever re-samples from them
retrieved = dp_retriever.retrieve(llm_input, k=RAG_K, fetch_k=len(pre_filtered))
logger.info(f">>>>>>> RAG RETRIEVED post_filtered: {retrieved}")
else:
retrieved = []
else:
retrieved = retriever.retrieve(llm_input, k=RAG_K, fetch_k=RAG_FETCH_K, min_similarity=min_sim)
logger.info(f">>>>>>> RAG RETRIEVED: {retrieved}")
retrieval_elapsed = time.time() - retrieval_start
logger.info(f" β Retrieved {len(retrieved)} docs in {retrieval_elapsed:.2f}s (LOCAL)")
except Exception as e:
logger.info(f" β RAG retrieval error: {e}")
retrieved = []
else:
logger.info(" β’ RAG disabled")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# STEP: Social Media Scraping (if enabled and retriever exists)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# STEP 1.5: Social Media Scraping (if enabled)
#
# Two modes:
# a) use_rag=True β scraped docs are appended to the existing retrieved set
# b) use_rag=False β scraped docs become the sole retrieved set and are
# treated as if RAG were enabled for this request only
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# effective_* variables let us override use_rag locally without touching
# the caller's state
effective_use_rag = use_rag
effective_retrieved = retrieved # same list reference; extended below
scraped_docs_for_log = [] # raw scraped documents saved for interaction log
if social_scraping_enabled:
if state._scraped_docs is not None:
# Already scraped in a previous turn β reuse cached result
scraped_docs = state._scraped_docs
logger.info(
f"π [LOCAL] Reusing {len(scraped_docs)} cached scraped docs (scraping already done this session)")
else:
logger.info("π [LOCAL] Social media scraping (first turn)...")
scraped_docs = []
try:
user_name = extract_user_name_from_query(user_input)
platforms_to_scrape = []
if social_platforms:
platforms_to_scrape = [p.lower() for p in social_platforms]
logger.info(f" Platforms selected: {platforms_to_scrape}")
if platforms_to_scrape and SOCIAL_SCRAPER and SOCIAL_SCRAPER.enabled:
scraped_docs = SOCIAL_SCRAPER.scrape_user_data(user_name, platforms_to_scrape,
fallback_data=_load_fallback_tweets(
getattr(state, "_scenario_mode", "real")))
except Exception as exc:
logger.error(f" β Social media scraping error: {exc}")
import traceback
logger.error(traceback.format_exc())
# Cache result regardless (even [] means "we tried, nothing found")
state._scraped_docs = scraped_docs
if scraped_docs:
scraped_docs_for_log = scraped_docs
if use_rag:
original_count = len(effective_retrieved)
effective_retrieved = list(effective_retrieved) + scraped_docs
logger.info(
f" β Appended {len(scraped_docs)} scraped docs to RAG "
f"(was {original_count}, now {len(effective_retrieved)})"
)
else:
effective_retrieved = scraped_docs
effective_use_rag = True
logger.info(
f" β RAG was off; using {len(scraped_docs)} scraped docs "
f"as the RAG source for this turn"
)
u_rag = [] # computed concurrently inside make_concurrent_llm_calls_optimized
else:
logger.info(" β’ No scraped documents available")
else:
logger.info(" β’ Social media scraping disabled")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# STEP 2: Make ALL concurrent LLM calls (max 3 calls)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
llm_response, u_pii, u_pii_perturbed, r_pii, probs_rag, probs_no_rag, evidence_rag, u_rag, r_rag = make_concurrent_llm_calls_optimized(
llm_input, provider, model_id, effective_use_rag, effective_retrieved,
conversation_state=state,
original_user_input=user_input, # PII always detected on original text
)
probs_rag_raw = probs_rag # only LLM-inferred attrs β for build_inference_warning
probs_rag_display = _fill_missing_with_uniform(probs_rag) # all 6 attrs β for avatar card
# ββ Accumulate best (highest-confidence) per-attribute predictions ββ
for attr, dist in probs_rag.items():
if not dist:
continue
new_top_p = max(dist.values())
old_top_p = max(state._best_probs_rag.get(attr, {}).values(), default=0.0)
if new_top_p >= old_top_p:
state._best_probs_rag[attr] = dist
state._best_evidence_rag[attr] = evidence_rag.get(attr, [])
# ββ Early exit on LLM error: add the error message to the conversation
# but do NOT update any privacy state, risk score, or analysis panels.
if _is_llm_error(llm_response):
state.add("user", user_input, [], [], dp_metadata=None)
state.add("assistant", llm_response, [], [])
conv_html = fmt_conversation(state.messages, show_tips, show_rag_hl, show_pii_hl)
privacy_settings_text = _build_privacy_settings_html(epsilon)
logger.warning("process_message: LLM error detected β risk panel suppressed.")
# Return a sentinel risk_score of None so _send knows to hide the panel
return conv_html, None, "", "", privacy_settings_text
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# STEP 3: Post-processing (LOCAL - no LLM calls)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
logger.info("π§ [LOCAL] Post-processing...")
# Build dp_metadata for the user message bubble
dp_meta = None
if input_dp_substitutions:
dp_meta = {
"epsilon": epsilon,
"num_substitutions": len(input_dp_substitutions),
"substitutions": input_dp_substitutions,
"original_text": user_input, # what the user typed (shown in chat)
"perturbed_text": llm_input, # what the LLM received
}
# Add messages to state (original text always stored; perturbed text goes to LLM only)
if dp_meta:
# Re-anchor: take PII *values* detected on the perturbed text and
# search for them in the original text so start/end offsets are correct
# for highlighting inside m.content (which is the original text).
perturbed_pii_as_items = [
{"type": p.fine_type, "value": p.text, "confidence": p.confidence}
for p in u_pii_perturbed
]
u_pii_for_bubble = _extract_pii_spans_from_values(user_input, perturbed_pii_as_items)
u_pii_for_card = u_pii_perturbed # perturbed-text spans for the right-panel card
else:
u_pii_for_bubble = u_pii
u_pii_for_card = u_pii
state.add("user", user_input, u_pii_for_bubble, u_rag, dp_metadata=dp_meta, pii_card_matches=u_pii_for_card)
# state.add("user", user_input, u_pii, u_rag, dp_metadata=dp_meta)
# r_rag was computed concurrently inside make_concurrent_llm_calls_optimized.
state.add("assistant", llm_response, r_pii, r_rag)
# Store probabilities
state.last_probs_rag = probs_rag_display
state.last_probs_no_rag = probs_no_rag
state.last_evidence_rag = evidence_rag
# Calculate risk and build outputs
all_pii = (u_pii or []) + (r_pii or [])
all_rag = (u_rag or []) + (r_rag or [])
# ββ Build the inference warning from the current turn βββββββββββββββββ
warning_html, inference_metrics, inference_warning_shown = build_inference_warning(
user_input, state._best_probs_rag, probs_no_rag, effective_use_rag, state._best_evidence_rag, effective_retrieved,
input_dp_metadata=dp_meta,
perturbed_user_input=llm_input if dp_meta else None,
conversation_state=state,
)
# ββ Update peak (highest-lift) inference metrics across all turns ββββββ
for attr, metrics in inference_metrics.items():
current_lift = metrics.get('lift', 0)
peak_lift = state._peak_inference_metrics.get(attr, {}).get('lift', -float('inf'))
if current_lift > peak_lift:
state._peak_inference_metrics[attr] = metrics
# ββ Compute risk from the FULL conversation, not just this turn ββββββββ
# Collect all PII and RAG links stored across every message in the session
session_pii = [m for msg in state.messages for m in (msg.pii_matches or [])]
session_rag = [r for msg in state.messages for r in (msg.rag_links or [])]
risk_score, risk_breakdown = calculate_privacy_risk(
session_pii, session_rag, epsilon, state._peak_inference_metrics,
use_rag=effective_use_rag
)
conv_html = fmt_conversation(state.messages, show_tips, show_rag_hl, show_pii_hl)
# analysis = _build_analysis(u_pii, u_rag, risk_score, effective_use_rag, epsilon, probs_rag, probs_no_rag)
# session_user_pii = [m for msg in state.messages if msg.role == "user" for m in (msg.pii_matches or [])]
session_user_pii = [m for msg in state.messages if msg.role == "user"for m in (msg.pii_card_matches if msg.pii_card_matches is not None else (msg.pii_matches or []))]
# Build and cache the inference card HTML independently so _send can control its visibility
# avatar_html = _build_inferred_avatar(probs_rag_display, warning_html, inference_warning_shown)
avatar_html = _build_inferred_avatar(_fill_missing_with_uniform(state._best_probs_rag), warning_html, inference_warning_shown)
state._last_avatar_html = avatar_html
analysis = _build_analysis(session_user_pii, u_rag, risk_score, effective_use_rag, epsilon, probs_rag_display,
probs_no_rag, inference_warning_html=warning_html, is_warning=inference_warning_shown,
include_avatar=False, show_pii=show_pii_hl)
privacy_settings_text = _build_privacy_settings_html(epsilon)
state._last_privacy_html = privacy_settings_text
logger.info("β Post-processing completed")
logger.info("=" * 60 + "\n")
# ββ Persist interaction to CSV log ββββββββββββββββββββββββ
state._turn_count += 1
# corpus_source: prefer uploaded file, then social scrape, else system
if state._uploaded_file_path:
corpus_source = "uploaded_csv"
elif social_scraping_enabled and effective_retrieved and not use_rag:
corpus_source = "social_scrape"
else:
corpus_source = "system"
# ββ Compute how many inferred attribute top-values changed this turn ββ
curr_attr_vals = {attr: m.get("top_value", "") for attr, m in inference_metrics.items()}
num_attributes_changed = sum(
1 for a in set(state._prev_inference_attrs) | set(curr_attr_vals)
if state._prev_inference_attrs.get(a) != curr_attr_vals.get(a)
)
state._prev_inference_attrs = curr_attr_vals
state._last_model = model_str
state._last_epsilon = epsilon
state._last_rag_enabled = effective_use_rag
state._last_social_scraping = social_scraping_enabled
state._last_corpus_source = corpus_source
state._demo_mode = False
append_interaction_log(
session_source=state._session_source,
turn_number=state._turn_count,
demo_mode=False,
scenario_mode=getattr(state, "_scenario_mode", "real"),
persona_attributes=getattr(state, "_persona_attributes", {}),
model=model_str,
epsilon=epsilon,
rag_enabled=effective_use_rag,
show_risk=getattr(state, "_show_risk", True),
show_rag_highlights=show_rag_hl,
show_tips=show_tips,
show_pii_highlights=getattr(state, "_show_pii_hl", show_pii_hl),
show_settings=getattr(state, "_show_settings", False),
access_token=state._access_token,
social_scraping_enabled=social_scraping_enabled,
corpus_source=corpus_source,
uploaded_file_path=state._uploaded_file_path,
user_prompt=user_input,
llm_response=llm_response,
risk_score=risk_score,
u_rag=u_rag,
r_rag=r_rag,
u_pii=u_pii,
u_pii_perturbed=u_pii_perturbed,
r_pii=r_pii,
inference_metrics=inference_metrics,
inference_warning_shown=inference_warning_shown,
scraped_docs=scraped_docs_for_log if social_scraping_enabled else None,
user_prompt_perturbed=llm_input,
num_input_dp_substitutions=len(input_dp_substitutions),
num_attributes_changed=num_attributes_changed,
show_dp=getattr(state, "_show_dp", 1),
show_infr_attr_card=getattr(state, "_show_infr_attr_card", 1),
show_social_scraping=getattr(state, "_show_social_scraping", False),
show_upload_data=getattr(state, "_show_upload_data", False),
rag_corpus_path=getattr(state, "_rag_corpus_path", ""),
)
return conv_html, create_risk_display(risk_score, risk_breakdown), "", analysis, privacy_settings_text
# def _build_analysis(pii, rag, risk, use_rag, eps, pr, pnr):
# """Build analysis markdown."""
# parts = ["### Detailed Privacy Analysis\n\n**Detected PIIs:**\n"]
# if pii:
# for p in pii:
# parts.append(f"- {p.category.value}: `{p.text}` (confidence: {p.confidence:.0%})\n")
# else:
# parts.append("- None detected\n")
#
# parts.append("\n**RAG Linkages:**\n")
# if rag:
# for r in rag:
# parts.append(f"- `{r.text}` (similarity: {r.top_similarity:.2f})\n")
# else:
# parts.append("- None detected\n")
#
# parts.append(f"\n**Privacy Settings:**\n")
# parts.append(f"- RAG: {'Enabled' if use_rag else 'Disabled'}\n")
# parts.append(f"- Ξ΅ (epsilon): {eps if eps != float('inf') else 'β (no DP)'}\n")
# parts.append(f"- Risk Score: {risk}/100\n")
#
# return "".join(parts)
def _build_inference_plot(probs_rag, probs_no_rag):
"""Build HTML visualization of inference privacy metrics (lift alerts only, no bars)."""
metrics_data = []
for attr in SENSITIVE_ATTRIBUTES:
if attr not in probs_rag:
continue
dist_rag = probs_rag[attr]
if not dist_rag:
continue
top_val_rag = max(dist_rag, key=dist_rag.get)
p_rag = dist_rag[top_val_rag]
p_no_rag = (probs_no_rag or {}).get(attr, {}).get(top_val_rag, 0.0)
# Compute lift against population prior
p_pop = POPULATION_PRIORS.get(attr, {}).get(top_val_rag, 0.0)
if p_pop > 0:
z = InferentialPrivacyMetrics.compute_z(p_rag, p_pop)
lift = InferentialPrivacyMetrics.compute_lift(z)
else:
lift = 0.0
metrics_data.append({
'attr': attr,
'top_val': top_val_rag,
'p_rag': p_rag,
'p_pop': p_pop,
'lift': lift,
})
if not metrics_data:
return ""
rows = ['<div style="margin-top:6px;">']
for data in metrics_data:
attr = data['attr']
top_val = data['top_val']
p_rag = data['p_rag']
p_pop = data['p_pop']
lift = data['lift'] if not math.isinf(data['lift']) else -1
rows.append(
f'<div style="display:flex;align-items:center;justify-content:space-between;'
f'margin-bottom:5px;font-size:0.82em;">'
f'<span style="color:#333;font-weight:600;">{attr}</span>'
f'<span style="color:#555;">{top_val} ({p_rag:.0%})</span>'
)
if lift > 0:
rows.append(
f'<span style="color:#b71c1c;font-weight:700;margin-left:6px;">'
f'+{lift:.0%} above base rate</span>'
)
else:
rows.append('<span></span>')
rows.append('</div>')
rows.append('</div>')
return "\n".join(rows)
# def _build_analysis(pii, rag, risk, use_rag, eps, pr, pnr):
# """Build analysis markdown with improved visualizations."""
# parts = ["### Detailed Privacy Analysis\n\n**Detected PIIs:**\n"]
# if pii:
# for p in pii:
# parts.append(f"- {p.category.value}: `{p.text}` (confidence: {p.confidence:.0%})\n")
# else:
# parts.append("- None detected\n")
#
# parts.append("\n**RAG Linkages:**\n")
# if rag:
# # Group by document and create a more narrative format
# doc_groups = {}
# for r in rag:
# # Assume r has a doc_id or source attribute, fallback to grouping by similarity range
# doc_key = getattr(r, 'doc_id', f"doc_{hash(r.text) % 100}")
# if doc_key not in doc_groups:
# doc_groups[doc_key] = []
# doc_groups[doc_key].append(r)
#
# for doc_id, matches in doc_groups.items():
# avg_sim = sum(m.top_similarity for m in matches) / len(matches)
# match_count = len(matches)
# relevance = "High" if avg_sim > 0.8 else "Medium" if avg_sim > 0.6 else "Moderate"
# parts.append(f"- **Document {doc_id}**: {match_count} match{'es' if match_count > 1 else ''} "
# f"(relevance: {relevance}, avg similarity: {avg_sim:.2f})\n")
# else:
# parts.append("- None detected\n")
#
# parts.append(f"\n**Privacy Settings:**\n")
# parts.append(f"- RAG: {'Enabled' if use_rag else 'Disabled'}\n")
# parts.append(f"- Ξ΅ (epsilon): {eps if eps != float('inf') else 'β (no DP)'}\n")
#
# # Add privacy metrics plot if we have inference data
# if use_rag and pr and pnr:
# parts.append("\n**Inference Privacy Metrics:**\n")
# parts.append(_build_inference_plot(pr, pnr))
#
# return "".join(parts)
# def _build_analysis(pii, rag, risk, use_rag, eps, pr, pnr):
# """Build analysis markdown with improved visualizations."""
# parts = ["### Detailed Privacy Analysis\n\n**Detected PIIs:**\n"]
#
# # Count PIIs in prompts vs responses
# user_pii_count = sum(1 for p in pii if hasattr(p, 'start')) # Simplified check
# # Note: This assumes you have a way to distinguish user vs response PIIs
# # If pii is combined, you may need to track this separately
#
# if pii:
# # Group by category
# category_counts = {}
# for p in pii:
# cat_name = p.category.value
# category_counts[cat_name] = category_counts.get(cat_name, 0) + 1
#
# parts.append(f"- Total detected: {len(pii)}\n")
# for cat, count in category_counts.items():
# parts.append(f" - {cat}: {count}\n")
# else:
# parts.append("- None detected\n")
#
# parts.append(f"\n**Privacy Settings:**\n")
#
# # Add DP explanation
# if eps != float('inf'):
# parts.append(f"- **Differential Privacy**: ENABLED (Ξ΅ = {eps:.1f})\n")
# parts.append(f" *Lower epsilon values provide stronger privacy protection "
# f"by adding noise to data retrieval, making it harder to infer sensitive "
# f"information about individuals.*\n")
# else:
# parts.append(f"- **Differential Privacy**: DISABLED (Ξ΅ = β)\n")
# parts.append(f" *No privacy protection applied. Consider enabling DP (lower epsilon) "
# f"to add noise-based protection.*\n")
#
# # Add privacy metrics plot if we have inference data
# if use_rag and pr and pnr:
# parts.append("\n**Inference Privacy Metrics:**\n")
# parts.append(_build_inference_plot(pr, pnr))
#
# return "".join(parts) # last worked
# def _build_privacy_settings_html(eps):
# """Build Privacy Settings HTML for left panel with fixed tooltip positioning."""
#
# parts = ['<div style="padding:12px;">']
# parts.append('<h4 style="margin-top:0;">Privacy Settings</h4>')
#
# # Tooltip content - formatted as HTML with !important for font sizes
# dp_tooltip = (
# '<div style="font-weight:700;font-size:1.1em!important;margin-bottom:5px;color:#111;">What is Differential Privacy?</div>'
# '<div style="height:1px;background:#ddd;margin:8px 0;"></div>'
# '<div style="color:#333;line-height:1.4!important;margin-bottom:6px;font-size:0.95em!important;">'
# 'Differential Privacy (DP) is a mathematical framework that adds calibrated noise '
# 'to data retrieval, ensuring individual records cannot be distinguished. '
# 'This protects sensitive information from being inferred when external data sources are used.'
# '</div>'
# '<div style="height:1px;background:#ddd;margin:8px 0;"></div>'
# '<div style="font-weight:600;color:#2c3e50;margin-bottom:4px;font-size:1.0em!important;">Understanding Epsilon (Ξ΅):</div>'
# '<div style="color:#333;line-height:1.4!important;font-size:0.95em!important;">'
# '<strong>Lower values (0.1-1.0):</strong> Stronger privacy, more noise added.<br>'
# '<strong>Higher values (5.0-10.0):</strong> Less privacy, better data utility.<br>'
# '<strong>Epsilon = β:</strong> No privacy protection applied.'
# '</div>'
# )
#
# # Unique IDs for this tooltip instance
# tooltip_id = f"dp_tooltip_{abs(hash(str(eps)))}"
# anchor_id = f"dp_anchor_{abs(hash(str(eps)))}"
#
# # JavaScript to position tooltip above anchor
# # JavaScript to position tooltip next to the current mouse cursor
# position_script = f"""
# <script>
# (function() {{
# var _mouseX = 0, _mouseY = 0;
# document.addEventListener('mousemove', function(e) {{
# _mouseX = e.clientX;
# _mouseY = e.clientY;
# }}, true);
#
# window.position_{tooltip_id} = function() {{
# var tooltip = document.getElementById('{tooltip_id}');
# if (!tooltip) return;
#
# var tooltipWidth = 380;
# var tooltipHeight = tooltip.offsetHeight || 220;
# var offset = 14; // gap between cursor tip and tooltip edge
# var viewportW = window.innerWidth;
# var viewportH = window.innerHeight;
# var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
# var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
#
# // Prefer: above-right of cursor
# var left = _mouseX + offset + scrollLeft;
# var top = _mouseY - tooltipHeight - offset + scrollTop;
#
# // Flip to below if not enough room above
# if (_mouseY < tooltipHeight + offset + 10) {{
# top = _mouseY + offset + scrollTop;
# }}
#
# // Flip to left of cursor if not enough room on the right
# if (_mouseX + offset + tooltipWidth > viewportW - 10) {{
# left = _mouseX - tooltipWidth - offset + scrollLeft;
# }}
#
# // Final clamp to viewport edges
# left = Math.max(scrollLeft + 8, Math.min(left, scrollLeft + viewportW - tooltipWidth - 8));
# top = Math.max(scrollTop + 8, top);
#
# tooltip.style.left = left + 'px';
# tooltip.style.top = top + 'px';
# }};
# }})();
# </script>
# """
#
# # Add DP status with fixed-position tooltip
# if eps != float('inf'):
# parts.append(
# f'{position_script}'
# '<p style="margin-bottom:8px;">'
# f'<span id="{anchor_id}" style="position:relative;display:inline-block;cursor:help;border-bottom:1.5px dotted #1976D2;font-weight:600;color:#1976D2;" '
# f'onmouseover="document.getElementById(\'{tooltip_id}\').style.display=\'block\';position_{tooltip_id}();" '
# f'onmouseout="document.getElementById(\'{tooltip_id}\').style.display=\'none\'">'
# 'Differential Privacy'
# '</span>: '
# '<span style="color:#2E7D32;font-weight:600;">ENABLED</span>'
# '</p>'
# f'<p style="margin-top:4px;font-size:0.95em;">'
# f'<strong>Differential Privacy guarantee level (epsilon parameter) =</strong> <span style="color:#1976D2;font-weight:700;font-size:1.1em;">{eps:.1f}</span>'
# f'</p>'
# f'<div id="{tooltip_id}" style="display:none;position:fixed;z-index:999999;'
# 'background:#fff;color:#111;'
# 'border:2px solid #bbb;border-radius:10px;padding:10px 12px;width:380px;'
# 'box-shadow:0 8px 20px rgba(0,0,0,0.15);font-size:0.7em!important;line-height:1.25!important;'
# 'text-align:left;font-weight:normal;pointer-events:none;">'
# f'{dp_tooltip}'
# '</div>'
# )
# else:
# parts.append(
# f'{position_script}'
# '<p style="margin-bottom:8px;">'
# f'<span id="{anchor_id}" style="position:relative;display:inline-block;cursor:help;border-bottom:1.5px dotted #1976D2;font-weight:600;color:#1976D2;" '
# f'onmouseover="document.getElementById(\'{tooltip_id}\').style.display=\'block\';position_{tooltip_id}();" '
# f'onmouseout="document.getElementById(\'{tooltip_id}\').style.display=\'none\'">'
# 'Differential Privacy'
# '</span>: '
# '<span style="color:#D32F2F;font-weight:600;">DISABLED</span>'
# '</p>'
# f'<p style="margin-top:4px;font-size:0.95em;">'
# f'<strong>Differential Privacy Guarantee =</strong> <span style="color:#D32F2F;font-weight:700;font-size:1.1em;">β</span>'
# f'</p>'
# f'<p style="font-size:0.85em;color:#666;margin-top:8px;font-style:italic;">'
# f'Consider enabling DP for privacy protection.'
# f'</p>'
# f'<div id="{tooltip_id}" style="display:none;position:fixed;z-index:999999;'
# 'background:#fff;color:#111;'
# 'border:2px solid #bbb;border-radius:10px;padding:10px 12px;width:380px;'
# 'box-shadow:0 8px 20px rgba(0,0,0,0.15);font-size:0.7em!important;line-height:1.25!important;'
# 'text-align:left;font-weight:normal;pointer-events:none;">'
# f'{dp_tooltip}'
# '</div>'
# )
#
# parts.append("</div>")
# return "".join(parts)
def _build_privacy_settings_html(eps):
"""Build Privacy Settings HTML for left panel with a CSS-native expandable DP explainer."""
if eps != float('inf'):
status_html = '<span style="color:#2E7D32;font-weight:600;">ENABLED</span>'
# Translate epsilon to a human-readable protection level
if eps <= 30.0:
level_label = "High"
level_color = "#2E7D32"
level_desc = "Strong protection β the AI's view of your data is significantly randomised."
elif eps <= 60.0:
level_label = "Medium"
level_color = "#F57C00"
level_desc = "Moderate protection β some noise is added to limit profiling accuracy."
else:
level_label = "Low"
level_color = "#D32F2F"
level_desc = "Weak protection β only a small amount of noise is applied."
eps_line = (
f'<p style="margin-top:4px;margin-bottom:0;font-size:0.95em;">'
f'<strong>Protection level:</strong> '
f'<span style="color:{level_color};font-weight:700;font-size:1.1em;">{level_label}</span>'
f'</p>'
f'<p style="font-size:0.85em;color:#000000;margin-top:3px;font-style:italic;">'
# f'{level_desc}</p>'
)
else:
status_html = '<span style="color:#D32F2F;font-weight:600;">DISABLED</span>'
eps_line = (
f'<p style="margin-top:4px;margin-bottom:0;font-size:0.95em;">'
f'<strong>Protection level:</strong> '
f'<span style="color:#D32F2F;font-weight:700;font-size:1.1em;">None</span>'
f'</p>'
# f'<p style="font-size:0.85em;color:#000000;margin-top:4px;font-style:italic;">'
# f'Consider enabling privacy protection to reduce profiling risk.</p>'
)
return (
f'<div style="border:1px solid #aaaaaa; color:#000000;">'
f'<p style="margin-bottom:4px; color:#000000;"><strong>Differential Privacy</strong>: {status_html}</p>'
f'{eps_line}'
# ββ Pure-CSS expandable explainer ββββββββββββββββββββββββββ
f'<details style="margin-top:10px;">'
f'<summary style="cursor:pointer;font-size:0.9em;font-weight:600;color:#1976D2;'
f'list-style:none;display:inline-flex;align-items:center;gap:4px;'
f'user-select:none;">'
f'<span class="dp-arrow" style="font-size:0.8em;">βΌ</span> What is this?'
f'</summary>'
f'<div style="margin-top:8px;padding:10px 12px;'
f'background:#f0f4f8;border-left:3px solid #1976D2;border-radius:0 6px 6px 0;'
f'font-size:0.88em;color:#000000;">'
f'<div style="margin-bottom:10px;color:#000000;">'
f'When privacy protection is enabled, this tool automatically modifies your messages '
f'to make it harder for the AI to identify you, <em>before</em> any information '
f'leaves your device:'
f'</div>'
f'<div style="display:flex;align-items:flex-start;gap:8px;margin-bottom:10px;">'
f'<span style="font-size:1.3em;flex-shrink:0;">βοΈ</span>'
f'<div>'
f'<strong>Some words in your message.</strong> '
f'Some words in what you type are automatically swapped for similar-sounding '
f'alternatives before the AI reads them. '
f'A π badge on your message shows when this happened and which words changed.'
f'</div>'
f'</div>'
f'<div style="height:1px;background:#aaaaaa;margin:8px 0;"></div>'
f'<div style="font-weight:600;color:#000000;margin-bottom:6px;">'
f'About the protection level:</div>'
f'<div style="margin-bottom:3px;color:#000000;">π’ <strong>High:</strong> '
f'Strong privacy β more noise applied, AI responses may be slightly less precise.</div>'
f'<div style="margin-bottom:3px;color:#000000;">π‘ <strong>Medium:</strong> '
f'Balanced β moderate noise, reasonable trade-off between privacy and accuracy.</div>'
f'<div style="margin-bottom:3px;color:#000000;">π΄ <strong>Low:</strong> '
f'Weak privacy β minimal noise, AI responses are more accurate but you are easier to profile.</div>'
f'<div style="margin-bottom:3px;color:#D32F2F;"><strong>Off:</strong> '
f'No protection β the AI sees your exact message and data.</div>'
f'</div>' # end inner content div
f'</details>'
f'</div>' # end outer padding div
)
def _pii_severity(category):
"""Return (label, color) severity info for a PIICategory."""
if category == PIICategory.SENSITIVE:
return "High", "#b71c1c"
elif category == PIICategory.IDENTITY:
return "High", "#b71c1c"
elif category == PIICategory.CONTACT:
return "Medium", "#e65100"
else: # LOCATION
return "Low", "#1976D2"
def _load_avatar_image_b64(gender, age):
"""Load a profile photo from the avatars/ directory and return a base64 data-URI.
Expected filenames in <project_root>/avatars/:
male_teen.jpg female_teen.jpg (Age bin: 0-17)
male_young.jpg female_young.jpg (Age bin: 18-29)
male_adult.jpg female_adult.jpg (Age bin: 30-44)
male_middle.jpg female_middle.jpg (Age bin: 45-59)
male_senior.jpg female_senior.jpg (Age bin: 60+)
default.jpg (fallback when gender unknown)
Also accepts .png variants of every name above.
Fallback chain (tried in order):
1. avatars/{gender}_{age_label}.jpg/.png
2. avatars/{gender}_adult.jpg/.png β same gender, adult age
3. avatars/default.jpg/.png β universal fallback
4. Returns ("", False) β caller shows an initials circle
Returns: (data_uri_string, found_bool)
"""
import base64
gender_prefix = {"Male": "male", "Female": "female"}.get(gender, None)
age_suffix = {
"0-17": "teen",
"18-29": "young",
"30-44": "adult",
"45-59": "middle",
"60+": "senior",
}.get(age, "adult")
root = os.path.dirname(os.path.abspath(__file__))
avatars_dir = os.path.join(root, "avatars")
candidates = []
if gender_prefix:
candidates.append(os.path.join(avatars_dir, f"{gender_prefix}_{age_suffix}.jpg"))
candidates.append(os.path.join(avatars_dir, f"{gender_prefix}_{age_suffix}.png"))
candidates.append(os.path.join(avatars_dir, f"{gender_prefix}_adult.jpg"))
candidates.append(os.path.join(avatars_dir, f"{gender_prefix}_adult.png"))
candidates.append(os.path.join(avatars_dir, "default.jpg"))
candidates.append(os.path.join(avatars_dir, "default.png"))
for path in candidates:
if os.path.isfile(path):
try:
ext = os.path.splitext(path)[1].lower()
mime = "image/png" if ext == ".png" else "image/jpeg"
with open(path, "rb") as f:
b64 = base64.b64encode(f.read()).decode("ascii")
return f"data:{mime};base64,{b64}", True
except Exception:
continue
return "", False
def _build_inferred_avatar(probs_rag, inference_warning_html="", is_warning=False):
"""Build the 'How the AI sees you' card.
Layout (top β bottom inside the card):
β’ Header: title left, confidence badge right
β’ Subtitle
β’ Photo: 130Γ130 px circle, centred
β’ Attribute rows: one per inferred attribute, full card width.
Each row embeds the inner content of the matching <li> verbatim from
inference_warning_html, preserving:
β the predicted value
β the Why? tt-anchor with its evidence tt-popup spans (hover works)
β the probability (p = X%)
β the red lift message if above threshold
Nothing is added or duplicated by this function.
Card colour:
is_warning=True β yellow (#FFFDE7) + amber border
is_warning=False β blue (#E3F2FD) + blue border
"""
# Always fill any missing attributes with a uniform distribution so the
# panel is always populated, even when the inference LLM call fails.
probs_rag = _fill_missing_with_uniform(probs_rag or {})
# ββ Minimum-confidence gate βββββββββββββββββββββββββββββββββββββββββββ
def _top(dist, min_p=0.15):
if not dist:
return None, 0.0
v = max(dist, key=dist.get)
p = dist[v]
return (v, p) if p >= min_p else (None, p)
gender, gender_p = _top(probs_rag.get("Gender", {}), 0.20)
age, age_p = _top(probs_rag.get("Age bin", {}), 0.15)
finance, finance_p = _top(probs_rag.get("Finance Status", {}), 0.15)
if max(gender_p, age_p, finance_p) < 0.15:
return ""
# ββ Card colour scheme ββββββββββββββββββββββββββββββββββββββββββββββββ
if is_warning:
card_bg = "#FFFDE7"
card_border = "#F9A825"
title_color = "#E65100"
title_icon = "β οΈ"
# subtitle = "External data raised the risk of accurate profiling."
subtitle = "Based on your conversation so far."
else:
card_bg = "#E3F2FD"
card_border = "#1976D2"
title_color = "#0D47A1"
title_icon = "π€"
subtitle = "Based on your conversation so far."
# ββ Profile photo (or initials fallback) ββββββββββββββββββββββββββββββ
img_uri, img_found = _load_avatar_image_b64(gender, age)
if img_found:
photo_html = (
f'<img src="{img_uri}" alt="AI-inferred profile photo" '
f'style="width:130px;height:130px;object-fit:cover;border-radius:50%;'
f'border:3px solid {card_border};display:block;margin:0 auto 12px;">'
)
else:
initials = "F" if gender == "Female" else ("M" if gender == "Male" else "?")
photo_html = (
f'<div style="width:130px;height:130px;border-radius:50%;'
f'background:{card_border};border:3px solid {card_border};'
f'display:flex;align-items:center;justify-content:center;'
f'margin:0 auto 12px;color:white;font-size:2em;font-weight:700;">'
f'{initials}</div>'
)
# ββ Per-attribute content extractor ββββββββββββββββββββββββββββββββββ
# The <li> structure produced by build_inference_warning (after Fix 1) is:
#
# <li style="..."><b>ATTR</b>: VALUE
# <span class="tt-anchor">Why?
# <span class="tt-popup tt-above">EVIDENCE</span>
# <span class="tt-popup tt-below">EVIDENCE</span>
# </span>
# <span style="color:#555;">(p = X%)</span>
# [<span style="color:#b71c1c;">lift</span>]
# </li>
#
# We grab everything between "<b>ATTR</b>: " and "</li>" and embed it
# verbatim β value, Why? anchor with evidence popups, probability, lift
# β all intact, with no duplication.
def _extract_li_content(html, attr):
"""Return inner content of the <li> for attr (after the colon), or ''."""
marker = f'<b>{attr}</b>: '
idx = html.find(marker)
if idx == -1:
return ""
content_start = idx + len(marker)
li_close = html.find('</li>', content_start)
if li_close == -1:
return ""
return html[content_start:li_close].strip()
# ββ Build attribute rows ββββββββββββββββββββββββββββββββββββββββββββββ
ATTR_ORDER = [
"Gender", "Age bin", "Locale",
"Marital Status", "Finance Status", "Education",
]
rows = []
for attr in ATTR_ORDER:
dist = probs_rag.get(attr, {})
if not dist:
continue
top_val = max(dist, key=dist.get)
top_p = dist[top_val]
li_content = _extract_li_content(inference_warning_html, attr)
if li_content:
# Embed verbatim β Why? tooltip, probability, and lift all included
row_content = (
f'<span style="font-weight:600;color:#333;font-size:0.9em;">'
f'{attr}:</span> {li_content}'
)
else:
# Fallback: plain text when warning_html is absent or attr missing
row_content = (
f'<span style="font-weight:600;color:#333;font-size:0.9em;">'
f'{attr}:</span> '
f'<span style="color:#111;">{top_val}</span> '
f'<span style="color:#666;font-size:0.83em;">'
f'(p = {top_p:.0%})</span>'
)
rows.append(
f'<div style="margin-bottom:8px;line-height:1.5;">'
f'{row_content}'
f'</div>'
)
attrs_html = "".join(rows) if rows else (
'<p style="color:#999;font-size:0.82em;font-style:italic;">'
'No attributes inferred yet.</p>'
)
# ββ Confidence badge ββββββββββββββββββββββββββββββββββββββββββββββββββ
conf = max(gender_p, age_p, finance_p)
conf_pct = int(conf * 100)
# ββ Assemble card βββββββββββββββββββββββββββββββββββββββββββββββββββββ
return (
f'<div style="background:{card_bg};border:2px solid {card_border};'
f'border-radius:10px;padding:12px 14px 10px;margin-bottom:8px;">'
# Header
f'<div style="display:flex;align-items:center;'
f'justify-content:space-between;margin-bottom:5px;">'
f'<span style="font-size:0.82em;font-weight:700;color:{title_color};">'
f'{title_icon} How the AI sees you'
f'</span>'
f'<span style="font-size:0.68em;color:{title_color};opacity:0.8;'
f'border:1px solid {card_border};border-radius:10px;padding:1px 7px;">'
f'confidence: {conf_pct}%'
f'</span>'
f'</div>'
# Subtitle
f'<div style="font-size:0.72em;color:#666;font-style:italic;margin-bottom:10px;">'
f'{subtitle}'
f'</div>'
# Photo centred
f'{photo_html}'
# Attribute rows β full card width
f'<div style="font-size:0.88em;">'
f'{attrs_html}'
f'</div>'
# Footer disclaimer
f'<div style="font-size:0.62em;color:#999;margin-top:8px;font-style:italic;">'
f'Photo is illustrative only β selected to match inferred profile. '
f'These are AI predictions and may be incorrect.'
f'</div>'
f'</div>'
)
def _build_analysis(pii, rag, risk, use_rag, eps, pr, pnr, inference_warning_html="", is_warning=False, include_avatar=True, show_pii=True):
"""Build analysis HTML for the right panel.
Layout (top β bottom):
1. 'How the AI sees you' card (photo + inferred attrs + Why? tooltips)
β omitted when include_avatar=False (card rendered separately)
2. Divider
3. Detected PIIs (tags with severity)
"""
parts = ['<div style="padding-left:8px;padding-right:8px;font-size:0.88em;">']
# ββ Section 1: Combined avatar + inferable attributes ββββββββββββββββ
if include_avatar:
avatar_html = _build_inferred_avatar(pr or {}, inference_warning_html, is_warning)
if avatar_html:
parts.append(avatar_html)
parts.append('<div style="height:1px;background:#e0e0e0;margin:8px 0;"></div>')
# ββ Section 2: Detected PIIs (last, as requested) ββββββββββββββββββββ
if show_pii:
parts.append('<p style="margin-bottom:5px;font-weight:700;color:#2c3e50;">Detected Personally Identifiable Information (PII) in user\'s messages:</p>')
if pii:
# De-duplicate by (text, fine_type)
seen = set()
unique_pii = []
for p in pii:
key = (p.text.strip().lower(), p.fine_type.lower())
if key not in seen:
seen.add(key)
unique_pii.append(p)
parts.append('<div style="display:flex;flex-wrap:wrap;gap:5px;margin-bottom:8px;">')
for p in unique_pii:
col = PII_COLORS.get(p.category, {"bg": "#f5f5f5", "border": "#999"})
sev_label, sev_color = _pii_severity(p.category)
cat_label = p.category.value.capitalize()
fine = p.fine_type.title() if p.fine_type else cat_label
text_disp = p.text[:28] + ("β¦" if len(p.text) > 28 else "")
parts.append(
f'<span title="{fine} Β· {sev_label} severity Β· confidence {p.confidence:.0%}" '
f'style="display:inline-flex;align-items:center;gap:3px;'
f'background:{col["bg"]};border:1.5px solid {col["border"]};'
f'border-radius:4px;padding:2px 6px;cursor:default;">'
f'<span style="font-size:0.78em;font-weight:600;color:#333;">{text_disp}</span>'
f'<span style="font-size:0.68em;color:#666;margin-left:1px;">[{fine}]</span>'
f'<span style="font-size:0.65em;font-weight:700;color:{sev_color};'
f'margin-left:2px;border-left:1px solid {col["border"]};padding-left:3px;">{sev_label}</span>'
f'</span>'
)
parts.append('</div>')
# Category legend
cat_counts = {}
for p in unique_pii:
cat_counts[p.category.value] = cat_counts.get(p.category.value, 0) + 1
legend_parts = [f'<span style="color:#555;">{v}Γ {k}</span>' for k, v in cat_counts.items()]
parts.append(f'<div style="font-size:0.78em;color:#888;margin-bottom:8px;">'
f'Total: {len(unique_pii)} β {", ".join(legend_parts)}</div>')
else:
parts.append('<div style="color:#888;font-size:0.85em;margin-bottom:8px;">None detected</div>')
parts.append("</div>")
return "".join(parts)
# def build_inference_warning(probs_rag, probs_no_rag, use_rag):
# ============================================================
# SECTION 14.5 β ROBUST EVIDENCE SOURCE MATCHING
# ============================================================
def tokenize_for_matching(text):
"""
Tokenize text for matching, removing punctuation and common words.
Returns:
set: Set of significant tokens (lowercased, >3 chars)
"""
import re
# Remove punctuation and split
tokens = re.findall(r'\b\w+\b', text.lower())
# Keep only significant tokens (>3 chars, not common stop words)
significant_tokens = {t for t in tokens if len(t) > 3 and t not in _DP_STOPWORDS}
return significant_tokens
def calculate_token_overlap(quote_tokens, doc_tokens, bidirectional=False):
"""Calculate token overlap ratio between two token sets.
Parameters
----------
quote_tokens, doc_tokens : set
bidirectional : bool
If True, return max(forward, reverse) overlap so that a short quote
with a high fraction of user-input words is also detected, not only
a quote whose words are mostly covered by the user input.
Use True when matching against user input; False (default) for docs.
Returns
-------
float: overlap ratio in [0, 1]
"""
if not quote_tokens or not doc_tokens:
return 0.0
intersection = len(quote_tokens & doc_tokens)
forward = intersection / len(quote_tokens)
if not bidirectional:
return forward
reverse = intersection / len(doc_tokens) # doc_tokens is user_tokens here
return max(forward, reverse)
def extract_doc_source_label(doc):
"""
Extract human-readable source label from document metadata.
Args:
doc: Document object with metadata
Returns:
str: Source label (e.g., "From uploaded data", "From Twitter (Internet)")
"""
if not hasattr(doc, 'metadata') or not isinstance(doc.metadata, dict):
return "From system data"
source_type = doc.metadata.get('source', '')
platform = doc.metadata.get('platform', '').strip().lower()
if source_type == 'uploaded_csv':
return "From uploaded data"
elif source_type.startswith('social_media_'):
platform_label = platform.capitalize() if platform else "Social Media"
return f"From {platform_label} (Internet)" if "web" not in platform_label.lower() else f"From the {platform_label} (Internet)"
elif source_type == 'fallback_tweet_data':
return "From Twitter (Internet)"
else:
# Documents from the default background corpus (e.g. PANORAMA synthetic profiles)
return DEFAULT_CORPUS_SOURCE_LABEL
def _doc_source_url(doc):
"""Extract the source URL from a document's metadata, or '' if absent."""
if not hasattr(doc, "metadata") or not isinstance(doc.metadata, dict):
return ""
return (
doc.metadata.get("source_url") or
doc.metadata.get("tweet_url") or
doc.metadata.get("url") or
""
)
def find_evidence_source(quote, user_input, retrieved_docs, min_overlap_ratio=0.4,
perturbed_input=None, prior_turns=None):
"""
Find the source of an evidence quote using robust multi-strategy matching.
Matching priority:
Strategy 1 β Exact substring: user turns first, then docs (high precision)
Strategy 2 β Token overlap: docs first, then user turns (avoids topic-word FPs)
Strategy 3 β Phrase windows: docs first, then user turns
Evidence quotes are LLM-generated paraphrases. Because the LLM is asked
to explain inferences drawn from retrieved documents, most evidence quotes
describe external-data content even when they share topic words with the
user's message. Checking docs before user turns in the fuzzy strategies
prevents those shared topic words from causing false "From your input" labels.
When no source can be matched the quote is considered LLM internal reasoning
(rationale) rather than a verbatim excerpt from any document.
Returns:
tuple: (is_from_user, is_from_docs, doc_source, match_confidence,
matched_turn_dp_active, matched_turn_original_text, doc_url)
doc_url β URL of the matched document, or "" if not from a doc / unknown.
"""
import re as _re
quote_lower = str(quote).lower().strip()
user_input_lower = user_input.lower() if user_input else ""
perturbed_lower = perturbed_input.lower() if perturbed_input else ""
# Build the full list of user turns to search
current_dp_active = bool(perturbed_lower)
turns_to_search = [(user_input_lower, perturbed_lower,
current_dp_active, user_input or "")]
if prior_turns:
for pt in prior_turns:
orig_l = (pt.get("original") or "").lower()
pert_l = (pt.get("perturbed") or "").lower()
dp_on = bool(pt.get("dp_active", False))
orig_d = pt.get("original") or ""
turns_to_search.append((orig_l, pert_l, dp_on, orig_d))
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Strategy 1: Exact substring β user turns first, then docs
# Rationale: if the evidence is a verbatim copy of text the user typed,
# it genuinely comes from the user.
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
for orig_l, pert_l, dp_on, orig_disp in turns_to_search:
if quote_lower in orig_l:
return True, False, "From your input", "exact", dp_on, orig_disp, ""
if len(quote_lower) > 50 and quote_lower[:50] in orig_l:
return True, False, "From your input", "exact", dp_on, orig_disp, ""
if pert_l and quote_lower in pert_l:
return True, False, "From your input", "exact", dp_on, orig_disp, ""
if pert_l and len(quote_lower) > 50 and quote_lower[:50] in pert_l:
return True, False, "From your input", "exact", dp_on, orig_disp, ""
if retrieved_docs:
for doc in retrieved_docs:
doc_text = doc.page_content if hasattr(doc, "page_content") else str(doc)
doc_lower = doc_text.lower()
if quote_lower in doc_lower or (
len(quote_lower) > 50 and quote_lower[:50] in doc_lower):
doc_source = extract_doc_source_label(doc)
return False, True, doc_source, "exact", False, "", _doc_source_url(doc)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Strategy 2: Token overlap β docs first, then user turns
# Rationale: LLM-generated evidence paraphrases retrieved docs. Both
# the evidence and the user message discuss the same topic, so shared
# topic tokens do NOT indicate the evidence came from the user.
# We check docs first; user turns are only a fallback with a high bar.
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
quote_tokens = tokenize_for_matching(quote)
DOC_OVERLAP_THRESHOLD = 0.35 # forward overlap: fraction of quote tokens in doc
USER_OVERLAP_THRESHOLD = 0.65 # bidirectional: raised from 0.25 to avoid FPs
if len(quote_tokens) >= 3:
# 2a: Check retrieved docs first
if retrieved_docs:
best_overlap, best_doc = 0.0, None
for doc in retrieved_docs:
doc_text = doc.page_content if hasattr(doc, "page_content") else str(doc)
doc_tokens = tokenize_for_matching(doc_text)
ov = calculate_token_overlap(quote_tokens, doc_tokens)
if ov > best_overlap:
best_overlap, best_doc = ov, doc
if best_overlap >= DOC_OVERLAP_THRESHOLD and best_doc:
doc_source = extract_doc_source_label(best_doc)
logger.info(
f" π Token overlap match with doc ({best_overlap:.0%}): '{quote[:50]}...'")
return False, True, doc_source, "token_overlap", False, "", _doc_source_url(best_doc)
# 2b: Fall back to user turns only with a high threshold
for orig_l, pert_l, dp_on, orig_disp in turns_to_search:
turn_tokens = tokenize_for_matching(orig_l)
if calculate_token_overlap(quote_tokens, turn_tokens,
bidirectional=True) >= USER_OVERLAP_THRESHOLD:
logger.info(
f" π Token overlap match with user turn: '{quote[:50]}...'")
return True, False, "From your input", "token_overlap", dp_on, orig_disp, ""
if pert_l:
pert_tokens = tokenize_for_matching(pert_l)
if calculate_token_overlap(quote_tokens, pert_tokens,
bidirectional=True) >= USER_OVERLAP_THRESHOLD:
logger.info(
f" π Token overlap match with perturbed user turn: '{quote[:50]}...'")
return True, False, "From your input", "token_overlap", dp_on, orig_disp, ""
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Strategy 3: Short-phrase windows β docs first, then user turns
# 3a: 3-word windows against docs
# 3b: 3-word windows against user turns (stricter guard: phrase must NOT
# appear in any retrieved doc first)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
quote_words = _re.findall(r'\b\w+\b', quote_lower)
if len(quote_words) >= 3 and retrieved_docs:
doc_texts_lower = [
(doc.page_content if hasattr(doc, "page_content") else str(doc)).lower()
for doc in retrieved_docs
]
for i in range(len(quote_words) - 2):
phrase = ' '.join(quote_words[i:i+3])
for doc, doc_lower in zip(retrieved_docs, doc_texts_lower):
if phrase in doc_lower:
doc_source = extract_doc_source_label(doc)
logger.info(
f" π 3-word phrase match with doc: '{phrase}' β {doc_source}")
return False, True, doc_source, "partial", False, "", _doc_source_url(doc)
# 3b: 3-word phrase scan against user turns β only attribute to user when
# the phrase does NOT appear in any retrieved doc (disambiguates shared
# topic phrases).
if len(quote_words) >= 3:
doc_texts_lower_flat = [
(doc.page_content if hasattr(doc, "page_content") else str(doc)).lower()
for doc in (retrieved_docs or [])
]
for i in range(len(quote_words) - 2):
phrase = ' '.join(quote_words[i:i+3])
phrase_in_doc = any(phrase in dt for dt in doc_texts_lower_flat)
if phrase_in_doc:
continue # ambiguous β skip, will fall through to "General"
for orig_l, pert_l, dp_on, orig_disp in turns_to_search:
if phrase in orig_l:
logger.info(
f" π 3-word phrase (user-only) match: '{phrase}'")
return True, False, "From your input", "partial", dp_on, orig_disp, ""
if pert_l and phrase in pert_l:
logger.info(
f" π 3-word phrase (perturbed, user-only) match: '{phrase}'")
return True, False, "From your input", "partial", dp_on, orig_disp, ""
# No match found β the text is LLM-internal reasoning, not a source quote.
logger.info(f" β οΈ No source match for evidence (treated as rationale): '{quote[:50]}...'")
return False, False, "General", "none", False, "", ""
# ============================================================
# SECTION 15 β BUILD INFERENCE WARNING
# ============================================================
# def build_inference_warning(probs_rag, probs_no_rag, use_rag):
# """Build yellow warning banner with lift threshold and tooltips."""
# if not probs_rag:
# return ""
#
# items = []
# max_lift = 0.0 # Track highest lift across all attributes
#
# for attr in SENSITIVE_ATTRIBUTES:
# dist = probs_rag.get(attr, {})
# if not dist:
# continue
# top_val = max(dist, key=dist.get)
# top_p = dist[top_val]
#
# lift = 0.0
# extra = ""
# if use_rag and probs_no_rag:
# p_nr = probs_no_rag.get(attr, {}).get(top_val, 0.0)
# z = InferentialPrivacyMetrics.compute_z(top_p, p_nr)
# lift = InferentialPrivacyMetrics.compute_lift(z)
#
# # Track maximum lift
# if lift > max_lift:
# max_lift = lift
#
# # Only show attributes that meet the lift threshold
# if lift > INFERENCE_LIFT_THRESHOLD and not math.isinf(lift):
# extra = (f' <span style="color:#b71c1c;">'
# f'(+{lift:.0%} risk from external data)</span>')
# else:
# # Skip this attribute if lift is below threshold
# continue
#
# # Add attribute with tooltip - wrap in container with tooltip class
# items.append(
# f'<li><b>{attr}</b>: '
# f'<span class="tooltip-container" style="position:relative;display:inline-block;cursor:help;border-bottom:1px dotted #999;">'
# f'{top_val}'
# f'<span class="tt-popup">This is a predicted value based on AI inference, '
# f'not confirmed information about you.</span>'
# f'</span> '
# f'<span style="color:#555;">(p = {top_p:.0%})</span>'
# f'{extra}</li>')
#
# # Only show warning if at least one attribute exceeds threshold
# if not items or (use_rag and max_lift <= INFERENCE_LIFT_THRESHOLD):
# return ""
#
# source_note = ("your input <b>and external data sources</b>"
# if use_rag else "your input alone")
#
# # Add tooltip to the warning title - wrap in container
# return (
# '<div style="background:#FFF9C4;border:2px solid #F9A825;'
# 'border-radius:8px;padding:14px 18px;margin-top:14px;">'
# '<span class="tt-anchor" style="position:relative;display:inline-block;cursor:help;border-bottom:1px dotted #E65100;">'
# '<b style="color:#E65100;">β Privacy Warning β Inferable Attributes</b>'
# '<span class="tt-popup tt-above">'
# '<div class="tt-title">These attributes are AI predictions based on your input '
# 'and may not accurately reflect your actual information.</div>'
# '</span>'
# '<span class="tt-popup tt-below">'
# '<div class="tt-note">These attributes are AI predictions based on your input '
# 'and may not accurately reflect your actual information.</div>'
# '</span>'
# '</span><br>'
# f'<span style="color:#333;">Based on {source_note}, the AI system '
# f'could potentially infer the following about you:</span>'
# f'<ul style="margin:8px 0 4px 18px;padding:0;">{"".join(items)}</ul>'
# '</div>')
def build_inference_warning(user_input, probs_rag, probs_no_rag, use_rag,
evidence_rag=None, retrieved_docs=None,
input_dp_metadata=None, perturbed_user_input=None,
conversation_state=None):
"""Build yellow warning banner with lift threshold and explainability tooltips.
Inference metrics are ALWAYS collected for every attribute so they can be
logged regardless of whether the banner is ultimately shown to the user.
The warning banner is only shown when at least one attribute exceeds
INFERENCE_LIFT_THRESHOLD, but inference_metrics always captures everything.
evidence_rag may contain either:
- New format: {attr: [{"quote": "...", "type": "explicit"|"implicit"}, ...]}
- Legacy format: {attr: ["...", ...]} (plain strings)
Returns:
tuple: (warning_html, inference_metrics_dict, warning_shown_bool)
warning_html β HTML string (empty string if threshold not met)
inference_metrics β dict with ALL attributes, for logging
warning_shown β True iff the banner will be visible to the user
"""
# print("In build_inference_warning")
if not probs_rag:
return "", {}, False
if evidence_rag is None:
evidence_rag = {}
# Build prior_turns list: all user messages with original + perturbed text.
prior_turns = []
if conversation_state and conversation_state.messages:
for msg in conversation_state.messages:
if msg.role != "user":
continue
dp_m = getattr(msg, "dp_metadata", None)
prior_turns.append({
"original": msg.content,
"perturbed": dp_m.get("perturbed_text", "") if dp_m else "",
"dp_active": bool(dp_m and dp_m.get("num_substitutions", 0) > 0),
})
items = [] # list items for the HTML banner (only threshold-passing attrs)
max_lift = 0.0
inference_metrics = {} # collected for ALL attributes (logging)
for attr in SENSITIVE_ATTRIBUTES:
dist = probs_rag.get(attr, {})
if not dist:
continue
top_val = max(dist, key=dist.get)
top_p = dist[top_val]
lift = 0.0
p_nr = 0.0
p_pop = POPULATION_PRIORS.get(attr, {}).get(top_val, 0.0)
# Lift is always computed against the population prior so that
# ALL experimental conditions (with or without RAG) produce a
# non-zero score when the posterior exceeds the base rate.
if p_pop > 0:
z = InferentialPrivacyMetrics.compute_z(top_p, p_pop)
lift = InferentialPrivacyMetrics.compute_lift(z)
if lift > max_lift:
max_lift = lift
# Still track the no-RAG probability for logging / comparison.
if probs_no_rag:
p_nr = probs_no_rag.get(attr, {}).get(top_val, 0.0)
# ββ Determine dominant evidence_type for this attribute ββββββββββ
# evidence_rag[attr] is a list of either:
# new: {"quote": str, "type": "explicit"|"implicit"}
# legacy: plain string β treated as "implicit"
raw_evidence = evidence_rag.get(attr, [])
evidence_type = "unknown"
if raw_evidence:
for item in raw_evidence:
if isinstance(item, dict):
t = item.get("type", "implicit")
else:
t = "implicit"
# If any evidence item is explicit, mark the whole attribute explicit
if t == "explicit":
evidence_type = "explicit"
break
else:
evidence_type = "implicit"
# ββ Always record in inference_metrics (for logging) βββββββββββββ
if not (math.isinf(lift) or math.isnan(lift)):
inference_metrics[attr] = {
'top_value': top_val,
'confidence': round(float(top_p), 4),
'lift': round(float(lift), 4),
'prob_rag': round(float(top_p), 4),
'prob_no_rag': round(float(p_nr), 4),
'p_pop': round(float(p_pop), 4),
'evidence_type': evidence_type,
}
# ββ Only add to the visible banner if threshold is met βββββββββββ
# print(lift, INFERENCE_LIFT_THRESHOLD)
above_threshold = lift > INFERENCE_LIFT_THRESHOLD and not math.isinf(lift)
# if not above_threshold:
# continue
extra = (
f' <span style="color:#b71c1c;">(+{lift:.0%} increase based on external data)</span>'
if above_threshold and use_rag else ""
)
# ββ Build evidence tooltip ββββββββββββββββββββββββββββββββββββββββ
# Normalise evidence items to dicts so we can extract quote text
normalised_evidence = []
for item in raw_evidence:
if isinstance(item, dict):
quote_text = str(item.get("quote", item.get("text", ""))).strip()
ev_type = item.get("type", "implicit")
else:
quote_text = str(item).strip()
ev_type = "implicit"
if (quote_text and
'no evidence' not in quote_text.lower() and
'not found' not in quote_text.lower() and
'no specific' not in quote_text.lower() and
'unavailable' not in quote_text.lower() and
len(quote_text) > 5):
normalised_evidence.append({"quote": quote_text, "type": ev_type})
if normalised_evidence:
evidence_html = '<div class="tt-title">Evidence for Prediction</div>'
evidence_html += '<div class="tt-sub" style="color:#666;font-size:1.1em!important;margin-bottom:6px;">The following evidence from your input or external data sources supports this prediction:</div>'
evidence_html += '<div class="tt-divider"></div>'
for ev_item in normalised_evidence[:3]:
q = ev_item["quote"]
ev_badge = (
'<span style="background:#e8f5e9;color:#2e7d32;border-radius:3px;'
'padding:1px 5px;font-size:0.8em;font-weight:600;margin-left:4px;">explicit</span>'
if ev_item["type"] == "explicit" else
'<span style="background:#fff3e0;color:#e65100;border-radius:3px;'
'padding:1px 5px;font-size:0.8em;font-weight:600;margin-left:4px;">implicit</span>'
)
q_esc = _esc(q)
(is_from_user, is_from_docs, doc_source, _conf,
matched_turn_dp, matched_turn_orig, doc_url) = find_evidence_source(
q, user_input, retrieved_docs, min_overlap_ratio=0.4,
perturbed_input=perturbed_user_input,
prior_turns=prior_turns,
)
# Determine whether this is a genuine source excerpt or LLM
# internal reasoning (rationale). When neither user input nor
# any retrieved document can be matched, the text is the model's
# own thinking and should NOT be displayed like a quoted source.
is_rationale = (not is_from_user) and (not is_from_docs)
if is_from_user:
source_label = "From your input"
source_color = "#a119d2"
elif is_from_docs:
source_label = doc_source
source_color = "#1976D2" if doc_source == "From uploaded data" else (
"#F57C00" if "(Internet)" in doc_source else "#666"
)
else:
source_label = "AI reasoning"
source_color = "#757575"
if len(q_esc) > ATTR_INFERENCE_EVIDENCE_STORED_EXCERPT_LENGTH:
q_esc = q_esc[:ATTR_INFERENCE_EVIDENCE_STORED_EXCERPT_LENGTH] + "..."
evidence_html += '<div class="tt-section" style="margin-bottom:12px;">'
evidence_html += f'<div class="tt-label" style="margin-bottom:3px;">'
evidence_html += f'<span style="color:{source_color};font-size:0.85em!important;">β {source_label}</span>{ev_badge}'
evidence_html += '</div>'
if is_rationale:
# Display as plain italic reasoning text, not as a quoted excerpt.
evidence_html += (
f'<div class="tt-sub" style="margin-top:4px;color:#555;font-size:1.2em!important;'
f'font-style:italic;line-height:1.4;">{q_esc}</div>'
)
else:
# Display as a quoted source excerpt.
evidence_html += f'<div class="tt-doc" style="margin-top:4px;">"{q_esc}"</div>'
# For doc sources, add a clickable URL if available.
if is_from_docs and doc_url:
url_esc = _esc(doc_url)
evidence_html += (
f'<div style="margin-top:4px;font-size:0.82em;">'
f'<span class="tt-k" style="color:#888;">Source link: </span>'
f'<a href="{url_esc}" target="_blank" '
f'style="color:#1976D2;word-break:break-all;text-decoration:underline;">'
f'{url_esc}</a></div>'
)
# When DP was active for the matched turn, note that the AI saw
# a word-substituted version, not the original wording.
if is_from_user and matched_turn_dp and matched_turn_orig:
orig_display = matched_turn_orig[:400] + ("β¦" if len(matched_turn_orig) > 400 else "")
orig_esc = _esc(orig_display)
evidence_html += (
f'<div style="margin-top:6px;padding:5px 8px;background:#fff8e1;'
f'border-left:3px solid #f9a825;border-radius:0 4px 4px 0;'
f'font-size:0.88em;color:#555;">'
f'π Privacy protection was active for this message β the AI '
f'received a modified (word-substituted) version of your text, '
f'not your original wording.<br>'
f'<span style="display:block;margin-top:5px;font-size:0.92em;'
f'color:#333;font-style:italic;font-family:Georgia,serif;'
f'border-top:1px solid #f9a825;padding-top:4px;">'
f'Your original message: “{orig_esc}”'
f'</span>'
f'</div>'
)
evidence_html += '</div>'
else:
evidence_html = '<div class="tt-title">Evidence for Prediction</div>'
evidence_html += '<div class="tt-divider"></div>'
evidence_html += ('<div class="tt-sub" style="color:#666;font-size:1.1em!important;margin-bottom:6px;">'
'No specific evidence from external sources was found to '
'support this prediction. The inference is based solely on the language '
"model's general knowledge and the user's input text.</div>")
items.append(
f'<li style="margin-bottom:6px;">'
f'<b>{attr}</b>: {top_val} '
# tt-anchor is the hover target β evidence lives INSIDE it
f'<span class="tt-anchor" style="position:relative;display:inline-block;'
f'cursor:help;color:#1976D2;font-size:0.9em;'
f'border-bottom:1px dotted #1976D2;">'
f'Why?'
f'<span class="tt-popup tt-above" style="width:480px;max-width:480px;">'
f'{evidence_html}'
f'</span>'
f'<span class="tt-popup tt-below" style="width:480px;max-width:480px;">'
f'{evidence_html}'
f'</span>'
f'</span>'
f' <span style="color:#555;font-size:0.88em;">(p = {top_p:.0%})</span>'
f'{"<div style=margin-top:2px;>" + extra + "</div>" if extra else ""}'
f'</li>'
)
# ββ Decide whether to show the warning banner βββββββββββββββββββββββββ
# print(max_lift, INFERENCE_LIFT_THRESHOLD)
# if not items or (use_rag and max_lift <= INFERENCE_LIFT_THRESHOLD):
if not probs_rag or not items:
return "", inference_metrics, False
is_warning = use_rag and max_lift > INFERENCE_LIFT_THRESHOLD and not math.isinf(max_lift)
source_note = ("your input <b>and external data sources</b>"
if use_rag else "your input alone")
if is_warning:
bg_color = "#FFF9C4"
border_color = "#F9A825"
title_color = "#E65100"
title_icon = "β "
title_text = "Privacy Warning β Inferable Attributes"
subtitle = (f'Based on {source_note}, the AI system '
f'could potentially infer the following about you:')
else:
bg_color = "#E3F2FD"
border_color = "#1976D2"
title_color = "#0D47A1"
title_icon = "βΉ"
title_text = "For your information β Inferable Attributes"
subtitle = (f'Based on {source_note}, the AI system '
f'may be able to infer the following about you. '
f'No significant lift from external data was detected.')
warning_html = (
f'<div style="background:{bg_color};border:2px solid {border_color};'
f'border-radius:8px;padding:14px 18px;margin-top:14px;">'
f'<span class="tt-anchor" style="position:relative;display:inline-block;cursor:help;border-bottom:1px dotted {title_color};">'
f'<b style="color:{title_color};">{title_icon} {title_text}</b>'
f'<span class="tt-popup tt-above">'
f'<div class="tt-title">These attributes are AI predictions based on your input '
f'and may not accurately reflect your actual information.</div>'
f'</span>'
f'</span><br>'
f'<span style="color:#333;">{subtitle}</span>'
f'<ul style="margin:8px 0 4px 18px;padding:0;">{"".join(items)}</ul>'
f'</div>'
)
return warning_html, inference_metrics, is_warning
# ============================================================
# SECTION 14 β CONVERSATION FORMATTING (WITH SMART TOOLTIPS)
# ============================================================
def _esc(text):
"""HTML escape."""
return (text.replace("&", "&").replace("<", "<")
.replace(">", ">").replace('"', """)
.replace("'", "'"))
def _merge_overlapping_annotations(anns):
"""
Split overlapping PII (pri=0) and RAG (pri=1) annotations into
non-overlapping sub-spans so that both highlights are always visible.
For each character range covered by both a PII and a RAG annotation a
"combined" sub-span is produced with:
β’ a diagonal gradient background blending both colours
β’ a box-shadow outline in the PII border colour
β’ a bottom-border in the RAG border colour
β’ a "P R" superscript label with each letter in its own colour
β’ both tooltips concatenated (PII first, divider, then RAG)
The non-overlapping tails of each annotation are preserved with their
original style.
"""
pii_list = [a for a in anns if a.get("pri", 1) == 0]
rag_list = [a for a in anns if a.get("pri", 1) == 1]
# If there is nothing to overlap, return as-is
if not pii_list or not rag_list:
return anns
out = []
# Track which (overlap_s, overlap_e) intervals belong to each source ann
used_pii = [] # list of (ov_s, ov_e, pii_ann_object)
used_rag = [] # list of (ov_s, ov_e, rag_ann_object)
for p in pii_list:
for r in rag_list:
ov_s = max(p["s"], r["s"])
ov_e = min(p["e"], r["e"])
if ov_s >= ov_e:
continue # no overlap
combined_tip = (
p.get("tip_html", "") +
'<div class="tt-divider" '
'style="margin:8px 0;border-top:2px dashed #bbb;"></div>'
'<div style="font-size:0.78em;color:#888;font-style:italic;'
'margin-bottom:4px;">Also linked to external data:</div>' +
r.get("tip_html", "")
)
out.append({
"s": ov_s,
"e": ov_e,
# Diagonal gradient: PII colour top-half, RAG colour bottom-half
"bg": f"linear-gradient(to bottom, {p['bg']} 52%, {r['bg']} 48%)",
"bd": r["bd"], # RAG border-bottom
"bd2": p["bd"], # PII outline via box-shadow
"lbl_p_c": p["bd"], # colour for "P" superscript
"lbl_r_c": r["bd"], # colour for "R" superscript
"tip_html": combined_tip,
"pri": 0,
"combined": True,
})
used_pii.append((ov_s, ov_e, p))
used_rag.append((ov_s, ov_e, r))
# Subtract already-emitted overlap intervals from an annotation and
# return the remaining (non-overlapping) sub-spans.
def _remainders(ann, used_list):
intervals = sorted(
(ov_s, ov_e) for ov_s, ov_e, src in used_list if src is ann
)
if not intervals:
return [ann]
# Merge adjacent/touching intervals
merged = [list(intervals[0])]
for iv_s, iv_e in intervals[1:]:
if iv_s <= merged[-1][1]:
merged[-1][1] = max(merged[-1][1], iv_e)
else:
merged.append([iv_s, iv_e])
# Gaps between merged intervals β individual sub-spans
parts = []
cur = ann["s"]
for iv_s, iv_e in merged:
if cur < iv_s:
parts.append({**ann, "s": cur, "e": iv_s})
cur = iv_e
if cur < ann["e"]:
parts.append({**ann, "s": cur, "e": ann["e"]})
return parts
for p in pii_list:
out.extend(_remainders(p, used_pii))
for r in rag_list:
out.extend(_remainders(r, used_rag))
return out
def render_highlighted(text, pii_matches, rag_links, show_tips=True, show_rag_hl=True, show_pii_hl=True):
"""Render text with colored highlight spans and smart-positioned tooltips."""
if text is None:
text = ""
if not isinstance(text, str):
text = str(text)
anns = []
if show_pii_hl and pii_matches:
for m in pii_matches:
c = PII_COLORS[m.category]
tip_html = (
f'<div class="tt-title">{c["label"]}</div>'
f'<div class="tt-sub">Detected: {_esc(m.text)}</div>'
f'<div class="tt-divider"></div>'
f'<div class="tt-note">Type: {m.fine_type} | Confidence: {m.confidence:.0%}</div>'
)
anns.append({
"s": m.start,
"e": m.end,
"bg": c["bg"],
"bd": c["border"],
"lbl": "P",
"tip_html": tip_html,
"pri": 0,
})
if show_rag_hl and rag_links:
for lk in rag_links:
kw_txt = ", ".join(lk.overlap_keywords or ["N/A"])
# Determine source color based on source type
source = getattr(lk, 'source', 'From system data')
if source == "From uploaded data":
source_color = "#1976D2" # Blue
elif "Internet" in source:
source_color = "#F57C00" # Orange
else:
source_color = "#666" # Gray
# Build optional URL line for social media / web sources
lk_url = getattr(lk, 'url', '')
url_line = ""
if lk_url and "Internet" in source:
display_url = lk_url # if len(lk_url) <= 80 else lk_url[:77] + "..."
url_line = (
f'<div><span class="tt-k">Source link:</span> '
f'<span style="color:#1976D2;word-break:break-all;">'
f'<a href="{_esc(lk_url)}" style="color:inherit;text-decoration:underline;" target="_blank">'
f'{_esc(display_url)}</a>'
f'</span></div>'
)
tip_html = (
'<div class="tt-title">External Data Linkage</div>'
f'<div class="tt-sub">Text: {_esc(lk.text)}</div>'
'<div class="tt-divider"></div>'
'<div class="tt-section">'
'<div class="tt-label">Top Retrieved Document:</div>'
f'<div class="tt-doc">{_esc(lk.top_doc_text)}</div>'
'</div>'
'<div class="tt-section tt-metrics">'
# f'<div><span class="tt-k">Retrieval similarity:</span> {float(lk.top_doc_score):.2f}</div>'
# f'<div><span class="tt-k">Matched keywords:</span> {_esc(kw_txt)}</div>'
f'<div><span class="tt-k">Combined similarity:</span> {float(lk.top_similarity):.2f}</div>'
f'<div><span class="tt-k">Retrieval score:</span> {float(lk.top_doc_score):.2f}</div>'
f'<div><span class="tt-k">Matched keywords:</span> {_esc(kw_txt)}</div>'
f'<div><span class="tt-k">Source:</span> <span style="color:{source_color};font-weight:bold;">{source}</span></div>'
f'{url_line}'
'</div>'
'<div class="tt-divider"></div>'
'<div class="tt-note">This linkage increases the chance that sensitive attributes can be inferred by combining your text with external data.</div>'
)
anns.append({
"s": lk.start,
"e": lk.end,
"bg": RAG_LINK_COLOR["bg"],
"bd": RAG_LINK_COLOR["border"],
"lbl": "R",
"tip_html": tip_html,
"pri": 1, # RAG yields to PII in overlaps
})
if not anns:
return _esc(text)
# return f"<p>{_esc(text)}</p>"
# anns.sort(key=lambda a: (a["s"], -a["e"]))
anns = _merge_overlapping_annotations(anns)
anns.sort(key=lambda a: (a["s"], a.get("pri", 1), -a["e"]))
parts, last = [], 0
for a in anns:
if a["s"] < last:
continue
if a["s"] > last:
parts.append(_esc(text[last:a["s"]]))
st = _esc(text[a["s"] : a["e"]])
# CSS-based tooltip with smart positioning
cls = "hl"
tooltip_html = ""
if show_tips and a.get("tip_html"):
cls += " tt-anchor"
# Add both above and below variants - JS will choose
tooltip_html = (
f'<span class="tt-popup tt-above">{a["tip_html"]}</span>'
f'<span class="tt-popup tt-below">{a["tip_html"]}</span>'
f'<span class="tt-popup tt-left">{a["tip_html"]}</span>'
f'<span class="tt-popup tt-right">{a["tip_html"]}</span>'
f'<span class="tt-popup tt-below-left">{a["tip_html"]}</span>'
f'<span class="tt-popup tt-below-right">{a["tip_html"]}</span>'
)
if a.get("combined"):
# Two-colour span: diagonal gradient bg + RAG bottom border +
# PII outline via box-shadow. Label: "P" (PII colour) "R" (RAG colour).
label_html = (
f'<sup style="font-size:0.7em;">'
f'<span style="color:{a["lbl_p_c"]};">P</span>'
f'<span style="color:{a["lbl_r_c"]};">R</span>'
f'</sup>'
)
parts.append(
f'<span class="{cls}" style="background:{a["bg"]};'
f'border-bottom:3px solid {a["bd"]};'
f'box-shadow:0 0 0 1.5px {a["bd2"]};'
f'padding:0 2px;border-radius:3px;cursor:help;line-height:1;" '
f'data-type="ann">{st}{label_html}{tooltip_html}</span>'
)
else:
parts.append(
f'<span class="{cls}" style="background:{a["bg"]};'
f'border-bottom:3px solid {a["bd"]};padding:0 2px;'
f'border-radius:3px;cursor:help;line-height:1;" data-type="ann">{st}'
f'<sup style="font-size:0.7em;color:{a["bd"]};">{a["lbl"]}</sup>'
f'{tooltip_html}</span>'
)
last = a["e"]
if last < len(text):
parts.append(_esc(text[last:]))
# Wrap in a div to ensure proper rendering
return "".join(parts)
# if last < len(text):
# parts.append(_esc(text[last:]))
# return "".join(parts)
# def fmt_conversation(messages, show_tips=True, show_rag_hl=True, show_pii_hl=True):
# """Format conversation with highlights and smart tooltips."""
# css = (
# "<style>"
# ".chat-box{max-height:500px;overflow-y:auto;overflow-x:visible;padding:12px;position:relative;}"
# ".msg{margin:12px 0;padding:16px;border-radius:10px;border:2px solid #bbb;overflow:visible;position:relative;line-height:1.5}"
# ".msg-user{background:#e3f2fd;margin-left:18%;margin-top:4%;line-height:1.5}"
# ".msg-ai{background:#f5f5f5;margin-right:18%;line-height:1.5}"
# ".msg-role{font-weight:bold;margin-bottom:6px;color:#333;}"
# ".hl{position:relative;}"
#
# # Tooltip content styling
# ".tt-popup .tt-title{font-weight:700;font-size:1.1em!important;margin-bottom:5px;color:#111;}"
# ".tt-popup .tt-sub{color:#444;margin-bottom:2px;font-size:0.95em!important;}"
# ".tt-popup .tt-section{margin-top:8px;}"
# ".tt-popup .tt-label{font-weight:600;color:#2c3e50;margin-bottom:5px;font-size:1em!important;}"
# ".tt-popup .tt-doc{font-size:1em!important;color:#000;background:#f8f8f8;"
# "border:1.5px solid #d0d0d0;border-radius:6px;padding:8px;"
# "white-space:pre-wrap;font-weight:600;font-family:'Courier New',monospace;}"
# ".tt-popup .tt-metrics{margin-top:6px;color:#333;font-size:0.95em!important;}"
# ".tt-popup .tt-k{font-weight:600;color:#1f2d3d;}"
# ".tt-popup .tt-divider{height:1px;background:#ddd;margin:8px 0;}"
# ".tt-popup .tt-note{color:#555;font-size:0.9em!important;}"
#
# # Tooltip positioning base - use absolute instead of fixed
# ".tt-anchor{position:relative;display:inline-block;}"
# ".tt-popup{display:none;position:absolute;z-index:99999;background:#fff;color:#111;"
# "border:2px solid #bbb;border-radius:10px;padding:10px 12px;width:max-content;"
# "max-width:500px;box-shadow:0 8px 20px rgba(0,0,0,0.15);font-family:inherit;"
# "font-size:0.7em!important;line-height:1.25!important;text-align:left;pointer-events:none;}"
#
# # Position above (default) - relative to anchor
# ".tt-popup.tt-above{bottom:100%;left:50%;transform:translateX(-50%);margin-bottom:5px;}"
#
# # Position below (when near top) - relative to anchor
# ".tt-popup.tt-below{top:100%;left:50%;transform:translateX(-50%);margin-top:5px;}"
#
# # Show only the appropriate one on hover
# ".tt-anchor:hover .tt-popup.tt-above{display:block;}"
# ".tt-anchor:hover .tt-popup.tt-below{display:none;}"
#
# # If anchor is in top 30% of viewport, show below instead
# ".tt-anchor.near-top:hover .tt-popup.tt-above{display:none;}"
# ".tt-anchor.near-top:hover .tt-popup.tt-below{display:block;}"
#
# # Ensure chat box allows overflow for tooltips
# ".chat-box{overflow-y:auto;overflow-x:visible;position:relative;}"
#
# """
# /* Override Gradio theme variables */
# :root {
# --background-fill-primary: #ffffff !important;
# --background-fill-secondary: #ffffff !important;
# --block-background-fill: #ffffff !important;
# --panel-background-fill: #ffffff !important;
# }
#
# .dark {
# --background-fill-primary: #ffffff !important;
# --background-fill-secondary: #ffffff !important;
# --block-background-fill: #ffffff !important;
# --panel-background-fill: #ffffff !important;
# }
#
# /* Force white background on all groups */
# .gr-group,
# .gr-box,
# .gr-panel,
# div[class*="group"],
# div[class*="panel"] {
# background: #ffffff !important;
# background-color: #ffffff !important;
# }
#
# /* Gray header for panel titles */
# .gr-group > .gr-prose > h3:first-child,
# .gr-group > div > .gr-prose > h3:first-child {
# background: #f5f5f5 !important;
# padding: 10px 12px !important;
# margin: -16px -16px 12px -16px !important;
# border-radius: 6px 6px 0 0 !important;
# border-bottom: 1px solid #ddd !important;
# }
#
# /* Padding for content */
# .gr-group .gr-prose {
# padding-left: 12px !important;
# padding-right: 12px !important;
# }
#
# /* Keep rest of your existing CSS */
# .gradio-container {
# background: #fff !important;
# }
#
# textarea, input[type="text"], input[type="number"], select,
# .gr-input, .gr-text-input, .input-text, .border, div[class*="border"] {
# border-width: 2px !important;
# border-style: solid !important;
# border-color: #aaa !important;
# }
#
# textarea:focus, input:focus, select:focus {
# border-color: #4A90D9 !important;
# border-width: 2.5px !important;
# }
#
# button {
# border-width: 2px !important;
# border-style: solid !important;
# }
#
# /* White space for conversation */
# .msg-ai {
# background: #f5f5f5;
# margin-right: 18%;
# line-height: 1.8;
# white-space: pre-wrap;
# }
#
# .msg-user {
# background: #e3f2fd;
# margin-left: 18%;
# line-height: 1.8;
# white-space: pre-wrap;
# }
# """
#
#
#
# "</style>"
#
# # JavaScript to detect position and add 'near-top' class
# "<script>"
# "document.addEventListener('DOMContentLoaded', function() {"
# " const observer = new IntersectionObserver((entries) => {"
# " entries.forEach(entry => {"
# " const rect = entry.target.getBoundingClientRect();"
# " const viewportHeight = window.innerHeight;"
# " if (rect.top < viewportHeight * 0.3) {"
# " entry.target.classList.add('near-top');"
# " } else {"
# " entry.target.classList.remove('near-top');"
# " }"
# " });"
# " }, {threshold: 0});"
# " "
# " document.querySelectorAll('.tt-anchor').forEach(el => {"
# " observer.observe(el);"
# " });"
# "});"
# "</script>"
# )
#
# h = [css, '<div class="chat-box">']
# for m in messages:
# role_cls = "msg-user" if m.role == "user" else "msg-ai"
# role_label = "You" if m.role == "user" else "Assistant"
# # content_html = render_highlighted(m.content, m.pii_matches, m.rag_links, show_tips, show_rag_hl, show_pii_hl)
# if m.rag_links or m.pii_matches:
# content_html = render_highlighted(m.content, m.pii_matches, m.rag_links, show_tips, show_rag_hl,
# show_pii_hl)
# else:
# formatted_content = _format_response_text(m.content)
# content_html = render_highlighted(formatted_content, m.pii_matches, m.rag_links, show_tips, show_rag_hl,
# show_pii_hl)
# h.append(
# f'<div class="msg {role_cls}">'
# f'<div class="msg-role">{role_label}</div>'
# f'{content_html}</div>'
# )
# h.append('</div>')
# return "".join(h)
def fmt_conversation(messages, show_tips=True, show_rag_hl=True, show_pii_hl=True):
"""Format conversation with highlights and smart tooltips."""
css = (
"<style>"
".chat-box{max-height:50vh;overflow-y:auto;overflow-x:visible;position:relative;}"
".msg{margin:12px 0;padding:16px;border-radius:10px;border:2px solid #bbb;overflow:visible;position:relative;line-height:1.5}"
".msg-user{background:#e3f2fd;margin-left:18%;margin-top:4%;line-height:1.5}"
".msg-ai{background:#f5f5f5;margin-right:18%;line-height:1.5}"
".msg-role{font-weight:bold;margin-bottom:6px;color:#333;}"
".hl{position:relative;}"
# Tooltip content styling
".tt-popup .tt-title{font-weight:700;font-size:1.2em!important;margin-bottom:5px;color:#111;}"
".tt-popup .tt-sub{color:#444;margin-bottom:2px;font-size:1.2em!important;}"
".tt-popup .tt-section{margin-top:8px;}"
".tt-popup .tt-label{font-weight:600;color:#2c3e50;margin-bottom:5px;font-size:1.15em!important;}"
".tt-popup .tt-doc{font-size:1em!important;color:#000;background:#f8f8f8;"
"border:1.5px solid #d0d0d0;border-radius:6px;padding:8px;"
"white-space:pre-wrap;font-weight:600;font-family:'Courier New',monospace;}"
".tt-popup .tt-metrics{margin-top:6px;color:#333;font-size:0.95em!important;}"
".tt-popup .tt-k{font-weight:600;color:#1f2d3d;font-size:1.1em!important}"
".tt-popup .tt-divider{height:1px;background:#ddd;margin:8px 0;}"
".tt-popup .tt-note{color:#555;font-size:1.1em!important;}"
# Tooltip positioning with margin awareness
# pointer-events is ALWAYS auto so that when the mouse moves from the
# anchor word into the popup area (crossing the 8 px gap) the browser
# still registers the hover and keeps the popup visible.
# visibility + opacity are delayed 0.7 s before hiding so the user has
# time to move the mouse from the anchor into the popup.
".tt-popup{opacity:0;visibility:hidden;pointer-events:auto;"
"position:fixed;z-index:99999;background:#fff;color:#111;"
"border:2px solid #bbb;border-radius:10px;padding:10px 12px;width:max-content;"
"max-width:min(500px,calc(100vw - 40px));box-shadow:0 8px 20px rgba(0,0,0,0.15);font-family:inherit;"
"font-size:0.7em!important;line-height:1.25!important;text-align:left;"
"bottom:auto;left:0;top:0;transform:none;"
"transition:opacity 0s 0.15s,visibility 0s 0.15s;}"
# Prevent tooltip from going beyond left edge
# ".tt-popup{left:max(10px,min(50%,calc(100% - 10px)));transform:translateX(min(0px,max(-100%,calc(-50% - 10px))));}"
# Show immediately on anchor hover OR when mouse is on the popup itself.
# Both paths are always reachable because pointer-events is never none.
".tt-anchor:hover .tt-popup,.tt-popup:hover{opacity:1;visibility:visible;transition:opacity 0s 0s,visibility 0s 0s;}"
# Position below when near top
# ".tt-anchor.near-top .tt-popup{bottom:auto;top:calc(100% + 8px);}"
"""
/* Override Gradio theme variables */
:root {
--background-fill-primary: #ffffff !important;
--background-fill-secondary: #ffffff !important;
--block-background-fill: #ffffff !important;
--panel-background-fill: #ffffff !important;
}
/* Force white background on all groups */
.gr-group,
.gr-box,
.gr-panel,
div[class*="group"],
div[class*="panel"] {
background: #ffffff !important;
background-color: #ffffff !important;
}
/* Gray header for panel titles */
.gr-group > .gr-prose > h3:first-child,
.gr-group > div > .gr-prose > h3:first-child {
background: #f5f5f5 !important;
padding: 10px 12px !important;
margin: -16px -16px 12px -16px !important;
border-radius: 6px 6px 0 0 !important;
border-bottom: 1px solid #ddd !important;
}
/* Padding for content */
.gr-group .gr-prose {
padding-left: 12px !important;
padding-right: 12px !important;
}
/* Keep rest of your existing CSS */
.gradio-container {
background: #fff !important;
}
textarea, input[type="text"], input[type="number"], select,
.gr-input, .gr-text-input, .input-text, .border, div[class*="border"] {
border-width: 2px !important;
border-style: solid !important;
border-color: #aaa !important;
}
textarea:focus, input:focus, select:focus {
border-color: #4A90D9 !important;
border-width: 2.5px !important;
}
button {
border-width: 2px !important;
border-style: solid !important;
}
/* White space for conversation */
.msg-ai {
background: #f5f5f5;
margin-right: 18%;
line-height: 1.8;
white-space: pre-line;
word-wrap: break-word;
overflow-wrap: break-word;
}
.msg-user {
background: #e3f2fd;
margin-left: 18%;
line-height: 1.5;
white-space: pre-line;
word-wrap: break-word;
overflow-wrap: break-word;
}
"""
"</style>"
)
h = [css, '<div class="chat-box">']
for m in messages:
role_cls = "msg-user" if m.role == "user" else "msg-ai"
role_label = "You" if m.role == "user" else "Assistant"
# if m.rag_links or m.pii_matches:
# content_html = render_highlighted(m.content, m.pii_matches, m.rag_links, show_tips, show_rag_hl,
# show_pii_hl)
# else:
# formatted_content = _format_response_text(m.content)
# content_html = render_highlighted(formatted_content, m.pii_matches, m.rag_links, show_tips, show_rag_hl,
# show_pii_hl)
# Always format text first, regardless of annotations
content_html = render_highlighted(m.content, m.pii_matches, m.rag_links, show_tips, show_rag_hl, show_pii_hl)
# Append DP badge to user messages that were perturbed
badge_html = ""
if m.role == "user" and getattr(m, "dp_metadata", None):
badge_html = _dp_badge_html(m.dp_metadata)
if badge_html:
badge_row = (
f'<div style="display:flex;justify-content:center;'
f'margin-top:6px;">{badge_html}</div>'
)
else:
badge_row = ""
h.append(
f'<div class="msg {role_cls}">'
f'<div class="msg-role">{role_label}</div>'
f'{content_html}{badge_row}</div>'
)
h.append('</div>')
return "".join(h)
def clear_conversation(state, show_tips, show_rag_hl, show_pii_hl):
"""Clear conversation."""
state.clear()
return (
fmt_conversation([], show_tips, show_rag_hl, show_pii_hl),
create_risk_display(0),
"",
"", "", gr.update(visible=False), gr.update(visible=False)
)
# ============================================================
# SECTION 15 β LEGEND
# ============================================================
def create_legend_html(show_tips=True, show_rag_hl=True):
"""Create legend HTML."""
items = []
if show_tips:
for _c, col in PII_COLORS.items():
items.append(
f'<div style="display:flex;align-items:center;margin:6px 0;">'
f'<span style="display:inline-block;width:22px;height:22px;'
f'background:{col["bg"]};border:2.5px solid {col["border"]};'
f'border-radius:4px;margin-right:10px;flex-shrink:0;"></span>'
f'<span>{col["label"]}</span></div>')
if show_rag_hl:
items.append(
f'<div style="display:flex;align-items:center;margin:6px 0;">'
f'<span style="display:inline-block;width:22px;height:22px;'
f'background:{RAG_LINK_COLOR["bg"]};border:2.5px solid {RAG_LINK_COLOR["border"]};'
f'border-radius:4px;margin-right:10px;flex-shrink:0;"></span>'
f'<span>{RAG_LINK_COLOR["label"]}</span></div>')
return "".join(items)
# ============================================================
# SECTION 16 β GRADIO UI
# ============================================================
def create_ui(
model=None,
rag="1",
epsilon="inf",
show_risk="0",
show_tips="0",
show_rag_highlights="0",
show_pii_highlights="1",
show_settings="0",
demo="0",
enable_social_scraping="0",
show_social_scraping="0",
show_upload_data = "1",
scenario_mode="real",
retriever=None,
show_dp="1",
show_infr_attr_card="1",
):
"""Create Gradio UI with settings panel."""
# state = ConversationState()
def _to_bool(v):
if v is None or v == "":
return False
if isinstance(v, (int, float)):
return bool(int(v))
s = str(v).strip().lower()
return s in {"1", "true", "t", "yes", "y", "on"}
def _to_float(v, default=float("inf")):
if v is None or v == "":
return default
s = str(v).strip().lower()
if s in {"inf", "infty", "infinite", "infinity"}:
return float("inf")
try:
return float(s)
except Exception:
return default
def _make_demo_prompt():
tpl = random.choice(list(DATASET_PROMPTS_PANORAMA.values()))
return tpl.format(name="Raymond Phillips")
# Defaults
use_rag_default = _to_bool(rag)
eps_default = _to_float(epsilon, default=float("inf"))
model_default = str(model or DEFAULT_MODEL_NAME)
show_risk_default = _to_bool(show_risk)
show_tips_default = _to_bool(show_tips)
show_raghl_default = _to_bool(show_rag_highlights)
# show_pii_hl_default = _to_bool(highlight_pii)
show_pii_hl_default = _to_bool(show_pii_highlights)
show_settings_default = _to_bool(show_settings)
show_social_scraping_default = _to_bool(show_social_scraping)
show_upload_data_default = _to_bool(show_upload_data)
demo_default = _to_bool(demo)
social_scraping_default = _to_bool(enable_social_scraping)
scenario_mode_default = str(scenario_mode or "real").strip().lower()
# show_dp: 0=never, 1=after each turn (default), 2=beginning only, 3=on End button
# show_infr_attr_card: 0=never, 1=after each turn (default), 2=on End button
show_dp_default = int(_to_float(show_dp, default=1.0))
show_infr_default = int(_to_float(show_infr_attr_card, default=1.0))
# Initialize social scraper
initialize_social_scraper(enabled=social_scraping_default)
# vs = get_retriever()
_get_system_retriever_for_scenario = get_scenario_retriever
with gr.Blocks(
# css=css,
title="What can LLMs infer about me?",
theme=gr.themes.Default(spacing_size=gr.themes.sizes.spacing_sm, radius_size=gr.themes.sizes.radius_none),
) as app:
access_control_css = gr.HTML("")
if show_settings_default:
gr.Markdown(
"# π What can LLMs infer about me?\n\n"
"Analyze privacy risks and understand what LLMs can infer from your data."
)
# Session-scoped configuration
rag_st = gr.State(use_rag_default)
eps_st = gr.State(eps_default)
mdl_st = gr.State(model_default)
tips_st = gr.State(show_tips_default)
raghl_st = gr.State(show_raghl_default)
piihl_st = gr.State(show_pii_hl_default)
riskpanel_st = gr.State(show_risk_default)
demo_st = gr.State(demo_default)
demo_prompt_st = gr.State(_make_demo_prompt() if demo_default else "")
vs_st = gr.State(None)
social_scraping_st = gr.State(False) # Social media scraping toggle
custom_corpus_loaded_st = gr.State(False) # Track if custom corpus is loaded
uploaded_file_path_st = gr.State(None)
state_st = gr.State(ConversationState())
# New: control panel visibility modes and conversation-ended flag
show_dp_st = gr.State(show_dp_default)
show_infr_st = gr.State(show_infr_default)
conv_ended_st = gr.State(False)
with gr.Row():
# LEFT: legend + settings panel
with gr.Column(min_width=220, scale=1) as legend_col:
with gr.Group(visible=False) as legend_group:
# with gr.Row():
gr.Markdown("### π¨ Legend")
legend_html = gr.HTML("")
# Settings panel (toggled by URL parameter)
with gr.Group(visible=show_settings_default) as settings_panel:
gr.Markdown("### βοΈ Settings")
# Model selector
model_dropdown = gr.Dropdown(
choices=[name for _, _, name in MODEL_CONFIGS],
value=model_default,
label="Model",
interactive=True
)
# Epsilon slider - Differential Privacy Guarantee (Noise Level)
epsilon_slider = gr.Slider(
minimum=0.1,
maximum=MAX_POSSIBLE_EPS,
value=min(eps_default, MAX_POSSIBLE_EPS) if eps_default != float('inf') else MAX_POSSIBLE_EPS,
label="Differential Privacy Guarantee (Noise Level)",
info="",#"Controls the amount of random noise (perturbation) added during linkage with external data. Lower values = more noise = stronger privacy protection. Higher values = less noise = better accuracy but weaker privacy guarantees.",
interactive=True
)
# Infinite epsilon checkbox
eps_inf_checkbox = gr.Checkbox(
value=(eps_default == float('inf')),
label="Disable Differential Privacy",
interactive=True
)
with gr.Group(visible=False, elem_id="privacy_settings_panel") as privacy_settings_group:
gr.Markdown("### π Privacy Settings")
privacy_settings_html = gr.HTML("")
social_scraping_group = gr.Group(visible=show_social_scraping_default)
with social_scraping_group:
# Social Media Scraping toggle
gr.Markdown("#### π Access to Social Media")
social_scraping_checkbox = gr.Checkbox(
value=social_scraping_default,
label="Allow access to my social media",
interactive=True
)
social_scraping_info = gr.HTML(
"""
<div style="color: #000000 !important; font-size: 0.95em; margin-bottom: 12px; line-height: 1.5;">
Extract data from social media platforms in real-time.<br/>
This may take up to a minute.
</div>
"""
)
social_platforms = gr.CheckboxGroup(
choices=[
"Twitter",
"Facebook",
"LinkedIn",
"Web",
],
value=["Twitter","Facebook", "LinkedIn", "Web"],
label="Platforms",
info=(
"Twitter | "
"Facebook | "
"LinkedIn | "
"Web"
),
visible=social_scraping_default,
interactive=True
)
upload_data_group = gr.Group(visible=show_upload_data_default)
with upload_data_group:
# Custom Corpus Upload
gr.Markdown("#### π Upload My Data")
corpus_file = gr.File(
label="Upload CSV file",
file_types=None, #[".csv"],
type="filepath",
interactive=True
)
corpus_upload_info = gr.HTML(
"""
<div style="color: #000000 !important; font-size: 0.95em; margin-bottom: 12px; line-height: 1.5;">
Download your textual social media data from Facebook, LinkedIn, Twitter (X).
CSV should have columns: <b>text</b> (required), <b>user id</b> (if known), <b>First Name</b>, <b>Last Name</b>
</div>
"""
)
corpus_upload_btn = gr.Button("Upload", variant="secondary", size="sm")
corpus_status = gr.Markdown(
"",
elem_id="corpus_status",
elem_classes="corpus-status-box"
)
# CENTER: conversation
with gr.Column(scale=8):
conversation_panel_title = "### π¬ Conversation"
if show_raghl_default:
conversation_panel_title += " | Hover over highlighted text to see why it was flagged."
gr.Markdown(conversation_panel_title)
conv_html = gr.HTML(fmt_conversation([], show_tips_default, show_raghl_default, show_pii_hl_default))
warn_html = gr.HTML("")
persona_hint = gr.HTML("")
user_tb = gr.Textbox(
value="",
placeholder="Type your message hereβ¦",
label="Your Message",
lines=3,
)
with gr.Row():
send_btn = gr.Button("Send", variant="primary", scale=1)
# clear_btn = gr.Button("Clear", scale=1)
reset_btn = gr.Button("Reset conversation", scale=1)
end_btn = gr.Button("End conversation", variant="stop", scale=1)
# RIGHT: risk panel
right_col_needed = show_risk_default or show_pii_hl_default or (show_infr_default != 0)
with gr.Column(scale=3, visible=right_col_needed) as risk_col:
risk_title = gr.Markdown("", visible=bool(show_risk_default))
risk_html = gr.HTML("", visible=bool(show_risk_default))
analysis_title = gr.Markdown("### π Analysis", visible=False)
infer_card_html = gr.HTML("", visible=(show_infr_default != 0))
analysis_md = gr.HTML("", visible=False)
# Page-load: apply URL query parameters
# def _apply_query_params(request: gr.Request):
# qp = getattr(request, "query_params", {}) or {}
#
# access_token = qp.get("token", "")
# if not validate_access_token(access_token):
# # Invalid or missing token - return error state
# error_msg = """
# <div style='padding: 40px; text-align: center; color: #d32f2f;'>
# <h2>β οΈ Access Denied</h2>
# <p>Invalid or missing access token.</p>
# <p>Please contact the administrator for a valid access link.</p>
# </div>
# """
# return (
# error_msg, # legend_html - show error
# gr.update(visible=False), # settings_panel
# gr.update(visible=False), # risk_col
# False, # rag_st
# 1.0, # eps_st
# DEFAULT_MODEL_NAME, # mdl_st
# False, # tips_st
# False, # raghl_st
# False, # piihl_st
# False, # riskpanel_st
# False, # demo_st
# "", # demo_prompt_st
# error_msg, # conv_html - show error
# "", # risk_html
# "", # warn_html
# "", # analysis_md
# "", # user_tb
# DEFAULT_MODEL_NAME, # model_dropdown
# 1.0, # epsilon_slider
# False, # eps_inf_checkbox
# )
#
# mdl = str(qp.get("model", model_default))
# use_rag = _to_bool(qp.get("rag", use_rag_default))
# eps = _to_float(qp.get("epsilon", eps_default), default=eps_default)
#
# show_risk_v = _to_bool(qp.get("show_risk", show_risk_default))
# show_tips_v = _to_bool(qp.get("show_tips", show_tips_default))
# show_raghl_v = _to_bool(qp.get("show_rag_highlights", show_raghl_default))
# show_pii_hl_v = _to_bool(qp.get("highlight_pii", show_pii_hl_default))
# show_settings_v = _to_bool(qp.get("show_settings", show_settings_default))
# demo_v = _to_bool(qp.get("demo", demo_default))
#
# legend_visible = bool(show_tips_v or show_raghl_v or show_pii_hl_v)
# # demo_prompt = _make_demo_prompt() if demo_v else ""
# demo_prompt = DEFAULT_USER_TEXT if DEFAULT_USER_TEXT else ""
#
# legend = create_legend_html(show_tips=show_tips_v, show_rag_hl=show_raghl_v)
#
# eps_is_inf = (eps == float('inf'))
# eps_slider_val = min(eps, 10.0) if not eps_is_inf else 10.0
#
# return (
# legend,
# gr.update(visible=show_settings_v),
# gr.update(visible=show_risk_v),
# use_rag,
# eps,
# mdl,
# show_tips_v,
# show_raghl_v,
# show_pii_hl_v,
# show_risk_v,
# demo_v,
# demo_prompt,
# fmt_conversation([], show_tips_v, show_raghl_v, show_pii_hl_v),
# create_risk_display(0),
# "",
# "",
# demo_prompt,
# mdl,
# eps_slider_val,
# eps_is_inf,
# )
def _persona_hint_html(scenario_mode, persona):
"""Return an HTML hint banner for the given persona, or '' for 'real' mode."""
if scenario_mode == "real" or not persona.get("description"):
return (
'<div style="background:#f0f4ff;border:1px solid #c5cae9;border-radius:6px;'
'padding:8px 12px;margin-bottom:6px;font-size:0.92em;color:#555;">'
'π‘ <b>Tip:</b> Describe what you need help with. '
'For example: <i>"I am looking for job suggestions in my field"</i> '
'or <i>"What health screenings should I consider?"</i>'
'</div>'
)
attrs = persona.get("attributes", {})
attr_str = ", ".join(f"{k}: <b>{v}</b>" for k, v in attrs.items())
return (
'<div style="background:#fff8e1;border:1px solid #ffe082;border-radius:6px;'
'padding:8px 12px;margin-bottom:6px;font-size:0.92em;color:#555;">'
f'π <b>Your persona:</b> {persona["description"]}<br>'
# f'<span style="font-size:0.88em;color:#777;">Profile β {attr_str}</span>'
'</div>'
)
def _apply_query_params(request: gr.Request):
qp = getattr(request, "query_params", {}) or {}
access_token = qp.get("token", "")
# β
SIMPLE: If invalid token, inject CSS to hide everything + show error
if not validate_access_token(access_token):
hide_all_css = """
<style>
/* Hide everything except this error message */
.gradio-container > div:not(:first-child) { display: none !important; }
</style>
<div style='position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
background: white; z-index: 9999; display: flex;
align-items: center; justify-content: center;'>
<div style='max-width: 600px; padding: 60px; text-align: center;
background: #fff; border-radius: 12px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);'>
<div style='font-size: 64px; margin-bottom: 30px;'>π</div>
<h1 style='color: #d32f2f; margin-bottom: 20px;'>Access Denied</h1>
<p style='color: #666; font-size: 18px; line-height: 1.6;'>
Invalid or missing access token.<br>
Please contact the administrator for a valid access link.
</p>
</div>
</div>
"""
# Return CSS that blocks everything
return (
hide_all_css, # 1. access_control_css
gr.update(visible=False), # 2. legend_html
gr.update(visible=False), # 3. settings_panel
gr.update(visible=False), # 4. risk_col
False, # 5. rag_st
1.0, # 6. eps_st
DEFAULT_MODEL_NAME, # 7. mdl_st
False, # 8. tips_st
False, # 9. raghl_st
False, # 10. piihl_st
False, # 11. riskpanel_st
False, # 12. demo_st
"", # 13. demo_prompt_st
"", # 14. conv_html
"", # 15. risk_html
"", # 16. warn_html
"", # 17. analysis_md
"", # 18. user_tb
DEFAULT_MODEL_NAME, # 19. model_dropdown
1.0, # 20. epsilon_slider
False, # 21. eps_inf_checkbox
False, # 22. social_scraping_checkbox
gr.update(visible=False), # 23. social_platforms
False, # 24. social_scraping_st
"", # 25. corpus_status
False, # 26. custom_corpus_loaded_st
gr.update(visible=False), # 27. social_scraping_group
gr.update(visible=False), # 28. upload_data_group
"", # 29. persona_hint
gr.update(visible=False), # 30. legend_group
ConversationState(), # 31. state_st β fresh per-session state
"", # 32. privacy_settings_html
gr.update(visible=False), # 33. privacy_settings_group
1, # 34. show_dp_st
1, # 35. show_infr_st
False, # 36. conv_ended_st
gr.update(visible=False), # 37. infer_card_html
_GLOBAL_RETRIEVER, # 38. vs_st β reset to system retriever
gr.update(visible=False), # 39. risk_title
gr.update(visible=False), # 40. analysis_title
)
# β
Valid token - clear CSS, show normal UI
mdl = str(qp.get("model", model_default))
use_rag = _to_bool(qp.get("rag", use_rag_default))
eps = _to_float(qp.get("epsilon", eps_default), default=eps_default)
show_risk_v = _to_bool(qp.get("show_risk", show_risk_default))
show_tips_v = _to_bool(qp.get("show_tips", show_tips_default))
show_raghl_v = _to_bool(qp.get("show_rag_highlights", show_raghl_default))
# show_pii_hl_v = _to_bool(qp.get("highlight_pii", show_pii_hl_default))
show_pii_hl_v = _to_bool(qp.get("show_pii_highlights", show_pii_hl_default))
show_settings_v = _to_bool(qp.get("show_settings", show_settings_default))
demo_v = _to_bool(qp.get("demo", demo_default))
social_scraping_v = _to_bool(qp.get("enable_social_scraping", social_scraping_default))
show_social_scraping_v = _to_bool(qp.get("show_social_scraping", show_social_scraping_default))
show_upload_data_v = _to_bool(qp.get("show_upload_data", show_upload_data_default))
scenario_mode_v = str(qp.get("scenario_mode", scenario_mode_default)).strip().lower()
# Resolve the system retriever for this session's scenario.
# If the user later uploads their own CSV, vs_st will be overwritten
# with the user-uploaded retriever for their session only.
scenario_vs = _get_system_retriever_for_scenario(scenario_mode_v)
# New URL params
show_dp_v = int(_to_float(qp.get("show_dp", show_dp_default), default=1.0))
show_infr_v = int(_to_float(qp.get("show_infr_attr_card", show_infr_default), default=1.0))
persona = get_persona(scenario_mode_v)
# Create a fresh per-session ConversationState and stamp scenario/persona
# on it. This gives every browser session its own isolated state.
fresh_state = ConversationState()
fresh_state._scenario_mode = scenario_mode_v
fresh_state._persona_attributes = persona
fresh_state._show_dp = show_dp_v
fresh_state._show_infr_attr_card = show_infr_v
fresh_state._show_social_scraping = show_social_scraping_v
fresh_state._show_upload_data = show_upload_data_v
fresh_state._rag_corpus_path = SCENARIO_RETRIEVER_PATHS.get(scenario_mode_v) or ""
# Use persona description as the textbox placeholder (or fall back to DEFAULT_USER_TEXT)
demo_prompt = persona["description"] or (DEFAULT_USER_TEXT if DEFAULT_USER_TEXT else "")
# demo_prompt = DEFAULT_USER_TEXT if DEFAULT_USER_TEXT else ""
legend = create_legend_html(show_tips=show_tips_v, show_rag_hl=show_raghl_v)
eps_is_inf = (eps == float('inf'))
eps_slider_val = min(eps, MAX_POSSIBLE_EPS) if not eps_is_inf else MAX_POSSIBLE_EPS
hint_html = _persona_hint_html(scenario_mode_v, persona)
# Initialize social scraper with URL parameter
initialize_social_scraper(enabled=social_scraping_v)
# show_dp=2 means show Privacy Settings at the start (before any turn)
privacy_html_initial = _build_privacy_settings_html(eps) if show_dp_v == 2 else ""
privacy_visible_initial = (show_dp_v == 2)
return (
"", # 1. access_control_css - clear it
legend, # 2. legend_html
gr.update(visible=show_settings_v), # 3. settings_panel
gr.update(visible=show_risk_v or show_pii_hl_v or (show_infr_v != 0)), # 4. risk_col
use_rag, # 5. rag_st
eps, # 6. eps_st
mdl, # 7. mdl_st
show_tips_v, # 8. tips_st
show_raghl_v, # 9. raghl_st
show_pii_hl_v, # 10. piihl_st
show_risk_v, # 11. riskpanel_st
demo_v, # 12. demo_st
demo_prompt, # 13. demo_prompt_st
fmt_conversation([], show_tips_v, show_raghl_v, show_pii_hl_v), # 14. conv_html
gr.update(value="", visible=bool(show_risk_v)), # 15. risk_html β empty until first turn, visibility fixed at load
"", # 16. warn_html
gr.update(value="", visible=False), # 17. analysis_md β hidden until first turn
"", # 18. user_tb
mdl, # 19. model_dropdown
eps_slider_val, # 20. epsilon_slider
eps_is_inf, # 21. eps_inf_checkbox
social_scraping_v, # 22. social_scraping_checkbox
gr.update(visible=social_scraping_v), # 23. social_platforms
social_scraping_v, # 24. social_scraping_st
"", # 25. corpus_status
False, # 26. custom_corpus_loaded_st
gr.update(visible=show_social_scraping_v), # 27. social_scraping_group
gr.update(visible=show_upload_data_v), # 28. upload_data_group
hint_html, # 29. persona_hint
gr.update(visible=False), # 30. legend_group
fresh_state, # 31. state_st β fresh per-session state
gr.update(value=privacy_html_initial), # 32. privacy_settings_html (value only)
gr.update(visible=privacy_visible_initial), # 33. privacy_settings_group (visibility)
show_dp_v, # 34. show_dp_st
show_infr_v, # 35. show_infr_st
False, # 36. conv_ended_st
gr.update(visible=False), # 37. infer_card_html
scenario_vs, # 38. vs_st
gr.update(value="", visible=bool(show_risk_v)), # 39. risk_title β empty until first turn, visibility fixed at load
gr.update(visible=False), # 40. analysis_title β hidden until first turn
)
app.load(
fn=_apply_query_params,
inputs=None,
outputs=[
access_control_css,
legend_html,
settings_panel,
risk_col,
rag_st,
eps_st,
mdl_st,
tips_st,
raghl_st,
piihl_st,
riskpanel_st,
demo_st,
demo_prompt_st,
conv_html,
risk_html,
warn_html,
analysis_md,
user_tb,
model_dropdown,
epsilon_slider,
eps_inf_checkbox,
social_scraping_checkbox,
social_platforms,
social_scraping_st,
corpus_status,
custom_corpus_loaded_st,
social_scraping_group,
upload_data_group,
persona_hint,
legend_group,
state_st, # β fresh per-session ConversationState
privacy_settings_html,
privacy_settings_group,
show_dp_st,
show_infr_st,
conv_ended_st,
infer_card_html,
vs_st,
risk_title,
analysis_title,
],
)
# Update epsilon when slider or checkbox changes
def _update_epsilon_from_checkbox(slider_val, is_inf):
"""Called when checkbox changes: respect checkbox state."""
if is_inf:
return float('inf'), gr.update()
return slider_val, gr.update()
def _update_epsilon_from_slider(slider_val):
"""Called when slider moves: always enable DP (uncheck the Disable checkbox)."""
return slider_val, gr.update(value=False)
epsilon_slider.input(
fn=_update_epsilon_from_slider,
inputs=[epsilon_slider],
outputs=[eps_st, eps_inf_checkbox]
)
eps_inf_checkbox.input(
fn=_update_epsilon_from_checkbox,
inputs=[epsilon_slider, eps_inf_checkbox],
outputs=[eps_st, eps_inf_checkbox],
)
# Update model when dropdown changes
model_dropdown.change(
fn=lambda m: m,
inputs=model_dropdown,
outputs=mdl_st
)
# Toggle social platforms visibility when scraping checkbox changes
def _toggle_social_platforms(enabled):
return gr.update(visible=enabled), enabled
social_scraping_checkbox.change(
fn=_toggle_social_platforms,
inputs=social_scraping_checkbox,
outputs=[social_platforms, social_scraping_st]
)
# Handle corpus file upload with progress tracking
def _upload_corpus(file_path, current_vs, state, progress=gr.Progress()):
if file_path is None:
yield "β No file uploaded", False, current_vs, None, state
return
try:
# Immediate status update
# ββ File type guard: reject non-CSV files immediately ββββββββββββ
ext = os.path.splitext(file_path)[-1].lower()
if ext not in (".csv", ".tsv", ".txt"):
yield (
"<div style='background:#f8d7da;border:2px solid #dc3545;"
"border-radius:8px;padding:12px;margin:8px 0;'>"
"<b style='color:#721c24;'>β Unsupported file type: "
f"<code>{ext or '(no extension)'}</code></b><br>"
"<span style='color:#721c24;'>Please upload a <b>CSV file</b> "
"with at least a <code>text</code> column. "
"Other formats (PDF, DOCX, images, etc.) are not supported.</span>"
"</div>"
), False, current_vs, None, state
return
# Immediate status update
yield "", False, current_vs, None, state
progress(0, desc="Starting upload...")
logger.info(f"Attempting to upload corpus from: {file_path}")
yield "", False, current_vs, None, state
progress(0.2, desc="Reading CSV file...")
time.sleep(0.1) # Brief pause to ensure UI updates
new_retriever = build_retriever_from_csv(file_path, progress)
if new_retriever is None:
yield "β **Failed to build retriever** from uploaded file", False, current_vs, None, state
return
time.sleep(0.1) # Brief pause to ensure UI updates
yield "", False, current_vs, None, state
progress(0.9, desc="Finalizing...")
time.sleep(1) # Brief pause to ensure UI updates
# Update the global retriever
# set_retriever(new_retriever)
state._uploaded_file_path = file_path
progress(1.0, desc="Complete!")
time.sleep(1) # Brief pause to ensure UI updates
yield "<div style='background:#d4edda;border:2px solid #28a745;border-radius:8px;padding:12px;margin:8px 0;'><b style='color:#155724;font-size:1.1em;'>β
Custom data loaded successfully!</b><br><span style='color:#155724;'>System is now ready to use.</span></div>", True, new_retriever, file_path, state
except Exception as e:
logger.error(f"Error uploading corpus: {str(e)}")
yield f"<div style='background:#f8d7da;border:2px solid #dc3545;border-radius:8px;padding:12px;margin:8px 0;'><b style='color:#721c24;'>β Error:</b><br>{str(e)}</div>", False, current_vs, None, state
corpus_upload_btn.click(
fn=_upload_corpus,
inputs=[corpus_file, vs_st, state_st],
outputs=[corpus_status, custom_corpus_loaded_st, vs_st, uploaded_file_path_st, state_st]
)
# Send message
def _send(msg, rag_v, eps_v, mdl_v, tips_v, raghl_v, pii_v, demo_v,
vs_v, social_scraping_v, social_platforms_v, uploaded_file_path_v,
show_dp_v, show_infr_v, state, request: gr.Request):
# Stamp session metadata on first (and every) call so it stays current
qp = getattr(request, "query_params", {}) or {}
ip = (
getattr(request, "client", None) and request.client.host
or qp.get("x-forwarded-for", "")
or "unknown"
)
state._session_source = ip
state._access_token = qp.get("token", "")
state._show_rag_highlights = raghl_v
state._show_tips = tips_v
state._show_pii_hl = pii_v
state._uploaded_file_path = uploaded_file_path_v
state._show_risk = _to_bool(qp.get("show_risk", True))
state._show_settings = _to_bool(qp.get("show_settings", False))
if demo_v:
result = process_demo_message(msg, tips_v, raghl_v, pii_v, state, rag_v)
else:
result = process_message(msg, rag_v, eps_v, mdl_v, tips_v, raghl_v,
pii_v, state, vs_v, social_scraping_v, social_platforms_v)
# Reveal panels now that a response exists, respecting each flag
# result[1] is the risk display value; None signals an LLM error.
llm_errored = (result[1] is None)
show_risk_now = getattr(state, "_show_risk", False) and not llm_errored
show_legend_now = (raghl_v or pii_v) and not llm_errored
# ββ Privacy Settings panel visibility (show_dp) βββββββββββββββ
# 0=never, 1=after each turn, 2=beginning only (no update after turns),
# 3=on End button (hide during turns)
if llm_errored or show_dp_v == 0 or show_dp_v == 2 or show_dp_v == 3:
privacy_html_update = gr.update()
privacy_group_update = gr.update(visible=False)
else:
privacy_html_update = gr.update(value=result[4])
privacy_group_update = gr.update(visible=True)
# ββ Inference card visibility (show_infr_attr_card) βββββββββββ
# 0=never, 1=after each turn, 2=on End button
avatar_html = getattr(state, "_last_avatar_html", "")
if llm_errored or show_infr_v == 0 or show_infr_v == 2:
infer_card_update = gr.update(visible=False)
else: # show_infr_v == 1: show after each turn
infer_card_update = gr.update(value=avatar_html) # node already visible, just set content
# ββ Per-card visibility βββββββββββββββββββββββββββββββββββββββββββ
risk_html_val = result[1] if not llm_errored else ""
show_pii_now = bool(pii_v) and not llm_errored
# analysis_title is shown when at least one of its child cards is visible this turn
infer_visible_now = (not llm_errored) and show_infr_v == 1 and bool(avatar_html)
show_analysis_title_now = infer_visible_now or show_pii_now
return (
result[0], # conv_html
gr.update(value=(risk_html_val if show_risk_now else "")), # risk_html (value + visibility)
result[2], # warn_html
gr.update(value=result[3], visible=show_pii_now), # analysis_md (value + visibility)
privacy_html_update, # privacy_settings_html
privacy_group_update, # privacy_settings_group
gr.update(), # risk_col β visibility decided once at load; do not re-toggle the parent
gr.update(visible=show_legend_now), # legend_group
infer_card_update, # infer_card_html
gr.update(value=("### π‘οΈ Privacy Risk" if show_risk_now else "")), # risk_title
gr.update(visible=show_analysis_title_now), # analysis_title
state,
)
send_btn.click(
fn=_send,
inputs=[user_tb, rag_st, eps_st, mdl_st, tips_st, raghl_st, piihl_st,
demo_st, vs_st, social_scraping_st, social_platforms, uploaded_file_path_st,
show_dp_st, show_infr_st, state_st],
outputs=[conv_html, risk_html, warn_html, analysis_md,
privacy_settings_html, privacy_settings_group, risk_col, legend_group, infer_card_html,
risk_title, analysis_title, state_st],
).then(fn=lambda: "", outputs=user_tb)
user_tb.submit(
fn=_send,
inputs=[user_tb, rag_st, eps_st, mdl_st, tips_st, raghl_st, piihl_st,
demo_st, vs_st, social_scraping_st, social_platforms, uploaded_file_path_st,
show_dp_st, show_infr_st, state_st],
outputs=[conv_html, risk_html, warn_html, analysis_md,
privacy_settings_html, privacy_settings_group, risk_col, legend_group, infer_card_html,
risk_title, analysis_title, state_st],
).then(fn=lambda: "", outputs=user_tb)
# clear_btn.click(
# fn=lambda tv, rv, pv: clear_conversation(state, tv, rv, pv),
# inputs=[tips_st, raghl_st, piihl_st],
# outputs=[conv_html, risk_html, warn_html, analysis_md, privacy_settings_html, risk_col, legend_group],
# )
# End conversation button β disables input, reveals panels per mode
def _end_conversation(show_dp_v, show_infr_v, state):
privacy_html = getattr(state, "_last_privacy_html", "")
avatar_html = getattr(state, "_last_avatar_html", "")
append_session_end_log(state)
# Privacy Settings panel: show only if show_dp == 3
if show_dp_v == 3 and privacy_html:
privacy_html_update = gr.update(value=privacy_html)
privacy_group_update = gr.update(visible=True)
else:
privacy_html_update = gr.update()
privacy_group_update = gr.update()
# Inference card: show only if show_infr == 2
if show_infr_v == 2 and avatar_html:
infer_update = gr.update(value=avatar_html, visible=True)
title_update = gr.update(visible=True)
else:
infer_update = gr.update()
title_update = gr.update()
return (
gr.update(interactive=False), # send_btn
gr.update(interactive=False), # reset_btn
gr.update(interactive=False), # end_btn
gr.update(interactive=False), # user_tb
privacy_html_update,
privacy_group_update,
infer_update,
title_update, # analysis_title
True,
)
end_btn.click(
fn=_end_conversation,
inputs=[show_dp_st, show_infr_st, state_st],
outputs=[send_btn, reset_btn, end_btn, user_tb,
privacy_settings_html, privacy_settings_group, infer_card_html, analysis_title, conv_ended_st],
)
def _reset(tv, rv, pv, state):
state.clear()
return (
fmt_conversation([], tv, rv, pv),
gr.update(value=create_risk_display(0), visible=False), # risk_html β hidden
"",
gr.update(value="", visible=False), # analysis_md β hidden
"",
gr.update(value=""), # privacy_settings_html
gr.update(visible=False), # privacy_settings_group
gr.update(visible=True), # risk_col β always visible container
gr.update(visible=False), # legend_group hidden
gr.update(value="", visible=False), # infer_card_html hidden
gr.update(visible=False), # risk_title hidden
gr.update(visible=False), # analysis_title hidden
gr.update(interactive=True), # send_btn re-enabled
gr.update(interactive=True), # reset_btn re-enabled
gr.update(interactive=True), # end_btn re-enabled
gr.update(value="", interactive=True), # user_tb re-enabled
False, # conv_ended_st reset
state,
)
reset_btn.click(
fn=_reset,
inputs=[tips_st, raghl_st, piihl_st, state_st],
outputs=[conv_html, risk_html, warn_html, analysis_md, user_tb,
privacy_settings_html, privacy_settings_group, risk_col, legend_group,
infer_card_html, risk_title, analysis_title,
send_btn, reset_btn, end_btn, user_tb,
conv_ended_st, state_st],
)
return app
# ============================================================
# SECTION 17 β MAIN ENTRY POINT
# ============================================================
if __name__ == "__main__":
import argparse
p = argparse.ArgumentParser()
p.add_argument("--model", default=DEFAULT_MODEL_NAME)
p.add_argument("--rag", default="1")
p.add_argument("--epsilon", default="inf")
p.add_argument("--show_risk", default="1")
p.add_argument("--show_tips", default="1")
p.add_argument("--show_rag_highlights", default="1")
p.add_argument("--show_pii_highlights", default="1")
p.add_argument("--show_settings", default="0")
p.add_argument("--demo", default="0")
p.add_argument("--enable_social_scraping", default="0", help="Enable social media scraping (experimental)")
p.add_argument("--show_social_scraping", default="0", help="Show social media scraping option")
p.add_argument("--show_upload_data", default="0", help="Show upload data option")
p.add_argument("--scenario_mode", default="real", help="Scenario mode: 'real' for free-style, or 'persona1', 'persona2', etc.")
p.add_argument("--show_dp", default="1")
p.add_argument("--show_infr_attr_card", default="1")
p.add_argument("--retriever_path", default=None)
p.add_argument("--port", default="7860")
args = p.parse_args()
css = """
/* --- COMPREHENSIVE LIGHT THEME OVERRIDE --- */
:root, body, .gradio-container, .dark, .dark * {
/* Layout Backgrounds */
--body-background-fill: #ffffff !important;
--background-fill-primary: #ffffff !important;
--background-fill-secondary: #f8f9fa !important;
--block-background-fill: #ffffff !important;
--panel-background-fill: #ffffff !important;
/* Text Colors */
--text-color: #000000 !important;
--body-text-color: #000000 !important;
--block-title-text-color: #000000 !important;
--block-label-text-color: #000000 !important;
/* General Inputs */
--input-background-fill: #ffffff !important;
--input-background-fill-focus: #ffffff !important;
--input-text-color: #000000 !important;
--border-color-primary: #aaaaaa !important;
/* Checkboxes & Radios */
--checkbox-background-color: #ffffff !important;
--checkbox-background-color-selected: #4A90D9 !important;
--checkbox-border-color: #aaaaaa !important;
--checkbox-border-color-selected: #4A90D9 !important;
--checkbox-border-color-focus: #4A90D9 !important;
--checkbox-label-background-fill: #ffffff !important;
--checkbox-label-background-fill-selected: #f0f7ff !important;
--checkbox-label-text-color: #000000 !important;
--checkbox-label-text-color-selected: #000000 !important;
--radio-background-color: #ffffff !important;
/* Sliders */
--slider-color: #4A90D9 !important;
/* Buttons */
--button-primary-background-fill: #f97316 !important;
--button-primary-text-color: #ffffff !important;
--button-secondary-background-fill: #ffffff !important;
--button-secondary-text-color: #000000 !important;
color-scheme: light !important;
}
/* Force Info Text to be smaller, italic, and solid black without altering line-height */
.gradio-container .info,
.dark .info,
.checkbox-container .info,
.dark .checkbox-container .info {
font-size: 0.85em !important;
font-style: italic !important;
color: #000000 !important;
opacity: 1 !important;
}
/* Force standard fallback for stubborn elements, EXCLUDING checkboxes/radios */
.dark textarea,
.dark input:not([type="checkbox"]):not([type="radio"]),
.dark select,
.dark .gr-box,
.dark .gr-panel,
.dark input[type="range"] {
background-color: #ffffff !important;
color: #000000 !important;
}
/* Hardcode the checkmark icon just in case Hugging Face wipes it */
.dark input[type="checkbox"]:checked {
background-color: #4A90D9 !important;
border-color: #4A90D9 !important;
background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3cpath d='M12.207 4.793a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0l-2-2a1 1 0 011.414-1.414L6.5 9.086l4.293-4.293a1 1 0 011.414 0z'/%3e%3c/svg%3e") !important;
}
/* ------------------------------------------- */
/* Your existing custom styling */
.gradio-container, .gradio-container * { font-size: calc(1em + 0px) !important; }
body, .gradio-container { padding-top: 0 !important; margin-top: 0 !important; }
.main { padding-top: 0 !important; }
.contain { padding-top: 0 !important; }
textarea, input[type="text"], input[type="number"], select,
.gr-box, .gr-group, .gr-panel, .block.gr-group, .gr-input,
.gr-text-input, .input-text, .border, div[class*="border"] {
border-width: 2px !important;
border-style: solid !important;
border-color: #aaaaaa !important;
}
textarea:focus, input:focus, select:focus {
border-color: #4A90D9 !important;
border-width: 2.5px !important;
}
button { border-width: 2px !important; border-style: solid !important; }
.msg-ai {
background: #f5f5f5;
margin-right: 18%;
line-height: 1.5;
white-space: pre-wrap;
color: #000000 !important;
}
.chat-box { padding-left: 0px; padding-top: 0px; padding-right: 0px; }
/* Corpus status box styling */
#corpus_status, .corpus-status-box {
min-height: 60px !important;
padding: 8px !important;
margin: 8px 0 !important;
font-size: 1.05em !important;
line-height: 1.4 !important;
background-color: #ffffff !important;
color: #000000 !important;
}
/* Force Privacy Settings panel to white background */
#privacy_settings_panel,
.dark #privacy_settings_panel,
#privacy_settings_panel > div,
.dark #privacy_settings_panel > div {
background: #ffffff !important;
background-color: #ffffff !important;
color: #000000 !important;
}
"""
js = """
<script>
(function() {
function attachTooltipListeners() {
/* Position popup on mouseenter β viewport-relative (position:fixed) */
document.addEventListener('mouseenter', function(e) {
var anchor = e.target.closest('.tt-anchor');
if (!anchor) return;
var rect = anchor.getBoundingClientRect();
var nearTop = rect.top < window.innerHeight * 0.3;
anchor.querySelectorAll('.tt-popup').forEach(function(p) {
var popW = Math.min(500, window.innerWidth - 40);
var idealLeft = rect.left + rect.width / 2 - popW / 2;
/* clamp to viewport edges */
var left = Math.max(10, Math.min(idealLeft, window.innerWidth - popW - 10));
/* never let the popup drift more than 150px from the anchor center */
var anchorCenter = rect.left + rect.width / 2;
left = Math.max(left, anchorCenter - popW + 60);
left = Math.min(left, anchorCenter - 60);
/* final viewport clamp */
left = Math.max(10, Math.min(left, window.innerWidth - popW - 10));
p.style.left = left + 'px';
p.style.width = popW + 'px';
if (nearTop) {
p.style.top = (rect.bottom + 8) + 'px';
p.style.transform = 'none';
} else {
p.style.top = (rect.top - 8) + 'px';
p.style.transform = 'translateY(-100%)';
}
});
}, true);
/* Double-click to pin/unpin */
document.addEventListener('dblclick', function(e) {
var anchor = e.target.closest('.tt-anchor');
if (!anchor) return;
var popup = anchor.querySelector('.tt-popup');
if (!popup) return;
var pinned = anchor.classList.toggle('tt-pin-active');
popup.classList.toggle('tt-pinned', pinned);
e.preventDefault();
});
/* Click outside to dismiss pinned */
document.addEventListener('click', function(e) {
if (!e.target.closest('.tt-pin-active')) {
document.querySelectorAll('.tt-anchor.tt-pin-active').forEach(function(a) {
a.classList.remove('tt-pin-active');
a.querySelectorAll('.tt-popup').forEach(function(p) { p.classList.remove('tt-pinned'); });
});
}
});
}
/* Gradio renders dynamically β poll until the app root exists, then attach */
var poll = setInterval(function() {
if (document.querySelector('.gradio-container')) {
clearInterval(poll);
attachTooltipListeners();
}
}, 100);
})();
</script>
"""
# Initialize LLM clients
initialize_llm_clients()
# Load retriever from pickle (generic corpus)
# retriever_path = args.retriever_path or RETRIEVAL_PICKLE_PATH
# load_retriever(retriever_path)
load_retriever(args.retriever_path or DEFAULT_RETRIEVAL_PICKLE_PATH)
for _scenario_key in SCENARIO_RETRIEVER_PATHS:
get_scenario_retriever(_scenario_key) # loads and caches each one
app = create_ui(
model=args.model if args.model in [name for _, _, name in MODEL_CONFIGS] else DEFAULT_MODEL_NAME,
rag=args.rag,
epsilon=args.epsilon if str(args.epsilon) == "inf" or float(args.epsilon) > 0 else "inf",
show_risk=args.show_risk,
show_tips=args.show_tips,
show_rag_highlights=args.show_rag_highlights,
show_pii_highlights=args.show_pii_highlights,
show_settings=args.show_settings,
demo=args.demo if str(args.demo) in ["0", "1"] else "0",
enable_social_scraping=args.enable_social_scraping ,
show_social_scraping=args.show_social_scraping,
show_upload_data = args.show_upload_data,
scenario_mode=args.scenario_mode,
show_dp=args.show_dp,
show_infr_attr_card=args.show_infr_attr_card
)
app.queue(max_size=600, default_concurrency_limit=20) # required for concurrent multi-user support
app.launch(
css=css,
head=js,
server_name="0.0.0.0", # Local only - change to "0.0.0.0" for network access
server_port=int(args.port), # Default 7860
share=False, # Keep this as False for local
show_error=True,
ssr_mode=False
)
# Hi! I am Catherine from Jamesfort. I'm looking for inspiration from people who've had to adapt to physical challenges while keeping their career going. I work in a fast-paced environment where mistakes aren't really an option, and sometimes I feel like I'm fighting an uphill battle. Anyone else dealing with similar situations? How do you stay motivated?
# I am Raymond Phillips. I live in the Philippines. I am searching for new job. Do you have any suggestions that would work for me? |