lyi029 commited on
Commit
a549c99
·
1 Parent(s): 9bd7dc4
Files changed (2) hide show
  1. app.py +0 -123
  2. results/embeddings_log.jsonl +0 -0
app.py CHANGED
@@ -103,129 +103,6 @@ def get_model_info():
103
  }
104
 
105
 
106
- # @app.post("/predict")
107
- # async def predict_malware(files: List[UploadFile] = File(...)):
108
- # """批次預測惡意軟體家族"""
109
- # if not files:
110
- # raise HTTPException(status_code=400, detail="No files provided")
111
-
112
- # predictions, all_embeddings = [], []
113
-
114
- # print(f"\n📥 Received {len(files)} files")
115
-
116
- # try:
117
- # with torch.no_grad():
118
- # for idx, file in enumerate(files, 1):
119
- # try:
120
- # content = await file.read()
121
- # asm_code = content.decode("utf-8").strip()
122
- # if not asm_code:
123
- # continue
124
-
125
- # print(f"📄 [{idx}/{len(files)}] {file.filename}")
126
-
127
- # inputs = tokenizer(
128
- # asm_code,
129
- # return_tensors="pt",
130
- # max_length=512,
131
- # truncation=True,
132
- # padding=True
133
- # ).to(DEVICE)
134
-
135
- # outputs = model(**inputs)
136
- # logits = outputs.logits
137
- # probs = torch.nn.functional.softmax(logits, dim=-1)
138
- # confidence, predicted_class = torch.max(probs, dim=-1)
139
-
140
- # predicted_id = predicted_class.item()
141
- # label_name = model.config.id2label.get(predicted_id, f"class_{predicted_id}")
142
-
143
- # # embedding
144
- # if outputs.hidden_states:
145
- # cls_embedding = outputs.hidden_states[-1][:, 0, :].cpu().numpy()[0]
146
- # else:
147
- # cls_embedding = logits.cpu().numpy()[0]
148
-
149
- # # attention
150
- # max_attention_score = 0.0
151
- # if outputs.attentions:
152
- # last_attention = outputs.attentions[-1].mean(dim=1)
153
- # cls_attention = last_attention[0, 0, :].cpu().numpy()
154
- # max_attention_score = float(cls_attention.max())
155
-
156
- # predictions.append({
157
- # "filename": file.filename,
158
- # "predicted_label": label_name,
159
- # "confidence": float(confidence.item()),
160
- # "attention_score": max_attention_score
161
- # })
162
- # all_embeddings.append({
163
- # "filename": file.filename,
164
- # "label": label_name,
165
- # "embedding": cls_embedding.tolist(),
166
- # "attention_score": max_attention_score
167
- # })
168
- # except Exception as e:
169
- # print(f"❌ {file.filename} error:", e)
170
-
171
- # if not predictions:
172
- # raise HTTPException(status_code=400, detail="No valid files processed")
173
-
174
- # # 多數投票
175
- # votes = [p["predicted_label"] for p in predictions]
176
- # final_label, count = Counter(votes).most_common(1)[0]
177
- # best_embedding = max(all_embeddings, key=lambda x: x["attention_score"])
178
-
179
- # result = {
180
- # "final_label": final_label,
181
- # "vote_count": count,
182
- # "total_segments": len(predictions),
183
- # "confidence": sum(p["confidence"] for p in predictions) / len(predictions),
184
- # "embedding": {
185
- # "values": best_embedding["embedding"],
186
- # "source_file": best_embedding["filename"],
187
- # "attention_score": best_embedding["attention_score"]
188
- # },
189
- # "segment_predictions": predictions
190
- # }
191
-
192
- # # 寫入本地 log
193
- # log_path = os.path.join("results", "embeddings_log.jsonl")
194
- # with open(log_path, "a") as f:
195
- # f.write(json.dumps({
196
- # "label": final_label,
197
- # "text" : asm_code,
198
- # "embedding": best_embedding["embedding"],
199
- # "source": best_embedding["filename"]
200
- # }) + "\n")
201
-
202
- # print(f"✅ Prediction complete ({final_label}), embedding logged.")
203
- # return result
204
-
205
- # except Exception as e:
206
- # raise HTTPException(status_code=500, detail=f"Prediction failed: {e}")
207
-
208
- # from huggingface_hub import HfApi
209
-
210
- # HF_DATASET_REPO = "lyi029/malvec-embeddings"
211
-
212
- # def upload_embeddings_to_hub(local_path: str):
213
- # """將 embeddings_log.jsonl 上傳到 Hugging Face Dataset"""
214
- # api = HfApi()
215
- # try:
216
- # api.upload_file(
217
- # path_or_fileobj=local_path,
218
- # path_in_repo="logs/embeddings_log.jsonl",
219
- # repo_id=HF_DATASET_REPO,
220
- # repo_type="dataset",
221
- # token=HF_TOKEN,
222
- # )
223
- # print(f"☁️ Uploaded embeddings to dataset: {HF_DATASET_REPO}/logs/embeddings_log.jsonl")
224
- # return True
225
- # except Exception as e:
226
- # print(f"⚠️ Failed to upload embeddings: {e}")
227
- # return False
228
-
229
  HF_DATASET_REPO = "lyi029/malvec-embeddings"
230
 
231
  def upload_embeddings_to_hub(local_path: str):
 
103
  }
104
 
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  HF_DATASET_REPO = "lyi029/malvec-embeddings"
107
 
108
  def upload_embeddings_to_hub(local_path: str):
results/embeddings_log.jsonl CHANGED
The diff for this file is too large to render. See raw diff