Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import uuid
|
|
| 5 |
import csv
|
| 6 |
from datetime import datetime
|
| 7 |
from pathlib import Path
|
| 8 |
-
from huggingface_hub import CommitScheduler
|
| 9 |
|
| 10 |
# --- 1. 配置区域 ---
|
| 11 |
DATASET_REPO_ID = "Emilyxml/moveit"
|
|
@@ -14,17 +14,33 @@ LOG_FOLDER = Path("logs")
|
|
| 14 |
LOG_FOLDER.mkdir(parents=True, exist_ok=True)
|
| 15 |
TOKEN = os.environ.get("HF_TOKEN")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# --- 2. 启动同步 ---
|
| 18 |
scheduler = CommitScheduler(
|
| 19 |
repo_id=DATASET_REPO_ID,
|
| 20 |
repo_type="dataset",
|
| 21 |
folder_path=LOG_FOLDER,
|
| 22 |
-
path_in_repo="
|
| 23 |
every=1,
|
| 24 |
token=TOKEN
|
| 25 |
)
|
| 26 |
|
| 27 |
-
# --- 3. 数据加载
|
| 28 |
def load_data():
|
| 29 |
groups = {}
|
| 30 |
if not os.path.exists(DATA_FOLDER):
|
|
@@ -64,7 +80,7 @@ def load_data():
|
|
| 64 |
|
| 65 |
ALL_GROUPS, ALL_GROUP_IDS = load_data()
|
| 66 |
|
| 67 |
-
# --- 4. 保存逻辑
|
| 68 |
def save_user_vote(user_id, group_id, choice_labels, method_names):
|
| 69 |
user_filename = f"user_{user_id}.csv"
|
| 70 |
user_file_path = LOG_FOLDER / user_filename
|
|
@@ -79,38 +95,24 @@ def save_user_vote(user_id, group_id, choice_labels, method_names):
|
|
| 79 |
writer.writerow(row)
|
| 80 |
print(f"Saved: {user_id} -> {choice_labels}")
|
| 81 |
|
| 82 |
-
# --- 5.
|
| 83 |
|
| 84 |
def init_state():
|
| 85 |
-
"""
|
| 86 |
-
return {
|
| 87 |
-
"user_id": str(uuid.uuid4())[:8],
|
| 88 |
-
"index": 0,
|
| 89 |
-
"is_finished": False
|
| 90 |
-
}
|
| 91 |
|
| 92 |
def next_question_data(user_state):
|
| 93 |
-
"""
|
| 94 |
-
计算下一题的数据,并返回给前端渲染。
|
| 95 |
-
这里只处理数据准备(打乱顺序等),不处理 UI。
|
| 96 |
-
"""
|
| 97 |
idx = user_state["index"]
|
| 98 |
-
|
| 99 |
if idx >= len(ALL_GROUP_IDS):
|
| 100 |
user_state["is_finished"] = True
|
| 101 |
-
return user_state, None, [], []
|
| 102 |
|
| 103 |
group_id = ALL_GROUP_IDS[idx]
|
| 104 |
group_data = ALL_GROUPS[group_id]
|
| 105 |
|
| 106 |
-
# 准备原图
|
| 107 |
origin_path = group_data["origin"]
|
| 108 |
-
|
| 109 |
-
# 准备候选图(打乱)
|
| 110 |
candidates = group_data["candidates"].copy()
|
| 111 |
random.shuffle(candidates)
|
| 112 |
|
| 113 |
-
# 构造候选图信息列表 [(path, "Option A"), (path, "Option B")...]
|
| 114 |
candidate_info = []
|
| 115 |
for i, path in enumerate(candidates):
|
| 116 |
label = f"Option {chr(65+i)}"
|
|
@@ -119,28 +121,22 @@ def next_question_data(user_state):
|
|
| 119 |
return user_state, origin_path, candidate_info, group_data["instruction"]
|
| 120 |
|
| 121 |
def submit_logic(user_state, current_candidates, selected_indices, is_none=False):
|
| 122 |
-
"""处理提交,保存数据,并进入下一题"""
|
| 123 |
if user_state["is_finished"]:
|
| 124 |
-
return user_state, []
|
| 125 |
|
| 126 |
current_idx = user_state["index"]
|
| 127 |
group_id = ALL_GROUP_IDS[current_idx]
|
| 128 |
|
| 129 |
-
# 1. 保存数据
|
| 130 |
if is_none:
|
| 131 |
save_user_vote(user_state["user_id"], group_id, "Rejected All", "None_Satisfied")
|
| 132 |
else:
|
| 133 |
-
# 如果不是 None 但没选,直接返回不跳转(或者你可以允许)
|
| 134 |
if not selected_indices:
|
| 135 |
raise gr.Error("请至少选择一张图片,或点击“都不满意”")
|
| 136 |
-
|
| 137 |
labels = []
|
| 138 |
methods = []
|
| 139 |
for idx in selected_indices:
|
| 140 |
info = current_candidates[idx]
|
| 141 |
labels.append(info["label"])
|
| 142 |
-
|
| 143 |
-
# 提取方法名
|
| 144 |
filename = os.path.basename(info["path"])
|
| 145 |
name_no_ext = os.path.splitext(filename)[0]
|
| 146 |
parts = name_no_ext.split('_', 1)
|
|
@@ -149,115 +145,80 @@ def submit_logic(user_state, current_candidates, selected_indices, is_none=False
|
|
| 149 |
|
| 150 |
save_user_vote(user_state["user_id"], group_id, "; ".join(labels), "; ".join(methods))
|
| 151 |
|
| 152 |
-
# 2. 索引+1
|
| 153 |
user_state["index"] += 1
|
| 154 |
-
|
| 155 |
-
# 3. 清空选择
|
| 156 |
-
return user_state, [] # 返回新的 state 和空的 selected_indices
|
| 157 |
|
| 158 |
-
# --- 6. 界面构建 (
|
| 159 |
with gr.Blocks(title="User Study") as demo:
|
| 160 |
|
| 161 |
-
#
|
| 162 |
-
# state_main: 存 user_id, index
|
| 163 |
-
state_main = gr.State(init_state())
|
| 164 |
-
# state_current_data: 存当前题目的数据 (原图, 候选图列表, 说明文)
|
| 165 |
state_origin = gr.State()
|
| 166 |
-
state_candidates = gr.State([])
|
| 167 |
state_instruction = gr.State("")
|
| 168 |
-
# state_selection: 存当前选中的索引 [0, 2]
|
| 169 |
state_selection = gr.State([])
|
| 170 |
|
| 171 |
-
#
|
| 172 |
-
header = gr.Markdown("Loading...")
|
| 173 |
-
|
| 174 |
-
# 动态渲染区域
|
| 175 |
@gr.render(inputs=[state_main, state_origin, state_candidates, state_selection, state_instruction])
|
| 176 |
def render_content(main_st, origin, candidates, selection, instruction):
|
| 177 |
-
|
| 178 |
-
# 1. 如果结束了
|
| 179 |
if main_st["is_finished"]:
|
| 180 |
gr.Markdown("## 🎉 测试结束!\n感谢您的参与,所有结果已保存。")
|
| 181 |
return
|
| 182 |
|
| 183 |
-
# 2. 显示说明
|
| 184 |
idx = main_st["index"]
|
| 185 |
-
|
|
|
|
| 186 |
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
| 188 |
if origin:
|
| 189 |
with gr.Row():
|
| 190 |
with gr.Column(scale=1):
|
| 191 |
gr.Image(origin, label="Reference (参考原图)", interactive=False, height=300)
|
| 192 |
with gr.Column(scale=2):
|
| 193 |
-
gr.Markdown("👈
|
| 194 |
|
| 195 |
-
# 4. 显示候选图 (Grid Layout)
|
| 196 |
-
# 使用 Row wrap=True 实现自动换行
|
| 197 |
with gr.Row(wrap=True):
|
| 198 |
for i, item in enumerate(candidates):
|
| 199 |
is_selected = i in selection
|
| 200 |
-
|
| 201 |
-
# 定义每个卡片的样式
|
| 202 |
-
with gr.Column(min_width=200): # 限制最小宽度,类似 Gallery 效果
|
| 203 |
gr.Image(item["path"], show_label=False, interactive=False)
|
| 204 |
-
|
| 205 |
-
# === 核心:每个图片下的按钮 ===
|
| 206 |
btn_text = f"✅ {item['label']} (已选)" if is_selected else f"⬜️ {item['label']} (点击选择)"
|
| 207 |
btn_variant = "primary" if is_selected else "secondary"
|
| 208 |
|
| 209 |
btn = gr.Button(btn_text, variant=btn_variant)
|
| 210 |
|
| 211 |
-
# 按钮点击逻辑:切换选中状态
|
| 212 |
def toggle(idx, current_sel):
|
| 213 |
-
if idx in current_sel:
|
| 214 |
-
|
| 215 |
-
else:
|
| 216 |
-
current_sel.append(idx)
|
| 217 |
current_sel.sort()
|
| 218 |
return current_sel
|
| 219 |
|
| 220 |
-
# 绑定点击事件,更新 state_selection,从而触发重绘
|
| 221 |
btn.click(fn=toggle, inputs=[gr.Number(i, visible=False), state_selection], outputs=[state_selection])
|
| 222 |
|
| 223 |
-
# === 底部操作栏 ===
|
| 224 |
with gr.Row():
|
| 225 |
btn_submit = gr.Button("🚀 提交 (Submit)", variant="primary", scale=2)
|
| 226 |
btn_none = gr.Button("🚫 都不满意 (None)", variant="stop", scale=1)
|
| 227 |
|
| 228 |
-
# === 事件流 ===
|
| 229 |
-
|
| 230 |
-
# 1. 初始化加载第一题
|
| 231 |
def load_first(main_st):
|
| 232 |
return next_question_data(main_st)
|
| 233 |
|
| 234 |
demo.load(load_first, inputs=[state_main], outputs=[state_main, state_origin, state_candidates, state_instruction])
|
| 235 |
|
| 236 |
-
# 2. 提交按钮 -> 保存 -> 准备下一题 -> 清空选择
|
| 237 |
def on_submit(main_st, cands, sel):
|
| 238 |
-
# 先保存
|
| 239 |
new_main, new_sel = submit_logic(main_st, cands, sel, is_none=False)
|
| 240 |
-
# 再加载下一题数据
|
| 241 |
updated_main, origin, new_cands, instr = next_question_data(new_main)
|
| 242 |
return updated_main, new_sel, origin, new_cands, instr
|
| 243 |
|
| 244 |
-
btn_submit.click(
|
| 245 |
-
fn=on_submit,
|
| 246 |
-
inputs=[state_main, state_candidates, state_selection],
|
| 247 |
-
outputs=[state_main, state_selection, state_origin, state_candidates, state_instruction]
|
| 248 |
-
)
|
| 249 |
|
| 250 |
-
# 3. 都不满意 -> 保存 -> 准备下一题
|
| 251 |
def on_none(main_st, cands, sel):
|
| 252 |
new_main, new_sel = submit_logic(main_st, cands, sel, is_none=True)
|
| 253 |
updated_main, origin, new_cands, instr = next_question_data(new_main)
|
| 254 |
return updated_main, new_sel, origin, new_cands, instr
|
| 255 |
|
| 256 |
-
btn_none.click(
|
| 257 |
-
fn=on_none,
|
| 258 |
-
inputs=[state_main, state_candidates, state_selection],
|
| 259 |
-
outputs=[state_main, state_selection, state_origin, state_candidates, state_instruction]
|
| 260 |
-
)
|
| 261 |
|
| 262 |
if __name__ == "__main__":
|
| 263 |
demo.launch()
|
|
|
|
| 5 |
import csv
|
| 6 |
from datetime import datetime
|
| 7 |
from pathlib import Path
|
| 8 |
+
from huggingface_hub import CommitScheduler, snapshot_download
|
| 9 |
|
| 10 |
# --- 1. 配置区域 ---
|
| 11 |
DATASET_REPO_ID = "Emilyxml/moveit"
|
|
|
|
| 14 |
LOG_FOLDER.mkdir(parents=True, exist_ok=True)
|
| 15 |
TOKEN = os.environ.get("HF_TOKEN")
|
| 16 |
|
| 17 |
+
# --- 1.5 自动下载数据 (关键修复) ---
|
| 18 |
+
# 只有当本地没有数据时才尝试下载
|
| 19 |
+
if not os.path.exists(DATA_FOLDER) or not os.listdir(DATA_FOLDER):
|
| 20 |
+
try:
|
| 21 |
+
print("正在从 Dataset 下载图片数据...")
|
| 22 |
+
snapshot_download(
|
| 23 |
+
repo_id=DATASET_REPO_ID,
|
| 24 |
+
repo_type="dataset",
|
| 25 |
+
local_dir=DATA_FOLDER,
|
| 26 |
+
token=TOKEN,
|
| 27 |
+
allow_patterns=["*.jpg", "*.png", "*.jpeg", "*.webp", "*.txt"]
|
| 28 |
+
)
|
| 29 |
+
print("下载完成!")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"下载数据失败 (可能是Token问题或Repo不存在): {e}")
|
| 32 |
+
|
| 33 |
# --- 2. 启动同步 ---
|
| 34 |
scheduler = CommitScheduler(
|
| 35 |
repo_id=DATASET_REPO_ID,
|
| 36 |
repo_type="dataset",
|
| 37 |
folder_path=LOG_FOLDER,
|
| 38 |
+
path_in_repo="logs", # 建议把日志分文件夹存
|
| 39 |
every=1,
|
| 40 |
token=TOKEN
|
| 41 |
)
|
| 42 |
|
| 43 |
+
# --- 3. 数据加载 ---
|
| 44 |
def load_data():
|
| 45 |
groups = {}
|
| 46 |
if not os.path.exists(DATA_FOLDER):
|
|
|
|
| 80 |
|
| 81 |
ALL_GROUPS, ALL_GROUP_IDS = load_data()
|
| 82 |
|
| 83 |
+
# --- 4. 保存逻辑 ---
|
| 84 |
def save_user_vote(user_id, group_id, choice_labels, method_names):
|
| 85 |
user_filename = f"user_{user_id}.csv"
|
| 86 |
user_file_path = LOG_FOLDER / user_filename
|
|
|
|
| 95 |
writer.writerow(row)
|
| 96 |
print(f"Saved: {user_id} -> {choice_labels}")
|
| 97 |
|
| 98 |
+
# --- 5. 核心逻辑 ---
|
| 99 |
|
| 100 |
def init_state():
|
| 101 |
+
return {"user_id": str(uuid.uuid4())[:8], "index": 0, "is_finished": False}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
def next_question_data(user_state):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
idx = user_state["index"]
|
|
|
|
| 105 |
if idx >= len(ALL_GROUP_IDS):
|
| 106 |
user_state["is_finished"] = True
|
| 107 |
+
return user_state, None, [], []
|
| 108 |
|
| 109 |
group_id = ALL_GROUP_IDS[idx]
|
| 110 |
group_data = ALL_GROUPS[group_id]
|
| 111 |
|
|
|
|
| 112 |
origin_path = group_data["origin"]
|
|
|
|
|
|
|
| 113 |
candidates = group_data["candidates"].copy()
|
| 114 |
random.shuffle(candidates)
|
| 115 |
|
|
|
|
| 116 |
candidate_info = []
|
| 117 |
for i, path in enumerate(candidates):
|
| 118 |
label = f"Option {chr(65+i)}"
|
|
|
|
| 121 |
return user_state, origin_path, candidate_info, group_data["instruction"]
|
| 122 |
|
| 123 |
def submit_logic(user_state, current_candidates, selected_indices, is_none=False):
|
|
|
|
| 124 |
if user_state["is_finished"]:
|
| 125 |
+
return user_state, []
|
| 126 |
|
| 127 |
current_idx = user_state["index"]
|
| 128 |
group_id = ALL_GROUP_IDS[current_idx]
|
| 129 |
|
|
|
|
| 130 |
if is_none:
|
| 131 |
save_user_vote(user_state["user_id"], group_id, "Rejected All", "None_Satisfied")
|
| 132 |
else:
|
|
|
|
| 133 |
if not selected_indices:
|
| 134 |
raise gr.Error("请至少选择一张图片,或点击“都不满意”")
|
|
|
|
| 135 |
labels = []
|
| 136 |
methods = []
|
| 137 |
for idx in selected_indices:
|
| 138 |
info = current_candidates[idx]
|
| 139 |
labels.append(info["label"])
|
|
|
|
|
|
|
| 140 |
filename = os.path.basename(info["path"])
|
| 141 |
name_no_ext = os.path.splitext(filename)[0]
|
| 142 |
parts = name_no_ext.split('_', 1)
|
|
|
|
| 145 |
|
| 146 |
save_user_vote(user_state["user_id"], group_id, "; ".join(labels), "; ".join(methods))
|
| 147 |
|
|
|
|
| 148 |
user_state["index"] += 1
|
| 149 |
+
return user_state, []
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
# --- 6. 界面构建 (Gradio 5.0+) ---
|
| 152 |
with gr.Blocks(title="User Study") as demo:
|
| 153 |
|
| 154 |
+
state_main = gr.State(init_state) # 修正:使用 init_state 函数引用
|
|
|
|
|
|
|
|
|
|
| 155 |
state_origin = gr.State()
|
| 156 |
+
state_candidates = gr.State([])
|
| 157 |
state_instruction = gr.State("")
|
|
|
|
| 158 |
state_selection = gr.State([])
|
| 159 |
|
| 160 |
+
# @gr.render 需要 Gradio 5.0+
|
|
|
|
|
|
|
|
|
|
| 161 |
@gr.render(inputs=[state_main, state_origin, state_candidates, state_selection, state_instruction])
|
| 162 |
def render_content(main_st, origin, candidates, selection, instruction):
|
|
|
|
|
|
|
| 163 |
if main_st["is_finished"]:
|
| 164 |
gr.Markdown("## 🎉 测试结束!\n感谢您的参与,所有结果已保存。")
|
| 165 |
return
|
| 166 |
|
|
|
|
| 167 |
idx = main_st["index"]
|
| 168 |
+
total = len(ALL_GROUP_IDS)
|
| 169 |
+
gr.Markdown(f"### 任务 ({idx + 1} / {total if total > 0 else 0})\n\n{instruction}")
|
| 170 |
|
| 171 |
+
if total == 0:
|
| 172 |
+
gr.Markdown("⚠️ **错误**:未找到任何数据。请检查 Dataset 设置。")
|
| 173 |
+
return
|
| 174 |
+
|
| 175 |
if origin:
|
| 176 |
with gr.Row():
|
| 177 |
with gr.Column(scale=1):
|
| 178 |
gr.Image(origin, label="Reference (参考原图)", interactive=False, height=300)
|
| 179 |
with gr.Column(scale=2):
|
| 180 |
+
gr.Markdown("👈 **请参考左侧原图**,并在下方选择您认为质量最好的图片(可多选)。")
|
| 181 |
|
|
|
|
|
|
|
| 182 |
with gr.Row(wrap=True):
|
| 183 |
for i, item in enumerate(candidates):
|
| 184 |
is_selected = i in selection
|
| 185 |
+
with gr.Column(min_width=200):
|
|
|
|
|
|
|
| 186 |
gr.Image(item["path"], show_label=False, interactive=False)
|
|
|
|
|
|
|
| 187 |
btn_text = f"✅ {item['label']} (已选)" if is_selected else f"⬜️ {item['label']} (点击选择)"
|
| 188 |
btn_variant = "primary" if is_selected else "secondary"
|
| 189 |
|
| 190 |
btn = gr.Button(btn_text, variant=btn_variant)
|
| 191 |
|
|
|
|
| 192 |
def toggle(idx, current_sel):
|
| 193 |
+
if idx in current_sel: current_sel.remove(idx)
|
| 194 |
+
else: current_sel.append(idx)
|
|
|
|
|
|
|
| 195 |
current_sel.sort()
|
| 196 |
return current_sel
|
| 197 |
|
|
|
|
| 198 |
btn.click(fn=toggle, inputs=[gr.Number(i, visible=False), state_selection], outputs=[state_selection])
|
| 199 |
|
|
|
|
| 200 |
with gr.Row():
|
| 201 |
btn_submit = gr.Button("🚀 提交 (Submit)", variant="primary", scale=2)
|
| 202 |
btn_none = gr.Button("🚫 都不满意 (None)", variant="stop", scale=1)
|
| 203 |
|
|
|
|
|
|
|
|
|
|
| 204 |
def load_first(main_st):
|
| 205 |
return next_question_data(main_st)
|
| 206 |
|
| 207 |
demo.load(load_first, inputs=[state_main], outputs=[state_main, state_origin, state_candidates, state_instruction])
|
| 208 |
|
|
|
|
| 209 |
def on_submit(main_st, cands, sel):
|
|
|
|
| 210 |
new_main, new_sel = submit_logic(main_st, cands, sel, is_none=False)
|
|
|
|
| 211 |
updated_main, origin, new_cands, instr = next_question_data(new_main)
|
| 212 |
return updated_main, new_sel, origin, new_cands, instr
|
| 213 |
|
| 214 |
+
btn_submit.click(on_submit, inputs=[state_main, state_candidates, state_selection], outputs=[state_main, state_selection, state_origin, state_candidates, state_instruction])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
|
|
|
| 216 |
def on_none(main_st, cands, sel):
|
| 217 |
new_main, new_sel = submit_logic(main_st, cands, sel, is_none=True)
|
| 218 |
updated_main, origin, new_cands, instr = next_question_data(new_main)
|
| 219 |
return updated_main, new_sel, origin, new_cands, instr
|
| 220 |
|
| 221 |
+
btn_none.click(on_none, inputs=[state_main, state_candidates, state_selection], outputs=[state_main, state_selection, state_origin, state_candidates, state_instruction])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
if __name__ == "__main__":
|
| 224 |
demo.launch()
|