updates
Browse files- app.py +129 -151
- results/embeddings_log.jsonl +1 -0
app.py
CHANGED
|
@@ -1,19 +1,20 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
| 19 |
allow_origins=["*"],
|
|
@@ -21,233 +22,210 @@ app.add_middleware(
|
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# ===== 模型設定 =====
|
| 25 |
-
MODEL_REPO = "lyi029/codebert-finetuned"
|
| 26 |
-
HF_TOKEN = os.
|
| 27 |
DEVICE = torch.device("cpu")
|
| 28 |
|
| 29 |
-
# =====
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
-
|
| 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())
|
|
|
|
| 72 |
"device": str(DEVICE),
|
| 73 |
-
"
|
| 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 |
-
|
| 93 |
-
|
| 94 |
-
print(f"{
|
| 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(
|
| 106 |
-
|
| 107 |
if not asm_code:
|
| 108 |
-
print(f"⚠️ Skip empty file: {file.filename}")
|
| 109 |
continue
|
| 110 |
-
|
| 111 |
-
print(f"📄 [{idx}/{len(files)}]
|
| 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 |
-
|
| 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 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 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
|
| 152 |
-
|
| 153 |
-
|
| 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"
|
| 179 |
-
|
| 180 |
-
|
| 181 |
if not predictions:
|
| 182 |
raise HTTPException(status_code=400, detail="No valid files processed")
|
| 183 |
-
|
| 184 |
-
#
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 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":
|
| 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 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
return result
|
| 230 |
-
|
| 231 |
except Exception as e:
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
"
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
-
#
|
| 250 |
if __name__ == "__main__":
|
| 251 |
import uvicorn
|
| 252 |
-
port = int(os.
|
| 253 |
-
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import torch
|
| 5 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, BackgroundTasks
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 8 |
from typing import List
|
| 9 |
from collections import Counter
|
|
|
|
| 10 |
|
| 11 |
+
# ====== FastAPI 初始化 ======
|
| 12 |
+
app = FastAPI(
|
| 13 |
+
title="Malvec Prediction Space (A)",
|
| 14 |
+
description="Handles inference and optionally triggers LoRA fine-tuning (Space B).",
|
| 15 |
+
version="1.3"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
app.add_middleware(
|
| 19 |
CORSMiddleware,
|
| 20 |
allow_origins=["*"],
|
|
|
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# ====== 模型設定 ======
|
| 26 |
+
MODEL_REPO = os.getenv("MODEL_REPO", "lyi029/codebert-finetuned")
|
| 27 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 28 |
DEVICE = torch.device("cpu")
|
| 29 |
|
| 30 |
+
# ====== 連接 Space B ======
|
| 31 |
+
SPACE_B_URL = os.getenv("SPACE_B_URL", "https://lyi029-model-update.hf.space")
|
| 32 |
+
|
| 33 |
+
# ====== Cache & 目錄 ======
|
| 34 |
+
CACHE_DIR = "/tmp/hf_cache"
|
| 35 |
+
os.environ.update({
|
| 36 |
+
"HF_HOME": CACHE_DIR,
|
| 37 |
+
"TRANSFORMERS_CACHE": CACHE_DIR,
|
| 38 |
+
"HUGGINGFACE_HUB_CACHE": CACHE_DIR
|
| 39 |
+
})
|
| 40 |
+
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 41 |
+
os.makedirs("results", exist_ok=True)
|
| 42 |
+
|
| 43 |
+
# ====== 載入模型 ======
|
| 44 |
+
print("🤗 Loading model from:", MODEL_REPO)
|
| 45 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO, token=HF_TOKEN)
|
| 46 |
model = AutoModelForSequenceClassification.from_pretrained(
|
| 47 |
+
MODEL_REPO,
|
| 48 |
token=HF_TOKEN,
|
| 49 |
+
output_attentions=True,
|
| 50 |
+
output_hidden_states=True
|
| 51 |
)
|
| 52 |
+
model.to(DEVICE).eval()
|
|
|
|
| 53 |
print("✅ Model loaded successfully")
|
| 54 |
|
| 55 |
+
|
| 56 |
+
# ----------------------------------------------------------
|
| 57 |
+
# ROUTES
|
| 58 |
+
# ----------------------------------------------------------
|
| 59 |
+
|
| 60 |
@app.get("/")
|
| 61 |
def home():
|
| 62 |
return {
|
| 63 |
+
"message": "Malware Classifier API (Space A)",
|
| 64 |
"model": MODEL_REPO,
|
| 65 |
+
"space_b_url": SPACE_B_URL,
|
| 66 |
"endpoints": {
|
| 67 |
"/predict": "POST - Upload TXT files for prediction",
|
| 68 |
+
"/model-info": "GET - Get model information",
|
| 69 |
+
"/trigger-train": "POST - Manually trigger Space B LoRA training"
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
+
|
| 74 |
@app.get("/model-info")
|
| 75 |
def get_model_info():
|
| 76 |
"""取得模型資訊"""
|
|
|
|
| 77 |
id2label_sample = {}
|
| 78 |
if hasattr(model.config, 'id2label'):
|
|
|
|
| 79 |
sample_keys = list(model.config.id2label.keys())[:3]
|
| 80 |
id2label_sample = {
|
| 81 |
+
k: {"type": str(type(k).__name__), "value": model.config.id2label[k]}
|
| 82 |
+
for k in sample_keys
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
+
|
| 85 |
return {
|
| 86 |
"model_name": MODEL_REPO,
|
| 87 |
"num_labels": model.config.num_labels,
|
| 88 |
+
"labels": list(model.config.id2label.values())
|
| 89 |
+
if hasattr(model.config, 'id2label') else None,
|
| 90 |
"device": str(DEVICE),
|
| 91 |
+
"id2label_sample": id2label_sample
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
|
| 95 |
@app.post("/predict")
|
| 96 |
async def predict_malware(files: List[UploadFile] = File(...)):
|
| 97 |
+
"""批次預測惡意軟體家族"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
if not files:
|
| 99 |
raise HTTPException(status_code=400, detail="No files provided")
|
| 100 |
+
|
| 101 |
+
predictions, all_embeddings = [], []
|
| 102 |
+
|
| 103 |
+
print(f"\n📥 Received {len(files)} files")
|
| 104 |
+
|
|
|
|
|
|
|
|
|
|
| 105 |
try:
|
| 106 |
with torch.no_grad():
|
| 107 |
for idx, file in enumerate(files, 1):
|
| 108 |
try:
|
|
|
|
| 109 |
content = await file.read()
|
| 110 |
+
asm_code = content.decode("utf-8").strip()
|
|
|
|
| 111 |
if not asm_code:
|
|
|
|
| 112 |
continue
|
| 113 |
+
|
| 114 |
+
print(f"📄 [{idx}/{len(files)}] {file.filename}")
|
| 115 |
+
|
|
|
|
| 116 |
inputs = tokenizer(
|
| 117 |
asm_code,
|
| 118 |
return_tensors="pt",
|
| 119 |
max_length=512,
|
| 120 |
truncation=True,
|
| 121 |
padding=True
|
| 122 |
+
).to(DEVICE)
|
| 123 |
+
|
|
|
|
|
|
|
| 124 |
outputs = model(**inputs)
|
|
|
|
|
|
|
| 125 |
logits = outputs.logits
|
| 126 |
probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 127 |
confidence, predicted_class = torch.max(probs, dim=-1)
|
| 128 |
+
|
|
|
|
| 129 |
predicted_id = predicted_class.item()
|
| 130 |
+
label_name = model.config.id2label.get(predicted_id, f"class_{predicted_id}")
|
| 131 |
+
|
| 132 |
+
# embedding
|
| 133 |
+
if outputs.hidden_states:
|
| 134 |
+
cls_embedding = outputs.hidden_states[-1][:, 0, :].cpu().numpy()[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
else:
|
|
|
|
| 136 |
cls_embedding = logits.cpu().numpy()[0]
|
| 137 |
+
|
| 138 |
+
# attention
|
| 139 |
max_attention_score = 0.0
|
| 140 |
+
if outputs.attentions:
|
| 141 |
+
last_attention = outputs.attentions[-1].mean(dim=1)
|
| 142 |
+
cls_attention = last_attention[0, 0, :].cpu().numpy()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
max_attention_score = float(cls_attention.max())
|
| 144 |
+
|
|
|
|
| 145 |
predictions.append({
|
| 146 |
"filename": file.filename,
|
| 147 |
"predicted_label": label_name,
|
| 148 |
"confidence": float(confidence.item()),
|
| 149 |
"attention_score": max_attention_score
|
| 150 |
})
|
|
|
|
| 151 |
all_embeddings.append({
|
| 152 |
"filename": file.filename,
|
| 153 |
"label": label_name,
|
| 154 |
"embedding": cls_embedding.tolist(),
|
| 155 |
"attention_score": max_attention_score
|
| 156 |
})
|
|
|
|
|
|
|
|
|
|
| 157 |
except Exception as e:
|
| 158 |
+
print(f"❌ {file.filename} error:", e)
|
| 159 |
+
|
|
|
|
| 160 |
if not predictions:
|
| 161 |
raise HTTPException(status_code=400, detail="No valid files processed")
|
| 162 |
+
|
| 163 |
+
# 多數投票
|
| 164 |
+
votes = [p["predicted_label"] for p in predictions]
|
| 165 |
+
final_label, count = Counter(votes).most_common(1)[0]
|
| 166 |
+
best_embedding = max(all_embeddings, key=lambda x: x["attention_score"])
|
| 167 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
result = {
|
| 169 |
"final_label": final_label,
|
| 170 |
+
"vote_count": count,
|
| 171 |
"total_segments": len(predictions),
|
| 172 |
"confidence": sum(p["confidence"] for p in predictions) / len(predictions),
|
|
|
|
| 173 |
"embedding": {
|
| 174 |
"values": best_embedding["embedding"],
|
| 175 |
"source_file": best_embedding["filename"],
|
| 176 |
+
"attention_score": best_embedding["attention_score"]
|
|
|
|
| 177 |
},
|
| 178 |
"segment_predictions": predictions
|
| 179 |
}
|
| 180 |
+
|
| 181 |
+
# 寫入本地 log
|
| 182 |
+
log_path = os.path.join("results", "embeddings_log.jsonl")
|
| 183 |
+
with open(log_path, "a") as f:
|
| 184 |
+
f.write(json.dumps({
|
| 185 |
+
"label": final_label,
|
| 186 |
+
"embedding": best_embedding["embedding"],
|
| 187 |
+
"source": best_embedding["filename"]
|
| 188 |
+
}) + "\n")
|
| 189 |
+
|
| 190 |
+
print(f"✅ Prediction complete ({final_label}), embedding logged.")
|
| 191 |
return result
|
| 192 |
+
|
| 193 |
except Exception as e:
|
| 194 |
+
raise HTTPException(status_code=500, detail=f"Prediction failed: {e}")
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
# ----------------------------------------------------------
|
| 198 |
+
# 手動觸發 Space B 訓練
|
| 199 |
+
# ----------------------------------------------------------
|
| 200 |
+
@app.post("/trigger-train")
|
| 201 |
+
async def trigger_train(background_tasks: BackgroundTasks):
|
| 202 |
+
"""手動觸發 Space B 進行 LoRA 訓練"""
|
| 203 |
+
if not SPACE_B_URL:
|
| 204 |
+
raise HTTPException(status_code=400, detail="SPACE_B_URL not set")
|
| 205 |
+
|
| 206 |
+
async def call_space_b():
|
| 207 |
+
print(f"🚀 Triggering LoRA training at {SPACE_B_URL}/train-lora ...")
|
| 208 |
+
try:
|
| 209 |
+
async with httpx.AsyncClient(timeout=600.0) as client:
|
| 210 |
+
resp = await client.post(f"{SPACE_B_URL}/train-lora")
|
| 211 |
+
print(f"🔁 Space B responded: {resp.status_code}")
|
| 212 |
+
except Exception as e:
|
| 213 |
+
print(f"❌ Failed to contact Space B:", e)
|
| 214 |
+
|
| 215 |
+
background_tasks.add_task(call_space_b)
|
| 216 |
+
return {"status": "ok", "detail": f"Training triggered at {SPACE_B_URL}"}
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
# ----------------------------------------------------------
|
| 220 |
+
# 健康檢查
|
| 221 |
+
# ----------------------------------------------------------
|
| 222 |
+
@app.get("/health")
|
| 223 |
+
def health():
|
| 224 |
+
return {"status": "ok", "space_b_url": SPACE_B_URL, "model": MODEL_REPO}
|
| 225 |
+
|
| 226 |
|
| 227 |
+
# ===== Hugging Face Space 啟動 =====
|
| 228 |
if __name__ == "__main__":
|
| 229 |
import uvicorn
|
| 230 |
+
port = int(os.getenv("PORT", 7860))
|
| 231 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
results/embeddings_log.jsonl
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"label": "LABEL_22", "embedding": [-1.1376512050628662, -0.047966666519641876, -0.24872556328773499, 0.3138405382633209, 1.3849672079086304, -1.633601427078247, 0.1983228325843811, -0.9281344413757324, -1.2320940494537354, -0.17736320197582245, -0.14521245658397675, -0.7206307053565979, 0.38130292296409607, -0.6596642136573792, -1.058794379234314, -0.05031924694776535, -0.3720412850379944, -1.0936486721038818, 0.28271904587745667, -0.8827933073043823, -1.1546648740768433, 0.3669872283935547, 0.525336742401123, 1.2668176889419556, 0.6567639112472534, -0.9213070869445801, 0.7756835222244263, 0.841020941734314, 2.1097068786621094, -0.5936853289604187, -0.24113062024116516, -0.325832337141037, 0.337377667427063, -0.13386991620063782, 0.5093280076980591, -0.31492793560028076, 1.2203612327575684, 0.3542757034301758, 0.7819508910179138, -0.5035881996154785, 0.681510329246521, -0.5969868898391724, 0.9250137805938721, 0.8779157400131226, -0.46198272705078125, -1.0527479648590088, -0.09535618871450424, 0.5992560982704163, 0.4059334695339203, 0.21735996007919312, -0.19251619279384613, -0.6925828456878662, -0.5036731958389282, 0.16192573308944702, -0.6349836587905884, 0.06005214899778366, 0.3224468529224396, 1.1263952255249023, 1.6230027675628662, 0.41462960839271545, 0.504364550113678, 0.6104910373687744, 0.011768733151257038, 0.8574330806732178, 0.564701497554779, -0.14947746694087982, 1.3354557752609253, -0.058740533888339996, -0.8358854651451111, 1.0691943168640137, 0.25602030754089355, -1.119339108467102, -0.7557674050331116, -1.1507818698883057, 0.28220075368881226, 0.770072340965271, -0.6029340028762817, -1.9243377447128296, -0.31591707468032837, 0.31989026069641113, 0.7232671976089478, 1.0281893014907837, -1.6793508529663086, -0.07632823288440704, 0.12640100717544556, -1.2264608144760132, -0.3532347083091736, -0.834219217300415, 0.05293191596865654, 1.4652807712554932, 0.9166657328605652, 0.7363359928131104, -0.8244483470916748, -0.0877017080783844, 0.6299440860748291, 0.3608165681362152, -0.2969774603843689, -2.4234206676483154, -0.5211418867111206, 0.7513505816459656, 0.31629493832588196, -0.008768882602453232, -0.7443650364875793, -0.7865290641784668, -0.5562754273414612, 0.21540501713752747, 0.4593517482280731, 0.7949761152267456, -0.31020835041999817, 0.12853683531284332, 0.5977621674537659, 0.7009657621383667, 0.6849643588066101, 0.0670325756072998, -0.3649844527244568, 1.3655753135681152, 0.8150240182876587, 0.2454439401626587, 0.3027573227882385, 0.7999396920204163, 0.45931771397590637, -0.4631280303001404, -1.2327587604522705, 1.0375596284866333, 0.21338415145874023, -1.0660752058029175, 1.3394678831100464, 0.4749479591846466, -0.43620967864990234, 0.9006527066230774, -0.33872389793395996, -0.4771420657634735, 0.5740822553634644, -1.1514320373535156, -0.6864111423492432, 0.9722400903701782, -0.4664333462715149, -0.001774907112121582, 0.4774153530597687, 0.25813156366348267, -0.8838087320327759, 0.8198972940444946, 1.1883636713027954, -0.10213352739810944, -0.01207643747329712, 0.6289607882499695, 0.6008293628692627, -0.46021589636802673, 0.04864867776632309, -0.13912782073020935, -0.2765611708164215, 1.6093056201934814, 0.4280846118927002, -0.15510843694210052, 0.06497648358345032, -0.6106284856796265, 1.6574655771255493, 0.51328444480896, -1.470613718032837, 0.6902266144752502, -1.6486538648605347, 0.4001755118370056, 0.22032597661018372, -0.13351278007030487, -0.4327280819416046, -0.0158231183886528, 0.8199860453605652, -0.8084433674812317, 0.43252086639404297, -0.1417301893234253, 0.16531914472579956, -0.9174373149871826, -0.2779422998428345, -0.3431832194328308, 1.612349510192871, 0.1897333264350891, 0.04867950826883316, 0.8501235246658325, 0.13211548328399658, 0.3107057809829712, -1.2989728450775146, 0.699346661567688, -0.9204074740409851, -0.5554434061050415, 1.4511620998382568, 0.12597593665122986, -0.412272185087204, -0.18743307888507843, -0.1193390041589737, 0.3981635272502899, -0.4291394352912903, -0.2560284435749054, -0.09665996581315994, 1.7018572092056274, 0.7826017141342163, 0.4460698068141937, -0.62934410572052, -0.9578490257263184, 0.41784414649009705, -0.338731050491333, -0.7362962365150452, -0.08496448397636414, -2.180903911590576, 0.8557265996932983, 1.0763882398605347, 0.3903621435165405, -1.3367023468017578, 0.0744004175066948, -0.17324423789978027, -0.4419287145137787, 0.36191844940185547, -1.0016173124313354, 0.5448305606842041, -0.003477104939520359, 0.8311629891395569, -0.024988185614347458, -0.008016861975193024, 0.5359987020492554, 0.128226637840271, 0.6882072687149048, 0.8257943987846375, -0.04032384604215622, 1.078633189201355, 1.1222411394119263, -1.8767560720443726, 1.0891131162643433, -0.402249813079834, 1.4461396932601929, 0.824389636516571, -1.0203402042388916, 0.21528729796409607, -1.5709600448608398, -0.23942002654075623, 0.819591760635376, 0.11764536052942276, 1.4720029830932617, -0.1017681285738945, 0.1394912600517273, 1.230941891670227, -1.5031625032424927, -1.2683907747268677, -0.1801721453666687, 0.5347336530685425, -0.07513493299484253, 0.1218331828713417, 0.47755515575408936, 0.03798113390803337, 1.1124496459960938, 0.4299278259277344, 0.5915526151657104, -0.9502110481262207, 0.2935713231563568, -0.6422731280326843, -0.39743155241012573, -0.9049035310745239, 0.349038690328598, -0.0379226952791214, 0.07872197777032852, 0.34557750821113586, 1.8472976684570312, -0.4882958233356476, 0.7459428906440735, 1.1113848686218262, -0.06080830097198486, 0.8112319707870483, 0.7219486832618713, 0.10409562289714813, 0.6121355891227722, 0.29704511165618896, 0.44084346294403076, 0.9389889240264893, -0.15789254009723663, -0.4516333043575287, 0.07116414606571198, -0.49739471077919006, 0.1357591450214386, 0.36766326427459717, -0.6527845859527588, 0.5385811924934387, -1.044918179512024, 0.4279027581214905, -0.4938076138496399, 0.30609652400016785, 0.1478549838066101, 0.22247837483882904, 0.6039690375328064, 0.40313035249710083, -0.13473469018936157, 0.32127711176872253, 0.024172328412532806, 0.03369198739528656, 0.1779123842716217, 0.3861633241176605, -1.2198516130447388, -0.495863139629364, -0.08574457466602325, -0.5072078108787537, -0.11470571160316467, -0.1780756711959839, 1.1086000204086304, -0.7676401734352112, 0.926301896572113, -2.1998932361602783, -0.8031148314476013, 0.35245656967163086, 1.2334141731262207, 0.06332505494356155, -1.607619285583496, -1.716624140739441, 1.875451683998108, 0.6916895508766174, -0.6706845760345459, 0.2505297064781189, 0.05007398873567581, 0.5342575311660767, 0.47351428866386414, 0.9753953218460083, -1.1287493705749512, 1.438072681427002, -0.32394251227378845, -0.275765597820282, 0.6293120384216309, -1.2603126764297485, 0.4366057515144348, 0.9122623205184937, -0.8527727127075195, -0.3357877731323242, -0.7113412022590637, -0.3522016406059265, 0.3006492853164673, 2.481539011001587, -0.6248143315315247, 0.171621173620224, 0.49543797969818115, 0.37707406282424927, 0.5047035217285156, -0.362292617559433, -0.11959059536457062, 0.25821995735168457, 0.8326072096824646, 0.25088125467300415, -1.5114065408706665, 0.5510380268096924, 0.12406320124864578, -0.37504440546035767, 0.7327953577041626, 0.394804984331131, 1.2905199527740479, 0.07248051464557648, 0.08519275486469269, -0.008324824273586273, -0.36052021384239197, 0.3371344208717346, 0.5731608867645264, -1.2077596187591553, 0.468056321144104, -0.30767157673835754, 0.01801905781030655, -0.8033738732337952, -0.717014491558075, 0.4809887409210205, -0.2340126633644104, -1.0320812463760376, -0.12543310225009918, -0.49874448776245117, -0.09569786489009857, 0.41829031705856323, -0.2447740137577057, -0.47840380668640137, -0.30974113941192627, 0.7045383453369141, 0.1554642617702484, -1.1876144409179688, 1.0300204753875732, 0.0016966015100479126, 0.4425871670246124, 0.26058489084243774, 0.7595421075820923, 0.5792287588119507, 1.7828278541564941, -0.06650681793689728, 0.41523271799087524, -0.4940042793750763, 0.29622453451156616, -1.168554663658142, 0.0839693695306778, 0.4629751443862915, 1.3304044008255005, 0.6933697462081909, 0.3566911518573761, 0.21990448236465454, -0.11611233651638031, -0.3264424800872803, 1.1001719236373901, 0.11055026948451996, -1.093000054359436, -0.5115755796432495, -1.6252870559692383, -1.410794973373413, -0.4606633484363556, 0.7520159482955933, -0.2483893483877182, -0.7026339769363403, -0.6609785556793213, 0.12372972071170807, 0.4270857274532318, 0.36552733182907104, 0.2347237467765808, -0.4384370446205139, -0.3036981523036957, 0.9358766674995422, 0.16927476227283478, 0.3284717798233032, -0.787688672542572, 0.2763821482658386, -0.07861903309822083, 0.2946012318134308, 1.4637089967727661, 1.1986099481582642, -0.21599194407463074, -1.1435601711273193, 0.8621698617935181, -0.5593662261962891, 1.0964921712875366, 0.5470489263534546, -0.2605631649494171, -0.6260429620742798, 0.043487630784511566, -0.8658447265625, 0.2495017647743225, 1.5001543760299683, 0.4422997236251831, 0.9022426605224609, 1.236467957496643, 0.49469447135925293, 0.7187694311141968, -0.4947529435157776, -0.1516530066728592, -0.9379710555076599, 0.380397766828537, -0.8127957582473755, 0.003505352884531021, 0.6832613348960876, -0.9180529713630676, -1.4029362201690674, -0.6004944443702698, 0.7151466608047485, 0.1457536816596985, -0.02120504155755043, 0.8785527944564819, -1.237352967262268, -0.09027865529060364, -0.8061809539794922, -2.388615608215332, 0.28401076793670654, 1.0735020637512207, 0.012411065399646759, -0.05949043482542038, -0.4365577697753906, -1.8886117935180664, 1.4948190450668335, 0.9103946089744568, -1.0833790302276611, 0.03449435532093048, -0.21496683359146118, -0.28727251291275024, -0.38611090183258057, -0.10638682544231415, -1.1744922399520874, 0.22374802827835083, 0.3620568513870239, 0.6518030166625977, -0.3846066892147064, 0.18523509800434113, 0.23693439364433289, 1.0845930576324463, -0.6164575219154358, -1.2501837015151978, -1.5960376262664795, -0.32354652881622314, 0.05106135830283165, 0.2258729636669159, -0.24277804791927338, -0.12007485330104828, -0.23482199013233185, -0.6425671577453613, 0.18879356980323792, 0.8171659708023071, 1.0350899696350098, -1.1254771947860718, 0.8126382827758789, -0.18043740093708038, -0.23748311400413513, -0.5251549482345581, -0.6313332319259644, -0.5546953678131104, -1.926604151725769, -0.3507324159145355, 0.3467325270175934, -0.1882975846529007, 1.1518970727920532, 0.4885515570640564, -0.6694130897521973, 0.06609175354242325, -0.0073983632028102875, 1.0050779581069946, 0.20297487080097198, 0.8105179071426392, 0.4610271751880646, -0.2354402244091034, 0.2583405673503876, 0.4179910719394684, -1.608094334602356, 0.2342132329940796, 0.5819965600967407, 0.8540791869163513, 0.3243853449821472, -1.0251133441925049, -0.7093964219093323, 1.4739199876785278, -0.45549315214157104, -0.9731098413467407, -0.2553293704986572, -0.6395350694656372, 0.5275095701217651, -0.7213915586471558, -0.29435640573501587, -0.20762582123279572, 0.2460426390171051, 0.23416614532470703, 0.7414902448654175, -0.11846373230218887, 0.3866358995437622, -0.005191192030906677, 0.3214361071586609, 0.3842426538467407, 1.2663463354110718, 0.9302275776863098, -0.45889413356781006, -1.232495903968811, -1.4062092304229736, 0.10079397261142731, 0.7571512460708618, -0.6989502906799316, 0.8614902496337891, 1.1606221199035645, 1.2070977687835693, 0.29366570711135864, -0.958355724811554, -0.1607455313205719, 0.22874091565608978, 1.7719188928604126, -0.3153890073299408, -0.8283708691596985, 0.047959983348846436, 0.11715264618396759, -0.8216304183006287, 0.5853899717330933, -0.0672457218170166, 1.6076334714889526, 0.9548808336257935, -0.25548797845840454, -0.52774977684021, 0.08401454240083694, 0.11539919674396515, -0.014103211462497711, 1.0305629968643188, 0.10122629255056381, 0.49746468663215637, 0.15758806467056274, 0.3838158845901489, -0.036926060914993286, 0.12871426343917847, 1.224318027496338, 0.23263531923294067, -0.6165709495544434, 1.2798727750778198, -1.0257856845855713, -0.3833020031452179, 0.2776530981063843, -0.6095693111419678, 0.4548713266849518, 0.19662176072597504, -0.25548961758613586, -0.11339510977268219, 0.6437106728553772, -0.4925786852836609, -0.5678867101669312, 4.173159122467041, 0.2547769546508789, -0.782534122467041, -0.6748127937316895, 0.48221802711486816, -0.2869008779525757, -0.6866371035575867, 1.2192351818084717, 0.8528581261634827, 0.9657674431800842, 0.03145359829068184, 0.10321536660194397, 0.5008635520935059, -0.6609427332878113, 0.7353428602218628, 0.5660674571990967, -0.31942060589790344, 1.4105873107910156, 0.20624706149101257, 0.41976991295814514, -0.5219880938529968, -0.1718677282333374, 0.9175398945808411, -0.175034761428833, -0.04898964986205101, -0.42784684896469116, -1.025199294090271, 0.6223952174186707, -0.985105574131012, -0.10066204518079758, 0.40717414021492004, -0.655312180519104, 0.8303356766700745, 0.3776390552520752, 0.15534557402133942, -0.09800048917531967, 1.3376610279083252, 0.6801286339759827, 0.4659770727157593, -0.14542736113071442, -0.5579668283462524, -0.7119702696800232, 0.6532291173934937, 0.8767310976982117, 0.17349426448345184, -0.2943628132343292, -0.009570084512233734, -0.08426351100206375, -0.03415360301733017, 0.6228379011154175, 0.46624910831451416, 0.8936226963996887, -0.5407970547676086, -0.46706077456474304, 0.2037450671195984, -0.7055444717407227, 0.6615667939186096, -0.17911507189273834, 0.9234349131584167, 1.1864943504333496, 1.2180473804473877, 0.9173684120178223, -0.006694331765174866, -0.894925594329834, -0.8766381144523621, -0.9501956701278687, -0.21952560544013977, 1.4721192121505737, -1.0872348546981812, 0.6008101105690002, 0.4873633086681366, 0.8791225552558899, -0.33959758281707764, -0.2543272078037262, 0.41046932339668274, -0.7741718292236328, -0.5860488414764404, -0.2643243074417114, 0.15743443369865417, 0.3545565605163574, 0.23382413387298584, 0.6985653638839722, -0.2733459174633026, -1.0774046182632446, 1.291937232017517, -1.2852239608764648, -0.9378688335418701, 0.5537691116333008, 0.5619140863418579, 0.6532622575759888, 1.890723705291748, 0.6445301175117493, 0.44120293855667114, -0.7754351496696472, -0.8213549852371216, 0.31523412466049194, 1.0800584554672241, -0.2328898310661316, -0.19260644912719727, 0.12346380949020386, 1.7472505569458008, -0.15700675547122955, -0.024698659777641296, -0.8263704180717468, -0.1900157928466797, 0.8186363577842712, 1.066175937652588, 0.3698233366012573, -1.1356091499328613, -1.1009531021118164, 0.08693907409906387, 0.727069616317749, 0.8463726043701172, -1.0667409896850586, -0.8655909299850464, -0.25945064425468445, -0.6226758360862732, 0.7319392561912537, -0.6086651086807251, -0.22415035963058472, -0.4496684968471527, 0.6642118692398071, 0.0263105109333992, -0.38482946157455444, -0.8316655158996582, 1.0445762872695923, -0.11858571320772171, -0.2873106598854065, -0.9061915874481201, 0.8231322765350342, 0.9962249398231506, 1.0116608142852783, 0.9149219393730164, -0.24691258370876312, 0.6333606243133545, 1.0195014476776123, 0.9560626149177551, 0.12173725664615631, -0.8001685738563538, 0.06103995442390442, -0.7071182727813721, 0.30323588848114014, -0.2757803201675415, 0.03853171691298485, 0.07303386181592941, 1.2166595458984375, -0.1969984769821167, 1.0186275243759155, 0.2797694504261017, 0.35673704743385315, 0.046594396233558655, -0.7105750441551208, 2.12198543548584, 0.03779672086238861, -0.1432829201221466, 0.3540559709072113, -0.048464175313711166, -1.4942448139190674, 0.6443394422531128, -0.5185648798942566, -0.3398434519767761, -0.9823211431503296, -0.06000126898288727, -1.0149345397949219, -1.2835886478424072, -1.2421504259109497, -0.17780427634716034, 1.146575689315796, -0.3107755482196808, -0.43603968620300293, -0.4832175374031067, -0.3776072561740875, 0.23192934691905975, -0.30174797773361206, -0.1066705584526062, 0.5134071111679077, 0.31211310625076294, 2.925279140472412, 1.3423659801483154, 0.22527103126049042], "source": "unpacked_Dogwaffle_Install_1_2_free_1.txt"}
|