111306047 commited on
Commit
6f1c7e3
·
1 Parent(s): 364645a

add presict label

Browse files
Files changed (2) hide show
  1. app.py +188 -36
  2. requirements.txt +2 -0
app.py CHANGED
@@ -6,9 +6,12 @@ os.environ["HUGGINGFACE_HUB_CACHE"] = CACHE_DIR
6
  os.makedirs(CACHE_DIR, exist_ok=True)
7
 
8
  import torch
9
- from fastapi import FastAPI
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
 
 
 
12
 
13
  app = FastAPI()
14
  app.add_middleware(
@@ -19,51 +22,200 @@ app.add_middleware(
19
  )
20
 
21
  # ===== 模型設定 =====
22
- MODEL_REPO = "raxhel/Malvec_predict" # 換成你的模型 repo
23
- HF_TOKEN = os.environ.get("hf_token") # 從 Space Secrets 取得;若模型是公開,None 也可
24
-
25
- # CPU 環境(Spaces CPU 機型)
26
  DEVICE = torch.device("cpu")
27
 
28
- # ===== 載入模型(啟動時載一次)=====
 
29
  tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO, token=HF_TOKEN)
30
- model = AutoModelForSequenceClassification.from_pretrained(MODEL_REPO, token=HF_TOKEN)
 
 
 
 
31
  model.to(DEVICE)
32
  model.eval()
 
33
 
34
  @app.get("/")
35
  def home():
36
- return {"message": "FastAPI Space is running!"}
37
-
38
- # 簡單測試路由:確認 tokenizer + model 真的能跑
39
- @app.post("/test")
40
- def test_model(data: dict):
41
- text = data.get("text", "")
42
- if not text:
43
- return {"error": "請提供 text"}
44
 
45
- inputs = tokenizer(
46
- text,
47
- return_tensors="pt",
48
- truncation=True,
49
- padding=True,
50
- max_length=512,
51
- )
52
- # 保證在 CPU
53
- inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
 
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  with torch.no_grad():
56
- outputs = model(**inputs)
57
-
58
- logits = outputs.logits # [1, num_labels]
59
- probs = torch.softmax(logits, dim=-1)
60
- pred_id = int(torch.argmax(probs, dim=-1).item())
61
- conf = float(probs[0, pred_id].item())
62
- label = model.config.id2label.get(pred_id, str(pred_id))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
 
 
 
 
 
 
 
64
  return {
65
- "input_text": text,
66
- "predicted_label": label,
67
- "confidence": round(conf, 4),
68
- "logits": logits.cpu().tolist(),
69
- }
 
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(
 
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
+ )
37
  model.to(DEVICE)
38
  model.eval()
39
+ print("✅ Model loaded successfully")
40
 
41
  @app.get("/")
42
  def home():
43
+ return {
44
+ "message": "Malware Classifier API",
45
+ "model": MODEL_REPO,
46
+ "endpoints": {
47
+ "/predict": "POST - Upload TXT files for prediction",
48
+ "/model-info": "GET - Get model information"
49
+ }
50
+ }
51
 
52
+ @app.get("/model-info")
53
+ def get_model_info():
54
+ """取得模型資訊"""
55
+ return {
56
+ "model_name": MODEL_REPO,
57
+ "num_labels": model.config.num_labels,
58
+ "labels": list(model.config.id2label.values()) if hasattr(model.config, 'id2label') else None,
59
+ "device": str(DEVICE),
60
+ "max_length": 512
61
+ }
62
 
63
+ @app.post("/predict")
64
+ async def predict_malware(files: List[UploadFile] = File(...)):
65
+ """
66
+ 批次預測惡意軟體家族
67
+
68
+ 接收多個 TXT 檔案 (來自同一個執行檔的分段)
69
+ 返回:
70
+ - final_label: 多數投票的家族標籤
71
+ - embeddings: 最高注意力權重的 embedding
72
+ """
73
+
74
+ if not files:
75
+ raise HTTPException(status_code=400, detail="No files provided")
76
+
77
+ print(f"\n{'='*60}")
78
+ print(f"📥 Received {len(files)} files")
79
+ print(f"{'='*60}")
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(...)):
213
+ """
214
+ 簡化版: 只返回 label 和 embedding,不包含詳細資訊
215
+ """
216
+ result = await predict_malware(files)
217
+
218
  return {
219
+ "final_label": result["final_label"],
220
+ "embedding": result["embedding"]["values"]
221
+ }
 
 
requirements.txt CHANGED
@@ -6,3 +6,5 @@ scikit-learn
6
  plotly
7
  numpy
8
  pandas
 
 
 
6
  plotly
7
  numpy
8
  pandas
9
+ python-multipart
10
+ httpx