lyi029 commited on
Commit
ad79b46
·
1 Parent(s): 141e1a8

updates for demo

Browse files
Files changed (1) hide show
  1. app.py +49 -256
app.py CHANGED
@@ -387,246 +387,6 @@ def _block_attention_score(tokens, chunk_size=512):
387
  scores.append(mean_attn.item())
388
  return float(np.mean(scores)) if scores else 0.0
389
 
390
- # @app.post("/attention-heatmap")
391
- # async def attention_heatmap(file: UploadFile = File(...)):
392
- # """
393
- # 適用格式:整行以 '</s>' 分隔的 disasm tokens。
394
- # 每個 block 以 jump/call/ret 結束。
395
- # 最多取前 1000 個 block。
396
- # 會同時回傳:
397
- # - blocks:每個 basic block 的注意力平均
398
- # - attention_heatmap:base64 圖像
399
- # - heatmap_matrix:數值陣列(可供前端繪圖)
400
- # """
401
- # import io, sys, numpy as np, csv, base64, matplotlib.pyplot as plt
402
- # csv.field_size_limit(sys.maxsize)
403
-
404
- # try:
405
- # # === 讀檔 ===
406
- # content = await file.read()
407
- # text = content.decode("utf-8", errors="ignore")
408
-
409
- # # === 以 </s> 分隔整個 disasm ===
410
- # lines = text.replace("\n", " ").split("</s>")
411
- # tokens = []
412
- # for line in lines:
413
- # toks = [t.strip() for t in line.split() if t.strip()]
414
- # if toks:
415
- # tokens.extend(toks)
416
-
417
- # print(f"📦 Total tokens parsed: {len(tokens)}")
418
-
419
- # # === 拆 basic blocks ===
420
- # blocks = _split_into_basic_blocks(tokens)
421
- # print(f"📊 Total blocks before truncation: {len(blocks)}")
422
-
423
- # # === 只取前 1000 個 block
424
- # MAX_BLOCKS = int(os.getenv("MAX_HEATMAP_BLOCKS", "1000"))
425
- # blocks = blocks[:MAX_BLOCKS]
426
-
427
- # # === 計算每個 block 的 attention
428
- # results = []
429
- # for i, blk in enumerate(blocks):
430
- # score = _block_attention_score(blk)
431
- # results.append({
432
- # "idx": i,
433
- # "size": len(blk),
434
- # "score": score
435
- # })
436
-
437
- # if not results:
438
- # return {"error": "No valid blocks parsed"}
439
-
440
- # scores = [b["score"] for b in results]
441
- # avg_score = float(np.mean(scores))
442
-
443
- # # === 視覺化成 heatmap ===
444
- # fig, ax = plt.subplots(figsize=(10, 2))
445
- # heatmap_data = np.expand_dims(scores, axis=0)
446
- # im = ax.imshow(heatmap_data, cmap="viridis", aspect="auto")
447
- # plt.colorbar(im, ax=ax, orientation="vertical", label="Attention Score")
448
- # ax.set_title("Attention Heatmap per Basic Block")
449
- # ax.set_xlabel("Block Index")
450
- # ax.set_yticks([]) # 不需要 Y 軸
451
- # plt.tight_layout()
452
-
453
- # # === 轉成 base64 ===
454
- # buf = io.BytesIO()
455
- # plt.savefig(buf, format="png")
456
- # buf.seek(0)
457
- # img_base64 = base64.b64encode(buf.read()).decode("utf-8")
458
- # plt.close(fig)
459
-
460
- # # === 回傳 JSON ===
461
- # return {
462
- # # "num_blocks": len(results),
463
- # # "avg_attention": avg_score,
464
- # # "blocks": results,
465
- # # "heatmap_matrix": heatmap_data.tolist(), # ✅ 可互動繪圖
466
- # "attention_heatmap": f"data:image/png;base64,{img_base64}" # ✅ 前端直接顯示
467
- # }
468
-
469
- # except Exception as e:
470
- # print(f"❌ attention_heatmap error: {e}")
471
- # import traceback
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
  """
@@ -672,7 +432,7 @@ async def attention_heatmap(file: UploadFile = File(...)):
672
  heatmap_data = np.expand_dims(scores, axis=0)
673
  im = ax.imshow(heatmap_data, cmap="viridis", aspect="auto")
674
  plt.colorbar(im, ax=ax, orientation="vertical", label="Attention Score")
675
- ax.set_title("Attention Heatmap per Basic Block")
676
  ax.set_xlabel("Block Index")
677
  ax.set_yticks([])
678
  plt.tight_layout()
@@ -801,28 +561,61 @@ async def attention_heatmap(file: UploadFile = File(...)):
801
 
802
 
803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
804
  @app.post("/check-new-samples")
805
- async def check_new_samples(background_tasks: BackgroundTasks):
806
  """
