lyi029 commited on
Commit
084a305
·
1 Parent(s): 039299d
Files changed (2) hide show
  1. __pycache__/app.cpython-310.pyc +0 -0
  2. app.py +148 -159
__pycache__/app.cpython-310.pyc ADDED
Binary file (6.79 kB). View file
 
app.py CHANGED
@@ -6,6 +6,7 @@ os.environ["HUGGINGFACE_HUB_CACHE"] = CACHE_DIR
6
  os.makedirs(CACHE_DIR, exist_ok=True)
7
 
8
  import torch
 
9
  from fastapi import FastAPI, UploadFile, File, HTTPException
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
@@ -22,23 +23,44 @@ app.add_middleware(
22
  )
23
 
24
  # ===== 模型設定 =====
25
- MODEL_REPO = "raxhel/Malvec_predict"
26
- HF_TOKEN = os.environ.get("hf_token")
27
  DEVICE = torch.device("cpu")
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # ===== 載入模型 =====
30
  print("🤗 Loading model...")
31
  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
- output_hidden_states=True # ✅ 關鍵: 輸出 hidden states
37
  )
38
  model.to(DEVICE)
39
  model.eval()
40
  print("✅ Model loaded successfully")
41
 
 
 
 
 
42
  @app.get("/")
43
  def home():
44
  return {
@@ -46,169 +68,105 @@ def home():
46
  "model": MODEL_REPO,
47
  "endpoints": {
48
  "/predict": "POST - Upload TXT files for prediction",
49
- "/model-info": "GET - Get model information"
 
 
50
  }
51
  }
52
 
53
  @app.get("/model-info")
54
  def get_model_info():
55
- """取得模型資訊"""
56
- # ✅ 檢查 id2label 的 key 類型
57
  id2label_sample = {}
58
  if hasattr(model.config, 'id2label'):
59
- # 取前 3 個 key 作為範例
60
  sample_keys = list(model.config.id2label.keys())[:3]
61
  id2label_sample = {
62
- k: {
63
- "type": str(type(k).__name__),
64
- "value": model.config.id2label[k]
65
- } for k in sample_keys
66
  }
67
-
68
  return {
69
  "model_name": MODEL_REPO,
70
  "num_labels": model.config.num_labels,
71
  "labels": list(model.config.id2label.values()) if hasattr(model.config, 'id2label') else None,
72
  "device": str(DEVICE),
73
  "max_length": 512,
74
- "id2label_sample": id2label_sample # ✅ 除錯用
75
  }
76
 
77
-
 
 
78
  @app.post("/predict")
79
  async def predict_malware(files: List[UploadFile] = File(...)):
80
- """
81
- 批次預測惡意軟體家族
82
-
83
- 接收多個 TXT 檔案 (來自同一個執行檔的分段)
84
- 返回:
85
- - final_label: 多數投票的家族標籤
86
- - embeddings: 最高注意力權重的 embedding
87
- """
88
-
89
  if not files:
90
  raise HTTPException(status_code=400, detail="No files provided")
91
-
92
- print(f"\n{'='*60}")
93
- print(f"📥 Received {len(files)} files")
94
- print(f"{'='*60}")
95
-
96
- predictions = []
97
- all_embeddings = []
98
-
99
  try:
100
  with torch.no_grad():
101
  for idx, file in enumerate(files, 1):
