Spaces:
Sleeping
Sleeping
Update src/module/AIP.py
Browse files- src/module/AIP.py +43 -32
src/module/AIP.py
CHANGED
|
@@ -12,15 +12,10 @@ import sys
|
|
| 12 |
|
| 13 |
# --- 1. IMPORT SÉCURISÉ DES FONCTIONS ---
|
| 14 |
try:
|
| 15 |
-
from
|
| 16 |
-
|
| 17 |
-
audit_data_quality,
|
| 18 |
-
get_stock_alerts,
|
| 19 |
-
get_patient_stats
|
| 20 |
-
)
|
| 21 |
except ImportError as e:
|
| 22 |
-
st.error(f"Erreur interne : impossible de trouver
|
| 23 |
-
calculate_occupancy_rate = audit_data_quality = get_stock_alerts = get_patient_stats = lambda x: "Fonction non chargée"
|
| 24 |
|
| 25 |
# --- STYLE EXTRAIT DU CODE 01 (GOTHAM THEME) ---
|
| 26 |
def apply_custom_styles():
|
|
@@ -59,33 +54,46 @@ def apply_custom_styles():
|
|
| 59 |
</style>
|
| 60 |
""", unsafe_allow_html=True)
|
| 61 |
|
| 62 |
-
class JasmineAgent:
|
| 63 |
-
def __init__(self, rdf_store, ontology_rules, dataframes=None):
|
| 64 |
-
self.google_key = os.environ.get("GOOGLE_API_KEY") or st.secrets.get("GOOGLE_API_KEY")
|
| 65 |
-
if self.google_key:
|
| 66 |
-
genai.configure(api_key=self.google_key)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
self.rdf_store = rdf_store
|
| 72 |
self.ontology_rules = ontology_rules
|
| 73 |
self.dataframes = dataframes or {}
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
"
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
"calculate_occupancy_rate": calculate_occupancy_rate,
|
| 84 |
-
"audit_data_quality": audit_data_quality,
|
| 85 |
-
"get_stock_alerts": get_stock_alerts,
|
| 86 |
-
"get_patient_stats": get_patient_stats
|
| 87 |
}
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
def build_base_system_prompt(self):
|
| 90 |
return """
|
| 91 |
ROLE: Tu es Jasmine, un agent intelligent d'analyse hospitalière.
|
|
@@ -145,10 +153,13 @@ FORMAT JSON STRICT :
|
|
| 145 |
tool = action.get("tool")
|
| 146 |
|
| 147 |
func_res = None
|
|
|
|
| 148 |
if tool and tool != "none":
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
| 152 |
except Exception as e:
|
| 153 |
last_error = str(e)
|
| 154 |
continue
|
|
|
|
| 12 |
|
| 13 |
# --- 1. IMPORT SÉCURISÉ DES FONCTIONS ---
|
| 14 |
try:
|
| 15 |
+
from module import Validation_Contract
|
| 16 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
except ImportError as e:
|
| 18 |
+
st.error(f"Erreur interne : impossible de trouver Validation_Contract. {e}")
|
|
|
|
| 19 |
|
| 20 |
# --- STYLE EXTRAIT DU CODE 01 (GOTHAM THEME) ---
|
| 21 |
def apply_custom_styles():
|
|
|
|
| 54 |
</style>
|
| 55 |
""", unsafe_allow_html=True)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
class JasmineAgent:
|
| 59 |
+
def __init__(self, rdf_store, ontology_rules, dataframes=None, gspread_client=None):
|
|
|
|
| 60 |
self.rdf_store = rdf_store
|
| 61 |
self.ontology_rules = ontology_rules
|
| 62 |
self.dataframes = dataframes or {}
|
| 63 |
+
self.gspread_client = gspread_client
|
| 64 |
+
|
| 65 |
+
# Liste des modules complets que l'agent peut exécuter
|
| 66 |
+
self.available_modules = {
|
| 67 |
+
"validation_contract": {
|
| 68 |
+
"class": Validation_Contract.ETL_Engine,
|
| 69 |
+
"start_method": "run_etl",
|
| 70 |
+
"args": {"gspread_client": self.gspread_client, "sheet_name": "bdd_mvp_faker"}
|
| 71 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
|
| 74 |
+
|
| 75 |
+
def execute_module(self, module_name):
|
| 76 |
+
"""
|
| 77 |
+
Lance un module complet et retourne les résultats.
|
| 78 |
+
"""
|
| 79 |
+
if module_name not in self.available_modules:
|
| 80 |
+
return {"error": f"Module {module_name} inconnu"}
|
| 81 |
+
|
| 82 |
+
mod_info = self.available_modules[module_name]
|
| 83 |
+
try:
|
| 84 |
+
# Instancier le module
|
| 85 |
+
instance = mod_info["class"](**mod_info.get("args", {}))
|
| 86 |
+
# Lancer la méthode de démarrage
|
| 87 |
+
result, explorer = getattr(instance, mod_info["start_method"])()
|
| 88 |
+
# Stocker DataExplorer dans session_state
|
| 89 |
+
st.session_state.data_explorer = explorer
|
| 90 |
+
return {"success": True, "result": {"run_status": result, "data_explorer": explorer.get_summary()}}
|
| 91 |
+
except Exception as e:
|
| 92 |
+
import traceback
|
| 93 |
+
return {"error": str(e), "trace": traceback.format_exc()}
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
def build_base_system_prompt(self):
|
| 98 |
return """
|
| 99 |
ROLE: Tu es Jasmine, un agent intelligent d'analyse hospitalière.
|
|
|
|
| 153 |
tool = action.get("tool")
|
| 154 |
|
| 155 |
func_res = None
|
| 156 |
+
tool = action.get("tool")
|
| 157 |
if tool and tool != "none":
|
| 158 |
+
if tool in self.available_functions:
|
| 159 |
+
func_res = self.execute_function(tool, action.get("args", {}))
|
| 160 |
+
elif tool in self.available_modules:
|
| 161 |
+
func_res = self.execute_module(tool)
|
| 162 |
+
|
| 163 |
except Exception as e:
|
| 164 |
last_error = str(e)
|
| 165 |
continue
|