807
- 手動觸發 Space B 的 Active Learning 探測與 LoRA 訓練流程
 
808
  """
809
  if not SPACE_B_URL:
810
  raise HTTPException(status_code=400, detail="SPACE_B_URL not set")
811
 
812
- async def call_space_b():
813
- try:
814
- async with httpx.AsyncClient(timeout=600.0) as client:
815
- params = {"batch_size": 1000, "mmd_threshold": 0.2, "entropy_threshold": 1.1}
816
- resp = await client.post(f"{SPACE_B_URL}/check-new-samples", params=params)
817
- print(f"🚀 Space B /check-new-samples → HTTP {resp.status_code}")
818
- except Exception as e:
819
- print("❌ Failed to contact Space B:", e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820
 
821
- background_tasks.add_task(call_space_b)
822
- return {
823
- "status": "ok",
824
- "message": f"Triggered Space B check-new-samples at {SPACE_B_URL}"
825
- }
826
 
827
  def _reload_model_by_tag(tag: str):
828
  global tokenizer, model, CURRENT_TAG
 
387
  scores.append(mean_attn.item())
388
  return float(np.mean(scores)) if scores else 0.0
389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
  @app.post("/attention-heatmap")
391
  async def attention_heatmap(file: UploadFile = File(...)):
392
  """
 
432
  heatmap_data = np.expand_dims(scores, axis=0)
433
  im = ax.imshow(heatmap_data, cmap="viridis", aspect="auto")
434
  plt.colorbar(im, ax=ax, orientation="vertical", label="Attention Score")
435
+ ax.set_title("Attention Heatmap")
436
  ax.set_xlabel("Block Index")
437
  ax.set_yticks([])
438
  plt.tight_layout()
 
561
 
562
 
563
 
564
+ # @app.post("/check-new-samples")
565
+ # async def check_new_samples(background_tasks: BackgroundTasks):
566
+ # """
567
+ # 手動觸發 Space B 的 Active Learning 探測與 LoRA 訓練流程
568
+ # """
569
+ # if not SPACE_B_URL:
570
+ # raise HTTPException(status_code=400, detail="SPACE_B_URL not set")
571
+
572
+ # async def call_space_b():
573
+ # try:
574
+ # async with httpx.AsyncClient(timeout=600.0) as client:
575
+ # params = {"batch_size": 1000, "mmd_threshold": 0.2, "entropy_threshold": 1.1}
576
+ # resp = await client.post(f"{SPACE_B_URL}/check-new-samples", params=params)
577
+ # print(f"🚀 Space B /check-new-samples → HTTP {resp.status_code}")
578
+ # except Exception as e:
579
+ # print("❌ Failed to contact Space B:", e)
580
+
581
+ # background_tasks.add_task(call_space_b)
582
+ # return {
583
+ # "status": "ok",
584
+ # "message": f"Triggered Space B check-new-samples at {SPACE_B_URL}"
585
+ # }
586
+
587
  @app.post("/check-new-samples")
588
+ async def check_new_samples():
589
  """
590
+ 觸發 Space B 的 Active Learning 探測與 LoRA 訓練流程
591
+ 並將 Space B 的回傳結果原樣傳回前端
592
  """
593
  if not SPACE_B_URL:
594
  raise HTTPException(status_code=400, detail="SPACE_B_URL not set")
595
 
596
+ try:
597
+ async with httpx.AsyncClient(timeout=600.0) as client:
598
+ params = {"batch_size": 1000, "mmd_threshold": 0.2, "entropy_threshold": 1.1}
599
+ resp = await client.post(f"{SPACE_B_URL}/check-new-samples", params=params)
600
+ print(f"🚀 Space B /check-new-samples → HTTP {resp.status_code}")
601
+
602
+ # 嘗試解析回傳 JSON
603
+ try:
604
+ data = resp.json()
605
+ print(f"📦 Space B response: {data}")
606
+ return data # ✅ 原樣回傳給前端
607
+ except Exception as e:
608
+ print(f"⚠️ Failed to parse Space B JSON: {e}")
609
+ return {
610
+ "status": "error",
611
+ "message": "Invalid JSON from Space B",
612
+ "raw_text": resp.text[:500]
613
+ }
614
+
615
+ except Exception as e:
616
+ print("❌ Failed to contact Space B:", e)
617
+ raise HTTPException(status_code=500, detail=f"Failed to contact Space B: {str(e)}")
618
 
 
 
 
 
 
619
 
620
  def _reload_model_by_tag(tag: str):
621
  global tokenizer, model, CURRENT_TAG