Spaces:
Sleeping
Sleeping
mock
Browse files- Dockerfile +16 -0
- app.py +142 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import os, random, time, json
|
| 3 |
+
from fastapi import FastAPI, Query
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
import httpx
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# ============================================================
|
| 10 |
+
# app_spaceB_demo.py — Demo版 Active Learning Space (模擬流程)
|
| 11 |
+
# ============================================================
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# === FastAPI 初始化 ===
|
| 15 |
+
app = FastAPI(
|
| 16 |
+
title="Malvec Space B (Demo)",
|
| 17 |
+
description="Simulated Active Learning backend, connected to real Space A.",
|
| 18 |
+
version="demo-2.0"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
app.add_middleware(
|
| 22 |
+
CORSMiddleware,
|
| 23 |
+
allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# === 模擬參數 ===
|
| 27 |
+
SIMULATED_BRANCHES = ["round_1", "round_2", "round_3"]
|
| 28 |
+
SIMULATED_FILES = [
|
| 29 |
+
"dropper_payload.exe", "trojbanker_loader.exe",
|
| 30 |
+
"ransom_v2.exe", "spyeye_injector.exe", "worm_autoexec.exe"
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
# === Space A 連線設定(請改成你的真實 Space A API endpoint)===
|
| 34 |
+
SPACE_A_URL = os.getenv("SPACE_A_URL", "https://huggingface.co/spaces/lyi029/test")
|
| 35 |
+
|
| 36 |
+
@app.get("/")
|
| 37 |
+
def greet_json():
|
| 38 |
+
return {"Hello": "World!"}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ============================================================
|
| 42 |
+
# 🧠 Endpoint 1️⃣: 模擬 Entropy 決策
|
| 43 |
+
# ============================================================
|
| 44 |
+
@app.post("/check-new-samples")
|
| 45 |
+
def check_new_samples(entropy_threshold: float = Query(1.1)):
|
| 46 |
+
"""
|
| 47 |
+
模擬 Active Learning 決策:
|
| 48 |
+
entropy 隨機 → 若高於門檻則要求人工標註;
|
| 49 |
+
否則推薦既有模型分支。
|
| 50 |
+
"""
|
| 51 |
+
mean_entropy = round(random.uniform(0.5, 1.5), 3)
|
| 52 |
+
print(f"[Demo B] 模擬 mean_entropy={mean_entropy}")
|
| 53 |
+
|
| 54 |
+
# 高熵 → 模擬需要人工標註
|
| 55 |
+
if mean_entropy > entropy_threshold:
|
| 56 |
+
samples = random.sample(SIMULATED_FILES, k=min(3, len(SIMULATED_FILES)))
|
| 57 |
+
label_file = f"./demo_selected_for_labeling_{time.strftime('%H%M%S')}.json"
|
| 58 |
+
|
| 59 |
+
result = {
|
| 60 |
+
"status": "need_labeling",
|
| 61 |
+
"mean_entropy": mean_entropy,
|
| 62 |
+
"samples_to_label": [
|
| 63 |
+
{"source": s, "entropy": round(random.uniform(1.2, 1.8), 3)} for s in samples
|
| 64 |
+
],
|
| 65 |
+
"label_file": label_file,
|
| 66 |
+
"cached_branches": len(SIMULATED_BRANCHES)
|
| 67 |
+
}
|
| 68 |
+
print(f"⚠️ 高熵 → 請人工標註: {samples}")
|
| 69 |
+
return result
|
| 70 |
+
|
| 71 |
+
# 低熵 → 模擬選擇最佳分支
|
| 72 |
+
else:
|
| 73 |
+
branch = random.choice(SIMULATED_BRANCHES)
|
| 74 |
+
result = {
|
| 75 |
+
"status": "use_existing_model",
|
| 76 |
+
"recommended_branch": branch,
|
| 77 |
+
"mean_entropy": mean_entropy,
|
| 78 |
+
"cached_branches": len(SIMULATED_BRANCHES)
|
| 79 |
+
}
|
| 80 |
+
print(f"✅ 低熵 → 推薦使用分支: {branch}")
|
| 81 |
+
return result
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# ============================================================
|
| 85 |
+
# 🧩 Endpoint 2️⃣: 模擬人工標註後的訓練
|
| 86 |
+
# ============================================================
|
| 87 |
+
@app.post("/train-from-labeled")
|
| 88 |
+
async def train_from_labeled(label_file: str = Query(...)):
|
| 89 |
+
"""
|
| 90 |
+
模擬人工標註完成後自動更新模型(假訓練)
|
| 91 |
+
並通知真正的 Space A。
|
| 92 |
+
"""
|
| 93 |
+
print(f"🧠 模擬訓練資料: {label_file}")
|
| 94 |
+
time.sleep(2.0)
|
| 95 |
+
|
| 96 |
+
tag = f"demo-al-{time.strftime('%H%M%S')}"
|
| 97 |
+
payload = {"status": "updated", "tag": tag}
|
| 98 |
+
print(f"📡 通知 Space A: {payload}")
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
async with httpx.AsyncClient(timeout=10) as client:
|
| 102 |
+
resp = await client.post(SPACE_A_URL, json=payload)
|
| 103 |
+
print(f"✅ Space A 回應: {resp.status_code}")
|
| 104 |
+
return {
|
| 105 |
+
"status": "ok",
|
| 106 |
+
"output_dir": f"./demo_output/{tag}",
|
| 107 |
+
"tag": tag,
|
| 108 |
+
"notified_spaceA": True,
|
| 109 |
+
"spaceA_response": resp.text
|
| 110 |
+
}
|
| 111 |
+
except Exception as e:
|
| 112 |
+
print(f"⚠️ 通知 Space A 失敗: {e}")
|
| 113 |
+
return {
|
| 114 |
+
"status": "ok",
|
| 115 |
+
"output_dir": f"./demo_output/{tag}",
|
| 116 |
+
"tag": tag,
|
| 117 |
+
"notified_spaceA": False,
|
| 118 |
+
"error": str(e)
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# ============================================================
|
| 123 |
+
# 🧭 Endpoint 3️⃣: 模擬列出快取 LoRA 分支
|
| 124 |
+
# ============================================================
|
| 125 |
+
@app.get("/list-lora-cache")
|
| 126 |
+
def list_lora_cache():
|
| 127 |
+
branches = [{"branch": b, "num_samples": random.randint(600, 1200)} for b in SIMULATED_BRANCHES]
|
| 128 |
+
print("[Demo B] 回傳模擬 LoRA 分支列表")
|
| 129 |
+
return {"status": "ok", "total_branches": len(branches), "branches": branches}
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# ============================================================
|
| 133 |
+
# ❤️ Endpoint 4️⃣: 健康檢查
|
| 134 |
+
# ============================================================
|
| 135 |
+
@app.get("/health")
|
| 136 |
+
def health_check():
|
| 137 |
+
return {
|
| 138 |
+
"status": "running",
|
| 139 |
+
"mode": "demo",
|
| 140 |
+
"connected_to": SPACE_A_URL,
|
| 141 |
+
"branches": SIMULATED_BRANCHES
|
| 142 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
httpx
|