102
- try:
103
- # 讀取檔案內容
104
- content = await file.read()
105
- asm_code = content.decode('utf-8').strip()
106
-
107
- if not asm_code:
108
- print(f"⚠️ Skip empty file: {file.filename}")
109
- continue
110
-
111
- print(f"📄 [{idx}/{len(files)}] Processing: {file.filename}")
112
-
113
- # Tokenize
114
- inputs = tokenizer(
115
- asm_code,
116
- return_tensors="pt",
117
- max_length=512,
118
- truncation=True,
119
- padding=True
120
- )
121
- inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
122
-
123
- # 模型預測
124
- outputs = model(**inputs)
125
-
126
- # 取得預測結果
127
- logits = outputs.logits
128
- probs = torch.nn.functional.softmax(logits, dim=-1)
129
- confidence, predicted_class = torch.max(probs, dim=-1)
130
-
131
- # ✅ 取得 label (使用整數 key)
132
- predicted_id = predicted_class.item()
133
- if hasattr(model.config, 'id2label') and predicted_id in model.config.id2label:
134
- label_name = model.config.id2label[predicted_id]
135
- else:
136
- label_name = f"class_{predicted_id}"
137
-
138
- print(f" 🔍 Predicted ID: {predicted_id}, Label: {label_name}")
139
-
140
- # ✅ 取得 embedding (使用 hidden_states)
141
- if hasattr(outputs, 'hidden_states') and outputs.hidden_states is not None:
142
- # 取最後一層的 [CLS] token embedding
143
- last_hidden = outputs.hidden_states[-1] # (batch, seq_len, hidden_size)
144
- cls_embedding = last_hidden[:, 0, :].cpu().numpy()[0] # (768,)
145
- else:
146
- # 備用方案: 使用 logits
147
- cls_embedding = logits.cpu().numpy()[0]
148
-
149
- # ✅ 取得注意力權重
150
- max_attention_score = 0.0
151
- if hasattr(outputs, 'attentions') and outputs.attentions is not None:
152
- # 取最後一層的注意力,平均所有 head
153
- last_attention = outputs.attentions[-1] # (1, num_heads, seq_len, seq_len)
154
- avg_attention = last_attention.mean(dim=1) # (1, seq_len, seq_len)
155
-
156
- # 取 [CLS] token 對所有 token 的注意力
157
- cls_attention = avg_attention[0, 0, :].cpu().numpy() # (seq_len,)
158
- max_attention_score = float(cls_attention.max())
159
-
160
- # 儲存結果
161
- predictions.append({
162
- "filename": file.filename,
163
- "predicted_label": label_name,
164
- "confidence": float(confidence.item()),
165
- "attention_score": max_attention_score
166
- })
167
-
168
- all_embeddings.append({
169
- "filename": file.filename,
170
- "label": label_name,
171
- "embedding": cls_embedding.tolist(),
172
- "attention_score": max_attention_score
173
- })
174
-
175
- print(f" ✅ Predicted: {label_name} (confidence: {confidence.item():.3f})")
176
-
177
- except Exception as e:
178
- print(f" ❌ Error processing {file.filename}: {e}")
179
  continue
180
-
181
- if not predictions:
182
- raise HTTPException(status_code=400, detail="No valid files processed")
183
-
184
- # ===== 多數投票 (Majority Voting) =====
185
- label_votes = [p["predicted_label"] for p in predictions]
186
- vote_counts = Counter(label_votes)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  final_label = vote_counts.most_common(1)[0][0]
188
  final_count = vote_counts[final_label]
189
-
190
- print(f"\n📊 Vote distribution: {dict(vote_counts)}")
191
- print(f"🏆 Final prediction: {final_label} ({final_count}/{len(predictions)} votes)")
192
-
193
- # ===== 選擇最高注意力權重的 embedding (屬於最終預測的 label) =====
194
- # 過濾出預測為 final_label 的 embeddings
195
- same_label_embeddings = [
196
- emb for emb in all_embeddings
197
- if emb["label"] == final_label
198
- ]
199
-
200
- if same_label_embeddings:
201
- # 選擇注意力權重最高的
202
- best_embedding = max(same_label_embeddings, key=lambda x: x["attention_score"])
203
- else:
204
- # 如果沒有,就選所有裡面注意力最高的
205
- best_embedding = max(all_embeddings, key=lambda x: x["attention_score"])
206
-
207
- print(f"🎯 Selected embedding from: {best_embedding['filename']}")
208
- print(f" Attention score: {best_embedding['attention_score']:.4f}")
209
-
210
-
211
- # ===== 返回結果 =====
212
  result = {
213
  "final_label": final_label,
214
  "vote_count": final_count,
@@ -223,30 +181,61 @@ async def predict_malware(files: List[UploadFile] = File(...)):
223
  },
224
  "segment_predictions": predictions
225
  }
226
-
227
- print(f"\n✅ Prediction complete!\n")
228
-
 
 
 
 
 
 
 
229
  return result
230
-
231
  except Exception as e:
232
- print(f"\n❌ Error in predict_malware: {e}")
233
  import traceback
234
  traceback.print_exc()
235
- raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}")
236
-
237
- @app.post("/predict-batch")
238
- async def predict_batch(files: List[UploadFile] = File(...)):
239
- """
240
- 簡化版: 只返回 label 和 embedding,不包含詳細資訊
241
- """
242
- result = await predict_malware(files)
243
-
244
- return {
245
- "final_label": result["final_label"],
246
- "embedding": result["embedding"]["values"]
247
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
 
249
- # ✅ HF Space 啟動入口
 
 
250
  if __name__ == "__main__":
251
  import uvicorn
252
  port = int(os.environ.get("PORT", 7860))
 
6
  os.makedirs(CACHE_DIR, exist_ok=True)
7
 
8
  import torch
9
+ import httpx
10
  from fastapi import FastAPI, UploadFile, File, HTTPException
11
  from fastapi.middleware.cors import CORSMiddleware
12
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
 
23
  )
