lyi029 commited on
Commit
cbea94d
·
1 Parent(s): 77c3185
Files changed (1) hide show
  1. app.py +188 -21
app.py CHANGED
@@ -472,6 +472,161 @@ def _block_attention_score(tokens, chunk_size=512):
472
  # traceback.print_exc()
473
  # return {"error": str(e)}
474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
475
  @app.post("/attention-heatmap")
476
  async def attention_heatmap(file: UploadFile = File(...)):
477
  """
@@ -542,7 +697,8 @@ async def attention_heatmap(file: UploadFile = File(...)):
542
  emb_vec = emb.squeeze().cpu().numpy().tolist()
543
 
544
  # === Step 7. 取得當前 label ===
545
- current_label = os.getenv("CURRENT_LABEL", "unknown")
 
546
 
547
  # === Step 8. 讀取舊 heatmap dataset ===
548
  api = HfApi()
@@ -556,8 +712,15 @@ async def attention_heatmap(file: UploadFile = File(...)):
556
  )
557
  with open(old_path, "r") as f:
558
  for line in f:
559
- if line.strip():
560
- old_records.append(json.loads(line))
 
 
 
 
 
 
 
561
  except Exception:
562
  print("ℹ️ No previous dataset found, will create new.")
563
 
@@ -566,20 +729,26 @@ async def attention_heatmap(file: UploadFile = File(...)):
566
  if old_records:
567
  same_label = [
568
  rec for rec in old_records
569
- if rec.get("label") == current_label and rec.get("embedding")
570
  ]
 
 
571
  if same_label:
572
- X = np.array([rec["embedding"] for rec in same_label])
573
- sim_scores = cosine_similarity([emb_vec], X)[0]
574
- max_idx = int(np.argmax(sim_scores))
575
- if sim_scores[max_idx] < 0.999: # 排除完全相同
576
- most_similar_id = same_label[max_idx]["source"]
577
- most_similar_score = float(sim_scores[max_idx])
578
- most_similar_img = same_label[max_idx].get("image_base64")
579
- print(f"[heatmap-match] 🔍 Found {most_similar_id} in label '{same_label}'")
580
- print(f"[heatmap-match] ✅ Best match: {most_similar_id}, sim={sim_scores:.4f}")
581
- else:
582
- print(f"[heatmap-match] ⚠️ No other heatmaps found under label '{same_label}'")
 
 
 
 
583
 
584
  # === Step 10. 儲存新紀錄並上傳 ===
585
  new_record = {
@@ -587,11 +756,11 @@ async def attention_heatmap(file: UploadFile = File(...)):
587
  "label": current_label,
588
  "embedding": emb_vec,
589
  "source": file.filename,
590
- "image_base64": b64_uri
591
  }
592
 
593
- merged = {rec["source"]: rec for rec in (old_records + [new_record])}
594
- merged_list = list(merged.values())
595
 
596
  local_path = os.path.join("results", "heatmaps_log.jsonl")
597
  with open(local_path, "w") as f:
@@ -617,7 +786,7 @@ async def attention_heatmap(file: UploadFile = File(...)):
617
  "embedding_dim": len(emb_vec),
618
  "most_similar_in_label": most_similar_id,
619
  "similarity_score": most_similar_score,
620
- "similar_heatmap_image": most_similar_img
621
  }
622
 
623
  except Exception as e:
@@ -628,8 +797,6 @@ async def attention_heatmap(file: UploadFile = File(...)):
628
 
629
 
630
 
631
-
632
-
633
  @app.post("/check-new-samples")
634
  async def check_new_samples(background_tasks: BackgroundTasks):
635
  """
 
472
  # traceback.print_exc()
473
  # return {"error": str(e)}
474
 
