updates
Browse files
app.py
CHANGED
|
@@ -257,130 +257,318 @@ def upload_embeddings_to_hub(local_path: str):
|
|
| 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 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 263 |
if not files:
|
| 264 |
raise HTTPException(status_code=400, detail="No files provided")
|
| 265 |
|
| 266 |
-
|
| 267 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
|
|
|
| 275 |
if not asm_code:
|
|
|
|
| 276 |
continue
|
| 277 |
|
| 278 |
-
print(f"📄 [{idx}/{len(files)}] {file.filename}")
|
| 279 |
-
|
| 280 |
-
# ===
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
except Exception as e:
|
| 331 |
-
print(f"❌ {file.filename}
|
|
|
|
| 332 |
|
| 333 |
if not predictions:
|
| 334 |
raise HTTPException(status_code=400, detail="No valid files processed")
|
| 335 |
|
| 336 |
# === 多數投票 ===
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
#
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
| 348 |
|
| 349 |
-
|
|
|
|
| 350 |
|
|
|
|
| 351 |
result = {
|
| 352 |
"final_label": final_label,
|
| 353 |
-
"vote_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
|
| 368 |
-
f.write(json.dumps(
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 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(
|
|
@@ -394,20 +582,17 @@ async def predict_malware(files: List[UploadFile] = File(...)):
|
|
| 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 |
-
|
| 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",
|
|
@@ -415,36 +600,61 @@ async def predict_malware(files: List[UploadFile] = File(...)):
|
|
| 415 |
repo_type="dataset",
|
| 416 |
token=HF_TOKEN,
|
| 417 |
)
|
| 418 |
-
print(f"☁️ Uploaded
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
# ----------------------------------------------------------
|
| 429 |
# 手動觸發 Space B 訓練
|
| 430 |
# ----------------------------------------------------------
|
| 431 |
-
@app.post("/trigger-train")
|
| 432 |
-
async def trigger_train(background_tasks: BackgroundTasks):
|
| 433 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
if not SPACE_B_URL:
|
| 435 |
raise HTTPException(status_code=400, detail="SPACE_B_URL not set")
|
| 436 |
|
| 437 |
async def call_space_b():
|
| 438 |
-
print(f"🚀 Triggering LoRA training at {SPACE_B_URL}/train-lora ...")
|
| 439 |
try:
|
| 440 |
async with httpx.AsyncClient(timeout=600.0) as client:
|
| 441 |
-
|
| 442 |
-
|
|
|
|
| 443 |
except Exception as e:
|
| 444 |
-
print(
|
| 445 |
|
| 446 |
background_tasks.add_task(call_space_b)
|
| 447 |
-
return {
|
|
|
|
|
|
|
|
|
|
| 448 |
|
| 449 |
def _reload_model_by_tag(tag: str):
|
| 450 |
global tokenizer, model, CURRENT_TAG
|
|
|
|
| 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 |
+
"filename": file.filename,
|
| 507 |
+
"label": label_name,
|
| 508 |
+
"embedding": cls_embedding.tolist(),
|
| 509 |
+
"attention_score": max_attention_score,
|
| 510 |
+
"text_excerpt": asm_code[:2000] # 方便後續分析
|
| 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(
|
|
|
|
| 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",
|
|
|
|
| 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 |
# ----------------------------------------------------------
|
| 616 |
# 手動觸發 Space B 訓練
|
| 617 |
# ----------------------------------------------------------
|
| 618 |
+
# @app.post("/trigger-train")
|
| 619 |
+
# async def trigger_train(background_tasks: BackgroundTasks):
|
| 620 |
+
# """手動觸發 Space B 進行 LoRA 訓練"""
|
| 621 |
+
# if not SPACE_B_URL:
|
| 622 |
+
# raise HTTPException(status_code=400, detail="SPACE_B_URL not set")
|
| 623 |
+
|
| 624 |
+
# async def call_space_b():
|
| 625 |
+
# print(f"🚀 Triggering LoRA training at {SPACE_B_URL}/train-lora ...")
|
| 626 |
+
# try:
|
| 627 |
+
# async with httpx.AsyncClient(timeout=600.0) as client:
|
| 628 |
+
# resp = await client.post(f"{SPACE_B_URL}/train-lora")
|
| 629 |
+
# print(f"🔁 Space B responded: {resp.status_code}")
|
| 630 |
+
# except Exception as e:
|
| 631 |
+
# print(f"❌ Failed to contact Space B:", e)
|
| 632 |
+
|
| 633 |
+
# background_tasks.add_task(call_space_b)
|
| 634 |
+
# return {"status": "ok", "detail": f"Training triggered at {SPACE_B_URL}"}
|
| 635 |
+
|
| 636 |
+
@app.post("/check-new-samples")
|
| 637 |
+
async def check_new_samples(background_tasks: BackgroundTasks):
|
| 638 |
+
"""
|
| 639 |
+
手動觸發 Space B 的 Active Learning 探測與 LoRA 訓練流程
|
| 640 |
+
"""
|
| 641 |
if not SPACE_B_URL:
|
| 642 |
raise HTTPException(status_code=400, detail="SPACE_B_URL not set")
|
| 643 |
|
| 644 |
async def call_space_b():
|
|
|
|
| 645 |
try:
|
| 646 |
async with httpx.AsyncClient(timeout=600.0) as client:
|
| 647 |
+
params = {"batch_size": 1000, "mmd_threshold": 0.2, "entropy_threshold": 1.1}
|
| 648 |
+
resp = await client.post(f"{SPACE_B_URL}/check-new-samples", params=params)
|
| 649 |
+
print(f"🚀 Space B /check-new-samples → HTTP {resp.status_code}")
|
| 650 |
except Exception as e:
|
| 651 |
+
print("❌ Failed to contact Space B:", e)
|
| 652 |
|
| 653 |
background_tasks.add_task(call_space_b)
|
| 654 |
+
return {
|
| 655 |
+
"status": "ok",
|
| 656 |
+
"message": f"Triggered Space B check-new-samples at {SPACE_B_URL}"
|
| 657 |
+
}
|
| 658 |
|
| 659 |
def _reload_model_by_tag(tag: str):
|
| 660 |
global tokenizer, model, CURRENT_TAG
|