lyi029 commited on
Commit
039299d
·
1 Parent(s): fd471fd
Files changed (3) hide show
  1. Dockerfile +12 -0
  2. app.py +253 -0
  3. requirements.txt +10 -0
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+ WORKDIR /app
3
+
4
+ COPY requirements.txt .
5
+ RUN pip install --no-cache-dir -r requirements.txt
6
+
7
+ COPY . .
8
+
9
+ # Hugging Face Spaces 會用這個環境變數
10
+ ENV PORT=7860
11
+
12
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ CACHE_DIR = "/tmp/hf_cache"
3
+ os.environ["HF_HOME"] = CACHE_DIR
4
+ os.environ["TRANSFORMERS_CACHE"] = CACHE_DIR
5
+ 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
12
+ from typing import List
13
+ from collections import Counter
14
+ import json
15
+
16
+ app = FastAPI()
17
+ app.add_middleware(
18
+ CORSMiddleware,
19
+ allow_origins=["*"],
20
+ allow_methods=["*"],
21
+ allow_headers=["*"],
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 {
45
+ "message": "Malware Classifier API",
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,
215
+ "total_segments": len(predictions),
216
+ "confidence": sum(p["confidence"] for p in predictions) / len(predictions),
217
+ "vote_distribution": dict(vote_counts),
218
+ "embedding": {
219
+ "values": best_embedding["embedding"],
220
+ "source_file": best_embedding["filename"],
221
+ "attention_score": best_embedding["attention_score"],
222
+ "dimension": len(best_embedding["embedding"])
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))
253
+ uvicorn.run(app, host="0.0.0.0", port=port)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ transformers
4
+ torch
5
+ scikit-learn
6
+ plotly
7
+ numpy
8
+ pandas
9
+ python-multipart
10
+ httpx