111306047 commited on
Commit
78ac2e4
·
1 Parent(s): 9e085a0

fix model

Browse files
Files changed (1) hide show
  1. app.py +134 -125
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
- with torch.no_grad():
86
- for idx, file in enumerate(files, 1):
87
- # 讀取檔案內容
88
- content = await file.read()
89
- asm_code = content.decode('utf-8').strip()
90
-
91
- if not asm_code:
92
- print(f"⚠️ Skip empty file: {file.filename}")
93
- continue
94
-
95
- print(f"📄 [{idx}/{len(files)}] Processing: {file.filename}")
96
-
97
- # Tokenize
98
- inputs = tokenizer(
99
- asm_code,
100
- return_tensors="pt",
101
- max_length=512,
102
- truncation=True,
103
- padding=True
104
- )
105
- inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
106
-
107
- # 模型預測
108
- outputs = model(**inputs)
109
-
110
- # 取得預測結果
111
- logits = outputs.logits
112
- probs = torch.nn.functional.softmax(logits, dim=-1)
113
- confidence, predicted_class = torch.max(probs, dim=-1)
114
-
115
- # 取得 label
116
- if hasattr(model.config, 'id2label'):
117
- label_name = model.config.id2label[str(predicted_class.item())]
118
- else:
119
- label_name = f"class_{predicted_class.item()}"
120
-
121
- # 取得最後一層的 hidden states (作為 embedding)
122
- # 注意: 需要模型支援 output_hidden_states
123
- if hasattr(outputs, 'hidden_states') and outputs.hidden_states is not None:
124
- # 取最後一層的 [CLS] token embedding
125
- last_hidden = outputs.hidden_states[-1] # (batch, seq_len, hidden_size)
126
- cls_embedding = last_hidden[:, 0, :].cpu().numpy() # (1, 768)
127
- else:
128
- # 如果沒有 hidden_states,用 logits 作為替代
129
- cls_embedding = logits.cpu().numpy()
130
-
131
- # 取得注意力權重
132
- if hasattr(outputs, 'attentions') and outputs.attentions is not None:
133
- # attentions: tuple of (batch, num_heads, seq_len, seq_len)
134
- # 取最後一層的注意力,平均所有 head
135
- last_attention = outputs.attentions[-1] # (1, num_heads, seq_len, seq_len)
136
- avg_attention = last_attention.mean(dim=1) # (1, seq_len, seq_len)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
- # 取 [CLS] token 對所有 token 的注意力
139
- cls_attention = avg_attention[0, 0, :].cpu().numpy() # (seq_len,)
140
- max_attention_score = cls_attention.max()
141
- else:
142
- cls_attention = None
143
- max_attention_score = 0.0
144
-
145
- # 儲存結果
146
- predictions.append({
147
- "filename": file.filename,
148
- "predicted_label": label_name,
149
- "confidence": confidence.item(),
150
- "attention_score": float(max_attention_score)
151
- })
152
-
153
- all_embeddings.append({
154
- "filename": file.filename,
155
- "label": label_name,
156
- "embedding": cls_embedding.tolist()[0], # 轉成 list
157
- "attention_score": float(max_attention_score)
158
- })
159
-
160
- print(f" ✅ Predicted: {label_name} (confidence: {confidence.item():.3f})")
161
-
162
- if not predictions:
163
- raise HTTPException(status_code=400, detail="No valid files processed")
164
-
165
- # ===== 多數投票 (Majority Voting) =====
166
- label_votes = [p["predicted_label"] for p in predictions]
167
- vote_counts = Counter(label_votes)
168
- final_label = vote_counts.most_common(1)[0][0]
169
- final_count = vote_counts[final_label]
170
-
171
- print(f"\n📊 Vote distribution: {dict(vote_counts)}")
172
- print(f"🏆 Final prediction: {final_label} ({final_count}/{len(predictions)} votes)")
173
-
174
- # ===== 選擇最高注意力權重的 embedding (且屬於最終預測的 label) =====
175
- # 過濾出預測為 final_label 的 embeddings
176
- same_label_embeddings = [
177
- emb for emb in all_embeddings
178
- if emb["label"] == final_label
179
- ]
180
-
181
- if same_label_embeddings:
182
- # 選擇注意力權重最高的
183
- best_embedding = max(same_label_embeddings, key=lambda x: x["attention_score"])
184
- else:
185
- # 如果沒有,就選所有裡面注意力最高的
186
- best_embedding = max(all_embeddings, key=lambda x: x["attention_score"])
187
-
188
- print(f"🎯 Selected embedding from: {best_embedding['filename']}")
189
- print(f" Attention score: {best_embedding['attention_score']:.4f}")
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
- return result
 
 
 
 
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))