Spaces:
Runtime error
Runtime error
Download app.py from malvec/codebert_Malvec: direct link, hf CLI and curl.
- Browser
- Download file 9.01 kB
-
https://huggingface.co/spaces/malvec/codebert_Malvec/resolve/78ac2e49b9b806964df7a806970b80caf9d19791/app.py
- Command line
-
hf download hf://spaces/malvec/codebert_Malvec@78ac2e49b9b806964df7a806970b80caf9d19791/app.py
-
curl -L -o app.py https://huggingface.co/spaces/malvec/codebert_Malvec/resolve/78ac2e49b9b806964df7a806970b80caf9d19791/app.py
9.01 kB
| import os | |
| CACHE_DIR = "/tmp/hf_cache" | |
| os.environ["HF_HOME"] = CACHE_DIR | |
| os.environ["TRANSFORMERS_CACHE"] = CACHE_DIR | |
| os.environ["HUGGINGFACE_HUB_CACHE"] = CACHE_DIR | |
| os.makedirs(CACHE_DIR, exist_ok=True) | |
| import torch | |
| from fastapi import FastAPI, UploadFile, File, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| from typing import List | |
| from collections import Counter | |
| import json | |
| app = FastAPI() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # ===== 模型設定 ===== | |
| MODEL_REPO = "raxhel/Malvec_predict" | |
| HF_TOKEN = os.environ.get("hf_token") | |
| DEVICE = torch.device("cpu") | |
| # ===== 載入模型 ===== | |
| print("🤗 Loading model...") | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO, token=HF_TOKEN) | |
| model = AutoModelForSequenceClassification.from_pretrained( | |
| MODEL_REPO, | |
| token=HF_TOKEN, | |
| output_attentions=True, # ✅ 輸出注意力權重 | |
| output_hidden_states=True # ✅ 關鍵: 輸出 hidden states | |
| ) | |
| model.to(DEVICE) | |
| model.eval() | |
| print("✅ Model loaded successfully") | |
| def home(): | |
| return { | |
| "message": "Malware Classifier API", | |
| "model": MODEL_REPO, | |
| "endpoints": { | |
| "/predict": "POST - Upload TXT files for prediction", | |
| "/model-info": "GET - Get model information" | |
| } | |
| } | |
| def get_model_info(): | |
| """取得模型資訊""" | |
| return { | |
| "model_name": MODEL_REPO, | |
| "num_labels": model.config.num_labels, | |
| "labels": list(model.config.id2label.values()) if hasattr(model.config, 'id2label') else None, | |
| "device": str(DEVICE), | |
| "max_length": 512 | |
| } | |
| async def predict_malware(files: List[UploadFile] = File(...)): | |
| """ | |
| 批次預測惡意軟體家族 | |
| 接收多個 TXT 檔案 (來自同一個執行檔的分段) | |
| 返回: | |
| - final_label: 多數投票的家族標籤 | |
| - embeddings: 最高注意力權重的 embedding | |
| """ | |
| if not files: | |
| raise HTTPException(status_code=400, detail="No files provided") | |
| print(f"\n{'='*60}") | |
| print(f"📥 Received {len(files)} files") | |
| print(f"{'='*60}") | |
| predictions = [] | |
| all_embeddings = [] | |
| try: | |
| with torch.no_grad(): | |
| for idx, file in enumerate(files, 1): | |
| try: | |
| # 讀取檔案內容 | |
| content = await file.read() | |
| asm_code = content.decode('utf-8').strip() | |
| if not asm_code: | |
| print(f"⚠️ Skip empty file: {file.filename}") | |
| continue | |
| print(f"📄 [{idx}/{len(files)}] Processing: {file.filename}") | |
| # Tokenize | |
| inputs = tokenizer( | |
| asm_code, | |
| return_tensors="pt", | |
| max_length=512, | |
| truncation=True, | |
| padding=True | |
| ) | |
| inputs = {k: v.to(DEVICE) for k, v in inputs.items()} | |
| # 模型預測 | |
| outputs = model(**inputs) | |
| # 取得預測結果 | |
| logits = outputs.logits | |
| probs = torch.nn.functional.softmax(logits, dim=-1) | |
| confidence, predicted_class = torch.max(probs, dim=-1) | |
| # 取得 label | |
| if hasattr(model.config, 'id2label'): | |
| label_name = model.config.id2label[str(predicted_class.item())] | |
| else: | |
| label_name = f"class_{predicted_class.item()}" | |
| # ✅ 取得 embedding (使用 hidden_states) | |
| if hasattr(outputs, 'hidden_states') and outputs.hidden_states is not None: | |
| # 取最後一層的 [CLS] token embedding | |
| last_hidden = outputs.hidden_states[-1] # (batch, seq_len, hidden_size) | |
| cls_embedding = last_hidden[:, 0, :].cpu().numpy()[0] # (768,) | |
| else: | |
| # 備用方案: 使用 logits | |
| cls_embedding = logits.cpu().numpy()[0] | |
| # ✅ 取得注意力權重 | |
| max_attention_score = 0.0 | |
| if hasattr(outputs, 'attentions') and outputs.attentions is not None: | |
| # 取最後一層的注意力,平均所有 head | |
| last_attention = outputs.attentions[-1] # (1, num_heads, seq_len, seq_len) | |
| avg_attention = last_attention.mean(dim=1) # (1, seq_len, seq_len) | |
| # 取 [CLS] token 對所有 token 的注意力 | |
| cls_attention = avg_attention[0, 0, :].cpu().numpy() # (seq_len,) | |
| max_attention_score = float(cls_attention.max()) | |
| # 儲存結果 | |
| predictions.append({ | |
| "filename": file.filename, | |
| "predicted_label": label_name, | |
| "confidence": float(confidence.item()), | |
| "attention_score": max_attention_score | |
| }) | |
| all_embeddings.append({ | |
| "filename": file.filename, | |
| "label": label_name, | |
| "embedding": cls_embedding.tolist(), | |
| "attention_score": max_attention_score | |
| }) | |
| print(f" ✅ Predicted: {label_name} (confidence: {confidence.item():.3f})") | |
| except Exception as e: | |
| print(f" ❌ Error processing {file.filename}: {e}") | |
| continue | |
| if not predictions: | |
| raise HTTPException(status_code=400, detail="No valid files processed") | |
| # ===== 多數投票 (Majority Voting) ===== | |
| label_votes = [p["predicted_label"] for p in predictions] | |
| vote_counts = Counter(label_votes) | |
| final_label = vote_counts.most_common(1)[0][0] | |
| final_count = vote_counts[final_label] | |
| print(f"\n📊 Vote distribution: {dict(vote_counts)}") | |
| print(f"🏆 Final prediction: {final_label} ({final_count}/{len(predictions)} votes)") | |
| # ===== 選擇最高注意力權重的 embedding (屬於最終預測的 label) ===== | |
| # 過濾出預測為 final_label 的 embeddings | |
| same_label_embeddings = [ | |
| emb for emb in all_embeddings | |
| if emb["label"] == final_label | |
| ] | |
| if same_label_embeddings: | |
| # 選擇注意力權重最高的 | |
| best_embedding = max(same_label_embeddings, key=lambda x: x["attention_score"]) | |
| else: | |
| # 如果沒有,就選所有裡面注意力最高的 | |
| best_embedding = max(all_embeddings, key=lambda x: x["attention_score"]) | |
| print(f"🎯 Selected embedding from: {best_embedding['filename']}") | |
| print(f" Attention score: {best_embedding['attention_score']:.4f}") | |
| # ===== 返回結果 ===== | |
| result = { | |
| "final_label": final_label, | |
| "vote_count": final_count, | |
| "total_segments": len(predictions), | |
| "confidence": sum(p["confidence"] for p in predictions) / len(predictions), | |
| "vote_distribution": dict(vote_counts), | |
| "embedding": { | |
| "values": best_embedding["embedding"], | |
| "source_file": best_embedding["filename"], | |
| "attention_score": best_embedding["attention_score"], | |
| "dimension": len(best_embedding["embedding"]) | |
| }, | |
| "segment_predictions": predictions | |
| } | |
| print(f"\n✅ Prediction complete!\n") | |
| return result | |
| except Exception as e: | |
| print(f"\n❌ Error in predict_malware: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}") | |
| async def predict_batch(files: List[UploadFile] = File(...)): | |
| """ | |
| 簡化版: 只返回 label 和 embedding,不包含詳細資訊 | |
| """ | |
| result = await predict_malware(files) | |
| return { | |
| "final_label": result["final_label"], | |
| "embedding": result["embedding"]["values"] | |
| } | |
| # ✅ HF Space 啟動入口 | |
| if __name__ == "__main__": | |
| import uvicorn | |
| port = int(os.environ.get("PORT", 7860)) | |
| uvicorn.run(app, host="0.0.0.0", port=port) |