PhilipL commited on
Commit
4eb81af
·
verified ·
1 Parent(s): 2c45079

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -65,22 +65,14 @@ def get_ip_category(ip_str):
65
  try:
66
  le_action = joblib.load('le_action.pkl')
67
 
68
- print("📂 正在處理靜態訓練基底資料...")
69
- static_file = 'static_train_data.csv'
70
-
71
- if os.path.exists('baseline_logs.csv') and os.path.getsize('baseline_logs.csv') > 500000:
72
- if not os.path.exists(static_file):
73
- shutil.copy('baseline_logs.csv', static_file)
74
- print(f"🛡️ 系統防護:已自動將 5 萬筆訓練資料備份為 '{static_file}'")
75
-
76
- if os.path.exists(static_file): base_df = pd.read_csv(static_file)
77
- elif os.path.exists('baseline_logs (1).csv'): base_df = pd.read_csv('baseline_logs (1).csv')
78
- else: base_df = pd.DataFrame(columns=["timestamp", "ip_address", "cookie_id", "account", "role", "action", "status"])
79
-
80
- print("🔄 正在從 Hugging Face 雲端同步最新動態歷史資料...")
81
- dynamic_df = logger_service.sync_from_hf()
82
-
83
- baseline_df = pd.concat([base_df, dynamic_df], ignore_index=True) if not dynamic_df.empty else base_df.copy()
84
 
85
  baseline_df['timestamp'] = pd.to_datetime(baseline_df['timestamp'])
86
  baseline_df['clean_ip'] = baseline_df['ip_address'].astype(str).apply(lambda x: x.split(" ")[0])
@@ -360,6 +352,10 @@ def record_log(ip, cookie, account, role, action, status, time_mode, custom_time
360
  if ai_judgment != "success": status = ai_judgment
361
 
362
  new_log = {"timestamp": log_time, "ip_address": ip, "cookie_id": cookie, "account": account, "role": role, "action": action, "status": status}
 
 
 
 
363
  try: logger_service.push_new_log(new_log)
364
  except: pass
365
 
 
65
  try:
66
  le_action = joblib.load('le_action.pkl')
67
 
68
+ print("📂 正在讀取本地歷史資料庫...")
69
+ # 👉 修改點 1: 直接讀取本地的 baseline_logs.csv,不再向雲端同步
70
+ if os.path.exists('baseline_logs.csv'):
71
+ baseline_df = pd.read_csv('baseline_logs.csv')
72
+ else:
73
+ # 如果檔案不存在,建立帶有標題的空 DataFrame 並存檔
74
+ baseline_df = pd.DataFrame(columns=["timestamp", "ip_address", "cookie_id", "account", "role", "action", "status"])
75
+ baseline_df.to_csv('baseline_logs.csv', index=False)
 
 
 
 
 
 
 
 
76
 
77
  baseline_df['timestamp'] = pd.to_datetime(baseline_df['timestamp'])
78
  baseline_df['clean_ip'] = baseline_df['ip_address'].astype(str).apply(lambda x: x.split(" ")[0])
 
352
  if ai_judgment != "success": status = ai_judgment
353
 
354
  new_log = {"timestamp": log_time, "ip_address": ip, "cookie_id": cookie, "account": account, "role": role, "action": action, "status": status}
355
+
356
+ new_df = pd.DataFrame([new_log])
357
+ new_df.to_csv('baseline_logs.csv', mode='a', header=not os.path.exists('baseline_logs.csv'), index=False)
358
+
359
  try: logger_service.push_new_log(new_log)
360
  except: pass
361