Spaces:
Runtime error
Runtime error
111306047 commited on
Commit ·
78ac2e4
1
Parent(s): 9e085a0
fix model
Browse files
app.py
CHANGED
|
@@ -32,7 +32,8 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO, token=HF_TOKEN)
|
|
| 32 |
model = AutoModelForSequenceClassification.from_pretrained(
|
| 33 |
MODEL_REPO,
|
| 34 |
token=HF_TOKEN,
|
| 35 |
-
output_attentions=True #
|
|
|
|
| 36 |
)
|
| 37 |
model.to(DEVICE)
|
| 38 |
model.eval()
|
|
@@ -80,133 +81,140 @@ async def predict_malware(files: List[UploadFile] = File(...)):
|
|
| 80 |
|
| 81 |
predictions = []
|
| 82 |
all_embeddings = []
|
| 83 |
-
all_attentions = []
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
# ===== 返回結果 =====
|
| 192 |
-
result = {
|
| 193 |
-
"final_label": final_label,
|
| 194 |
-
"vote_count": final_count,
|
| 195 |
-
"total_segments": len(predictions),
|
| 196 |
-
"confidence": sum(p["confidence"] for p in predictions) / len(predictions),
|
| 197 |
-
"vote_distribution": dict(vote_counts),
|
| 198 |
-
"embedding": {
|
| 199 |
-
"values": best_embedding["embedding"],
|
| 200 |
-
"source_file": best_embedding["filename"],
|
| 201 |
-
"attention_score": best_embedding["attention_score"],
|
| 202 |
-
"dimension": len(best_embedding["embedding"])
|
| 203 |
-
},
|
| 204 |
-
"segment_predictions": predictions
|
| 205 |
-
}
|
| 206 |
-
|
| 207 |
-
print(f"\n✅ Prediction complete!\n")
|
| 208 |
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
@app.post("/predict-batch")
|
| 212 |
async def predict_batch(files: List[UploadFile] = File(...)):
|
|
@@ -220,6 +228,7 @@ async def predict_batch(files: List[UploadFile] = File(...)):
|
|
| 220 |
"embedding": result["embedding"]["values"]
|
| 221 |
}
|
| 222 |
|
|
|
|
| 223 |
if __name__ == "__main__":
|
| 224 |
import uvicorn
|
| 225 |
port = int(os.environ.get("PORT", 7860))
|
|
|
|
| 32 |
model = AutoModelForSequenceClassification.from_pretrained(
|
| 33 |
MODEL_REPO,
|
| 34 |
token=HF_TOKEN,
|
| 35 |
+
output_attentions=True, # ✅ 輸出注意力權重
|
| 36 |
+
output_hidden_states=True # ✅ 關鍵: 輸出 hidden states
|
| 37 |
)
|
| 38 |
model.to(DEVICE)
|
| 39 |
model.eval()
|
|
|
|
| 81 |
|
| 82 |
predictions = []
|
| 83 |
all_embeddings = []
|
|
|
|
| 84 |
|
| 85 |
+
try:
|
| 86 |
+
with torch.no_grad():
|
| 87 |
+
for idx, file in enumerate(files, 1):
|
| 88 |
+
try:
|
| 89 |
+
# 讀取檔案內容
|
| 90 |
+
content = await file.read()
|
| 91 |
+
asm_code = content.decode('utf-8').strip()
|
| 92 |
+
|
| 93 |
+
if not asm_code:
|
| 94 |
+
print(f"⚠️ Skip empty file: {file.filename}")
|
| 95 |
+
continue
|
| 96 |
+
|
| 97 |
+
print(f"📄 [{idx}/{len(files)}] Processing: {file.filename}")
|
| 98 |
+
|
| 99 |
+
# Tokenize
|
| 100 |
+
inputs = tokenizer(
|
| 101 |
+
asm_code,
|
| 102 |
+
return_tensors="pt",
|
| 103 |
+
max_length=512,
|
| 104 |
+
truncation=True,
|
| 105 |
+
padding=True
|
| 106 |
+
)
|
| 107 |
+
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 108 |
+
|
| 109 |
+
# 模型預測
|
| 110 |
+
outputs = model(**inputs)
|
| 111 |
+
|
| 112 |
+
# 取得預測結果
|
| 113 |
+
logits = outputs.logits
|
| 114 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 115 |
+
confidence, predicted_class = torch.max(probs, dim=-1)
|
| 116 |
+
|
| 117 |
+
# 取得 label
|
| 118 |
+
if hasattr(model.config, 'id2label'):
|
| 119 |
+
label_name = model.config.id2label[str(predicted_class.item())]
|
| 120 |
+
else:
|
| 121 |
+
label_name = f"class_{predicted_class.item()}"
|
| 122 |
+
|
| 123 |
+
# ✅ 取得 embedding (使用 hidden_states)
|
| 124 |
+
if hasattr(outputs, 'hidden_states') and outputs.hidden_states is not None:
|
| 125 |
+
# 取最後一層的 [CLS] token embedding
|
| 126 |
+
last_hidden = outputs.hidden_states[-1] # (batch, seq_len, hidden_size)
|
| 127 |
+
cls_embedding = last_hidden[:, 0, :].cpu().numpy()[0] # (768,)
|
| 128 |
+
else:
|
| 129 |
+
# 備用方案: 使用 logits
|
| 130 |
+
cls_embedding = logits.cpu().numpy()[0]
|
| 131 |
+
|
| 132 |
+
# ✅ 取得注意力權重
|
| 133 |
+
max_attention_score = 0.0
|
| 134 |
+
if hasattr(outputs, 'attentions') and outputs.attentions is not None:
|
| 135 |
+
# 取最後一層的注意力,平均所有 head
|
| 136 |
+
last_attention = outputs.attentions[-1] # (1, num_heads, seq_len, seq_len)
|
| 137 |
+
avg_attention = last_attention.mean(dim=1) # (1, seq_len, seq_len)
|
| 138 |
+
|
| 139 |
+
# 取 [CLS] token 對所有 token 的注意力
|
| 140 |
+
cls_attention = avg_attention[0, 0, :].cpu().numpy() # (seq_len,)
|
| 141 |
+
max_attention_score = float(cls_attention.max())
|
| 142 |
+
|
| 143 |
+
# 儲存結果
|
| 144 |
+
predictions.append({
|
| 145 |
+
"filename": file.filename,
|
| 146 |
+
"predicted_label": label_name,
|
| 147 |
+
"confidence": float(confidence.item()),
|
| 148 |
+
"attention_score": max_attention_score
|
| 149 |
+
})
|
| 150 |
+
|
| 151 |
+
all_embeddings.append({
|
| 152 |
+
"filename": file.filename,
|
| 153 |
+
"label": label_name,
|
| 154 |
+
"embedding": cls_embedding.tolist(),
|
| 155 |
+
"attention_score": max_attention_score
|
| 156 |
+
})
|
| 157 |
+
|
| 158 |
+
print(f" ✅ Predicted: {label_name} (confidence: {confidence.item():.3f})")
|
| 159 |
|
| 160 |
+
except Exception as e:
|
| 161 |
+
print(f" ❌ Error processing {file.filename}: {e}")
|
| 162 |
+
continue
|
| 163 |
+
|
| 164 |
+
if not predictions:
|
| 165 |
+
raise HTTPException(status_code=400, detail="No valid files processed")
|
| 166 |
+
|
| 167 |
+
# ===== 多數投票 (Majority Voting) =====
|
| 168 |
+
label_votes = [p["predicted_label"] for p in predictions]
|
| 169 |
+
vote_counts = Counter(label_votes)
|
| 170 |
+
final_label = vote_counts.most_common(1)[0][0]
|
| 171 |
+
final_count = vote_counts[final_label]
|
| 172 |
+
|
| 173 |
+
print(f"\n📊 Vote distribution: {dict(vote_counts)}")
|
| 174 |
+
print(f"🏆 Final prediction: {final_label} ({final_count}/{len(predictions)} votes)")
|
| 175 |
+
|
| 176 |
+
# ===== 選擇最高注意力權重的 embedding (屬於最終預測的 label) =====
|
| 177 |
+
# 過濾出預測為 final_label 的 embeddings
|
| 178 |
+
same_label_embeddings = [
|
| 179 |
+
emb for emb in all_embeddings
|
| 180 |
+
if emb["label"] == final_label
|
| 181 |
+
]
|
| 182 |
+
|
| 183 |
+
if same_label_embeddings:
|
| 184 |
+
# 選擇注意力權重最高的
|
| 185 |
+
best_embedding = max(same_label_embeddings, key=lambda x: x["attention_score"])
|
| 186 |
+
else:
|
| 187 |
+
# 如果沒有,就選所有裡面注意力最高的
|
| 188 |
+
best_embedding = max(all_embeddings, key=lambda x: x["attention_score"])
|
| 189 |
+
|
| 190 |
+
print(f"🎯 Selected embedding from: {best_embedding['filename']}")
|
| 191 |
+
print(f" Attention score: {best_embedding['attention_score']:.4f}")
|
| 192 |
+
|
| 193 |
+
# ===== 返回結果 =====
|
| 194 |
+
result = {
|
| 195 |
+
"final_label": final_label,
|
| 196 |
+
"vote_count": final_count,
|
| 197 |
+
"total_segments": len(predictions),
|
| 198 |
+
"confidence": sum(p["confidence"] for p in predictions) / len(predictions),
|
| 199 |
+
"vote_distribution": dict(vote_counts),
|
| 200 |
+
"embedding": {
|
| 201 |
+
"values": best_embedding["embedding"],
|
| 202 |
+
"source_file": best_embedding["filename"],
|
| 203 |
+
"attention_score": best_embedding["attention_score"],
|
| 204 |
+
"dimension": len(best_embedding["embedding"])
|
| 205 |
+
},
|
| 206 |
+
"segment_predictions": predictions
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
print(f"\n✅ Prediction complete!\n")
|
| 210 |
+
|
| 211 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
except Exception as e:
|
| 214 |
+
print(f"\n❌ Error in predict_malware: {e}")
|
| 215 |
+
import traceback
|
| 216 |
+
traceback.print_exc()
|
| 217 |
+
raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}")
|
| 218 |
|
| 219 |
@app.post("/predict-batch")
|
| 220 |
async def predict_batch(files: List[UploadFile] = File(...)):
|
|
|
|
| 228 |
"embedding": result["embedding"]["values"]
|
| 229 |
}
|
| 230 |
|
| 231 |
+
# ✅ HF Space 啟動入口
|
| 232 |
if __name__ == "__main__":
|
| 233 |
import uvicorn
|
| 234 |
port = int(os.environ.get("PORT", 7860))
|