475
+ # @app.post("/attention-heatmap")
476
+ # async def attention_heatmap(file: UploadFile = File(...)):
477
+ # """
478
+ # 1️⃣ 從 disasm token 生成 block-level attention heatmap
479
+ # 2️⃣ 將 heatmap 轉成 base64 圖
480
+ # 3️⃣ 用 CLIP 將 heatmap 轉成 embedding
481
+ # 4️⃣ 上傳至 dataset 並在同 label 裡找出最相似的 heatmap
482
+ # """
483
+ # import io, base64, matplotlib.pyplot as plt, numpy as np, datetime, json
484
+ # from PIL import Image
485
+ # from transformers import CLIPProcessor, CLIPModel
486
+ # from sklearn.metrics.pairwise import cosine_similarity
487
+ # from huggingface_hub import HfApi, hf_hub_download
488
+
489
+ # HF_HEATMAP_REPO = "lyi029/malvec-heatmap"
490
+ # os.makedirs("results", exist_ok=True)
491
+
492
+ # try:
493
+ # # === Step 1. 讀檔 ===
494
+ # content = await file.read()
495
+ # text = content.decode("utf-8", errors="ignore")
496
+
497
+ # # === Step 2. 解析 disasm token ===
498
+ # tokens = []
499
+ # for line in text.replace("\n", " ").split("</s>"):
500
+ # toks = [t.strip() for t in line.split() if t.strip()]
501
+ # if toks:
502
+ # tokens.extend(toks)
503
+
504
+ # print(f"📦 Total tokens parsed: {len(tokens)}")
505
+
506
+ # # === Step 3. 拆 basic blocks ===
507
+ # blocks = _split_into_basic_blocks(tokens)[:1000]
508
+ # print(f"📊 Total blocks: {len(blocks)}")
509
+
510
+ # # === Step 4. 計算 block-level attention ===
511
+ # scores = [_block_attention_score(blk) for blk in blocks]
512
+ # if not scores:
513
+ # return {"error": "No valid blocks parsed"}
514
+
515
+ # # === Step 5. 畫出 heatmap 圖像 ===
516
+ # fig, ax = plt.subplots(figsize=(10, 2))
517
+ # heatmap_data = np.expand_dims(scores, axis=0)
518
+ # im = ax.imshow(heatmap_data, cmap="viridis", aspect="auto")
519
+ # plt.colorbar(im, ax=ax, orientation="vertical", label="Attention Score")
520
+ # ax.set_title("Attention Heatmap per Basic Block")
521
+ # ax.set_xlabel("Block Index")
522
+ # ax.set_yticks([])
523
+ # plt.tight_layout()
524
+
525
+ # buf = io.BytesIO()
526
+ # plt.savefig(buf, format="png", dpi=200)
527
+ # buf.seek(0)
528
+ # img_base64 = base64.b64encode(buf.read()).decode("utf-8")
529
+ # plt.close(fig)
530
+
531
+ # b64_uri = f"data:image/png;base64,{img_base64}"
532
+
533
+ # # === Step 6. 用 CLIP 轉成 embedding ===
534
+ # clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
535
+ # clip_proc = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
536
+
537
+ # img = Image.open(io.BytesIO(base64.b64decode(img_base64))).convert("RGB")
538
+ # inputs = clip_proc(images=img, return_tensors="pt")
539
+ # with torch.no_grad():
540
+ # emb = clip_model.get_image_features(**inputs)
541
+ # emb = emb / emb.norm(p=2, dim=-1, keepdim=True)
542
+ # emb_vec = emb.squeeze().cpu().numpy().tolist()
543
+
544
+ # # === Step 7. 取得當前 label ===
545
+ # current_label = os.getenv("CURRENT_LABEL", "unknown")
546
+
547
+ # # === Step 8. 讀取舊 heatmap dataset ===
548
+ # api = HfApi()
549
+ # old_records = []
550
+ # try:
551
+ # old_path = hf_hub_download(
552
+ # repo_id=HF_HEATMAP_REPO,
553
+ # filename="heatmaps_log.jsonl",
554
+ # repo_type="dataset",
555
+ # token=HF_TOKEN
556
+ # )
557
+ # with open(old_path, "r") as f:
558
+ # for line in f:
559
+ # if line.strip():
560
+ # old_records.append(json.loads(line))
561
+ # except Exception:
562
+ # print("ℹ️ No previous dataset found, will create new.")
563
+
564
+ # # === Step 9. 找同 label 最相似的 heatmap ===
565
+ # most_similar_id, most_similar_score, most_similar_img = None, -1, None
566
+ # if old_records:
567
+ # same_label = [
568
+ # rec for rec in old_records
569
+ # if rec.get("label") == current_label and rec.get("embedding")
570
+ # ]
571
+ # if same_label:
572
+ # X = np.array([rec["embedding"] for rec in same_label])
573
+ # sim_scores = cosine_similarity([emb_vec], X)[0]
574
+ # max_idx = int(np.argmax(sim_scores))
575
+ # if sim_scores[max_idx] < 0.999: # 排除完全相同
576
+ # most_similar_id = same_label[max_idx]["source"]
577
+ # most_similar_score = float(sim_scores[max_idx])
578
+ # most_similar_img = same_label[max_idx].get("image_base64")
579
+ # print(f"[heatmap-match] 🔍 Found {most_similar_id} in label '{same_label}'")
580
+ # print(f"[heatmap-match] ✅ Best match: {most_similar_id}, sim={sim_scores:.4f}")
581
+ # else:
582
+ # print(f"[heatmap-match] ⚠️ No other heatmaps found under label '{same_label}'")
583
+
584
+ # # === Step 10. 儲存新紀錄並上傳 ===
585
+ # new_record = {
586
+ # "timestamp": datetime.datetime.now().isoformat(),
587
+ # "label": current_label,
588
+ # "embedding": emb_vec,
589
+ # "source": file.filename,
590
+ # "image_base64": b64_uri
591
+ # }
592
+
593
+ # merged = {rec["source"]: rec for rec in (old_records + [new_record])}
594
+ # merged_list = list(merged.values())
595
+
596
+ # local_path = os.path.join("results", "heatmaps_log.jsonl")
597
+ # with open(local_path, "w") as f:
598
+ # for rec in merged_list:
599
+ # f.write(json.dumps(rec) + "\n")
600
+
601
+ # try:
602
+ # api.upload_file(
603
+ # path_or_fileobj=local_path,
604
+ # path_in_repo="heatmaps_log.jsonl",
605
+ # repo_id=HF_HEATMAP_REPO,
606
+ # repo_type="dataset",
607
+ # token=HF_TOKEN,
608
+ # )
609
+ # print(f"☁️ Uploaded {len(merged_list)} heatmaps to dataset: {HF_HEATMAP_REPO}")
610
+ # except Exception as e:
611
+ # print(f"⚠️ Upload failed: {e}")
612
+
613
+ # # === Step 11. 回傳結果 ===
614
+ # return {
615
+ # "label": current_label,
616
+ # "attention_heatmap": b64_uri,
617
+ # "embedding_dim": len(emb_vec),
618
+ # "most_similar_in_label": most_similar_id,
619
+ # "similarity_score": most_similar_score,
620
+ # "similar_heatmap_image": most_similar_img
621
+ # }
622
+
623
+ # except Exception as e:
624
+ # print(f"❌ attention_heatmap error: {e}")
625
+ # import traceback
626
+ # traceback.print_exc()
627
+ # return {"error": str(e)}
628
+
629
+
630
  @app.post("/attention-heatmap")
