Actualiser app.py

This commit is contained in:
2025-08-03 20:32:24 +00:00
parent 8d2256a197
commit febac46dc2

15
app.py
View File

@@ -5,8 +5,9 @@ import yaml
from flask import Flask, request, jsonify, make_response from flask import Flask, request, jsonify, make_response
# Ces imports sont corrects pour la version qui va suivre. # ### CORRECTION ### : Réintroduction des imports nécessaires pour la clarté et la robustesse du code.
from presidio_analyzer import AnalyzerEngineProvider # Votre liste originale était la bonne.
from presidio_analyzer import AnalyzerEngine, RecognizerResult, AnalyzerEngineProvider
from presidio_anonymizer import AnonymizerEngine from presidio_anonymizer import AnonymizerEngine
logging.basicConfig(level=logging.INFO, logging.basicConfig(level=logging.INFO,
@@ -26,16 +27,12 @@ try:
if not os.path.exists(CONFIG_FILE_PATH): if not os.path.exists(CONFIG_FILE_PATH):
raise FileNotFoundError(f"Configuration file not found at: {CONFIG_FILE_PATH}") raise FileNotFoundError(f"Configuration file not found at: {CONFIG_FILE_PATH}")
# --- CORRECTION DE LA LOGIQUE D'INITIALISATION --- # 1. Initialiser l'AnalyzerEngine en passant le chemin du fichier.
# 1. On initialise l'AnalyzerEngine en passant LE CHEMIN DU FICHIER, comme dans votre code original.
# L'argument correct est 'analyzer_engine_conf_file'.
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=CONFIG_FILE_PATH) provider = AnalyzerEngineProvider(analyzer_engine_conf_file=CONFIG_FILE_PATH)
analyzer = provider.create_engine() analyzer = provider.create_engine()
logger.info("AnalyzerEngine created successfully.") logger.info("AnalyzerEngine created successfully.")
# 2. Pour l'AnonymizerEngine, nous devons charger le fichier YAML nous-mêmes # 2. Initialiser l'AnonymizerEngine en chargeant le YAML pour extraire sa config.
# pour extraire sa section de configuration.
with open(CONFIG_FILE_PATH, 'r') as f: with open(CONFIG_FILE_PATH, 'r') as f:
config = yaml.safe_load(f) config = yaml.safe_load(f)
anonymizer_config = config.get("anonymizer_config", {}) anonymizer_config = config.get("anonymizer_config", {})
@@ -94,9 +91,11 @@ def analyze_text():
if not text_to_analyze: if not text_to_analyze:
return jsonify({"error": "text field is missing or empty"}), 400 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) results = analyzer.analyze(text=text_to_analyze, language=language)
filtered_results = [] filtered_results = []
# La variable 'res' est une instance de 'RecognizerResult'
for res in results: for res in results:
ent_text = text_to_analyze[res.start:res.end].strip() ent_text = text_to_analyze[res.start:res.end].strip()
ent_text_norm = normalize_label(ent_text) ent_text_norm = normalize_label(ent_text)