petter2025 commited on
Commit
79d9873
·
verified ·
1 Parent(s): 82eeaa2

Update hf_demo.py

Browse files
Files changed (1) hide show
  1. hf_demo.py +8 -3
hf_demo.py CHANGED
@@ -17,6 +17,7 @@ from enum import Enum
17
  from typing import Dict, List, Optional, Any, Tuple
18
 
19
  import yaml
 
20
  from fastapi import FastAPI, HTTPException, Depends, status
21
  from fastapi.middleware.cors import CORSMiddleware
22
  from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
@@ -260,7 +261,7 @@ class BayesianRiskEngine:
260
  except sqlite3.Error as e:
261
  logger.error(f"Failed to record outcome: {e}")
262
 
263
- # ---------- NEW: Enhanced risk using HMC coefficients ----------
264
  def enhanced_risk(self, action_text: str, context: Dict, hmc_coeffs: Optional[Dict] = None) -> float:
265
  """
266
  Compute a risk score using HMC coefficients if available.
@@ -280,7 +281,10 @@ class BayesianRiskEngine:
280
  env_prod = 1 if context.get('environment') == 'production' else 0
281
  role_junior = 1 if context.get('user_role') == 'junior' else 0
282
  hour = datetime.now().hour
283
- # Use the simple posterior risk as a feature (normalized)
 
 
 
284
  simple_risk = self.calculate_posterior(action_text, context)["score"]
285
  confidence = context.get('confidence', 0.85)
286
 
@@ -290,7 +294,8 @@ class BayesianRiskEngine:
290
  hmc_coeffs.get('β_env', {}).get('mean', 0) * env_prod +
291
  hmc_coeffs.get('β_role', {}).get('mean', 0) * role_junior +
292
  hmc_coeffs.get('β_risk', {}).get('mean', 0) * (simple_risk - 0.5) +
293
- hmc_coeffs.get('β_hour', {}).get('mean', 0) * ((hour - 12) / 12) +
 
294
  hmc_coeffs.get('β_conf', {}).get('mean', 0) * (confidence - 0.5)
295
  )
296
  # Convert to probability
 
17
  from typing import Dict, List, Optional, Any, Tuple
18
 
19
  import yaml
20
+ import numpy as np
21
  from fastapi import FastAPI, HTTPException, Depends, status
22
  from fastapi.middleware.cors import CORSMiddleware
23
  from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
 
261
  except sqlite3.Error as e:
262
  logger.error(f"Failed to record outcome: {e}")
263
 
264
+ # ---------- ENHANCED RISK USING HMC COEFFICIENTS ----------
265
  def enhanced_risk(self, action_text: str, context: Dict, hmc_coeffs: Optional[Dict] = None) -> float:
266
  """
267
  Compute a risk score using HMC coefficients if available.
 
281
  env_prod = 1 if context.get('environment') == 'production' else 0
282
  role_junior = 1 if context.get('user_role') == 'junior' else 0
283
  hour = datetime.now().hour
284
+ hour_sin = np.sin(2 * np.pi * hour / 24)
285
+ hour_cos = np.cos(2 * np.pi * hour / 24)
286
+
287
+ # Use the simple posterior risk as a feature (centered)
288
  simple_risk = self.calculate_posterior(action_text, context)["score"]
289
  confidence = context.get('confidence', 0.85)
290
 
 
294
  hmc_coeffs.get('β_env', {}).get('mean', 0) * env_prod +
295
  hmc_coeffs.get('β_role', {}).get('mean', 0) * role_junior +
296
  hmc_coeffs.get('β_risk', {}).get('mean', 0) * (simple_risk - 0.5) +
297
+ hmc_coeffs.get('β_hour_sin', {}).get('mean', 0) * hour_sin +
298
+ hmc_coeffs.get('β_hour_cos', {}).get('mean', 0) * hour_cos +
299
  hmc_coeffs.get('β_conf', {}).get('mean', 0) * (confidence - 0.5)
300
  )
301
  # Convert to probability