lyi029 commited on
Commit
9bc2d45
·
1 Parent(s): 774d4f3
Files changed (1) hide show
  1. app.py +148 -51
app.py CHANGED
@@ -392,8 +392,12 @@ def _block_attention_score(tokens, chunk_size=512):
392
  # 適用格式:整行以 '</s>' 分隔的 disasm tokens。
393
  # 每個 block 以 jump/call/ret 結束。
394
  # 最多取前 1000 個 block。
 
 
 
 
395
  # """
396
- # import io, sys, numpy as np, csv
397
  # csv.field_size_limit(sys.maxsize)
398
 
399
  # try:
@@ -429,94 +433,186 @@ def _block_attention_score(tokens, chunk_size=512):
429
  # "score": score
430
  # })
431
 
432
- # avg_score = float(np.mean([b["score"] for b in results])) if results else 0.0
433
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
434
  # return {
435
- # "num_blocks": len(results),
436
- # "avg_attention": avg_score,
437
- # "blocks": results
 
 
438
  # }
439
 
440
  # except Exception as e:
441
  # print(f"❌ attention_heatmap error: {e}")
 
 
442
  # return {"error": str(e)}
 
443
  @app.post("/attention-heatmap")
444
  async def attention_heatmap(file: UploadFile = File(...)):
445
  """
446
- 適用格式:整行以 '</s>' 分隔的 disasm tokens。
447
- 每個 block 以 jump/call/ret 結束。
448
- 最多取前 1000 個 block。
449
- 會同時回傳:
450
- - blocks:每個 basic block 的注意力平均
451
- - attention_heatmap:base64 圖像
452
- - heatmap_matrix:數值陣列(可供前端繪圖)
453
  """
454
- import io, sys, numpy as np, csv, base64, matplotlib.pyplot as plt
455
- csv.field_size_limit(sys.maxsize)
 
 
 
 
 
 
456
 
457
  try:
458
- # === 讀檔 ===
459
  content = await file.read()
460
  text = content.decode("utf-8", errors="ignore")
461
 
462
- # === 以 </s> 分隔整個 disasm ===
463
- lines = text.replace("\n", " ").split("</s>")
464
  tokens = []
465
- for line in lines:
466
  toks = [t.strip() for t in line.split() if t.strip()]
467
  if toks:
468
  tokens.extend(toks)
469
 
470
  print(f"📦 Total tokens parsed: {len(tokens)}")
471
 
472
- # === 拆 basic blocks ===
473
- blocks = _split_into_basic_blocks(tokens)
474
- print(f"📊 Total blocks before truncation: {len(blocks)}")
475
-
476
- # === 只取前 1000 個 block
477
- MAX_BLOCKS = int(os.getenv("MAX_HEATMAP_BLOCKS", "1000"))
478
- blocks = blocks[:MAX_BLOCKS]
479
-
480
- # === 計算每個 block 的 attention
481
- results = []
482
- for i, blk in enumerate(blocks):
483
- score = _block_attention_score(blk)
484
- results.append({
485
- "idx": i,
486
- "size": len(blk),
487
- "score": score
488
- })
489
-
490
- if not results:
491
- return {"error": "No valid blocks parsed"}
492
 
493
- scores = [b["score"] for b in results]
494
- avg_score = float(np.mean(scores))
 
 
495
 
496
- # === 視覺化成 heatmap ===
497
  fig, ax = plt.subplots(figsize=(10, 2))
498
  heatmap_data = np.expand_dims(scores, axis=0)
499
  im = ax.imshow(heatmap_data, cmap="viridis", aspect="auto")
500
  plt.colorbar(im, ax=ax, orientation="vertical", label="Attention Score")
501
  ax.set_title("Attention Heatmap per Basic Block")
502
  ax.set_xlabel("Block Index")
503
- ax.set_yticks([]) # 不需要 Y 軸
504
  plt.tight_layout()
505
 
506
- # === 轉成 base64 ===
507
  buf = io.BytesIO()
508
- plt.savefig(buf, format="png")
509
  buf.seek(0)
510
  img_base64 = base64.b64encode(buf.read()).decode("utf-8")
511
  plt.close(fig)
512
 
513
- # === 回傳 JSON ===
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514
  return {
515
- # "num_blocks": len(results),
516
- # "avg_attention": avg_score,
517
- # "blocks": results,
518
- # "heatmap_matrix": heatmap_data.tolist(), # ✅ 可互動繪圖
519
- "attention_heatmap": f"data:image/png;base64,{img_base64}" # ✅ 前端直接顯示
 
520
  }
521
 
522
  except Exception as e:
@@ -528,6 +624,7 @@ async def attention_heatmap(file: UploadFile = File(...)):
528
 
529
 
