File size: 2,265 Bytes
bda6294
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""
Utilitaires de conformite RGPD / GDPR compliance utilities (Space edition).
"""
import re
import json
from datetime import datetime

import config

_EMAIL_RE = re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")
_PHONE_RE = re.compile(r"(\+?\d[\d\s().-]{7,}\d)")


def anonymize(text: str) -> str:
    if not text:
        return text
    text = _EMAIL_RE.sub("[email masque]", text)
    text = _PHONE_RE.sub("[telephone masque]", text)
    return text


def safe_for_processing(text: str) -> str:
    return anonymize(text) if config.ANONYMIZE_INPUT else text


def log_interaction(question, answer, sources):
    if not config.ENABLE_LOGGING:
        return
    entry = {
        "ts": datetime.utcnow().isoformat(),
        "question": anonymize(question),
        "answer": anonymize(answer),
        "sources": [s.get("url") for s in sources],
    }
    log_file = config.LOG_DIR / f"log_{datetime.utcnow():%Y%m%d}.jsonl"
    with open(log_file, "a", encoding="utf-8") as f:
        f.write(json.dumps(entry, ensure_ascii=False) + "\n")


def purge_all_logs():
    n = 0
    for f in config.LOG_DIR.glob("log_*.jsonl"):
        f.unlink()
        n += 1
    return n


NOTICE_FR = (
    "**Confidentialite (RGPD).** Assistant reserve repondant a partir du "
    "contenu du site Vizyon Ayiti 360, y compris des documents internes selon "
    "vos droits. Vos questions et documents sont traites pour generer la "
    "reponse et ne sont pas conserves. Ne partagez ces reponses qu'avec les "
    "personnes autorisees."
)
NOTICE_EN = (
    "**Privacy (GDPR).** Restricted assistant answering from Vizyon Ayiti 360 "
    "website content, including internal documents according to your rights. "
    "Your questions and documents are processed to generate the answer and are "
    "not stored. Share these answers only with authorized people."
)
NOTICE_HT = (
    "**Konfidansyalite (RGPD).** Asistan rezeve k ap reponn ak kontni sit "
    "Vizyon Ayiti 360, ki gen ladan dokiman enten selon dwa ou. Kesyon ak "
    "dokiman ou yo trete pou bay repons lan epi yo pa konseve. Pataje repons "
    "sa yo selman ak moun ki otorize."
)


def notice(lang="fr"):
    return {"en": NOTICE_EN, "ht": NOTICE_HT}.get(lang, NOTICE_FR)