Spaces:
Sleeping
Sleeping
update mechanism
Browse files
app.py
CHANGED
|
@@ -8,6 +8,11 @@ import httpx
|
|
| 8 |
# app_spaceB_demo.py — Demo版 Active Learning Space (模擬流程)
|
| 9 |
# ============================================================
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# === FastAPI 初始化 ===
|
| 13 |
app = FastAPI(
|
|
@@ -39,47 +44,94 @@ def greet_json():
|
|
| 39 |
# ============================================================
|
| 40 |
# 🧠 Endpoint 1️⃣: 模擬 Entropy 決策
|
| 41 |
# ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
@app.post("/check-new-samples")
|
| 43 |
def check_new_samples(entropy_threshold: float = Query(0.9)):
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
mean_entropy = round(random.uniform(0.5, 1.5), 3)
|
| 50 |
-
print(f"[Demo B]
|
| 51 |
|
| 52 |
-
# 高熵
|
| 53 |
if mean_entropy > entropy_threshold:
|
| 54 |
samples = random.sample(SIMULATED_FILES, k=min(3, len(SIMULATED_FILES)))
|
| 55 |
label_file = f"./demo_selected_for_labeling_{time.strftime('%H%M%S')}.json"
|
| 56 |
|
| 57 |
-
|
| 58 |
"status": "need_labeling",
|
| 59 |
"mean_entropy": mean_entropy,
|
| 60 |
"samples_to_label": [
|
| 61 |
-
{"source": s, "entropy": round(random.uniform(1.2, 1.8), 3)}
|
|
|
|
| 62 |
],
|
| 63 |
"label_file": label_file,
|
| 64 |
-
"cached_branches": len(SIMULATED_BRANCHES)
|
|
|
|
| 65 |
}
|
| 66 |
-
print(f"⚠️ 高熵 → 請人工標註: {samples}")
|
| 67 |
-
print(result)
|
| 68 |
-
return result
|
| 69 |
|
| 70 |
-
# 低熵
|
| 71 |
else:
|
| 72 |
branch = random.choice(SIMULATED_BRANCHES)
|
| 73 |
-
|
| 74 |
"status": "use_existing_model",
|
| 75 |
"recommended_branch": branch,
|
| 76 |
"mean_entropy": mean_entropy,
|
| 77 |
-
"cached_branches": len(SIMULATED_BRANCHES)
|
|
|
|
| 78 |
}
|
| 79 |
-
print(f"✅ 低熵 → 推薦使用分支: {branch}")
|
| 80 |
-
print(result)
|
| 81 |
-
return result
|
| 82 |
-
|
| 83 |
|
| 84 |
# ============================================================
|
| 85 |
# 🧩 Endpoint 2️⃣: 模擬人工標註後的訓練
|
|
|
|
| 8 |
# app_spaceB_demo.py — Demo版 Active Learning Space (模擬流程)
|
| 9 |
# ============================================================
|
| 10 |
|
| 11 |
+
# === 全域計數器 ===
|
| 12 |
+
FILE_COUNTER = 0
|
| 13 |
+
FILE_THRESHOLD = 1000
|
| 14 |
+
|
| 15 |
+
|
| 16 |
|
| 17 |
# === FastAPI 初始化 ===
|
| 18 |
app = FastAPI(
|
|
|
|
| 44 |
# ============================================================
|
| 45 |
# 🧠 Endpoint 1️⃣: 模擬 Entropy 決策
|
| 46 |
# ============================================================
|
| 47 |
+
# @app.post("/check-new-samples")
|
| 48 |
+
# def check_new_samples(entropy_threshold: float = Query(0.9)):
|
| 49 |
+
# """
|
| 50 |
+
# 模擬 Active Learning 決策:
|
| 51 |
+
# entropy 隨機 → 若高於門檻則要求人工標註;
|
| 52 |
+
# 否則推薦既有模型分支。
|
| 53 |
+
# """
|
| 54 |
+
# mean_entropy = round(random.uniform(0.5, 1.5), 3)
|
| 55 |
+
# print(f"[Demo B] 模擬 mean_entropy={mean_entropy}")
|
| 56 |
+
|
| 57 |
+
# # 高熵 → 模擬需要人工標註
|
| 58 |
+
# if mean_entropy > entropy_threshold:
|
| 59 |
+
# samples = random.sample(SIMULATED_FILES, k=min(3, len(SIMULATED_FILES)))
|
| 60 |
+
# label_file = f"./demo_selected_for_labeling_{time.strftime('%H%M%S')}.json"
|
| 61 |
+
|
| 62 |
+
# result = {
|
| 63 |
+
# "status": "need_labeling",
|
| 64 |
+
# "mean_entropy": mean_entropy,
|
| 65 |
+
# "samples_to_label": [
|
| 66 |
+
# {"source": s, "entropy": round(random.uniform(1.2, 1.8), 3)} for s in samples
|
| 67 |
+
# ],
|
| 68 |
+
# "label_file": label_file,
|
| 69 |
+
# "cached_branches": len(SIMULATED_BRANCHES)
|
| 70 |
+
# }
|
| 71 |
+
# print(f"⚠️ 高熵 → 請人工標註: {samples}")
|
| 72 |
+
# print(result)
|
| 73 |
+
# return result
|
| 74 |
+
|
| 75 |
+
# # 低熵 → 模擬選擇最佳分支
|
| 76 |
+
# else:
|
| 77 |
+
# branch = random.choice(SIMULATED_BRANCHES)
|
| 78 |
+
# result = {
|
| 79 |
+
# "status": "use_existing_model",
|
| 80 |
+
# "recommended_branch": branch,
|
| 81 |
+
# "mean_entropy": mean_entropy,
|
| 82 |
+
# "cached_branches": len(SIMULATED_BRANCHES)
|
| 83 |
+
# }
|
| 84 |
+
# print(f"✅ 低熵 → 推薦使用分支: {branch}")
|
| 85 |
+
# print(result)
|
| 86 |
+
# return result
|
| 87 |
@app.post("/check-new-samples")
|
| 88 |
def check_new_samples(entropy_threshold: float = Query(0.9)):
|
| 89 |
+
global FILE_COUNTER
|
| 90 |
+
|
| 91 |
+
# Demo: 每次呼叫視為一個新 sample
|
| 92 |
+
FILE_COUNTER += 1
|
| 93 |
+
print(f"[Demo B] Accumulated samples: {FILE_COUNTER}/{FILE_THRESHOLD}")
|
| 94 |
+
|
| 95 |
+
# 未達 1000 → 只回覆「累積中」
|
| 96 |
+
if FILE_COUNTER < FILE_THRESHOLD:
|
| 97 |
+
return {
|
| 98 |
+
"status": "accumulating",
|
| 99 |
+
"received_samples": FILE_COUNTER,
|
| 100 |
+
"remaining": FILE_THRESHOLD - FILE_COUNTER
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
# === 達到 1000 隻 → 執行一次 entropy 評估 ===
|
| 104 |
+
FILE_COUNTER = 0 # reset
|
| 105 |
mean_entropy = round(random.uniform(0.5, 1.5), 3)
|
| 106 |
+
print(f"[Demo B] Batch entropy evaluation: mean_entropy={mean_entropy}")
|
| 107 |
|
| 108 |
+
# 高熵(需要人工標註)
|
| 109 |
if mean_entropy > entropy_threshold:
|
| 110 |
samples = random.sample(SIMULATED_FILES, k=min(3, len(SIMULATED_FILES)))
|
| 111 |
label_file = f"./demo_selected_for_labeling_{time.strftime('%H%M%S')}.json"
|
| 112 |
|
| 113 |
+
return {
|
| 114 |
"status": "need_labeling",
|
| 115 |
"mean_entropy": mean_entropy,
|
| 116 |
"samples_to_label": [
|
| 117 |
+
{"source": s, "entropy": round(random.uniform(1.2, 1.8), 3)}
|
| 118 |
+
for s in samples
|
| 119 |
],
|
| 120 |
"label_file": label_file,
|
| 121 |
+
"cached_branches": len(SIMULATED_BRANCHES),
|
| 122 |
+
"batch_size": FILE_THRESHOLD
|
| 123 |
}
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# 低熵(選 existing branch)
|
| 126 |
else:
|
| 127 |
branch = random.choice(SIMULATED_BRANCHES)
|
| 128 |
+
return {
|
| 129 |
"status": "use_existing_model",
|
| 130 |
"recommended_branch": branch,
|
| 131 |
"mean_entropy": mean_entropy,
|
| 132 |
+
"cached_branches": len(SIMULATED_BRANCHES),
|
| 133 |
+
"batch_size": FILE_THRESHOLD
|
| 134 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# ============================================================
|
| 137 |
# 🧩 Endpoint 2️⃣: 模擬人工標註後的訓練
|