Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,13 @@ from pathlib import Path
|
|
| 8 |
from huggingface_hub import CommitScheduler
|
| 9 |
|
| 10 |
# --- 1. 配置区域 ---
|
| 11 |
-
DATASET_REPO_ID = "Emilyxml/moveit"
|
| 12 |
-
DATA_FOLDER = "data"
|
| 13 |
-
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",
|
|
@@ -24,7 +24,7 @@ scheduler = CommitScheduler(
|
|
| 24 |
token=TOKEN
|
| 25 |
)
|
| 26 |
|
| 27 |
-
# --- 3.
|
| 28 |
def load_data():
|
| 29 |
groups = {}
|
| 30 |
if not os.path.exists(DATA_FOLDER):
|
|
@@ -34,21 +34,16 @@ def load_data():
|
|
| 34 |
for filename in os.listdir(DATA_FOLDER):
|
| 35 |
if filename.startswith('.'): continue
|
| 36 |
file_path = os.path.join(DATA_FOLDER, filename)
|
| 37 |
-
prefix = filename[:5]
|
| 38 |
|
| 39 |
if prefix not in groups:
|
| 40 |
-
# origin 存原图路径,candidates 存其他方法的图
|
| 41 |
groups[prefix] = {"origin": None, "candidates": [], "instruction": "暂无说明"}
|
| 42 |
|
| 43 |
-
# 图片处理
|
| 44 |
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
| 45 |
-
# 判断是否是原图 (文件名包含 _origin)
|
| 46 |
if "_origin" in filename.lower():
|
| 47 |
groups[prefix]["origin"] = file_path
|
| 48 |
else:
|
| 49 |
groups[prefix]["candidates"].append(file_path)
|
| 50 |
-
|
| 51 |
-
# 文本处理
|
| 52 |
elif filename.lower().endswith('.txt'):
|
| 53 |
try:
|
| 54 |
with open(file_path, "r", encoding="utf-8") as f:
|
|
@@ -57,7 +52,6 @@ def load_data():
|
|
| 57 |
with open(file_path, "r", encoding="gbk") as f:
|
| 58 |
groups[prefix]["instruction"] = f.read()
|
| 59 |
|
| 60 |
-
# 过滤掉既没有原图也没有候选图的组
|
| 61 |
valid_groups = {}
|
| 62 |
for k, v in groups.items():
|
| 63 |
if v["origin"] is not None or len(v["candidates"]) > 0:
|
|
@@ -70,18 +64,11 @@ def load_data():
|
|
| 70 |
|
| 71 |
ALL_GROUPS, ALL_GROUP_IDS = load_data()
|
| 72 |
|
| 73 |
-
# --- 4. 保存逻辑 ---
|
| 74 |
def save_user_vote(user_id, group_id, choice_labels, method_names):
|
| 75 |
user_filename = f"user_{user_id}.csv"
|
| 76 |
user_file_path = LOG_FOLDER / user_filename
|
| 77 |
-
|
| 78 |
-
row = [
|
| 79 |
-
user_id,
|
| 80 |
-
datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 81 |
-
group_id,
|
| 82 |
-
choice_labels,
|
| 83 |
-
method_names
|
| 84 |
-
]
|
| 85 |
|
| 86 |
with scheduler.lock:
|
| 87 |
file_exists = user_file_path.exists()
|
|
@@ -90,216 +77,186 @@ def save_user_vote(user_id, group_id, choice_labels, method_names):
|
|
| 90 |
if not file_exists:
|
| 91 |
writer.writerow(["user_id", "timestamp", "group_id", "selected_labels", "selected_methods"])
|
| 92 |
writer.writerow(row)
|
| 93 |
-
|
| 94 |
-
print(f"Saved: User {user_id} selected {method_names}")
|
| 95 |
|
| 96 |
-
# --- 5.
|
| 97 |
|
| 98 |
-
def
|
| 99 |
-
"""
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
group_data = ALL_GROUPS[group_id]
|
| 111 |
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
-
#
|
| 115 |
-
display_list = []
|
| 116 |
-
real_paths = [] # 记录真实的路径顺序
|
| 117 |
-
|
| 118 |
-
# 1. 先放原图 (如果有)
|
| 119 |
-
has_origin = False
|
| 120 |
-
if group_data["origin"]:
|
| 121 |
-
display_list.append((group_data["origin"], "Reference (原图)"))
|
| 122 |
-
real_paths.append(group_data["origin"])
|
| 123 |
-
has_origin = True
|
| 124 |
-
|
| 125 |
-
# 2. 再放候选图 (打乱顺序)
|
| 126 |
candidates = group_data["candidates"].copy()
|
| 127 |
random.shuffle(candidates)
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
# 动态计算列数 (原图+候选图的总数)
|
| 136 |
-
total_imgs = len(display_list)
|
| 137 |
-
# 如果总数是2(1原图+1候选),显示2列
|
| 138 |
-
# 如果总数是3,显示3列
|
| 139 |
-
# 如果总数是4或以上,显示3列换行,或者你可以改成 4
|
| 140 |
-
cols = min(total_imgs, 3)
|
| 141 |
|
| 142 |
-
return
|
| 143 |
-
gr.update(value=instruction_text, visible=True),
|
| 144 |
-
gr.update(value=display_list, columns=cols, visible=True),
|
| 145 |
-
gr.update(value="当前未选择任何图片", visible=True),
|
| 146 |
-
gr.update(visible=True),
|
| 147 |
-
gr.update(visible=False),
|
| 148 |
-
user_state,
|
| 149 |
-
real_paths, # 存下当前界面所有图片的真实路径
|
| 150 |
-
[], # 清空选中项
|
| 151 |
-
has_origin # 告诉前端第一张是不是原图
|
| 152 |
-
)
|
| 153 |
|
| 154 |
-
def
|
| 155 |
-
"""
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
"""
|
| 159 |
-
clicked_idx = evt.index
|
| 160 |
-
|
| 161 |
-
# --- 保护机制:如果第一张是原图,点击无效 ---
|
| 162 |
-
if has_origin and clicked_idx == 0:
|
| 163 |
-
# 你可以去掉这个if,允许选原图,但通常 User Study 不需要选 Reference
|
| 164 |
-
# 这里我们返回原样,不更新选中状态
|
| 165 |
-
return current_indices, "⚠️ 您点击的是参考原图 (Reference),请选择后面的选项。"
|
| 166 |
-
|
| 167 |
-
# 切换选中状态
|
| 168 |
-
if clicked_idx in current_indices:
|
| 169 |
-
current_indices.remove(clicked_idx)
|
| 170 |
-
else:
|
| 171 |
-
current_indices.append(clicked_idx)
|
| 172 |
-
current_indices.sort()
|
| 173 |
-
|
| 174 |
-
# 生成状态文本
|
| 175 |
-
if not current_indices:
|
| 176 |
-
status_text = "当前未选择任何图片"
|
| 177 |
-
else:
|
| 178 |
-
# 计算显示的 Label
|
| 179 |
-
labels = []
|
| 180 |
-
for idx in current_indices:
|
| 181 |
-
# 如果有原图,index 1 才是 Option A
|
| 182 |
-
if has_origin:
|
| 183 |
-
option_char = chr(65 + (idx - 1)) # idx=1 -> A, idx=2 -> B
|
| 184 |
-
else:
|
| 185 |
-
option_char = chr(65 + idx)
|
| 186 |
-
labels.append(f"Option {option_char}")
|
| 187 |
-
|
| 188 |
-
status_text = "已选中: " + ", ".join(labels)
|
| 189 |
-
|
| 190 |
-
return current_indices, status_text
|
| 191 |
|
| 192 |
-
def submit_vote(user_state, current_file_paths, current_indices, is_none=False, has_origin=False):
|
| 193 |
-
user_id = user_state["user_id"]
|
| 194 |
current_idx = user_state["index"]
|
| 195 |
-
|
| 196 |
-
if current_idx >= len(ALL_GROUP_IDS):
|
| 197 |
-
return get_current_question_ui(user_state)
|
| 198 |
-
|
| 199 |
group_id = ALL_GROUP_IDS[current_idx]
|
| 200 |
-
|
|
|
|
| 201 |
if is_none:
|
| 202 |
-
save_user_vote(user_id, group_id, "Rejected All", "None_Satisfied")
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
for idx in current_indices:
|
| 218 |
-
# 获取真实路径
|
| 219 |
-
real_path = current_file_paths[idx]
|
| 220 |
-
|
| 221 |
-
# 计算 Label (Option A/B...)
|
| 222 |
-
if has_origin:
|
| 223 |
-
label = f"Option {chr(65 + (idx - 1))}"
|
| 224 |
-
else:
|
| 225 |
-
label = f"Option {chr(65 + idx)}"
|
| 226 |
-
selected_labels.append(label)
|
| 227 |
-
|
| 228 |
-
# 提取方法名
|
| 229 |
-
filename = os.path.basename(real_path)
|
| 230 |
-
name_no_ext = os.path.splitext(filename)[0]
|
| 231 |
-
|
| 232 |
-
# 如果是原图被选中了(假设你去掉了保护机制),方法名就是 origin
|
| 233 |
-
if "_origin" in name_no_ext:
|
| 234 |
-
method = "reference_origin"
|
| 235 |
-
else:
|
| 236 |
parts = name_no_ext.split('_', 1)
|
| 237 |
method = parts[1] if len(parts) > 1 else name_no_ext
|
|
|
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
str_methods = "; ".join(selected_methods)
|
| 243 |
-
|
| 244 |
-
save_user_vote(user_id, group_id, str_labels, str_methods)
|
| 245 |
-
|
| 246 |
user_state["index"] += 1
|
| 247 |
-
|
|
|
|
|
|
|
| 248 |
|
| 249 |
-
# --- 6. 界面构建 ---
|
| 250 |
with gr.Blocks(title="User Study") as demo:
|
| 251 |
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
|
|
|
| 259 |
|
| 260 |
-
#
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
)
|
| 268 |
-
|
| 269 |
-
status_box = gr.Textbox(value="当前未选择任何图片", label="当前选中状态", interactive=False)
|
| 270 |
-
|
| 271 |
-
with gr.Row():
|
| 272 |
-
btn_submit = gr.Button("✅ 提交选择", variant="primary", scale=2)
|
| 273 |
-
btn_none = gr.Button("🚫 都不满意", variant="stop", scale=1)
|
| 274 |
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
btn_submit.click(
|
| 293 |
-
fn=
|
| 294 |
-
inputs=[
|
| 295 |
-
outputs=[
|
| 296 |
)
|
| 297 |
-
|
| 298 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
btn_none.click(
|
| 300 |
-
fn=
|
| 301 |
-
inputs=[
|
| 302 |
-
outputs=[
|
| 303 |
)
|
| 304 |
|
| 305 |
if __name__ == "__main__":
|
|
|
|
| 8 |
from huggingface_hub import CommitScheduler
|
| 9 |
|
| 10 |
# --- 1. 配置区域 ---
|
| 11 |
+
DATASET_REPO_ID = "Emilyxml/moveit"
|
| 12 |
+
DATA_FOLDER = "data"
|
| 13 |
+
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",
|
|
|
|
| 24 |
token=TOKEN
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# --- 3. 数据加载 (不变) ---
|
| 28 |
def load_data():
|
| 29 |
groups = {}
|
| 30 |
if not os.path.exists(DATA_FOLDER):
|
|
|
|
| 34 |
for filename in os.listdir(DATA_FOLDER):
|
| 35 |
if filename.startswith('.'): continue
|
| 36 |
file_path = os.path.join(DATA_FOLDER, filename)
|
| 37 |
+
prefix = filename[:5]
|
| 38 |
|
| 39 |
if prefix not in groups:
|
|
|
|
| 40 |
groups[prefix] = {"origin": None, "candidates": [], "instruction": "暂无说明"}
|
| 41 |
|
|
|
|
| 42 |
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
|
|
|
| 43 |
if "_origin" in filename.lower():
|
| 44 |
groups[prefix]["origin"] = file_path
|
| 45 |
else:
|
| 46 |
groups[prefix]["candidates"].append(file_path)
|
|
|
|
|
|
|
| 47 |
elif filename.lower().endswith('.txt'):
|
| 48 |
try:
|
| 49 |
with open(file_path, "r", encoding="utf-8") as f:
|
|
|
|
| 52 |
with open(file_path, "r", encoding="gbk") as f:
|
| 53 |
groups[prefix]["instruction"] = f.read()
|
| 54 |
|
|
|
|
| 55 |
valid_groups = {}
|
| 56 |
for k, v in groups.items():
|
| 57 |
if v["origin"] is not None or len(v["candidates"]) > 0:
|
|
|
|
| 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
|
| 71 |
+
row = [user_id, datetime.now().strftime("%Y-%m-%d %H:%M:%S"), group_id, choice_labels, method_names]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
with scheduler.lock:
|
| 74 |
file_exists = user_file_path.exists()
|
|
|
|
| 77 |
if not file_exists:
|
| 78 |
writer.writerow(["user_id", "timestamp", "group_id", "selected_labels", "selected_methods"])
|
| 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)}"
|
| 117 |
+
candidate_info.append({"path": path, "label": label})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 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)
|
| 147 |
method = parts[1] if len(parts) > 1 else name_no_ext
|
| 148 |
+
methods.append(method)
|
| 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. 界面构建 (使用 @gr.render 实现自定义布局) ---
|
| 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([]) # list of dict
|
| 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 |
+
gr.Markdown(f"### 任务 ({idx + 1} / {len(ALL_GROUP_IDS)})\n\n{instruction}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
+
# 3. 显示原图 (Reference)
|
| 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("👈 **请参考左侧原图**,并在下方选择您认为质量最好的图片(可多选)。\n\n**点击图片下方的按钮进行选择。**")
|
| 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 |
+
current_sel.remove(idx)
|
| 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__":
|