530
 
 
531
  @app.post("/check-new-samples")
532
  async def check_new_samples(background_tasks: BackgroundTasks):
533
  """
 
392
  # 適用格式:整行以 '</s>' 分隔的 disasm tokens。
393
  # 每個 block 以 jump/call/ret 結束。
394
  # 最多取前 1000 個 block。
395
+ # 會同時回傳:
396
+ # - blocks:每個 basic block 的注意力平均
397
+ # - attention_heatmap:base64 圖像
398
+ # - heatmap_matrix:數值陣列(可供前端繪圖)
399
  # """
400
+ # import io, sys, numpy as np, csv, base64, matplotlib.pyplot as plt
401
  # csv.field_size_limit(sys.maxsize)
402
 
403
  # try:
 
433
  # "score": score
434
  # })
435
 
436
+ # if not results:
437
+ # return {"error": "No valid blocks parsed"}
438
+
439
+ # scores = [b["score"] for b in results]
440
+ # avg_score = float(np.mean(scores))
441
+
442
+ # # === 視覺化成 heatmap ===
443
+ # fig, ax = plt.subplots(figsize=(10, 2))
444
+ # heatmap_data = np.expand_dims(scores, axis=0)
445
+ # im = ax.imshow(heatmap_data, cmap="viridis", aspect="auto")
446
+ # plt.colorbar(im, ax=ax, orientation="vertical", label="Attention Score")
447
+ # ax.set_title("Attention Heatmap per Basic Block")
448
+ # ax.set_xlabel("Block Index")
449
+ # ax.set_yticks([]) # 不需要 Y 軸
450
+ # plt.tight_layout()
451
+
452
+ # # === 轉成 base64 ===
453
+ # buf = io.BytesIO()
454
+ # plt.savefig(buf, format="png")
455
+ # buf.seek(0)
456
+ # img_base64 = base64.b64encode(buf.read()).decode("utf-8")
457
+ # plt.close(fig)
458
+
459
+ # # === 回傳 JSON ===
460
  # return {
461
+ # # "num_blocks": len(results),
462
+ # # "avg_attention": avg_score,
463
+ # # "blocks": results,
464
+ # # "heatmap_matrix": heatmap_data.tolist(), # ✅ 可互動繪圖
465
+ # "attention_heatmap": f"data:image/png;base64,{img_base64}" # ✅ 前端直接顯示
466
  # }
467
 
468
  # except Exception as e:
469
  # print(f"❌ attention_heatmap error: {e}")
470
+ # import traceback
471
+ # traceback.print_exc()
472
  # return {"error": str(e)}
473
+
474
  @app.post("/attention-heatmap")
475
  async def attention_heatmap(file: UploadFile = File(...)):
476
  """
477
+ 1️⃣ 從 disasm token 生成 block-level attention heatmap
478
+ 2️⃣ 將 heatmap 轉成 base64 圖
479
+ 3️⃣ 用 CLIP 將 heatmap 轉成 embedding
480
+ 4️⃣ 上傳至 dataset 並在同 label 裡找出最相似的 heatmap
 
 
 
481
  """
482
+ import io, base64, matplotlib.pyplot as plt, numpy as np, datetime, json
483
+ from PIL import Image
484
+ from transformers import CLIPProcessor, CLIPModel
485
+ from sklearn.metrics.pairwise import cosine_similarity
486
+ from huggingface_hub import HfApi, hf_hub_download
487
+
488
+ HF_HEATMAP_REPO = "lyi029/malvec-heatmaps"
489
+ os.makedirs("results", exist_ok=True)
490
 
491
  try:
492
+ # === Step 1. 讀檔 ===
493
  content = await file.read()
494
  text = content.decode("utf-8", errors="ignore")
495
 
496
+ # === Step 2. 解析 disasm token ===
 
497
  tokens = []
498
+ for line in text.replace("\n", " ").split("</s>"):
499
  toks = [t.strip() for t in line.split() if t.strip()]
500
  if toks:
501
  tokens.extend(toks)
502
 
503
  print(f"📦 Total tokens parsed: {len(tokens)}")
504
 
