lyi029 commited on
Commit
0a7174f
·
1 Parent(s): 6e2340f

performance

Browse files
Files changed (1) hide show
  1. app.py +70 -53
app.py CHANGED
@@ -14,6 +14,7 @@ import csv, sys
14
  import io
15
  import numpy as np
16
  from datasets import load_dataset
 
17
 
18
  # ⚠️ 注意: som_analyzer.py 必須存在於同一目錄
19
  try:
@@ -61,6 +62,14 @@ os.makedirs("results", exist_ok=True)
61
 
62
  app.mount("/results", StaticFiles(directory=RESULT_DIR), name="results")
63
 
 
 
 
 
 
 
 
 
64
 
65
  # ====== 載入模型 ======
66
  print("🤗 Loading model from:", MODEL_REPO)
@@ -77,7 +86,7 @@ print("✅ Model loaded successfully")
77
 
78
  # ===== 新增:載入 SOM 模型 (from app.py) =====
79
  print("🧠 Initializing SOM Analyzer...")
80
- #SOM_REPO = "malvec/codebert_Malvec" # (app.py 的 SOM repo)
81
  SOM_AVAILABLE = False
82
 
83
  if SOM_FILE_AVAILABLE: # 檢查 'som_analyzer.py' 是否 import 成功
@@ -124,7 +133,8 @@ def home():
124
  "/predict-batch": "POST - Simplified batch prediction (from app.py)",
125
  "/model-info": "GET - Get model information",
126
  "/attention-heatmap": "POST - Generate attention heatmap (with CLIP)",
127
- "/check-new-samples": "POST - Trigger Space B Active Learning"
 
128
  }
129
  }
130
 