24
 
25
  # ===== 模型設定 =====
26
+ MODEL_REPO = "lyi029/codebert-finetuned"
27
+ HF_TOKEN = os.environ.get("HF_TOKEN")
28
  DEVICE = torch.device("cpu")
29
 
30
+
31
+ # ===== Space B 設定 =====
32
+ SPACE_B_URL = "https://lyi029-model-update.hf.space" # 你的微調空間
33
+ SPACE_B_TOKEN = os.environ.get("HF_TOKEN") # 同樣 token 即可
34
+
35
+ # ===== FastAPI 初始化 =====
36
+ app = FastAPI()
37
+ app.add_middleware(
38
+ CORSMiddleware,
39
+ allow_origins=["*"],
40
+ allow_methods=["*"],
41
+ allow_headers=["*"],
42
+ )
43
+
44
+ RESULT_DIR = "results"
45
+ os.makedirs(RESULT_DIR, exist_ok=True)
46
+
47
  # ===== 載入模型 =====
48
  print("🤗 Loading model...")
49
  tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO, token=HF_TOKEN)
50
  model = AutoModelForSequenceClassification.from_pretrained(
51
+ MODEL_REPO,
52
  token=HF_TOKEN,
53
+ output_attentions=True,
54
+ output_hidden_states=True
55
  )
56
  model.to(DEVICE)
57
  model.eval()
58
  print("✅ Model loaded successfully")
59
 
60
+ # ===============================
61
+ # ROUTES
62
+ # ===============================
63
+
64
  @app.get("/")
65
  def home():
66
  return {
 
68
  "model": MODEL_REPO,
69
  "endpoints": {
70
  "/predict": "POST - Upload TXT files for prediction",
71
+ "/model-info": "GET - Get model information",
72
+ "/get-embeddings": "GET - 查看目前累積的樣本數",
73
+ "/trigger-training": "POST - 手動將 embeddings 傳給 Space B 微調"
74
  }
75
  }
76
 
77
  @app.get("/model-info")
78
  def get_model_info():
 
 
79
  id2label_sample = {}
80
  if hasattr(model.config, 'id2label'):
 
81
  sample_keys = list(model.config.id2label.keys())[:3]
82
  id2label_sample = {
83
+ k: {"type": str(type(k).__name__), "value": model.config.id2label[k]}
84
+ for k in sample_keys
 
 
85
  }
86
+
87
  return {
88
  "model_name": MODEL_REPO,
89
  "num_labels": model.config.num_labels,
90
  "labels": list(model.config.id2label.values()) if hasattr(model.config, 'id2label') else None,
91
  "device": str(DEVICE),
92
  "max_length": 512,
93
+ "id2label_sample": id2label_sample
94
  }
95
 
96
+ # -------------------------------
97
+ # 推論:只累積 embeddings
98
+ # -------------------------------
99
  @app.post("/predict")
100
  async def predict_malware(files: List[UploadFile] = File(...)):
 
 
 
 
 
 
 
 
 
101
  if not files:
102
  raise HTTPException(status_code=400, detail="No files provided")
103
+
104
+ print(f"\n📥 Received {len(files)} files")
105
+
106
+ predictions, all_embeddings = [], []
107
+
 
 
 
108
  try:
109
  with torch.no_grad():
110
  for idx, file in enumerate(files, 1):
