updates
Browse files
app.py
CHANGED
|
@@ -10,6 +10,10 @@ from collections import Counter
|
|
| 10 |
from fastapi.staticfiles import StaticFiles
|
| 11 |
from huggingface_hub import HfApi,hf_hub_download
|
| 12 |
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
CURRENT_TAG = os.getenv("MODEL_TAG", "main")
|
| 15 |
# ====== FastAPI 初始化 ======
|
|
@@ -103,7 +107,7 @@ def get_model_info():
|
|
| 103 |
}
|
| 104 |
|
| 105 |
|
| 106 |
-
HF_DATASET_REPO = "lyi029/malvec-embeddings"
|
| 107 |
|
| 108 |
def upload_embeddings_to_hub(local_path: str):
|
| 109 |
"""自動建立 Dataset 並分批上傳 embeddings_log.jsonl"""
|
|
@@ -150,468 +154,6 @@ def upload_embeddings_to_hub(local_path: str):
|
|
| 150 |
return False
|
| 151 |
|
| 152 |
|
| 153 |
-
# @app.post("/predict")
|
| 154 |
-
# async def predict_malware(files: List[UploadFile] = File(...)):
|
| 155 |
-
# """批次預測惡意軟體家族"""
|
| 156 |
-
# if not files:
|
| 157 |
-
# raise HTTPException(status_code=400, detail="No files provided")
|
| 158 |
-
|
| 159 |
-
# predictions, all_embeddings = [], []
|
| 160 |
-
|
| 161 |
-
# print(f"\n📥 Received {len(files)} files")
|
| 162 |
-
|
| 163 |
-
# try:
|
| 164 |
-
# with torch.no_grad():
|
| 165 |
-
# for idx, file in enumerate(files, 1):
|
| 166 |
-
# try:
|
| 167 |
-
# content = await file.read()
|
| 168 |
-
# asm_code = content.decode("utf-8").strip()
|
| 169 |
-
# if not asm_code:
|
| 170 |
-
# continue
|
| 171 |
-
|
| 172 |
-
# print(f"📄 [{idx}/{len(files)}] {file.filename}")
|
| 173 |
-
|
| 174 |
-
# inputs = tokenizer(
|
| 175 |
-
# asm_code,
|
| 176 |
-
# return_tensors="pt",
|
| 177 |
-
# max_length=512,
|
| 178 |
-
# truncation=True,
|
| 179 |
-
# padding=True
|
| 180 |
-
# ).to(DEVICE)
|
| 181 |
-
|
| 182 |
-
# outputs = model(**inputs)
|
| 183 |
-
# logits = outputs.logits
|
| 184 |
-
# probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 185 |
-
# confidence, predicted_class = torch.max(probs, dim=-1)
|
| 186 |
-
|
| 187 |
-
# predicted_id = predicted_class.item()
|
| 188 |
-
# label_name = model.config.id2label.get(predicted_id, f"class_{predicted_id}")
|
| 189 |
-
|
| 190 |
-
# # embedding
|
| 191 |
-
# if outputs.hidden_states:
|
| 192 |
-
# cls_embedding = outputs.hidden_states[-1][:, 0, :].cpu().numpy()[0]
|
| 193 |
-
# else:
|
| 194 |
-
# cls_embedding = logits.cpu().numpy()[0]
|
| 195 |
-
|
| 196 |
-
# # attention
|
| 197 |
-
# max_attention_score = 0.0
|
| 198 |
-
# if outputs.attentions:
|
| 199 |
-
# last_attention = outputs.attentions[-1].mean(dim=1)
|
| 200 |
-
# cls_attention = last_attention[0, 0, :].cpu().numpy()
|
| 201 |
-
# max_attention_score = float(cls_attention.max())
|
| 202 |
-
|
| 203 |
-
# predictions.append({
|
| 204 |
-
# "filename": file.filename,
|
| 205 |
-
# "predicted_label": label_name,
|
| 206 |
-
# "confidence": float(confidence.item()),
|
| 207 |
-
# "attention_score": max_attention_score
|
| 208 |
-
# })
|
| 209 |
-
# all_embeddings.append({
|
| 210 |
-
# "filename": file.filename,
|
| 211 |
-
# "label": label_name,
|
| 212 |
-
# "embedding": cls_embedding.tolist(),
|
| 213 |
-
# "attention_score": max_attention_score
|
| 214 |
-
# })
|
| 215 |
-
# except Exception as e:
|
| 216 |
-
# print(f"❌ {file.filename} error:", e)
|
| 217 |
-
|
| 218 |
-
# if not predictions:
|
| 219 |
-
# raise HTTPException(status_code=400, detail="No valid files processed")
|
| 220 |
-
|
| 221 |
-
# # 多數投票
|
| 222 |
-
# votes = [p["predicted_label"] for p in predictions]
|
| 223 |
-
# final_label, count = Counter(votes).most_common(1)[0]
|
| 224 |
-
# best_embedding = max(all_embeddings, key=lambda x: x["attention_score"])
|
| 225 |
-
|
| 226 |
-
# result = {
|
| 227 |
-
# "final_label": final_label,
|
| 228 |
-
# "vote_count": count,
|
| 229 |
-
# "total_segments": len(predictions),
|
| 230 |
-
# "confidence": sum(p["confidence"] for p in predictions) / len(predictions),
|
| 231 |
-
# "embedding": {
|
| 232 |
-
# "values": best_embedding["embedding"],
|
| 233 |
-
# "source_file": best_embedding["filename"],
|
| 234 |
-
# "attention_score": best_embedding["attention_score"]
|
| 235 |
-
# },
|
| 236 |
-
# "segment_predictions": predictions
|
| 237 |
-
# }
|
| 238 |
-
|
| 239 |
-
# # 寫入本地 log
|
| 240 |
-
# log_path = os.path.join("results", "embeddings_log.jsonl")
|
| 241 |
-
# with open(log_path, "a") as f:
|
| 242 |
-
# f.write(json.dumps({
|
| 243 |
-
# "label": final_label,
|
| 244 |
-
# "text": asm_code,
|
| 245 |
-
# "embedding": best_embedding["embedding"],
|
| 246 |
-
# "source": best_embedding["filename"]
|
| 247 |
-
# }) + "\n")
|
| 248 |
-
|
| 249 |
-
# print(f"✅ Prediction complete ({final_label}), embedding logged.")
|
| 250 |
-
|
| 251 |
-
# # ☁️ 自動上傳到 HF Dataset
|
| 252 |
-
# upload_embeddings_to_hub(log_path)
|
| 253 |
-
|
| 254 |
-
# return result
|
| 255 |
-
|
| 256 |
-
# except Exception as e:
|
| 257 |
-
# raise HTTPException(status_code=500, detail=f"Prediction failed: {e}")
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
# @app.post("/predict")
|
| 261 |
-
# async def predict_malware(files: List[UploadFile] = File(...)):
|
| 262 |
-
# """批次預測惡意軟體家族 + 自動上傳 embeddings 到 Dataset"""
|
| 263 |
-
# if not files:
|
| 264 |
-
# raise HTTPException(status_code=400, detail="No files provided")
|
| 265 |
-
|
| 266 |
-
# predictions, all_embeddings = [], []
|
| 267 |
-
# print(f"\n📥 Received {len(files)} files")
|
| 268 |
-
|
| 269 |
-
# try:
|
| 270 |
-
# with torch.no_grad():
|
| 271 |
-
# for idx, file in enumerate(files, 1):
|
| 272 |
-
# try:
|
| 273 |
-
# content = await file.read()
|
| 274 |
-
# asm_code = content.decode("utf-8").strip()
|
| 275 |
-
# if not asm_code:
|
| 276 |
-
# continue
|
| 277 |
-
|
| 278 |
-
# print(f"📄 [{idx}/{len(files)}] {file.filename}")
|
| 279 |
-
|
| 280 |
-
# # === 分段處理 ===
|
| 281 |
-
# segments = [seg.strip() for seg in asm_code.splitlines() if seg.strip()]
|
| 282 |
-
# for seg_idx, segment in enumerate(segments, 1):
|
| 283 |
-
# inputs = tokenizer(
|
| 284 |
-
# segment,
|
| 285 |
-
# return_tensors="pt",
|
| 286 |
-
# max_length=512,
|
| 287 |
-
# truncation=True,
|
| 288 |
-
# padding=True
|
| 289 |
-
# ).to(DEVICE)
|
| 290 |
-
|
| 291 |
-
# outputs = model(**inputs)
|
| 292 |
-
# logits = outputs.logits
|
| 293 |
-
# probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 294 |
-
# confidence, predicted_class = torch.max(probs, dim=-1)
|
| 295 |
-
|
| 296 |
-
# predicted_id = predicted_class.item()
|
| 297 |
-
# label_name = model.config.id2label.get(predicted_id, f"class_{predicted_id}")
|
| 298 |
-
|
| 299 |
-
# # === embedding ===
|
| 300 |
-
# if outputs.hidden_states:
|
| 301 |
-
# cls_embedding = outputs.hidden_states[-1][:, 0, :].cpu().numpy()[0]
|
| 302 |
-
# else:
|
| 303 |
-
# cls_embedding = logits.cpu().numpy()[0]
|
| 304 |
-
|
| 305 |
-
# # === attention ===
|
| 306 |
-
# max_attention_score = 0.0
|
| 307 |
-
# if outputs.attentions:
|
| 308 |
-
# last_attention = outputs.attentions[-1].mean(dim=1)
|
| 309 |
-
# cls_attention = last_attention[0, 0, :].cpu().numpy()
|
| 310 |
-
# max_attention_score = float(cls_attention.max())
|
| 311 |
-
|
| 312 |
-
# record = {
|
| 313 |
-
# "filename": file.filename,
|
| 314 |
-
# "segment_index": seg_idx,
|
| 315 |
-
# "text": segment,
|
| 316 |
-
# "predicted_label": label_name,
|
| 317 |
-
# "confidence": float(confidence.item()),
|
| 318 |
-
# "attention_score": max_attention_score,
|
| 319 |
-
# "embedding": cls_embedding.tolist()
|
| 320 |
-
# }
|
| 321 |
-
# all_embeddings.append(record)
|
| 322 |
-
|
| 323 |
-
# predictions.append({
|
| 324 |
-
# "filename": file.filename,
|
| 325 |
-
# "predicted_label": label_name,
|
| 326 |
-
# "confidence": float(confidence.item()),
|
| 327 |
-
# "attention_score": max_attention_score
|
| 328 |
-
# })
|
| 329 |
-
|
| 330 |
-
# except Exception as e:
|
| 331 |
-
# print(f"❌ {file.filename} error:", e)
|
| 332 |
-
|
| 333 |
-
# if not predictions:
|
| 334 |
-
# raise HTTPException(status_code=400, detail="No valid files processed")
|
| 335 |
-
|
| 336 |
-
# # === 多數投票 ===
|
| 337 |
-
# votes = [p["predicted_label"] for p in predictions]
|
| 338 |
-
# final_label, count = Counter(votes).most_common(1)[0]
|
| 339 |
-
|
| 340 |
-
# # result = {
|
| 341 |
-
# # "final_label": final_label,
|
| 342 |
-
# # "vote_count": count,
|
| 343 |
-
# # "total_segments": len(predictions),
|
| 344 |
-
# # "confidence": sum(p["confidence"] for p in predictions) / len(predictions),
|
| 345 |
-
# # "segment_predictions": predictions
|
| 346 |
-
# # }
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
# best_embedding = max(all_embeddings, key=lambda x: x["attention_score"])
|
| 350 |
-
|
| 351 |
-
# result = {
|
| 352 |
-
# "final_label": final_label,
|
| 353 |
-
# "vote_count": count,
|
| 354 |
-
# "total_segments": len(predictions),
|
| 355 |
-
# "confidence": sum(p["confidence"] for p in predictions) / len(predictions),
|
| 356 |
-
# "embedding": {
|
| 357 |
-
# "values": best_embedding["embedding"],
|
| 358 |
-
# "source_file": best_embedding["filename"],
|
| 359 |
-
# "attention_score": best_embedding["attention_score"]
|
| 360 |
-
# },
|
| 361 |
-
# "segment_predictions": predictions
|
| 362 |
-
# }
|
| 363 |
-
|
| 364 |
-
# # === 寫入本地 embeddings_log.jsonl ===
|
| 365 |
-
# log_path = os.path.join("results", "embeddings_log.jsonl")
|
| 366 |
-
# with open(log_path, "a") as f:
|
| 367 |
-
# for rec in all_embeddings:
|
| 368 |
-
# f.write(json.dumps({
|
| 369 |
-
# "label": rec["predicted_label"],
|
| 370 |
-
# "text": rec["text"],
|
| 371 |
-
# "embedding": rec["embedding"],
|
| 372 |
-
# "source": rec["filename"],
|
| 373 |
-
# "segment_index": rec["segment_index"]
|
| 374 |
-
# }) + "\n")
|
| 375 |
-
|
| 376 |
-
# print(f"✅ Logged {len(all_embeddings)} embeddings locally.")
|
| 377 |
-
|
| 378 |
-
# # === 合併上傳到 Hugging Face Dataset ===
|
| 379 |
-
# repo_id = "lyi029/malvec-embeddings"
|
| 380 |
-
# api = HfApi()
|
| 381 |
-
|
| 382 |
-
# try:
|
| 383 |
-
# # 下載雲端舊資料
|
| 384 |
-
# old_data = []
|
| 385 |
-
# try:
|
| 386 |
-
# old_path = hf_hub_download(
|
| 387 |
-
# repo_id=repo_id,
|
| 388 |
-
# filename="embeddings_log.jsonl",
|
| 389 |
-
# repo_type="dataset",
|
| 390 |
-
# token=HF_TOKEN
|
| 391 |
-
# )
|
| 392 |
-
# with open(old_path, "r") as f:
|
| 393 |
-
# old_data = [json.loads(l) for l in f if l.strip()]
|
| 394 |
-
# except Exception:
|
| 395 |
-
# print("ℹ️ No previous embeddings found in dataset.")
|
| 396 |
-
|
| 397 |
-
# # 新增本地新資料
|
| 398 |
-
# with open(log_path, "r") as f:
|
| 399 |
-
# new_data = [json.loads(l) for l in f if l.strip()]
|
| 400 |
-
|
| 401 |
-
# # 合併去重(依 source + segment_index 為唯一鍵)
|
| 402 |
-
# merged_dict = {(d["source"], d.get("segment_index", 0)): d for d in (old_data + new_data)}
|
| 403 |
-
# merged = list(merged_dict.values())
|
| 404 |
-
|
| 405 |
-
# # 寫回暫存檔
|
| 406 |
-
# with open(log_path, "w") as f:
|
| 407 |
-
# for item in merged:
|
| 408 |
-
# f.write(json.dumps(item) + "\n")
|
| 409 |
-
|
| 410 |
-
# # 上傳 dataset
|
| 411 |
-
# api.upload_file(
|
| 412 |
-
# path_or_fileobj=log_path,
|
| 413 |
-
# path_in_repo="embeddings_log.jsonl",
|
| 414 |
-
# repo_id=repo_id,
|
| 415 |
-
# repo_type="dataset",
|
| 416 |
-
# token=HF_TOKEN,
|
| 417 |
-
# )
|
| 418 |
-
# print(f"☁️ Uploaded merged {len(merged)} embeddings to dataset: {repo_id}")
|
| 419 |
-
|
| 420 |
-
# except Exception as e:
|
| 421 |
-
# print(f"⚠️ Dataset upload failed: {e}")
|
| 422 |
-
|
| 423 |
-
# print(f"✅ Prediction complete ({final_label})")
|
| 424 |
-
# return result
|
| 425 |
-
|
| 426 |
-
# except Exception as e:
|
| 427 |
-
# raise HTTPException(status_code=500, detail=f"Prediction failed: {e}")
|
| 428 |
-
# @app.post("/predict")
|
| 429 |
-
# async def predict_malware(files: List[UploadFile] = File(...)):
|
| 430 |
-
# """
|
| 431 |
-
# 批次預測惡意軟體家族(維持原回傳格式)
|
| 432 |
-
# 並自動寫入 embeddings_log.jsonl、上傳到 Hugging Face Dataset
|
| 433 |
-
# """
|
| 434 |
-
# if not files:
|
| 435 |
-
# raise HTTPException(status_code=400, detail="No files provided")
|
| 436 |
-
|
| 437 |
-
# print(f"\n{'='*60}")
|
| 438 |
-
# print(f"📥 Received {len(files)} files")
|
| 439 |
-
# print(f"{'='*60}")
|
| 440 |
-
|
| 441 |
-
# predictions = []
|
| 442 |
-
# all_embeddings = []
|
| 443 |
-
|
| 444 |
-
# try:
|
| 445 |
-
# with torch.no_grad():
|
| 446 |
-
# for idx, file in enumerate(files, 1):
|
| 447 |
-
# try:
|
| 448 |
-
# content = await file.read()
|
| 449 |
-
# asm_code = content.decode('utf-8').strip()
|
| 450 |
-
|
| 451 |
-
# if not asm_code:
|
| 452 |
-
# print(f"⚠️ Skip empty file: {file.filename}")
|
| 453 |
-
# continue
|
| 454 |
-
|
| 455 |
-
# print(f"📄 [{idx}/{len(files)}] Processing: {file.filename}")
|
| 456 |
-
|
| 457 |
-
# # === Tokenize ===
|
| 458 |
-
# inputs = tokenizer(
|
| 459 |
-
# asm_code,
|
| 460 |
-
# return_tensors="pt",
|
| 461 |
-
# max_length=512,
|
| 462 |
-
# truncation=True,
|
| 463 |
-
# padding=True
|
| 464 |
-
# )
|
| 465 |
-
# inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 466 |
-
|
| 467 |
-
# # === Forward ===
|
| 468 |
-
# outputs = model(**inputs)
|
| 469 |
-
# logits = outputs.logits
|
| 470 |
-
# probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 471 |
-
# confidence, predicted_class = torch.max(probs, dim=-1)
|
| 472 |
-
|
| 473 |
-
# predicted_id = predicted_class.item()
|
| 474 |
-
# label_name = (
|
| 475 |
-
# model.config.id2label[predicted_id]
|
| 476 |
-
# if hasattr(model.config, "id2label") and predicted_id in model.config.id2label
|
| 477 |
-
# else f"class_{predicted_id}"
|
| 478 |
-
# )
|
| 479 |
-
|
| 480 |
-
# print(f" 🔍 Predicted ID: {predicted_id}, Label: {label_name}")
|
| 481 |
-
|
| 482 |
-
# # === Embedding ===
|
| 483 |
-
# if hasattr(outputs, "hidden_states") and outputs.hidden_states is not None:
|
| 484 |
-
# last_hidden = outputs.hidden_states[-1]
|
| 485 |
-
# cls_embedding = last_hidden[:, 0, :].cpu().numpy()[0]
|
| 486 |
-
# else:
|
| 487 |
-
# cls_embedding = logits.cpu().numpy()[0]
|
| 488 |
-
|
| 489 |
-
# # === Attention ===
|
| 490 |
-
# max_attention_score = 0.0
|
| 491 |
-
# if hasattr(outputs, "attentions") and outputs.attentions is not None:
|
| 492 |
-
# last_attention = outputs.attentions[-1]
|
| 493 |
-
# avg_attention = last_attention.mean(dim=1)
|
| 494 |
-
# cls_attention = avg_attention[0, 0, :].cpu().numpy()
|
| 495 |
-
# max_attention_score = float(cls_attention.max())
|
| 496 |
-
|
| 497 |
-
# # === Collect Results ===
|
| 498 |
-
# predictions.append({
|
| 499 |
-
# "filename": file.filename,
|
| 500 |
-
# "predicted_label": label_name,
|
| 501 |
-
# "confidence": float(confidence.item()),
|
| 502 |
-
# "attention_score": max_attention_score
|
| 503 |
-
# })
|
| 504 |
-
|
| 505 |
-
# all_embeddings.append({
|
| 506 |
-
# "source": file.filename,
|
| 507 |
-
# "label": label_name,
|
| 508 |
-
# "embedding": cls_embedding.tolist(),
|
| 509 |
-
# "attention_score": max_attention_score,
|
| 510 |
-
# "text_excerpt": asm_code # 方便後續分析
|
| 511 |
-
# })
|
| 512 |
-
|
| 513 |
-
# print(f" ✅ Predicted: {label_name} (confidence: {confidence.item():.3f})")
|
| 514 |
-
|
| 515 |
-
# except Exception as e:
|
| 516 |
-
# print(f" ❌ Error processing {file.filename}: {e}")
|
| 517 |
-
# continue
|
| 518 |
-
|
| 519 |
-
# if not predictions:
|
| 520 |
-
# raise HTTPException(status_code=400, detail="No valid files processed")
|
| 521 |
-
|
| 522 |
-
# # === 多數投票 ===
|
| 523 |
-
# label_votes = [p["predicted_label"] for p in predictions]
|
| 524 |
-
# vote_counts = Counter(label_votes)
|
| 525 |
-
# final_label = vote_counts.most_common(1)[0][0]
|
| 526 |
-
# final_count = vote_counts[final_label]
|
| 527 |
-
|
| 528 |
-
# print(f"\n📊 Vote distribution: {dict(vote_counts)}")
|
| 529 |
-
# print(f"🏆 Final prediction: {final_label} ({final_count}/{len(predictions)} votes)")
|
| 530 |
-
|
| 531 |
-
# # === 最高注意力權重的 embedding ===
|
| 532 |
-
# same_label_embeddings = [emb for emb in all_embeddings if emb["label"] == final_label]
|
| 533 |
-
# if same_label_embeddings:
|
| 534 |
-
# best_embedding = max(same_label_embeddings, key=lambda x: x["attention_score"])
|
| 535 |
-
# else:
|
| 536 |
-
# best_embedding = max(all_embeddings, key=lambda x: x["attention_score"])
|
| 537 |
-
|
| 538 |
-
# print(f"🎯 Selected embedding from: {best_embedding['filename']}")
|
| 539 |
-
# print(f" Attention score: {best_embedding['attention_score']:.4f}")
|
| 540 |
-
|
| 541 |
-
# # === 回傳結果 ===
|
| 542 |
-
# result = {
|
| 543 |
-
# "final_label": final_label,
|
| 544 |
-
# "vote_count": final_count,
|
| 545 |
-
# "total_segments": len(predictions),
|
| 546 |
-
# "confidence": sum(p["confidence"] for p in predictions) / len(predictions),
|
| 547 |
-
# "vote_distribution": dict(vote_counts),
|
| 548 |
-
# "embedding": {
|
| 549 |
-
# "values": best_embedding["embedding"],
|
| 550 |
-
# "source_file": best_embedding["filename"],
|
| 551 |
-
# "attention_score": best_embedding["attention_score"],
|
| 552 |
-
# "dimension": len(best_embedding["embedding"])
|
| 553 |
-
# },
|
| 554 |
-
# "segment_predictions": predictions
|
| 555 |
-
# }
|
| 556 |
-
|
| 557 |
-
# print(f"\n✅ Prediction complete!\n")
|
| 558 |
-
|
| 559 |
-
# # === 寫入本地 embeddings_log.jsonl ===
|
| 560 |
-
# os.makedirs("results", exist_ok=True)
|
| 561 |
-
# log_path = os.path.join("results", "embeddings_log.jsonl")
|
| 562 |
-
# with open(log_path, "a") as f:
|
| 563 |
-
# for emb in all_embeddings:
|
| 564 |
-
# f.write(json.dumps(emb) + "\n")
|
| 565 |
-
# print(f"📝 Logged {len(all_embeddings)} embeddings locally.")
|
| 566 |
-
|
| 567 |
-
# # === 上傳到 Hugging Face Dataset ===
|
| 568 |
-
# repo_id = "lyi029/malvec-embeddings"
|
| 569 |
-
# api = HfApi()
|
| 570 |
-
|
| 571 |
-
# try:
|
| 572 |
-
# old_data = []
|
| 573 |
-
# try:
|
| 574 |
-
# old_path = hf_hub_download(
|
| 575 |
-
# repo_id=repo_id,
|
| 576 |
-
# filename="embeddings_log.jsonl",
|
| 577 |
-
# repo_type="dataset",
|
| 578 |
-
# token=HF_TOKEN
|
| 579 |
-
# )
|
| 580 |
-
# with open(old_path, "r") as f:
|
| 581 |
-
# old_data = [json.loads(l) for l in f if l.strip()]
|
| 582 |
-
# except Exception:
|
| 583 |
-
# print("ℹ️ No previous embeddings found in dataset.")
|
| 584 |
-
|
| 585 |
-
# # 合併去重
|
| 586 |
-
# with open(log_path, "r") as f:
|
| 587 |
-
# new_data = [json.loads(l) for l in f if l.strip()]
|
| 588 |
-
|
| 589 |
-
# merged_dict = {d["filename"]: d for d in (old_data + new_data)}
|
| 590 |
-
# merged = list(merged_dict.values())
|
| 591 |
-
|
| 592 |
-
# with open(log_path, "w") as f:
|
| 593 |
-
# for item in merged:
|
| 594 |
-
# f.write(json.dumps(item) + "\n")
|
| 595 |
-
|
| 596 |
-
# api.upload_file(
|
| 597 |
-
# path_or_fileobj=log_path,
|
| 598 |
-
# path_in_repo="embeddings_log.jsonl",
|
| 599 |
-
# repo_id=repo_id,
|
| 600 |
-
# repo_type="dataset",
|
| 601 |
-
# token=HF_TOKEN,
|
| 602 |
-
# )
|
| 603 |
-
# print(f"☁️ Uploaded {len(merged)} merged embeddings to dataset: {repo_id}")
|
| 604 |
-
# except Exception as e:
|
| 605 |
-
# print(f"⚠️ Dataset upload failed: {e}")
|
| 606 |
-
|
| 607 |
-
# return result
|
| 608 |
-
|
| 609 |
-
# except Exception as e:
|
| 610 |
-
# print(f"\n❌ Error in predict_malware: {e}")
|
| 611 |
-
# import traceback
|
| 612 |
-
# traceback.print_exc()
|
| 613 |
-
# raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}")
|
| 614 |
-
|
| 615 |
@app.post("/predict")
|
| 616 |
async def predict_malware(files: List[UploadFile] = File(...)):
|
| 617 |
"""
|
|
@@ -744,25 +286,7 @@ async def predict_malware(files: List[UploadFile] = File(...)):
|
|
| 744 |
"segment_predictions": predictions
|
| 745 |
}
|
| 746 |
|
| 747 |
-
print(f"\n✅ Prediction complete!\n")
|
| 748 |
-
|
| 749 |
-
# === 回傳結果 ===
|
| 750 |
-
# result = {
|
| 751 |
-
# "final_label": final_label,
|
| 752 |
-
# "vote_count": final_count,
|
| 753 |
-
# "total_segments": len(predictions),
|
| 754 |
-
# "confidence": sum(p["confidence"] for p in predictions) / len(predictions),
|
| 755 |
-
# "vote_distribution": dict(vote_counts),
|
| 756 |
-
# "embedding": {
|
| 757 |
-
# "values": best_embedding["embedding"],
|
| 758 |
-
# "source_file": best_embedding["filename"],
|
| 759 |
-
# "attention_score": best_embedding["attention_score"],
|
| 760 |
-
# "dimension": len(best_embedding["embedding"])
|
| 761 |
-
# },
|
| 762 |
-
# "segment_predictions": predictions
|
| 763 |
-
# }
|
| 764 |
-
|
| 765 |
-
# print(f"\n✅ Prediction complete!\n")
|
| 766 |
|
| 767 |
|
| 768 |
# === 寫入本地 embeddings_log.jsonl ===
|
|
@@ -819,28 +343,106 @@ async def predict_malware(files: List[UploadFile] = File(...)):
|
|
| 819 |
import traceback
|
| 820 |
traceback.print_exc()
|
| 821 |
raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 822 |
|
| 823 |
|
| 824 |
-
# ----------------------------------------------------------
|
| 825 |
-
# 手動觸發 Space B 訓練
|
| 826 |
-
# ----------------------------------------------------------
|
| 827 |
-
# @app.post("/trigger-train")
|
| 828 |
-
# async def trigger_train(background_tasks: BackgroundTasks):
|
| 829 |
-
# """手動觸發 Space B 進行 LoRA 訓練"""
|
| 830 |
-
# if not SPACE_B_URL:
|
| 831 |
-
# raise HTTPException(status_code=400, detail="SPACE_B_URL not set")
|
| 832 |
-
|
| 833 |
-
# async def call_space_b():
|
| 834 |
-
# print(f"🚀 Triggering LoRA training at {SPACE_B_URL}/train-lora ...")
|
| 835 |
-
# try:
|
| 836 |
-
# async with httpx.AsyncClient(timeout=600.0) as client:
|
| 837 |
-
# resp = await client.post(f"{SPACE_B_URL}/train-lora")
|
| 838 |
-
# print(f"🔁 Space B responded: {resp.status_code}")
|
| 839 |
-
# except Exception as e:
|
| 840 |
-
# print(f"❌ Failed to contact Space B:", e)
|
| 841 |
-
|
| 842 |
-
# background_tasks.add_task(call_space_b)
|
| 843 |
-
# return {"status": "ok", "detail": f"Training triggered at {SPACE_B_URL}"}
|
| 844 |
|
| 845 |
@app.post("/check-new-samples")
|
| 846 |
async def check_new_samples(background_tasks: BackgroundTasks):
|
|
|
|
| 10 |
from fastapi.staticfiles import StaticFiles
|
| 11 |
from huggingface_hub import HfApi,hf_hub_download
|
| 12 |
import datetime
|
| 13 |
+
import csv, sys
|
| 14 |
+
import io
|
| 15 |
+
import numpy as np
|
| 16 |
+
|
| 17 |
|
| 18 |
CURRENT_TAG = os.getenv("MODEL_TAG", "main")
|
| 19 |
# ====== FastAPI 初始化 ======
|
|
|
|
| 107 |
}
|
| 108 |
|
| 109 |
|
| 110 |
+
HF_DATASET_REPO = "lyi029/malvec-embeddings-new"
|
| 111 |
|
| 112 |
def upload_embeddings_to_hub(local_path: str):
|
| 113 |
"""自動建立 Dataset 並分批上傳 embeddings_log.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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
@app.post("/predict")
|
| 158 |
async def predict_malware(files: List[UploadFile] = File(...)):
|
| 159 |
"""
|
|
|
|
| 286 |
"segment_predictions": predictions
|
| 287 |
}
|
| 288 |
|
| 289 |
+
print(f"\n✅ Prediction complete!\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
|
| 292 |
# === 寫入本地 embeddings_log.jsonl ===
|
|
|
|
| 343 |
import traceback
|
| 344 |
traceback.print_exc()
|
| 345 |
raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}")
|
| 346 |
+
|
| 347 |
+
csv.field_size_limit(sys.maxsize)
|
| 348 |
+
|
| 349 |
+
JUMP_MNEMONICS = {"jmp", "je", "jne", "jl", "jg", "jle", "jge", "call", "ret"}
|
| 350 |
+
|
| 351 |
+
def _split_into_basic_blocks(tokens):
|
| 352 |
+
"""把指令 tokens 拆成 basic blocks(遇到跳轉/ret 就斷)"""
|
| 353 |
+
blocks, cur = [], []
|
| 354 |
+
for tok in tokens:
|
| 355 |
+
cur.append(tok)
|
| 356 |
+
if tok.lower() in JUMP_MNEMONICS:
|
| 357 |
+
blocks.append(cur)
|
| 358 |
+
cur = []
|
| 359 |
+
if cur:
|
| 360 |
+
blocks.append(cur)
|
| 361 |
+
return blocks
|
| 362 |
+
|
| 363 |
+
def _block_attention_score(tokens, chunk_size=512):
|
| 364 |
+
"""
|
| 365 |
+
對單一 block 計算注意力:
|
| 366 |
+
- 以 512 token 為單位分塊
|
| 367 |
+
- 取最後一層 attention → head 平均 → 整體平均
|
| 368 |
+
- 多個 chunk 取平均
|
| 369 |
+
"""
|
| 370 |
+
scores = []
|
| 371 |
+
for i in range(0, len(tokens), chunk_size):
|
| 372 |
+
chunk = tokens[i:i+chunk_size]
|
| 373 |
+
inputs = tokenizer(
|
| 374 |
+
chunk,
|
| 375 |
+
return_tensors="pt",
|
| 376 |
+
is_split_into_words=True,
|
| 377 |
+
truncation=True,
|
| 378 |
+
max_length=chunk_size,
|
| 379 |
+
padding=False
|
| 380 |
+
)
|
| 381 |
+
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 382 |
+
with torch.no_grad():
|
| 383 |
+
out = model(**inputs, output_attentions=True)
|
| 384 |
+
last_attn = out.attentions[-1] # (1, heads, seq, seq)
|
| 385 |
+
mean_attn = last_attn.mean(dim=1).mean() # scalar
|
| 386 |
+
scores.append(mean_attn.item())
|
| 387 |
+
return float(np.mean(scores)) if scores else 0.0
|
| 388 |
+
|
| 389 |
+
@app.post("/attention-heatmap")
|
| 390 |
+
async def attention_heatmap(file: UploadFile = File(...)):
|
| 391 |
+
"""
|
| 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:
|
| 400 |
+
# === 讀檔 ===
|
| 401 |
+
content = await file.read()
|
| 402 |
+
text = content.decode("utf-8", errors="ignore")
|
| 403 |
+
|
| 404 |
+
# === 以 </s> 分隔整個 disasm ===
|
| 405 |
+
lines = text.replace("\n", " ").split("</s>")
|
| 406 |
+
tokens = []
|
| 407 |
+
for line in lines:
|
| 408 |
+
toks = [t.strip() for t in line.split() if t.strip()]
|
| 409 |
+
if toks:
|
| 410 |
+
tokens.extend(toks)
|
| 411 |
+
|
| 412 |
+
print(f"📦 Total tokens parsed: {len(tokens)}")
|
| 413 |
+
|
| 414 |
+
# === 拆 basic blocks ===
|
| 415 |
+
blocks = _split_into_basic_blocks(tokens)
|
| 416 |
+
print(f"📊 Total blocks before truncation: {len(blocks)}")
|
| 417 |
+
|
| 418 |
+
# === 只取前 1000 個 block
|
| 419 |
+
MAX_BLOCKS = int(os.getenv("MAX_HEATMAP_BLOCKS", "1000"))
|
| 420 |
+
blocks = blocks[:MAX_BLOCKS]
|
| 421 |
+
|
| 422 |
+
# === 計算每個 block 的 attention
|
| 423 |
+
results = []
|
| 424 |
+
for i, blk in enumerate(blocks):
|
| 425 |
+
score = _block_attention_score(blk)
|
| 426 |
+
results.append({
|
| 427 |
+
"idx": i,
|
| 428 |
+
"size": len(blk),
|
| 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 |
+
|
| 444 |
|
| 445 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
@app.post("/check-new-samples")
|
| 448 |
async def check_new_samples(background_tasks: BackgroundTasks):
|