@@ -225,7 +235,6 @@ async def get_embeddings_from_hf_dataset(filename: str) -> Optional[List[np.ndar
225
  count += 1
226
  if count >= 1000: break # 保留限制
227
 
228
- # ✅ 修正縮排:這個 if/else 區塊必須在 'try' 內部
229
  if embeddings:
230
  print(f"✅ Found {len(embeddings)} embeddings for base name {base_name}")
231
  return embeddings
@@ -255,7 +264,6 @@ def analyze_som_region_probability(
255
  if len(region_samples) == 0:
256
  return {"dropper": 0.0, "APT30": 0.0}
257
 
258
- # ✅ 修正縮排:'feature_counts' 和 'if hasattr' 應該在同一層級
259
  feature_counts = {"dropper": [], "spreader": [], "APT30": [], "Codoso_Gh0st_1": []}
260
 
261
  if hasattr(som_analyzer, 'background_embeddings') and hasattr(som_analyzer, 'background_labels'):
@@ -268,7 +276,6 @@ def analyze_som_region_probability(
268
  som_analyzer.background_labels[feature_name][i]
269
  )
270
 
271
- # ✅ 修正縮排:'probabilities' 和 'for' 迴圈應該在 'if hasattr' 之外
272
  probabilities = {}
273
  for feature_name, values in feature_counts.items():
274
  if len(values) > 0:
@@ -293,22 +300,17 @@ async def analyze_with_som(embedding, predicted_family, filename):
293
  try:
294
  print("\n🗺️ Performing SOM analysis (New Logic)...")
295
 
296
- # 1. 決定用於 "定位" 的 Embedding
297
- # (我們假設傳入的 'embedding' 已經是最好的了,例如高注意力的那一個)
298
  if isinstance(embedding, list):
299
  embedding_to_use = np.array(embedding, dtype=np.float32)
300
  else:
301
  embedding_to_use = embedding
302
 
303
- # 4. 根據選定的 embedding 決定座標
304
  winner_row, winner_col = som_analyzer.find_winner_neuron(embedding_to_use)
305
  print(f" 📍 Winner neuron: ({winner_row}, {winner_col}) (Source: high-attention segment)")
306
 
307
- # 1. (原步驟1) 現在去抓取 "所有" 相關的 embeddings,用於 "鄰域分析"
308
  hf_embeddings = await get_embeddings_from_hf_dataset(filename)
309
- data_source = "high-attention (pos) + " # 標記數據源
310
 
311
- # 5. 分析座標周圍 (使用已抓取的 hf_embeddings 或 fallback)
312
  if hf_embeddings and len(hf_embeddings) > 0:
313
  print(f" Analysing region using {len(hf_embeddings)} HF embeddings...")
314
  features_prob = analyze_som_region_probability(
@@ -316,7 +318,6 @@ async def analyze_with_som(embedding, predicted_family, filename):
316
  )
317
  data_source += "hf_dataset (prob)"
318
  else:
319
- # (使用原有的 fallback 邏輯)
320
  print(f"⚠️ No HF data for prob, using background data")
321
  features_dict = som_analyzer.predict_features_from_position(
322
  winner_row, winner_col, radius=1
@@ -324,7 +325,6 @@ async def analyze_with_som(embedding, predicted_family, filename):
324
  features_prob = {k: float(v) for k, v in features_dict.items()}
325
  data_source += "background_data (prob)"
326
 
327
- # 6. 組合最終結果 (此部分不變)
328
  features_binary = {
329
  feature: 1 if prob > 0.2 else 0
330
  for feature, prob in features_prob.items()
@@ -345,16 +345,9 @@ async def analyze_with_som(embedding, predicted_family, filename):
345
  import traceback
346
  traceback.print_exc()
347
  return None
348
-
349
- except Exception as e:
350
- print(f"❌ SOM analysis error: {e}")
351
- import traceback
352
- traceback.print_exc()
353
- return None
354
 
355
 
356
  @app.post("/predict")
357
- #async def predict_malware(files: List[UploadFile] = File(...)):
358
  async def predict_malware(files: List[UploadFile] = File(...), original_filename: Optional[str] = None):
359
  """
360
  批次預測惡意軟體家族(維持原回傳格式)
@@ -363,6 +356,8 @@ async def predict_malware(files: List[UploadFile] = File(...), original_filename
363
  if not files:
364
  raise HTTPException(status_code=400, detail="No files provided")
365
 
 
 
366
  print(f"\n{'='*60}")
367
  print(f"📥 Received {len(files)} files")
368
  print(f"{'='*60}")
@@ -489,7 +484,6 @@ async def predict_malware(files: List[UploadFile] = File(...), original_filename
489
 
490
  print(f"\n✅ Prediction complete!\n")
491
 
492
-
493
  # === 寫入本地 embeddings_log.jsonl ===
494
  log_path = os.path.join("results", "embeddings_log.jsonl")
495
  with open(log_path, "a") as f:
@@ -544,15 +538,12 @@ async def predict_malware(files: List[UploadFile] = File(...), original_filename
544
  if SOM_AVAILABLE and final_embedding_values:
545
  print("\n🗺️ Performing SOM analysis...")
546
  try:
547
- # 假設 SOM 分析需要第一個檔案的名稱作為參考
548
- # ✨ 決定要用於 SOM 查找的檔名
549
  filename_for_som = original_filename if original_filename else files[0].filename
550
  print(f" Using filename for SOM lookup: {filename_for_som}")
551
 
552
  som_analysis = await analyze_with_som(
553
  embedding=final_embedding_values,
554
  predicted_family=result.get("final_label", "Unknown"),
555
- #filename=files[0].filename
556
  filename=filename_for_som
557
  )
558
  except Exception as e:
@@ -562,6 +553,14 @@ async def predict_malware(files: List[UploadFile] = File(...), original_filename
562
  result["som_analysis"] = som_analysis
563
  print(f"✅ SOM analysis complete.")
564
 
 
 
 
 
 
 
 
 
565
  return result
566
 
567
  except Exception as e:
@@ -577,7 +576,6 @@ async def predict_batch(files: List[UploadFile] = File(...)):
577
  (from app.py)
578
  簡化版: 只返回 label 和 embedding,不包含詳細資訊
579
  """
580
- # 這將調用上面被修改過的 predict_malware 函數
581
  result = await predict_malware(files)
582
 
583
  return {
@@ -729,8 +727,6 @@ async def attention_heatmap(file: UploadFile = File(...)):
729
  most_similar_id, most_similar_score, most_similar_img = None, -1, None
730
  if old_records:
731
  same_label = [
732
- # rec for rec in old_records
733
- # if str(rec.get("label", "")).strip() == current_label
734
  rec for rec in old_records
735
  if rec.get("label") == current_label
736
  and rec.get("embedding")
@@ -801,30 +797,6 @@ async def attention_heatmap(file: UploadFile = File(...)):
801
  return {"error": str(e)}
802
 
803
 
804
-
805
- # @app.post("/check-new-samples")
806
- # async def check_new_samples(background_tasks: BackgroundTasks):
807
- # """
808
- # 手動觸發 Space B 的 Active Learning 探測與 LoRA 訓練流程
809
- # """
810
- # if not SPACE_B_URL:
811
- # raise HTTPException(status_code=400, detail="SPACE_B_URL not set")
812
-
813
- # async def call_space_b():
814
- # try:
815
- # async with httpx.AsyncClient(timeout=600.0) as client:
816
- # params = {"batch_size": 1000, "mmd_threshold": 0.2, "entropy_threshold": 1.1}
817
- # resp = await client.post(f"{SPACE_B_URL}/check-new-samples", params=params)
818
- # print(f"🚀 Space B /check-new-samples → HTTP {resp.status_code}")
819
- # except Exception as e:
820
- # print("❌ Failed to contact Space B:", e)
821
-
822
- # background_tasks.add_task(call_space_b)
823
- # return {
824
- # "status": "ok",
825
- # "message": f"Triggered Space B check-new-samples at {SPACE_B_URL}"
826
- # }
827
-
828
  @app.post("/check-new-samples")
829
  async def check_new_samples():
830
  """
@@ -886,16 +858,61 @@ async def update_model(payload: dict):
886
 
887
 
888
  # ----------------------------------------------------------
889
- # 健康檢查
890
  # ----------------------------------------------------------
891
  @app.get("/health")
892
  def health():
893
  return {"status": "ok", "space_b_url": SPACE_B_URL, "model": MODEL_REPO}
894
 
895
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
896
  # ===== Hugging Face Space 啟動 =====
897
  if __name__ == "__main__":
898
  import uvicorn
899
  port = int(os.getenv("PORT", 7860))
900
  uvicorn.run(app, host="0.0.0.0", port=port)
901
-
 
14
  import io
15
  import numpy as np
16
  from datasets import load_dataset
17
+ import time # ✨ 新增:用來量測 performance
18
 
19
  # ⚠️ 注意: som_analyzer.py 必須存在於同一目錄
20
  try:
 
62
 
63
  app.mount("/results", StaticFiles(directory=RESULT_DIR), name="results")
64
 
65
+ # ====== 簡單 Performance 統計 ======
66
+ PERF_STATS = {
67
+ "predict_calls": 0,
68
+ "predict_total_seconds": 0.0,
69
+ "predict_last_ms": 0.0,
70
+ "predict_last_files": 0,
71
+ "last_updated": None,
72
+ }
73
 
74
  # ====== 載入模型 ======
75
  print("🤗 Loading model from:", MODEL_REPO)
 
86
 
87
  # ===== 新增:載入 SOM 模型 (from app.py) =====
88
  print("🧠 Initializing SOM Analyzer...")
89
+ SOM_REPO = os.getenv("SOM_REPO", "malvec/codebert_Malvec") # ✨ 補上 SOM_REPO,避免 NameError
90
  SOM_AVAILABLE = False
91
 
92
  if SOM_FILE_AVAILABLE: # 檢查 'som_analyzer.py' 是否 import 成功
 
133
  "/predict-batch": "POST - Simplified batch prediction (from app.py)",
134
  "/model-info": "GET - Get model information",
135
  "/attention-heatmap": "POST - Generate attention heatmap (with CLIP)",
136
+ "/check-new-samples": "POST - Trigger Space B Active Learning",
137
+ "/performance": "GET - Simple runtime performance stats" # ✨ 新增說明
138
  }
139
  }
140
 
 
235
  count += 1
236
  if count >= 1000: break # 保留限制
237
 
 
238
  if embeddings:
239
  print(f"✅ Found {len(embeddings)} embeddings for base name {base_name}")
240
  return embeddings
 
264
  if len(region_samples) == 0:
265
  return {"dropper": 0.0, "APT30": 0.0}
266
 
 
267
  feature_counts = {"dropper": [], "spreader": [], "APT30": [], "Codoso_Gh0st_1": []}
268
 
269
  if hasattr(som_analyzer, 'background_embeddings') and hasattr(som_analyzer, 'background_labels'):
 
276
  som_analyzer.background_labels[feature_name][i]
277
  )
278
 
 
279
  probabilities = {}
280
  for feature_name, values in feature_counts.items():
281
  if len(values) > 0:
 
300
  try:
301
  print("\n🗺️ Performing SOM analysis (New Logic)...")
302
 
 
 
303
  if isinstance(embedding, list):
304
  embedding_to_use = np.array(embedding, dtype=np.float32)
305
  else:
306
  embedding_to_use = embedding
307
 
 
308
  winner_row, winner_col = som_analyzer.find_winner_neuron(embedding_to_use)
309
  print(f" 📍 Winner neuron: ({winner_row}, {winner_col}) (Source: high-attention segment)")
310
 
 
311
  hf_embeddings = await get_embeddings_from_hf_dataset(filename)
312
+ data_source = "high-attention (pos) + "
313
 
 
314
  if hf_embeddings and len(hf_embeddings) > 0:
315
  print(f" Analysing region using {len(hf_embeddings)} HF embeddings...")
316
  features_prob = analyze_som_region_probability(
 
318
  )
319
  data_source += "hf_dataset (prob)"
320
  else:
 
321
  print(f"⚠️ No HF data for prob, using background data")
322
  features_dict = som_analyzer.predict_features_from_position(
323
  winner_row, winner_col, radius=1
 
325
  features_prob = {k: float(v) for k, v in features_dict.items()}
326
  data_source += "background_data (prob)"
327
 
 
328
  features_binary = {
329
  feature: 1 if prob > 0.2 else 0
330
  for feature, prob in features_prob.items()
 
345
  import traceback
346
  traceback.print_exc()
347
  return None
 
 
 
 
 
 
348
 
349
 
350
  @app.post("/predict")
 
351
  async def predict_malware(files: List[UploadFile] = File(...), original_filename: Optional[str] = None):
352
  """
353
  批次預測惡意軟體家族(維持原回傳格式)
 
356
  if not files:
357
  raise HTTPException(status_code=400, detail="No files provided")
358
 
359
+ start_time = time.time() # ✨ 開始量測時間
360
+
361
  print(f"\n{'='*60}")
362
  print(f"📥 Received {len(files)} files")
363
  print(f"{'='*60}")
 
484
 
485
  print(f"\n✅ Prediction complete!\n")
486
 
 
487
  # === 寫入本地 embeddings_log.jsonl ===
488
  log_path = os.path.join("results", "embeddings_log.jsonl")
489
  with open(log_path, "a") as f:
 
538
  if SOM_AVAILABLE and final_embedding_values:
539
  print("\n🗺️ Performing SOM analysis...")
540
  try:
 
 
541
  filename_for_som = original_filename if original_filename else files[0].filename
542
  print(f" Using filename for SOM lookup: {filename_for_som}")
543
 
544
  som_analysis = await analyze_with_som(
545
  embedding=final_embedding_values,
546
  predicted_family=result.get("final_label", "Unknown"),
 
547
  filename=filename_for_som
548
  )
549
  except Exception as e:
 
553
  result["som_analysis"] = som_analysis
554
  print(f"✅ SOM analysis complete.")
555
 
556
+ # ✨ 更新 performance 統計
557
+ elapsed = time.time() - start_time
558
+ PERF_STATS["predict_calls"] += 1
559
+ PERF_STATS["predict_total_seconds"] += elapsed
560
+ PERF_STATS["predict_last_ms"] = elapsed * 1000.0
561
+ PERF_STATS["predict_last_files"] = len(files)
562
+ PERF_STATS["last_updated"] = datetime.datetime.utcnow().isoformat()
563
+
564
  return result
565
 
566
  except Exception as e:
 
576
  (from app.py)
577
  簡化版: 只返回 label 和 embedding,不包含詳細資訊
578
  """
 
579
  result = await predict_malware(files)
580
 
581
  return {
 
727
  most_similar_id, most_similar_score, most_similar_img = None, -1, None
728
  if old_records:
729
  same_label = [
 
 
730
  rec for rec in old_records
731
  if rec.get("label") == current_label
732
  and rec.get("embedding")
 
797
  return {"error": str(e)}
798
 
799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
800
  @app.post("/check-new-samples")
801
  async def check_new_samples():
802
  """
 
858
 
859
 
860
  # ----------------------------------------------------------
861
+ # 健康檢查 & Performance
862
  # ----------------------------------------------------------
863
  @app.get("/health")
864
  def health():
865
  return {"status": "ok", "space_b_url": SPACE_B_URL, "model": MODEL_REPO}
866
 
867
 
868
+ @app.get("/performance")
869
+ async def performance():
870
+ """
871
+ 簡單回報目前的推論 performance 統計
872
+ (目前只統�� /predict 的執行時間與呼叫次數)
873
+ """
874
+ avg_sec = (
875
+ PERF_STATS["predict_total_seconds"] / PERF_STATS["predict_calls"]
876
+ if PERF_STATS["predict_calls"] > 0 else 0.0
877
+ )
878
+
879
+ base_info = {
880
+ "model_name": MODEL_REPO,
881
+ "current_tag": CURRENT_TAG,
882
+ "device": str(DEVICE),
883
+ "som_available": SOM_AVAILABLE,
884
+ "space_b_url": SPACE_B_URL,
885
+ }
886
+
887
+ perf_info = {
888
+ "predict_calls": PERF_STATS["predict_calls"],
889
+ "predict_total_seconds": PERF_STATS["predict_total_seconds"],
890
+ "predict_avg_seconds": avg_sec,
891
+ "predict_last_ms": PERF_STATS["predict_last_ms"],
892
+ "predict_last_files": PERF_STATS["predict_last_files"],
893
+ "last_updated": PERF_STATS["last_updated"],
894
+ }
895
+
896
+ # (可選)試著從 Space B 拿 performance(如果那邊有 /performance 的話)
897
+ space_b_perf = None
898
+ if SPACE_B_URL:
899
+ try:
900
+ async with httpx.AsyncClient(timeout=5.0) as client:
901
+ r = await client.get(f"{SPACE_B_URL}/performance")
902
+ if r.status_code == 200:
903
+ space_b_perf = r.json()
904
+ except Exception:
905
+ space_b_perf = None
906
+
907
+ return {
908
+ "base": base_info,
909
+ "space_a_performance": perf_info,
910
+ "space_b_performance": space_b_perf,
911
+ }
912
+
913
+
914
  # ===== Hugging Face Space 啟動 =====
915
  if __name__ == "__main__":
916
  import uvicorn
917
  port = int(os.getenv("PORT", 7860))
918
  uvicorn.run(app, host="0.0.0.0", port=port)