111
+ content = await file.read()
112
+ asm_code = content.decode("utf-8").strip()
113
+ if not asm_code:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  continue
115
+
116
+ inputs = tokenizer(
117
+ asm_code,
118
+ return_tensors="pt",
119
+ max_length=512,
120
+ truncation=True,
121
+ padding=True
122
+ )
123
+ inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
124
+ outputs = model(**inputs)
125
+
126
+ logits = outputs.logits
127
+ probs = torch.nn.functional.softmax(logits, dim=-1)
128
+ confidence, predicted_class = torch.max(probs, dim=-1)
129
+
130
+ predicted_id = predicted_class.item()
131
+ label_name = model.config.id2label.get(predicted_id, f"class_{predicted_id}")
132
+
133
+ # embedding
134
+ if hasattr(outputs, "hidden_states") and outputs.hidden_states is not None:
135
+ last_hidden = outputs.hidden_states[-1]
136
+ cls_embedding = last_hidden[:, 0, :].cpu().numpy()[0]
137
+ else:
138
+ cls_embedding = logits.cpu().numpy()[0]
139
+
140
+ # 注意力
141
+ max_attention_score = 0.0
142
+ if hasattr(outputs, "attentions") and outputs.attentions is not None:
143
+ last_attention = outputs.attentions[-1]
144
+ avg_attention = last_attention.mean(dim=1)
145
+ cls_attention = avg_attention[0, 0, :].cpu().numpy()
146
+ max_attention_score = float(cls_attention.max())
147
+
148
+ predictions.append({
149
+ "filename": file.filename,
150
+ "predicted_label": label_name,
151
+ "confidence": float(confidence.item()),
152
+ "attention_score": max_attention_score
153
+ })
154
+
155
+ all_embeddings.append({
156
+ "filename": file.filename,
157
+ "label": label_name,
158
+ "embedding": cls_embedding.tolist(),
159
+ "attention_score": max_attention_score
160
+ })
161
+
162
+ # 多數投票
163
+ vote_counts = Counter([p["predicted_label"] for p in predictions])
164
  final_label = vote_counts.most_common(1)[0][0]
165
  final_count = vote_counts[final_label]
166
+
167
+ same_label_embeddings = [emb for emb in all_embeddings if emb["label"] == final_label]
168
+ best_embedding = max(same_label_embeddings, key=lambda x: x["attention_score"]) if same_label_embeddings else max(all_embeddings, key=lambda x: x["attention_score"])
169
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  result = {
171
  "final_label": final_label,
172
  "vote_count": final_count,
 
181
  },
182
  "segment_predictions": predictions
183
  }
184
+
185
+ # 累積 embeddings 到 CSV
186
+ df = pd.DataFrame(all_embeddings)
187
+ csv_path = os.path.join(RESULT_DIR, "embeddings_buffer.csv")
188
+ if os.path.exists(csv_path):
189
+ old = pd.read_csv(csv_path)
190
+ df = pd.concat([old, df], ignore_index=True)
191
+ df.to_csv(csv_path, index=False)
192
+ print(f"💾 Accumulated {len(df)} embeddings so far in {csv_path}")
193
+
194
  return result
195
+
196
  except Exception as e:
 
197
  import traceback
198
  traceback.print_exc()
199
+ raise HTTPException(status_code=500, detail=f"Prediction failed: {e}")
200
+
201
+ # -------------------------------
202
+ # 查看目前累積的 embeddings
203
+ # -------------------------------
204
+ @app.get("/get-embeddings")
205
+ def get_embeddings_summary():
206
+ csv_path = os.path.join(RESULT_DIR, "embeddings_buffer.csv")
207
+ if not os.path.exists(csv_path):
208
+ return {"count": 0}
209
+ df = pd.read_csv(csv_path)
210
+ return {"count": len(df), "path": csv_path}
211
+
212
+ # -------------------------------
213
+ # 手動觸發 Space B 微調
214
+ # -------------------------------
215
+ @app.post("/trigger-training")
216
+ async def trigger_training():
217
+ csv_path = os.path.join(RESULT_DIR, "embeddings_buffer.csv")
218
+ if not os.path.exists(csv_path):
219
+ return {"status": "no data yet"}
220
+
221
+ with open(csv_path, "rb") as f:
222
+ csv_bytes = f.read()
223
+
224
+ files = {"file": ("embeddings.csv", io.BytesIO(csv_bytes), "text/csv")}
225
+ headers = {"Authorization": f"Bearer {SPACE_B_TOKEN}"} if SPACE_B_TOKEN else {}
226
+
227
+ try:
228
+ async with httpx.AsyncClient(timeout=60.0) as client:
229
+ resp = await client.post(f"{SPACE_B_URL}/trigger-training", files=files, headers=headers)
230
+ print(f"🚀 Sent embeddings to Space B, status={resp.status_code}")
231
+ return {"status": "training triggered", "response": resp.text}
232
+ except Exception as e:
233
+ print(f"❌ Failed to send embeddings to Space B: {e}")
234
+ return {"status": "failed", "error": str(e)}
235
 
236
+ # -------------------------------
237
+ # HF Space 啟動入口
238
+ # -------------------------------
239
  if __name__ == "__main__":
240
  import uvicorn
241
  port = int(os.environ.get("PORT", 7860))