Actualiser app.py
This commit is contained in:
72
app.py
72
app.py
@@ -5,9 +5,8 @@ from flask import Flask, request, jsonify, make_response
|
||||
|
||||
from presidio_analyzer import AnalyzerEngineProvider
|
||||
|
||||
# Config du logging
|
||||
logging.basicConfig(level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
app = Flask(__name__)
|
||||
@@ -25,11 +24,11 @@ except Exception as e:
|
||||
analyzer = None
|
||||
|
||||
|
||||
# Regex IBAN strict : 2 lettres, 2 chiffres, puis 4 groupes de 4 alphanum (minimum),
|
||||
# avec optional whitespace between groups
|
||||
IBAN_REGEX = re.compile(r"\b[A-Z]{2}[0-9]{2}(?:\s?[A-Z0-9]{4}){4,7}\b", re.IGNORECASE)
|
||||
# Regex pour recadrage strict
|
||||
IBAN_REGEX = re.compile(r"\b[A-Z]{2}[0-9]{2}(?:\s[A-Z0-9]{4}){4,7}\b", re.IGNORECASE)
|
||||
IPV4_REGEX = re.compile(r"\b(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}\b")
|
||||
|
||||
# Labels / titres à exclure de l'anonymisation (en minuscules pour normalisation)
|
||||
# Liste des labels/phrases à exclure de l'anonymisation (en minuscules)
|
||||
IGNORE_LABELS = {
|
||||
"témoins",
|
||||
"témoins clés",
|
||||
@@ -41,15 +40,13 @@ IGNORE_LABELS = {
|
||||
"montrent",
|
||||
"montrent des",
|
||||
"montrent des irrégularités",
|
||||
"bénéficiaire"
|
||||
# ajouter d'autres labels ici si besoin
|
||||
"bénéficiaire",
|
||||
}
|
||||
|
||||
def normalize_label(txt):
|
||||
return txt.strip().lower()
|
||||
def normalize_label(text: str) -> str:
|
||||
return text.strip().lower()
|
||||
|
||||
|
||||
@app.route('/analyze', methods=['POST'])
|
||||
@app.route("/analyze", methods=["POST"])
|
||||
def analyze_text():
|
||||
if not analyzer:
|
||||
return jsonify({"error": "Analyzer engine is not available. Check startup logs."}), 500
|
||||
@@ -62,47 +59,60 @@ def analyze_text():
|
||||
if not text_to_analyze:
|
||||
return jsonify({"error": "text field is missing or empty"}), 400
|
||||
|
||||
results = analyzer.analyze(
|
||||
text=text_to_analyze,
|
||||
language=language
|
||||
)
|
||||
results = analyzer.analyze(text=text_to_analyze, language=language)
|
||||
|
||||
filtered_results = []
|
||||
for res in results:
|
||||
ent_text = text_to_analyze[res.start:res.end].strip()
|
||||
ent_text_norm = normalize_label(ent_text)
|
||||
|
||||
# 1. Ignorer les entités correspondant exactement aux labels/titres à préserver
|
||||
# Skip anonymization for labels to keep
|
||||
if ent_text_norm in IGNORE_LABELS:
|
||||
logger.debug(f"Skip anonymizing label: '{ent_text}'")
|
||||
logger.debug(f"Skipping anonymization of label: '{ent_text}'")
|
||||
continue
|
||||
|
||||
# 2. Si entité de type IBAN, resserrer la sélection strictement au format IBAN
|
||||
# Recadrage stricte IBAN
|
||||
if res.entity_type == "IBAN":
|
||||
match = IBAN_REGEX.search(ent_text)
|
||||
if match:
|
||||
true_iban = match.group(0)
|
||||
# Recalcule start/end dans le texte original
|
||||
start_offset = ent_text.find(true_iban)
|
||||
if start_offset != -1:
|
||||
old_start = res.start
|
||||
old_end = res.end
|
||||
old_start, old_end = res.start, res.end
|
||||
res.start += start_offset
|
||||
res.end = res.start + len(true_iban)
|
||||
ent_text = true_iban
|
||||
logger.debug(f"Correct IBAN span from ({old_start}-{old_end}) to ({res.start}-{res.end}): '{ent_text}'")
|
||||
logger.debug(f"Adjusted IBAN span from ({old_start}-{old_end}) to ({res.start}-{res.end}): '{ent_text}'")
|
||||
else:
|
||||
# Aucun start trouvé (peu probable), garder tel quel
|
||||
logger.warning(f"Could not find exact IBAN substring in entity text: '{ent_text}'")
|
||||
logger.warning(f"Cannot find exact IBAN substring inside entity: '{ent_text}'")
|
||||
else:
|
||||
# Pas de correspondance valide au regex IBAN, on peut choisir d'ignorer ou garder tel quel
|
||||
logger.warning(f"Entity IBAN does not match IBAN regex: '{ent_text}'")
|
||||
# Pas un IBAN valide, ignorer cette entité
|
||||
logger.warning(f"Entity IBAN does not match strict IBAN regex: '{ent_text}'")
|
||||
continue
|
||||
|
||||
# Recadrage stricte IP_ADDRESS (IPv4 uniquement ici)
|
||||
if res.entity_type == "IP_ADDRESS":
|
||||
match = IPV4_REGEX.search(ent_text)
|
||||
if match:
|
||||
true_ip = match.group(0)
|
||||
start_offset = ent_text.find(true_ip)
|
||||
if start_offset != -1:
|
||||
old_start, old_end = res.start, res.end
|
||||
res.start += start_offset
|
||||
res.end = res.start + len(true_ip)
|
||||
ent_text = true_ip
|
||||
logger.debug(f"Adjusted IP_ADDRESS span from ({old_start}-{old_end}) to ({res.start}-{res.end}): '{ent_text}'")
|
||||
else:
|
||||
logger.warning(f"Cannot find exact IP substring inside entity: '{ent_text}'")
|
||||
else:
|
||||
logger.warning(f"Entity IP_ADDRESS does not match IPv4 regex: '{ent_text}'")
|
||||
continue
|
||||
|
||||
filtered_results.append(res)
|
||||
|
||||
response_data = [res.to_dict() for res in filtered_results]
|
||||
# Optionnel: ici tu peux aussi filtrer les chevauchements si tu veux
|
||||
|
||||
# **Important :** retourne uniquement les entités à anonymiser (sans labels exclus)
|
||||
response_data = [res.to_dict() for res in filtered_results]
|
||||
return make_response(jsonify(response_data), 200)
|
||||
|
||||
except Exception as e:
|
||||
@@ -112,5 +122,5 @@ def analyze_text():
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5001)
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5001)
|
||||
|
||||
Reference in New Issue
Block a user