From febac46dc2db852c312b84cf7765bae5de1c6547 Mon Sep 17 00:00:00 2001 From: nacim Date: Sun, 3 Aug 2025 20:32:24 +0000 Subject: [PATCH] Actualiser app.py --- app.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 548dd01..031a28a 100644 --- a/app.py +++ b/app.py @@ -5,8 +5,9 @@ import yaml from flask import Flask, request, jsonify, make_response -# Ces imports sont corrects pour la version qui va suivre. -from presidio_analyzer import AnalyzerEngineProvider +# ### CORRECTION ### : Réintroduction des imports nécessaires pour la clarté et la robustesse du code. +# Votre liste originale était la bonne. +from presidio_analyzer import AnalyzerEngine, RecognizerResult, AnalyzerEngineProvider from presidio_anonymizer import AnonymizerEngine logging.basicConfig(level=logging.INFO, @@ -26,16 +27,12 @@ try: if not os.path.exists(CONFIG_FILE_PATH): raise FileNotFoundError(f"Configuration file not found at: {CONFIG_FILE_PATH}") - # --- CORRECTION DE LA LOGIQUE D'INITIALISATION --- - - # 1. On initialise l'AnalyzerEngine en passant LE CHEMIN DU FICHIER, comme dans votre code original. - # L'argument correct est 'analyzer_engine_conf_file'. + # 1. Initialiser l'AnalyzerEngine en passant le chemin du fichier. provider = AnalyzerEngineProvider(analyzer_engine_conf_file=CONFIG_FILE_PATH) analyzer = provider.create_engine() logger.info("AnalyzerEngine created successfully.") - # 2. Pour l'AnonymizerEngine, nous devons charger le fichier YAML nous-mêmes - # pour extraire sa section de configuration. + # 2. Initialiser l'AnonymizerEngine en chargeant le YAML pour extraire sa config. with open(CONFIG_FILE_PATH, 'r') as f: config = yaml.safe_load(f) anonymizer_config = config.get("anonymizer_config", {}) @@ -94,9 +91,11 @@ def analyze_text(): if not text_to_analyze: return jsonify({"error": "text field is missing or empty"}), 400 + # La variable 'results' est une liste d'objets 'RecognizerResult' results = analyzer.analyze(text=text_to_analyze, language=language) filtered_results = [] + # La variable 'res' est une instance de 'RecognizerResult' for res in results: ent_text = text_to_analyze[res.start:res.end].strip() ent_text_norm = normalize_label(ent_text)