631
  async def attention_heatmap(file: UploadFile = File(...)):
632
  """
 
697
  emb_vec = emb.squeeze().cpu().numpy().tolist()
698
 
699
  # === Step 7. 取得當前 label ===
700
+ current_label = os.getenv("CURRENT_LABEL", "unknown").strip()
701
+ print(f"[heatmap] 🏷️ Current label: {current_label}")
702
 
703
  # === Step 8. 讀取舊 heatmap dataset ===
704
  api = HfApi()
 
712
  )
713
  with open(old_path, "r") as f:
714
  for line in f:
715
+ line = line.strip()
716
+ if not line:
717
+ continue
718
+ try:
719
+ record = json.loads(line)
720
+ if isinstance(record, dict):
721
+ old_records.append(record)
722
+ except json.JSONDecodeError:
723
+ print(f"[heatmap-load] ⚠️ Skip invalid line.")
724
  except Exception:
725
  print("ℹ️ No previous dataset found, will create new.")
726
 
 
729
  if old_records:
730
  same_label = [
731
  rec for rec in old_records
732
+ if str(rec.get("label", "")).strip() == current_label
733
  ]
734
+ print(f"[heatmap-match] Found {len(same_label)} old heatmaps under label '{current_label}'")
735
+
736
  if same_label:
737
+ X = np.array([rec["embedding"] for rec in same_label if rec.get("embedding")])
738
+ if X.size > 0:
739
+ sim_scores = cosine_similarity([emb_vec], X)[0]
740
+ max_idx = int(np.argmax(sim_scores))
741
+ best_sim = float(sim_scores[max_idx])
742
+ best_rec = same_label[max_idx]
743
+ if best_sim < 0.999: # 排除完全相同
744
+ most_similar_id = best_rec.get("source")
745
+ most_similar_score = best_sim
746
+ most_similar_img = best_rec.get("image_base64")
747
+ print(f"[heatmap-match] ✅ Best match: {most_similar_id}, sim={best_sim:.4f}")
748
+ else:
749
+ print(f"[heatmap-match] ⚠️ No sufficiently different heatmaps found under label '{current_label}'")
750
+ else:
751
+ print(f"[heatmap-match] ⚠️ No existing heatmaps for label '{current_label}' yet")
752
 
753
  # === Step 10. 儲存新紀錄並上傳 ===
754
  new_record = {
 
756
  "label": current_label,
757
  "embedding": emb_vec,
758
  "source": file.filename,
759
+ "image_base64": b64_uri,
760
  }
761
 
762
+ merged_dict = {rec["source"]: rec for rec in (old_records + [new_record])}
763
+ merged_list = list(merged_dict.values())
764
 
765
  local_path = os.path.join("results", "heatmaps_log.jsonl")
766
  with open(local_path, "w") as f:
 
786
  "embedding_dim": len(emb_vec),
787
  "most_similar_in_label": most_similar_id,
788
  "similarity_score": most_similar_score,
789
+ "similar_heatmap_image": most_similar_img,
790
  }
791
 
792
  except Exception as e:
 
797
 
798
 
799
 
 
 
800
  @app.post("/check-new-samples")
801
  async def check_new_samples(background_tasks: BackgroundTasks):
802
  """