505
+ # === Step 3. 拆 basic blocks ===
506
+ blocks = _split_into_basic_blocks(tokens)[:1000]
507
+ print(f"📊 Total blocks: {len(blocks)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508
 
509
+ # === Step 4. 計算 block-level attention ===
510
+ scores = [_block_attention_score(blk) for blk in blocks]
511
+ if not scores:
512
+ return {"error": "No valid blocks parsed"}
513
 
514
+ # === Step 5. 畫出 heatmap 圖像 ===
515
  fig, ax = plt.subplots(figsize=(10, 2))
516
  heatmap_data = np.expand_dims(scores, axis=0)
517
  im = ax.imshow(heatmap_data, cmap="viridis", aspect="auto")
518
  plt.colorbar(im, ax=ax, orientation="vertical", label="Attention Score")
519
  ax.set_title("Attention Heatmap per Basic Block")
520
  ax.set_xlabel("Block Index")
521
+ ax.set_yticks([])
522
  plt.tight_layout()
523
 
 
524
  buf = io.BytesIO()
525
+ plt.savefig(buf, format="png", dpi=200)
526
  buf.seek(0)
527
  img_base64 = base64.b64encode(buf.read()).decode("utf-8")
528
  plt.close(fig)
529
 
530
+ b64_uri = f"data:image/png;base64,{img_base64}"
531
+
532
+ # === Step 6. 用 CLIP 轉成 embedding ===
533
+ clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
534
+ clip_proc = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
535
+
536
+ img = Image.open(io.BytesIO(base64.b64decode(img_base64))).convert("RGB")
537
+ inputs = clip_proc(images=img, return_tensors="pt")
538
+ with torch.no_grad():
539
+ emb = clip_model.get_image_features(**inputs)
540
+ emb = emb / emb.norm(p=2, dim=-1, keepdim=True)
541
+ emb_vec = emb.squeeze().cpu().numpy().tolist()
542
+
543
+ # === Step 7. 取得當前 label ===
544
+ current_label = os.getenv("CURRENT_LABEL", "unknown")
545
+
546
+ # === Step 8. 讀取舊 heatmap dataset ===
547
+ api = HfApi()
548
+ old_records = []
549
+ try:
550
+ old_path = hf_hub_download(
551
+ repo_id=HF_HEATMAP_REPO,
552
+ filename="heatmaps_log.jsonl",
553
+ repo_type="dataset",
554
+ token=HF_TOKEN
555
+ )
556
+ with open(old_path, "r") as f:
557
+ for line in f:
558
+ if line.strip():
559
+ old_records.append(json.loads(line))
560
+ except Exception:
561
+ print("ℹ️ No previous dataset found, will create new.")
562
+
563
+ # === Step 9. 找同 label 最相似的 heatmap ===
564
+ most_similar_id, most_similar_score, most_similar_img = None, -1, None
565
+ if old_records:
566
+ same_label = [
567
+ rec for rec in old_records
568
+ if rec.get("label") == current_label and rec.get("embedding")
569
+ ]
570
+ if same_label:
571
+ X = np.array([rec["embedding"] for rec in same_label])
572
+ sim_scores = cosine_similarity([emb_vec], X)[0]
573
+ max_idx = int(np.argmax(sim_scores))
574
+ if sim_scores[max_idx] < 0.999: # 排除完全相同
575
+ most_similar_id = same_label[max_idx]["source"]
576
+ most_similar_score = float(sim_scores[max_idx])
577
+ most_similar_img = same_label[max_idx].get("image_base64")
578
+
579
+ # === Step 10. 儲存新紀錄並上傳 ===
580
+ new_record = {
581
+ "timestamp": datetime.datetime.now().isoformat(),
582
+ "label": current_label,
583
+ "embedding": emb_vec,
584
+ "source": file.filename,
585
+ "image_base64": b64_uri
586
+ }
587
+
588
+ merged = {rec["source"]: rec for rec in (old_records + [new_record])}
589
+ merged_list = list(merged.values())
590
+
591
+ local_path = os.path.join("results", "heatmaps_log.jsonl")
592
+ with open(local_path, "w") as f:
593
+ for rec in merged_list:
594
+ f.write(json.dumps(rec) + "\n")
595
+
596
+ try:
597
+ api.upload_file(
598
+ path_or_fileobj=local_path,
599
+ path_in_repo="heatmaps_log.jsonl",
600
+ repo_id=HF_HEATMAP_REPO,
601
+ repo_type="dataset",
602
+ token=HF_TOKEN,
603
+ )
604
+ print(f"☁️ Uploaded {len(merged_list)} heatmaps to dataset: {HF_HEATMAP_REPO}")
605
+ except Exception as e:
606
+ print(f"⚠️ Upload failed: {e}")
607
+
608
+ # === Step 11. 回傳結果 ===
609
  return {
610
+ "label": current_label,
611
+ "attention_heatmap": b64_uri,
612
+ "embedding_dim": len(emb_vec),
613
+ "most_similar_in_label": most_similar_id,
614
+ "similarity_score": most_similar_score,
615
+ "similar_heatmap_image": most_similar_img
616
  }
617
 
618
  except Exception as e:
 
624
 
625
 
626
 
627
+
628
  @app.post("/check-new-samples")
629
  async def check_new_samples(background_tasks: BackgroundTasks):
630
  """