Spaces:
Sleeping
Sleeping
Commit ·
dbe2dd0
1
Parent(s): 325b47c
added project id of evidently ai
Browse files- app.py +5 -5
- monitoring/evidently_monitor.py +39 -1
app.py
CHANGED
|
@@ -75,20 +75,20 @@ async def training_route():
|
|
| 75 |
try:
|
| 76 |
logging.info("Starting training pipeline...")
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
if ENABLE_PREFECT
|
| 80 |
try:
|
| 81 |
logging.info("Triggering training via Prefect Cloud...")
|
| 82 |
from prefect_flows.training_flow import training_flow
|
| 83 |
result = training_flow()
|
| 84 |
-
return Response(f"Training triggered via Prefect Cloud! Check dashboard
|
| 85 |
except Exception as prefect_error:
|
| 86 |
logging.warning(f"Prefect training failed, falling back to direct: {prefect_error}")
|
| 87 |
|
| 88 |
-
#
|
| 89 |
training_pipeline = Trainingpipeline()
|
| 90 |
training_pipeline.run_pipeline()
|
| 91 |
-
return Response("Training
|
| 92 |
except Exception as e:
|
| 93 |
raise NetworkSecurityException(e, sys)
|
| 94 |
|
|
|
|
| 75 |
try:
|
| 76 |
logging.info("Starting training pipeline...")
|
| 77 |
|
| 78 |
+
# Use Prefect Cloud if available
|
| 79 |
+
if ENABLE_PREFECT:
|
| 80 |
try:
|
| 81 |
logging.info("Triggering training via Prefect Cloud...")
|
| 82 |
from prefect_flows.training_flow import training_flow
|
| 83 |
result = training_flow()
|
| 84 |
+
return Response(f"Training triggered via Prefect Cloud! Check dashboard: https://app.prefect.cloud/")
|
| 85 |
except Exception as prefect_error:
|
| 86 |
logging.warning(f"Prefect training failed, falling back to direct: {prefect_error}")
|
| 87 |
|
| 88 |
+
# Fallback: Direct training
|
| 89 |
training_pipeline = Trainingpipeline()
|
| 90 |
training_pipeline.run_pipeline()
|
| 91 |
+
return Response("Training completed successfully (direct mode)")
|
| 92 |
except Exception as e:
|
| 93 |
raise NetworkSecurityException(e, sys)
|
| 94 |
|
monitoring/evidently_monitor.py
CHANGED
|
@@ -23,10 +23,40 @@ class PhishingModelMonitor:
|
|
| 23 |
self.reference_data = pd.read_csv(reference_data_path)
|
| 24 |
self.reports_dir = "monitoring/reports"
|
| 25 |
os.makedirs(self.reports_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def generate_data_drift_report(self, current_data: pd.DataFrame):
|
| 28 |
"""
|
| 29 |
Generate data drift report comparing current data with reference
|
|
|
|
| 30 |
|
| 31 |
Args:
|
| 32 |
current_data: Recent prediction data
|
|
@@ -45,11 +75,19 @@ class PhishingModelMonitor:
|
|
| 45 |
current_data=current_data
|
| 46 |
)
|
| 47 |
|
| 48 |
-
# Save report
|
| 49 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 50 |
report_path = f"{self.reports_dir}/drift_report_{timestamp}.html"
|
| 51 |
report.save_html(report_path)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# Extract metrics
|
| 54 |
report_json = report.as_dict()
|
| 55 |
|
|
|
|
| 23 |
self.reference_data = pd.read_csv(reference_data_path)
|
| 24 |
self.reports_dir = "monitoring/reports"
|
| 25 |
os.makedirs(self.reports_dir, exist_ok=True)
|
| 26 |
+
|
| 27 |
+
# Check for Evidently Cloud integration
|
| 28 |
+
self.evidently_cloud_token = os.getenv("EVIDENTLY_CLOUD_TOKEN") or os.getenv("EVIDENTLY_API_KEY")
|
| 29 |
+
self.evidently_project_id = os.getenv("EVIDENTLY_PROJECT_ID") or "nss" # Default to "nss"
|
| 30 |
+
self.use_evidently_cloud = self.evidently_cloud_token is not None
|
| 31 |
+
|
| 32 |
+
if self.use_evidently_cloud:
|
| 33 |
+
print(f"✅ Evidently Cloud integration enabled (Project: {self.evidently_project_id})")
|
| 34 |
+
try:
|
| 35 |
+
from evidently.ui.workspace.cloud import CloudWorkspace
|
| 36 |
+
# Initialize Evidently Cloud workspace
|
| 37 |
+
self.workspace = CloudWorkspace(
|
| 38 |
+
token=self.evidently_cloud_token,
|
| 39 |
+
url="https://app.evidently.cloud"
|
| 40 |
+
)
|
| 41 |
+
# Get or create project
|
| 42 |
+
try:
|
| 43 |
+
self.project = self.workspace.get_project(self.evidently_project_id)
|
| 44 |
+
print(f"✅ Connected to Evidently Cloud project: {self.evidently_project_id}")
|
| 45 |
+
except:
|
| 46 |
+
# Project might be referenced by name, try to find it
|
| 47 |
+
print(f"📦 Looking for project: {self.evidently_project_id}")
|
| 48 |
+
self.project = None # Will create reports without project reference
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"⚠️ Evidently Cloud setup warning: {e}")
|
| 52 |
+
self.use_evidently_cloud = False
|
| 53 |
+
else:
|
| 54 |
+
print("ℹ️ Using Evidently open-source (local reports only)")
|
| 55 |
|
| 56 |
def generate_data_drift_report(self, current_data: pd.DataFrame):
|
| 57 |
"""
|
| 58 |
Generate data drift report comparing current data with reference
|
| 59 |
+
Saves locally AND pushes to Evidently Cloud if enabled
|
| 60 |
|
| 61 |
Args:
|
| 62 |
current_data: Recent prediction data
|
|
|
|
| 75 |
current_data=current_data
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# Save local HTML report
|
| 79 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 80 |
report_path = f"{self.reports_dir}/drift_report_{timestamp}.html"
|
| 81 |
report.save_html(report_path)
|
| 82 |
|
| 83 |
+
# Push to Evidently Cloud if enabled
|
| 84 |
+
if self.use_evidently_cloud and self.project:
|
| 85 |
+
try:
|
| 86 |
+
self.workspace.add_report(self.project.id, report)
|
| 87 |
+
print(f"✅ Report pushed to Evidently Cloud: {self.evidently_project_id}")
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"⚠️ Failed to push to Evidently Cloud: {e}")
|
| 90 |
+
|
| 91 |
# Extract metrics
|
| 92 |
report_json = report.as_dict()
|
| 93 |
|