Spaces:
Runtime error
Runtime error
111306047 commited on
Commit ·
8dcc174
1
Parent(s): 6ff5685
add tsne
Browse files
app.py
CHANGED
|
@@ -74,6 +74,67 @@ def get_model_info():
|
|
| 74 |
"id2label_sample": id2label_sample # ✅ 除錯用
|
| 75 |
}
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
@app.post("/predict")
|
| 78 |
async def predict_malware(files: List[UploadFile] = File(...)):
|
| 79 |
"""
|
|
@@ -205,6 +266,7 @@ async def predict_malware(files: List[UploadFile] = File(...)):
|
|
| 205 |
|
| 206 |
print(f"🎯 Selected embedding from: {best_embedding['filename']}")
|
| 207 |
print(f" Attention score: {best_embedding['attention_score']:.4f}")
|
|
|
|
| 208 |
|
| 209 |
# ===== 返回結果 =====
|
| 210 |
result = {
|
|
|
|
| 74 |
"id2label_sample": id2label_sample # ✅ 除錯用
|
| 75 |
}
|
| 76 |
|
| 77 |
+
# ===== 載入預先降維的資料 =====
|
| 78 |
+
# 在模型載入時執行一次
|
| 79 |
+
print("📊 Loading pre-computed t-SNE embeddings...")
|
| 80 |
+
df_tsne = pd.read_json("tsne_points.json") # 你上傳的 JSON
|
| 81 |
+
tsne_coords = df_tsne[['x', 'y']].values # (N, 2)
|
| 82 |
+
|
| 83 |
+
# 載入原始 embeddings(用於 kNN 投影)
|
| 84 |
+
df_original = pd.read_csv("cobert_finetune_embedding_checkpoint-126000.csv")
|
| 85 |
+
dim_cols = [f'emb_{i}' for i in range(768)]
|
| 86 |
+
original_embeddings = df_original[dim_cols].to_numpy() # (N, 768)
|
| 87 |
+
|
| 88 |
+
# 標準化(必須和訓練時一致)
|
| 89 |
+
scaler = StandardScaler()
|
| 90 |
+
original_embeddings_scaled = scaler.fit_transform(original_embeddings)
|
| 91 |
+
|
| 92 |
+
# 建立 kNN 模型(用於投影新點)
|
| 93 |
+
knn_model = NearestNeighbors(n_neighbors=10, metric='euclidean')
|
| 94 |
+
knn_model.fit(original_embeddings_scaled)
|
| 95 |
+
|
| 96 |
+
print(f"✅ Loaded {len(df_tsne)} pre-computed t-SNE points")
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def project_new_embedding_to_tsne(new_embedding_768d, k=10):
|
| 100 |
+
"""
|
| 101 |
+
將新的 768 維 embedding 投影到已有的 t-SNE 空間
|
| 102 |
+
|
| 103 |
+
方法:找到 k 個最近鄰,用距離加權平均它們的 t-SNE 座標
|
| 104 |
+
|
| 105 |
+
Args:
|
| 106 |
+
new_embedding_768d: array of shape (768,)
|
| 107 |
+
k: 使用幾個最近鄰
|
| 108 |
+
|
| 109 |
+
Returns:
|
| 110 |
+
dict: {'x': float, 'y': float}
|
| 111 |
+
"""
|
| 112 |
+
# 1. 標準化新 embedding(使用相同的 scaler)
|
| 113 |
+
new_emb_scaled = scaler.transform([new_embedding_768d]) # (1, 768)
|
| 114 |
+
|
| 115 |
+
# 2. 找到 k 個最近鄰
|
| 116 |
+
distances, indices = knn_model.kneighbors(new_emb_scaled, n_neighbors=k)
|
| 117 |
+
distances = distances[0] # (k,)
|
| 118 |
+
indices = indices[0] # (k,)
|
| 119 |
+
|
| 120 |
+
# 3. 距離加權平均(距離越近權重越大)
|
| 121 |
+
epsilon = 1e-8
|
| 122 |
+
weights = 1 / (distances + epsilon)
|
| 123 |
+
weights = weights / weights.sum()
|
| 124 |
+
|
| 125 |
+
# 4. 計算加權平均的 t-SNE 座標
|
| 126 |
+
neighbor_coords = tsne_coords[indices] # (k, 2)
|
| 127 |
+
projected_x = np.dot(weights, neighbor_coords[:, 0])
|
| 128 |
+
projected_y = np.dot(weights, neighbor_coords[:, 1])
|
| 129 |
+
|
| 130 |
+
return {
|
| 131 |
+
'x': float(projected_x),
|
| 132 |
+
'y': float(projected_y),
|
| 133 |
+
'method': 'kNN_projection',
|
| 134 |
+
'k': k,
|
| 135 |
+
'nearest_distance': float(distances[0])
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
@app.post("/predict")
|
| 139 |
async def predict_malware(files: List[UploadFile] = File(...)):
|
| 140 |
"""
|
|
|
|
| 266 |
|
| 267 |
print(f"🎯 Selected embedding from: {best_embedding['filename']}")
|
| 268 |
print(f" Attention score: {best_embedding['attention_score']:.4f}")
|
| 269 |
+
|
| 270 |
|
| 271 |
# ===== 返回結果 =====
|
| 272 |
result = {
|