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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
# Configuration du mot de passe admin (à définir dans les secrets de l'application)
|
| 6 |
+
ADMIN_PASSWORD = os.environ.get("ADMIN_PASSWORD", "default_admin_password")
|
| 7 |
+
|
| 8 |
+
def load_referentials_from_json():
|
| 9 |
+
"""
|
| 10 |
+
Charge les référentiels depuis les fichiers JSON dans le dossier referentiels/
|
| 11 |
+
"""
|
| 12 |
+
# Structure améliorée des référentiels (pour compatibilité)
|
| 13 |
+
referentials = {
|
| 14 |
+
"BRCGS": {
|
| 15 |
+
"Education_and_Experience": {
|
| 16 |
+
"requirements": [
|
| 17 |
+
"The auditor shall have a degree in a food-related, bioscience, or science and engineering discipline.",
|
| 18 |
+
"The auditor shall have a minimum of 5 years post-qualification experience related to the food industry.",
|
| 19 |
+
"This experience shall include roles such as quality assurance, food safety, technical management, or risk management functions within manufacturing, retailing, or inspection/enforcement."
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
"Qualifications": {
|
| 23 |
+
"Lead_Auditor": [
|
| 24 |
+
"The auditor shall have completed a recognized lead auditor qualification, including training on quality management systems with a minimum of 40 hours.",
|
| 25 |
+
"Examples of recognized courses include IRCA registered courses, ASQ Certified Quality Auditor, or BRCGS Lead Auditor courses."
|
| 26 |
+
],
|
| 27 |
+
"HACCP": [
|
| 28 |
+
"The auditor shall have completed a HACCP training course of at least 2 days (16 hours) based on Codex Alimentarius principles."
|
| 29 |
+
],
|
| 30 |
+
"Global_Standard_Food_Safety": [
|
| 31 |
+
"Auditors shall have successfully completed a Global Standard Food Safety (Issue 9) training course with corresponding examinations."
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
"Auditor_Training": {
|
| 35 |
+
"Training_Program": [
|
| 36 |
+
"Certification bodies must develop tailored training programs for each auditor depending on their background.",
|
| 37 |
+
"Training shall include product safety, HACCP, prerequisite programs, and relevant laws and regulations.",
|
| 38 |
+
"Assessment of knowledge and skills for each category is required before certification."
|
| 39 |
+
],
|
| 40 |
+
"Exceptions": [
|
| 41 |
+
"Certification bodies may employ auditors who do not fully meet the specified criteria, provided there is documented justification approved by BRCGS."
|
| 42 |
+
]
|
| 43 |
+
}
|
| 44 |
+
},
|
| 45 |
+
"FSSC_22000": {
|
| 46 |
+
"Initial_Training_and_Experience": {
|
| 47 |
+
"Work_Experience": [
|
| 48 |
+
"The auditor shall have at least 2 years full-time experience in the food or associated industry.",
|
| 49 |
+
"Consultancy experience may be recognized for up to 6 months, provided it is equivalent to work experience."
|
| 50 |
+
],
|
| 51 |
+
"Education": [
|
| 52 |
+
"A degree in a food-related or bioscience discipline, or completion of a higher education course in a related field."
|
| 53 |
+
],
|
| 54 |
+
"Training": [
|
| 55 |
+
"Successful completion of a Lead Auditor Course for FSMS or QMS (minimum 40 hours).",
|
| 56 |
+
"Completed HACCP training of at least 16 hours."
|
| 57 |
+
]
|
| 58 |
+
},
|
| 59 |
+
"Audits": {
|
| 60 |
+
"Audit_Requirements": [
|
| 61 |
+
"Auditors must have completed at least 10 audit days consisting of 5 third-party certification audits covering FSMS, HACCP, and PRP requirements.",
|
| 62 |
+
"At least 2 audits must be performed under supervision of an FSSC 22000 qualified auditor."
|
| 63 |
+
],
|
| 64 |
+
"Special_Categories": {
|
| 65 |
+
"Packaging": [
|
| 66 |
+
"Auditors specializing in packaging must have a primary qualification in packaging technology.",
|
| 67 |
+
"Training must cover topics like packaging legislation, standards, and safety control."
|
| 68 |
+
]
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
},
|
| 72 |
+
"IFS": {
|
| 73 |
+
"General_Requirements": {
|
| 74 |
+
"Education": [
|
| 75 |
+
"A food-related or bioscience degree (minimum Bachelor's degree or equivalent) or a successfully completed food-related professional higher education."
|
| 76 |
+
],
|
| 77 |
+
"Work_Experience": [
|
| 78 |
+
"A minimum of 3 years full-time professional experience in the food industry, including quality assurance, food safety, R&D, or food safety inspection/enforcement.",
|
| 79 |
+
"Consultancy experience may be recognized for up to 1 year, provided it is documented."
|
| 80 |
+
]
|
| 81 |
+
},
|
| 82 |
+
"Qualifications": {
|
| 83 |
+
"Lead_Auditor": [
|
| 84 |
+
"Completed a recognized Lead Auditor course (e.g., IFS, IRCA) with a duration of at least 40 hours.",
|
| 85 |
+
"Completed a Food Hygiene and HACCP course with a duration of at least 2 days/16 hours."
|
| 86 |
+
],
|
| 87 |
+
"Audit_Experience": [
|
| 88 |
+
"Performed a minimum of 7 food safety audits (GFSI recognized food safety certification audits and/or second party audits) in the past 5 years.",
|
| 89 |
+
"For candidates with no audit experience, participation in 7 audits is required, including 2 shadowing audits and 5 supervised audits."
|
| 90 |
+
]
|
| 91 |
+
},
|
| 92 |
+
"Advanced_Requirements": {
|
| 93 |
+
"Product_Scope": [
|
| 94 |
+
"One year of professional experience in the food industry in relation to food processing activities for each applied product scope.",
|
| 95 |
+
"At least 5 audits per scope, including GFSI recognized or second party audits."
|
| 96 |
+
],
|
| 97 |
+
"Technology_Scope": [
|
| 98 |
+
"One year of professional experience in the food industry for each applied technology scope.",
|
| 99 |
+
"At least 5 audits per scope, including GFSI recognized or second party audits."
|
| 100 |
+
],
|
| 101 |
+
"Language": [
|
| 102 |
+
"Proof of fluency in audit language via CEFR B2 certificate, 2 years work experience, or 10 audits conducted in that language."
|
| 103 |
+
]
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
referentials_dir = "referentiels"
|
| 109 |
+
if os.path.exists(referentials_dir):
|
| 110 |
+
for filename in os.listdir(referentials_dir):
|
| 111 |
+
if filename.endswith(".json") and filename != "template.json":
|
| 112 |
+
filepath = os.path.join(referentials_dir, filename)
|
| 113 |
+
try:
|
| 114 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 115 |
+
data = json.load(f)
|
| 116 |
+
referential_name = data.get("metadata", {}).get("name", filename.replace(".json", ""))
|
| 117 |
+
referentials[referential_name] = data
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Erreur lors du chargement de {filename}: {str(e)}")
|
| 120 |
+
|
| 121 |
+
return referentials
|
| 122 |
+
|
| 123 |
+
# Charger les référentiels
|
| 124 |
+
REFERENTIALS = load_referentials_from_json()
|
| 125 |
+
|
| 126 |
+
def create_referential_with_ai(exigences_text, groq_client):
|
| 127 |
+
"""
|
| 128 |
+
Utilise l'IA pour créer un référentiel à partir de texte brut
|
| 129 |
+
"""
|
| 130 |
+
prompt = f"""
|
| 131 |
+
Structure ces exigences de référentiel en JSON formaté avec pondérations et références :
|
| 132 |
+
|
| 133 |
+
TEXTES À STRUCTURER:
|
| 134 |
+
{exigences_text}
|
| 135 |
+
|
| 136 |
+
FORMAT DEMANDÉ:
|
| 137 |
+
{{
|
| 138 |
+
"metadata": {{
|
| 139 |
+
"name": "Nom à déterminer",
|
| 140 |
+
"version": "1.0",
|
| 141 |
+
"description": "Description du référentiel",
|
| 142 |
+
"last_updated": "2025-07-28",
|
| 143 |
+
"source": "Source des exigences"
|
| 144 |
+
}},
|
| 145 |
+
"categories": {{
|
| 146 |
+
"Category_Name": {{
|
| 147 |
+
"weight": 0.3,
|
| 148 |
+
"description": "Description de la catégorie",
|
| 149 |
+
"subcategories": {{
|
| 150 |
+
"Subcategory_Name": {{
|
| 151 |
+
"weight": 0.5,
|
| 152 |
+
"requirements": [
|
| 153 |
+
{{
|
| 154 |
+
"id": "REF-001",
|
| 155 |
+
"text": "Texte de l'exigence",
|
| 156 |
+
"critical": true,
|
| 157 |
+
"minimum_acceptable": "Critère minimum acceptable",
|
| 158 |
+
"references": ["Section du standard"]
|
| 159 |
+
}}
|
| 160 |
+
]
|
| 161 |
+
}}
|
| 162 |
+
}}
|
| 163 |
+
}}
|
| 164 |
+
}}
|
| 165 |
+
}}
|
| 166 |
+
|
| 167 |
+
CONSIGNES:
|
| 168 |
+
- Identifie les catégories principales et sous-catégories logiques
|
| 169 |
+
- Attribue des pondérations pertinentes (total = 1.0)
|
| 170 |
+
- Numérote les exigences de manière logique
|
| 171 |
+
- Propose un nom de référentiel pertinent
|
| 172 |
+
- Maintiens le sens exact des exigences
|
| 173 |
+
- Ajoute des références aux sections du standard si identifiables
|
| 174 |
+
"""
|
| 175 |
+
|
| 176 |
+
try:
|
| 177 |
+
response = groq_client.chat.completions.create(
|
| 178 |
+
messages=[
|
| 179 |
+
{"role": "system", "content": "Vous êtes un expert en structuration de référentiels de conformité."},
|
| 180 |
+
{"role": "user", "content": prompt}
|
| 181 |
+
],
|
| 182 |
+
model="llama-3.1-8b-instant",
|
| 183 |
+
max_tokens=4000,
|
| 184 |
+
temperature=0.1
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# Extraire le JSON de la réponse
|
| 188 |
+
content = response.choices[0].message.content
|
| 189 |
+
# Trouver le JSON dans la réponse (entre ```json et ```)
|
| 190 |
+
import re
|
| 191 |
+
json_match = re.search(r'```json\s*(\{.*?\})\s*```', content, re.DOTALL)
|
| 192 |
+
if json_match:
|
| 193 |
+
return json.loads(json_match.group(1))
|
| 194 |
+
else:
|
| 195 |
+
# Essayer de parser directement si c'est du JSON pur
|
| 196 |
+
return json.loads(content)
|
| 197 |
+
except Exception as e:
|
| 198 |
+
st.error(f"Erreur lors de la génération du référentiel : {str(e)}")
|
| 199 |
+
return None
|
| 200 |
+
|
| 201 |
+
def save_referential_to_json(referential_data, filename):
|
| 202 |
+
"""
|
| 203 |
+
Sauvegarde un référentiel au format JSON
|
| 204 |
+
"""
|
| 205 |
+
try:
|
| 206 |
+
filepath = f"referentiels/{filename}.json"
|
| 207 |
+
with open(filepath, 'w', encoding='utf-8') as f:
|
| 208 |
+
json.dump(referential_data, f, ensure_ascii=False, indent=2)
|
| 209 |
+
return True
|
| 210 |
+
except Exception as e:
|
| 211 |
+
st.error(f"Erreur lors de la sauvegarde : {str(e)}")
|
| 212 |
+
return False
|
| 213 |
+
|
| 214 |
+
def is_admin_authenticated(password):
|
| 215 |
+
"""
|
| 216 |
+
Vérifie si le mot de passe admin est correct
|
| 217 |
+
"""
|
| 218 |
+
return password == ADMIN_PASSWORD
|
| 219 |
+
|
| 220 |
+
# Template pour nouveaux référentiels
|
| 221 |
+
TEMPLATE_NOUVEAU_REFERENTIEL = {
|
| 222 |
+
"NOM_DU_REFERENTIEL": {
|
| 223 |
+
"General_Requirements": {
|
| 224 |
+
"requirements": [
|
| 225 |
+
"Exigence 1",
|
| 226 |
+
"Exigence 2"
|
| 227 |
+
]
|
| 228 |
+
},
|
| 229 |
+
"Qualifications": {
|
| 230 |
+
"required_certifications": [
|
| 231 |
+
"Certification 1",
|
| 232 |
+
"Certification 2"
|
| 233 |
+
]
|
| 234 |
+
},
|
| 235 |
+
"Audit_Experience": {
|
| 236 |
+
"minimum_experience": "Description de l'expérience requise"
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
}
|