YanmHa commited on
Commit
e1d6d08
·
verified ·
1 Parent(s): d425a13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +783 -29
app.py CHANGED
@@ -1,42 +1,796 @@
1
  import gradio as gr
2
  import os
3
- import shutil
4
- import tempfile
 
 
 
 
 
 
 
 
5
 
6
- # 目标保存路径
7
- SAVE_DIR = "/data/images/images"
 
 
 
 
 
 
8
 
9
- def upload_folder(folder_zip):
10
- if folder_zip is None:
11
- return "请上传一个文件夹(打包为 zip 文件)"
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- # 创建一个临时目录解压上传内容
14
- with tempfile.TemporaryDirectory() as tmpdir:
15
- shutil.unpack_archive(folder_zip.name, tmpdir)
 
16
 
17
- # 遍历所有文件并保存到目标路径
18
- for root, dirs, files in os.walk(tmpdir):
19
- for file in files:
20
- src_file_path = os.path.join(root, file)
21
 
22
- # 保持相对路径结构
23
- rel_path = os.path.relpath(src_file_path, tmpdir)
24
- dst_file_path = os.path.join(SAVE_DIR, rel_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
 
 
 
26
 
27
- os.makedirs(os.path.dirname(dst_file_path), exist_ok=True)
28
- shutil.copy2(src_file_path, dst_file_path)
 
 
 
 
29
 
 
 
 
 
 
 
 
 
 
30
 
31
- return f"上传完成,文件已保存到 {SAVE_DIR}"
 
 
 
32
 
33
- # Gradio 界面
34
- demo = gr.Interface(
35
- fn=upload_folder,
36
- inputs=gr.File(label="上传打包的文件夹(.zip 格式)", file_types=[".zip"]),
37
- outputs="text",
38
- title="上传文件夹内容到服务器",
39
- description="上传一个包含图像或其他文件的 zip 文件,内容将被保存到 `/data/images/images` 目录下,保留原始子目录结构。"
40
- )
41
 
42
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import os
3
+ import random
4
+ import time
5
+ from datetime import datetime
6
+ from functools import partial
7
+ import json
8
+ import io
9
+ from huggingface_hub import HfApi
10
+ from huggingface_hub.hf_api import HfHubHTTPError
11
+ import traceback
12
+ from itertools import combinations, product
13
 
14
+ # ==== 全局配置 ====
15
+ # ---- 测试模式开关 ----
16
+ REPEAT_SINGLE_TARGET_FOR_TESTING = False # 设置为 True 以启用“重复单一目标图”测试模式
17
+ NUM_REPEATED_TRIALS_FOR_TESTING = 5 # 在该测试模式下,单个目标图片重复的次数 (原为20,改为5方便测试)
18
+ # ---- 常规配置 ----
19
+ BASE_IMAGE_DIR = "/data/images/images"
20
+ TARGET_DIR_BASENAME = "gt"
21
+ TARGET_DIR = os.path.join(BASE_IMAGE_DIR, TARGET_DIR_BASENAME)
22
 
23
+ METHOD_ROOTS = []
24
+ if os.path.exists(BASE_IMAGE_DIR):
25
+ try:
26
+ METHOD_ROOTS = [
27
+ os.path.join(BASE_IMAGE_DIR, d)
28
+ for d in os.listdir(BASE_IMAGE_DIR)
29
+ if os.path.isdir(os.path.join(BASE_IMAGE_DIR, d)) and \
30
+ d != TARGET_DIR_BASENAME and \
31
+ not d.startswith('.')
32
+ ]
33
+ if not METHOD_ROOTS: print(f"警告:在 '{BASE_IMAGE_DIR}' 中没有找到有效的方法目录 (除了 '{TARGET_DIR_BASENAME}')。")
34
+ else: print(f"已识别的方法根目录 (原始): {METHOD_ROOTS}")
35
+ except Exception as e: print(f"错误:在扫描 '{BASE_IMAGE_DIR}' 时发生错误: {e}"); METHOD_ROOTS = []
36
+ else: print(f"警告:基础目录 '{BASE_IMAGE_DIR}' 不存在。将无法加载候选图片。")
37
 
38
+ SUBJECTS = ["subj01", "subj02", "subj05", "subj07"] # 修正了 "subj05," 的拼写
39
+ SENTINEL_TRIAL_INTERVAL = 20
40
+ NUM_TRIALS_PER_RUN = 100 # 正常运行时的每轮试验数
41
+ LOG_BATCH_SIZE = 20
42
 
43
+ DATASET_REPO_ID = "YanmHa/image-aligned-experiment-data"
44
+ BATCH_LOG_FOLDER = "run_logs_batch"
45
+ CSS = ".gr-block {margin-top: 4px !important; margin-bottom: 4px !important;} .compact_button { padding: 4px 8px; min-width: auto; }"
 
46
 
47
+ # ---- 测试模式的列表缩减逻辑 (仅当 REPEAT_SINGLE_TARGET_FOR_TESTING 为 True 时生效) ----
48
+ if REPEAT_SINGLE_TARGET_FOR_TESTING:
49
+ print(f"--- 特殊测试模式 (重复单一目标图) 已激活 ---")
50
+ NUM_TRIALS_PER_RUN = NUM_REPEATED_TRIALS_FOR_TESTING # 确保UI显示的数字和实际测试一致
51
+ print(f"测试模式:NUM_TRIALS_PER_RUN 已被设置为: {NUM_TRIALS_PER_RUN}")
52
+
53
+ if METHOD_ROOTS:
54
+ original_method_roots_count = len(METHOD_ROOTS)
55
+ METHOD_ROOTS = [METHOD_ROOTS[0]]
56
+ print(f"测试模式:METHOD_ROOTS 已从 {original_method_roots_count} 个缩减为仅包含第一个方法: {METHOD_ROOTS}")
57
+ else:
58
+ print("测试模式警告:METHOD_ROOTS 为空,无法缩减。")
59
+
60
+ if len(METHOD_ROOTS) == 1: # 只有一种方法时,确保至少有两个subject以形成对比
61
+ if len(SUBJECTS) >= 2:
62
+ original_subjects_count = len(SUBJECTS)
63
+ SUBJECTS = [SUBJECTS[0], SUBJECTS[1]]
64
+ print(f"测试模式:由于方法仅1种,SUBJECTS 已从 {original_subjects_count} 个缩减为前两个: {SUBJECTS}")
65
+ elif SUBJECTS:
66
+ print(f"测试模式:SUBJECTS 只有一个元素 ({SUBJECTS}),方法也只有一种。注意:可能无法形成候选对。")
67
+ else:
68
+ print("测试模式警告:SUBJECTS 为空。")
69
+ print(f"--- 特殊测试模式配置结束 ---")
70
+ else:
71
+ print(f"正常模式:使用完整配置。每轮目标试验数: {NUM_TRIALS_PER_RUN}")
72
+ print(f"方法根目录: {METHOD_ROOTS}")
73
+ print(f"Subjects: {SUBJECTS}")
74
 
75
+ # ==== 全局持久化历史记录 ====
76
+ PERSISTENT_STORAGE_BASE = "/data"
77
+ DATA_SUBDIR_NAME = "my_user_study_persistent_history"
78
 
79
+ if not os.path.exists(PERSISTENT_STORAGE_BASE):
80
+ try:
81
+ os.makedirs(PERSISTENT_STORAGE_BASE, exist_ok=True)
82
+ print(f"信息:基础持久化目录 '{PERSISTENT_STORAGE_BASE}' 尝试确保其存在。")
83
+ except Exception as e:
84
+ print(f"警告:操作基础持久化目录 '{PERSISTENT_STORAGE_BASE}' 时出现问题: {e}。")
85
 
86
+ full_subdir_path = os.path.join(PERSISTENT_STORAGE_BASE, DATA_SUBDIR_NAME)
87
+ if not os.path.exists(full_subdir_path):
88
+ try:
89
+ os.makedirs(full_subdir_path)
90
+ print(f"成功创建持久化子目录: {full_subdir_path}")
91
+ except Exception as e:
92
+ print(f"错误:创建持久化子目录 '{full_subdir_path}' 失败: {e}")
93
+ else:
94
+ print(f"信息:持久化子目录 '{full_subdir_path}' 已存在。")
95
 
96
+ GLOBAL_HISTORY_FILE = os.path.join(full_subdir_path, "global_experiment_shown_pairs.json")
97
+ if not (os.path.isdir(full_subdir_path) and os.access(full_subdir_path, os.W_OK)):
98
+ print(f"严重警告:持久化子目录 '{full_subdir_path}' 无效或不可写。")
99
+ print(f"全局历史文件将被加载/保存到: {GLOBAL_HISTORY_FILE}")
100
 
101
+ global_shown_pairs_cache = {}
102
+ global_history_has_unsaved_changes = False
103
+ exhausted_target_images = set()
 
 
 
 
 
104
 
105
+ def load_global_shown_pairs():
106
+ global global_shown_pairs_cache, global_history_has_unsaved_changes, exhausted_target_images
107
+ exhausted_target_images = set()
108
+
109
+ if not GLOBAL_HISTORY_FILE or not os.path.exists(GLOBAL_HISTORY_FILE):
110
+ print(f"信息:全局历史文件 '{GLOBAL_HISTORY_FILE}' 未找到或路径无效。将创建新的空历史记录。")
111
+ global_shown_pairs_cache = {}
112
+ global_history_has_unsaved_changes = False
113
+ return
114
+
115
+ try:
116
+ with open(GLOBAL_HISTORY_FILE, 'r', encoding='utf-8') as f:
117
+ content = f.read()
118
+ if not content.strip():
119
+ print(f"信息:全局历史文件 '{GLOBAL_HISTORY_FILE}' 为空。将使用空历史记录。")
120
+ global_shown_pairs_cache = {}
121
+ else:
122
+ data_from_file = json.loads(content)
123
+ global_shown_pairs_cache = {
124
+ target_img: {frozenset(pair) for pair in pairs_list}
125
+ for target_img, pairs_list in data_from_file.items()
126
+ }
127
+ print(f"已成功从 '{GLOBAL_HISTORY_FILE}' 加载全局已展示图片对历史。")
128
+ except json.JSONDecodeError as jde:
129
+ print(f"错误:加载全局历史文件 '{GLOBAL_HISTORY_FILE}' 失败 (JSON解析错误: {jde})。文件内容可能已损坏。将使用空历史记录。")
130
+ global_shown_pairs_cache = {}
131
+ except Exception as e:
132
+ print(f"错误:加载全局历史文件 '{GLOBAL_HISTORY_FILE}' 时发生其他错误: {e}。将使用空历史记录。")
133
+ global_shown_pairs_cache = {}
134
+ global_history_has_unsaved_changes = False
135
+
136
+ def save_global_shown_pairs():
137
+ global global_shown_pairs_cache, global_history_has_unsaved_changes
138
+ if not GLOBAL_HISTORY_FILE:
139
+ print("错误:GLOBAL_HISTORY_FILE 未定义。无法保存历史。")
140
+ return False
141
+ final_save_path = os.path.abspath(GLOBAL_HISTORY_FILE)
142
+ try:
143
+ parent_dir = os.path.dirname(final_save_path)
144
+ if not os.path.exists(parent_dir):
145
+ try:
146
+ os.makedirs(parent_dir, exist_ok=True)
147
+ print(f"信息: 为保存历史文件,创建了父目录 {parent_dir}")
148
+ except Exception as e_mkdir:
149
+ print(f"错误: 创建历史文件的父目录 {parent_dir} 失败: {e_mkdir}。保存可能失败。")
150
+ return False
151
+ data_to_save = {
152
+ target_img: [sorted(list(pair_fset)) for pair_fset in pairs_set]
153
+ for target_img, pairs_set in global_shown_pairs_cache.items()
154
+ }
155
+
156
+ temp_file_path = final_save_path + ".tmp"
157
+ with open(temp_file_path, 'w', encoding='utf-8') as f:
158
+ json.dump(data_to_save, f, ensure_ascii=False, indent=2)
159
+ os.replace(temp_file_path, final_save_path)
160
+ print(f"已成功将全局已展示图片对历史保存到 '{final_save_path}'。")
161
+ global_history_has_unsaved_changes = False
162
+ return True
163
+ except Exception as e:
164
+ print(f"错误:保存全局历史文件 '{final_save_path}' 失败: {e}")
165
+ return False
166
+
167
+ load_global_shown_pairs()
168
+
169
+ # ==== 加载所有可用的目标图片 ====
170
+ master_image_list = []
171
+ if os.path.exists(TARGET_DIR):
172
+ try:
173
+ master_image_list = sorted(
174
+ [f for f in os.listdir(TARGET_DIR) if f.lower().endswith((".jpg", ".png", ".jpeg"))],
175
+ key=lambda x: int(os.path.splitext(x)[0])
176
+ )
177
+ except ValueError:
178
+ master_image_list = sorted([f for f in os.listdir(TARGET_DIR) if f.lower().endswith((".jpg", ".png", ".jpeg"))])
179
+ if master_image_list: print(f"警告: '{TARGET_DIR}' 文件名非纯数字,按字母排序。")
180
+ if not master_image_list: print(f"警告:�� '{TARGET_DIR}' 中无有效图片。")
181
+ elif not os.path.exists(TARGET_DIR) and os.path.exists(BASE_IMAGE_DIR): print(f"错误:目标目录 '{TARGET_DIR}' 未找到。")
182
+
183
+ # ---- 测试模式:缩减 master_image_list (仅当 REPEAT_SINGLE_TARGET_FOR_TESTING 为 True 时生效) ----
184
+ if REPEAT_SINGLE_TARGET_FOR_TESTING:
185
+ if not master_image_list:
186
+ print(f"测试模式错误:master_image_list 为空,无法进行重复单一目标图测试。")
187
+ else:
188
+ original_first_image = master_image_list[0]
189
+ master_image_list = [original_first_image]
190
+ print(f"测试模式:master_image_list 已被缩减为原列表的第一个图像: {master_image_list}")
191
+ if not master_image_list:
192
+ print(f"关键错误:无目标图片可用 (master_image_list为空)。实验无法进行。")
193
+
194
+ # ==== 辅助函数 ====
195
+ # #############################################################################
196
+ # ############# 函数修改点:get_next_trial_info ################################
197
+ # #############################################################################
198
+ def get_next_trial_info(current_trial_idx_in_run, current_run_image_list_for_trial, num_trials_in_this_run_for_trial):
199
+ global TARGET_DIR, METHOD_ROOTS, SUBJECTS, SENTINEL_TRIAL_INTERVAL
200
+ global global_shown_pairs_cache, global_history_has_unsaved_changes, exhausted_target_images
201
+
202
+ if not current_run_image_list_for_trial or current_trial_idx_in_run >= num_trials_in_this_run_for_trial:
203
+ return None, current_trial_idx_in_run
204
+
205
+ img_filename_original = current_run_image_list_for_trial[current_trial_idx_in_run]
206
+ target_full_path = os.path.join(TARGET_DIR, img_filename_original)
207
+ trial_number_for_display = current_trial_idx_in_run + 1
208
+
209
+ # ---- MODIFICATION START: Create two separate pools for candidates ----
210
+ pool_generated_color = []
211
+ pool_other_methods = []
212
+
213
+ for m_root_path in METHOD_ROOTS:
214
+ method_name = os.path.basename(m_root_path)
215
+
216
+ subjects_for_method = SUBJECTS
217
+ if method_name.lower() == "takagi":
218
+ if "subj01" in SUBJECTS:
219
+ subjects_for_method = ["subj01"]
220
+ else:
221
+ continue
222
+
223
+ for s_id in subjects_for_method:
224
+ base, ext = os.path.splitext(img_filename_original)
225
+ reconstructed_filename = f"{base}_0{ext}"
226
+ candidate_path = os.path.join(m_root_path, s_id, reconstructed_filename)
227
+ if os.path.exists(candidate_path):
228
+ internal_label = f"{method_name}/{s_id}/{reconstructed_filename}"
229
+ candidate_tuple = (internal_label, candidate_path)
230
+
231
+ # Segregate candidates into the two pools
232
+ if method_name == "generated_images_color":
233
+ pool_generated_color.append(candidate_tuple)
234
+ else:
235
+ pool_other_methods.append(candidate_tuple)
236
+ # ---- MODIFICATION END: Candidate pools are now populated ----
237
+
238
+ trial_info = {"image_id": img_filename_original, "target_path": target_full_path, "cur_no": trial_number_for_display, "is_sentinel": False,
239
+ "left_display_label": "N/A", "left_internal_label": "N/A", "left_path": None,
240
+ "right_display_label": "N/A", "right_internal_label": "N/A", "right_path": None}
241
+
242
+ is_potential_sentinel_trial = (trial_number_for_display > 0 and trial_number_for_display % SENTINEL_TRIAL_INTERVAL == 0)
243
+
244
+ if is_potential_sentinel_trial:
245
+ # For sentinel trials, we just need one random reconstruction. Combine pools to pick one.
246
+ combined_pool = pool_generated_color + pool_other_methods
247
+ if not combined_pool:
248
+ print(f"警告:哨兵图 '{img_filename_original}' (trial {trial_number_for_display}) 无任何候选图。")
249
+ else:
250
+ print(f"生成哨兵试验 for '{img_filename_original}' (trial {trial_number_for_display})")
251
+ trial_info["is_sentinel"] = True
252
+ sentinel_candidate_target_tuple = ("目标图像", target_full_path)
253
+ random_reconstruction_candidate_tuple = random.choice(combined_pool)
254
+ candidates_for_sentinel = [
255
+ (("目标图像", target_full_path), sentinel_candidate_target_tuple[0]),
256
+ (("重建图", random_reconstruction_candidate_tuple[1]), random_reconstruction_candidate_tuple[0])
257
+ ]
258
+ random.shuffle(candidates_for_sentinel)
259
+ trial_info.update({
260
+ "left_display_label": candidates_for_sentinel[0][0][0], "left_path": candidates_for_sentinel[0][0][1], "left_internal_label": candidates_for_sentinel[0][1],
261
+ "right_display_label": candidates_for_sentinel[1][0][0], "right_path": candidates_for_sentinel[1][0][1], "right_internal_label": candidates_for_sentinel[1][1],
262
+ })
263
+ else: # 常规试验
264
+ # ---- MODIFICATION START: New check and pairing logic ----
265
+ # Check if both pools have at least one candidate
266
+ if not pool_generated_color or not pool_other_methods:
267
+ print(f"警告:常规图 '{img_filename_original}' (trial {trial_number_for_display}) 候选不足以形成指定对。 "
268
+ f"('generated_images_color' 找到 {len(pool_generated_color)} 个, "
269
+ f"其他方法找到 {len(pool_other_methods)} 个)。此试验无法进行。")
270
+ return None, current_trial_idx_in_run
271
+
272
+ target_global_history_set = global_shown_pairs_cache.setdefault(img_filename_original, set())
273
+
274
+ # Generate all pairs by picking one from each pool
275
+ all_possible_pairs_in_pool = []
276
+ for c_color, c_other in product(pool_generated_color, pool_other_methods):
277
+ pair_labels_fset = frozenset({c_color[0], c_other[0]})
278
+ all_possible_pairs_in_pool.append( ((c_color, c_other), pair_labels_fset) )
279
+ # ---- MODIFICATION END: New pairing logic is complete ----
280
+
281
+ unseen_globally_pairs_with_data = [
282
+ item for item in all_possible_pairs_in_pool if item[1] not in target_global_history_set
283
+ ]
284
+ selected_candidates_tuples = None
285
+
286
+ if unseen_globally_pairs_with_data:
287
+ chosen_pair_data_and_labels = random.choice(unseen_globally_pairs_with_data)
288
+ selected_candidates_tuples = chosen_pair_data_and_labels[0]
289
+ chosen_pair_frozenset = chosen_pair_data_and_labels[1]
290
+ target_global_history_set.add(chosen_pair_frozenset)
291
+ global_history_has_unsaved_changes = True
292
+ else:
293
+ print(f"警告:目标图 '{img_filename_original}' (trial {trial_number_for_display}): 所有 ({len(all_possible_pairs_in_pool)}) 个 'generated_color' vs 'other' 对均已在全局展示过。")
294
+ if all_possible_pairs_in_pool:
295
+ print(f"目标图 '{img_filename_original}' 将被标记为已耗尽,未来轮次中将被跳过。")
296
+ exhausted_target_images.add(img_filename_original)
297
+ return None, current_trial_idx_in_run
298
+
299
+ display_order_candidates = list(selected_candidates_tuples)
300
+ if random.random() > 0.5:
301
+ display_order_candidates = display_order_candidates[::-1]
302
+ trial_info.update({
303
+ "left_display_label": "候选图 1", "left_path": display_order_candidates[0][1], "left_internal_label": display_order_candidates[0][0],
304
+ "right_display_label": "候选图 2", "right_path": display_order_candidates[1][1], "right_internal_label": display_order_candidates[1][0],
305
+ })
306
+ return trial_info, current_trial_idx_in_run + 1
307
+
308
+ # ==== 批量保存用户选择日志函数 (保持不变) ====
309
+ def save_single_log_to_hf_dataset(log_entry, user_identifier_str):
310
+ global DATASET_REPO_ID, INDIVIDUAL_LOGS_FOLDER
311
+ if not isinstance(log_entry, dict):
312
+ print(f"错误:单个日志条目不是字典格式,无法保存:{log_entry}")
313
+ return False
314
+ current_user_id = user_identifier_str if user_identifier_str else "unknown_user_session"
315
+ identifier_safe = str(current_user_id).replace('.', '_').replace(':', '_').replace('/', '_')
316
+ print(f"用户 {identifier_safe} - 准备保存单条日志 for image {log_entry.get('image_id', 'Unknown')}...")
317
+ try:
318
+ token = os.getenv("HF_TOKEN")
319
+ if not token:
320
+ print("错误:环境变量 HF_TOKEN 未设置。无法保存单条日志到Dataset。")
321
+ return False
322
+ if not DATASET_REPO_ID:
323
+ print("错误:DATASET_REPO_ID 未配置。无法保存单条日志到Dataset。")
324
+ return False
325
+ api = HfApi(token=token)
326
+ image_id_safe_for_filename = os.path.splitext(log_entry.get("image_id", "unknown_img"))[0].replace('.', '_').replace(':', '_').replace('/', '_')
327
+ file_creation_timestamp_str = datetime.now().strftime('%Y%m%d_%H%M%S_%f')
328
+ unique_filename = (f"run{log_entry.get('run_no', 'X')}_trial{log_entry.get('trial_sequence_in_run', 'Y')}_img{image_id_safe_for_filename}_{file_creation_timestamp_str}.json")
329
+ path_in_repo = f"{INDIVIDUAL_LOGS_FOLDER}/{identifier_safe}/{unique_filename}"
330
+
331
+ try:
332
+ json_content = json.dumps(log_entry, ensure_ascii=False, indent=2)
333
+ except Exception as json_err:
334
+ print(f"错误:序列化单条日志时出错: {log_entry}. 错误: {json_err}")
335
+ error_log_content = {"error": "serialization_failed_single", "original_data_keys": list(log_entry.keys()) if isinstance(log_entry, dict) else None, "timestamp": datetime.now().isoformat()}
336
+ json_content = json.dumps(error_log_content, ensure_ascii=False, indent=2)
337
+
338
+ log_bytes = json_content.encode('utf-8')
339
+ file_like_object = io.BytesIO(log_bytes)
340
+ print(f"准备上传单条日志文件: {path_in_repo} ({len(log_bytes)} bytes)")
341
+ api.upload_file(
342
+ path_or_fileobj=file_like_object,
343
+ path_in_repo=path_in_repo,
344
+ repo_id=DATASET_REPO_ID,
345
+ repo_type="dataset",
346
+ commit_message=(f"Log choice: img {log_entry.get('image_id', 'N/A')}, run {log_entry.get('run_no', 'N/A')}, trial {log_entry.get('trial_sequence_in_run', 'N/A')} by {identifier_safe}")
347
+ )
348
+ print(f"单条日志已成功保存到 HF Dataset: {DATASET_REPO_ID}/{path_in_repo}")
349
+ return True
350
+ except HfHubHTTPError as hf_http_error:
351
+ print(f"保存单条日志到 Hugging Face Dataset 时发生 HTTP 错误 (可能被限流或权限问题): {hf_http_error}")
352
+ traceback.print_exc()
353
+ return False
354
+ except Exception as e:
355
+ print(f"保存单条日志 (image {log_entry.get('image_id', 'Unknown')}, user {identifier_safe}) 到 Hugging Face Dataset 时发生严重错误: {e}")
356
+ traceback.print_exc()
357
+ return False
358
+
359
+ # ==== 批量保存用户选择日志函数 (确保返回 True/False) ====
360
+ def save_collected_logs_batch(list_of_log_entries, user_identifier_str, batch_identifier):
361
+ global DATASET_REPO_ID, BATCH_LOG_FOLDER
362
+ if not list_of_log_entries:
363
+ print("批量保存用户日志:没有累积的日志。")
364
+ return True
365
+ identifier_safe = str(user_identifier_str if user_identifier_str else "unknown_user_session").replace('.', '_').replace(':', '_').replace('/', '_').replace(' ', '_')
366
+ print(f"用户 {identifier_safe} - 准备批量保存 {len(list_of_log_entries)} 条选择日志 (批次标识: {batch_identifier})...")
367
+ try:
368
+ token = os.getenv("HF_TOKEN")
369
+ if not token:
370
+ print("错误:HF_TOKEN 未设置。无法批量保存选择日志。")
371
+ return False
372
+ if not DATASET_REPO_ID:
373
+ print("错误:DATASET_REPO_ID 未配置。无法批量保存选择日志。")
374
+ return False
375
+ api = HfApi(token=token)
376
+ timestamp_str = datetime.now().strftime('%Y%m%d_%H%M%S_%f')
377
+ batch_filename = f"batch_user-{identifier_safe}_id-{batch_identifier}_{timestamp_str}_logs-{len(list_of_log_entries)}.jsonl"
378
+ path_in_repo = f"{BATCH_LOG_FOLDER}/{identifier_safe}/{batch_filename}"
379
+ jsonl_content = ""
380
+ for log_entry in list_of_log_entries:
381
+ try:
382
+ if isinstance(log_entry, dict): jsonl_content += json.dumps(log_entry, ensure_ascii=False) + "\n"
383
+ else: print(f"警告:批量保存选择日志时,条目非字典:{log_entry}")
384
+ except Exception as json_err:
385
+ print(f"错误:批量保存选择日志序列化单条时出错: {log_entry}. 错误: {json_err}")
386
+ jsonl_content += json.dumps({"error": "serialization_failed_in_batch_user_log", "original_data_preview": str(log_entry)[:100],"timestamp": datetime.now().isoformat()}, ensure_ascii=False) + "\n"
387
+
388
+ if not jsonl_content.strip():
389
+ print(f"用户 {identifier_safe} (批次 {batch_identifier}) 无可序列化选择日志。")
390
+ return True
391
+
392
+ log_bytes = jsonl_content.encode('utf-8')
393
+ file_like_object = io.BytesIO(log_bytes)
394
+ print(f"准备批量上传选择日志文件: {path_in_repo} ({len(log_bytes)} bytes)")
395
+ api.upload_file(
396
+ path_or_fileobj=file_like_object,
397
+ path_in_repo=path_in_repo,
398
+ repo_id=DATASET_REPO_ID,
399
+ repo_type="dataset",
400
+ commit_message=f"Batch user choice logs for {identifier_safe}, batch_id {batch_identifier} ({len(list_of_log_entries)} entries)"
401
+ )
402
+ print(f"批量选择日志已成功保存到 HF Dataset: {DATASET_REPO_ID}/{path_in_repo}")
403
+ return True
404
+ except HfHubHTTPError as hf_http_error:
405
+ print(f"批量保存选择日志到 Hugging Face Dataset 时发生 HTTP 错误 (可能被限流或权限问题): {hf_http_error}")
406
+ traceback.print_exc()
407
+ return False
408
+ except Exception as e:
409
+ print(f"批量保存选择日志 (user {identifier_safe}, batch_id {batch_identifier}) 失败: {e}")
410
+ traceback.print_exc()
411
+ return False
412
+
413
+
414
+ # ==== 主要的 Gradio 事件处理函数 ====
415
+ def process_experiment_step(
416
+ s_trial_idx_val, s_run_no_val, s_user_logs_val, s_current_trial_data_val, s_user_session_id_val,
417
+ s_current_run_image_list_val, s_num_trials_this_run_val,
418
+ action_type=None, choice_value=None, request: gr.Request = None
419
+ ):
420
+ global master_image_list, NUM_TRIALS_PER_RUN, outputs_ui_components_definition, LOG_BATCH_SIZE
421
+ global REPEAT_SINGLE_TARGET_FOR_TESTING, NUM_REPEATED_TRIALS_FOR_TESTING
422
+ global exhausted_target_images, global_history_has_unsaved_changes
423
+
424
+ output_s_trial_idx = s_trial_idx_val; output_s_run_no = s_run_no_val
425
+ output_s_user_logs = list(s_user_logs_val); output_s_current_trial_data = dict(s_current_trial_data_val) if s_current_trial_data_val else {}
426
+ output_s_user_session_id = s_user_session_id_val; output_s_current_run_image_list = list(s_current_run_image_list_val)
427
+ output_s_num_trials_this_run = s_num_trials_this_run_val
428
+ user_ip_fallback = request.client.host if request else "unknown_ip"
429
+ user_identifier_for_logging = output_s_user_session_id if output_s_user_session_id else user_ip_fallback
430
+
431
+ len_ui_outputs = len(outputs_ui_components_definition)
432
+
433
+ def create_ui_error_tuple(message, progress_msg_text, stop_experiment=False):
434
+ btn_start_interactive = not stop_experiment
435
+ btn_choices_interactive = not stop_experiment
436
+ return (gr.update(visible=False),) * 3 + \
437
+ ("", "") + \
438
+ (message, progress_msg_text) + \
439
+ (gr.update(interactive=btn_start_interactive), gr.update(interactive=btn_choices_interactive), gr.update(interactive=btn_choices_interactive)) + \
440
+ (gr.update(visible=False),)
441
+
442
+ def create_no_change_tuple(): return (gr.update(),) * len_ui_outputs
443
+ user_id_display_text = output_s_user_session_id if output_s_user_session_id else "用户ID待分配"
444
+
445
+ if action_type == "record_choice":
446
+ if output_s_current_trial_data.get("data") and output_s_current_trial_data["data"].get("left_internal_label"):
447
+ chosen_internal_label = (output_s_current_trial_data["data"]["left_internal_label"] if choice_value == "left" else output_s_current_trial_data["data"]["right_internal_label"])
448
+ parsed_chosen_method, parsed_chosen_subject, parsed_chosen_filename = "N/A", "N/A", "N/A"
449
+ if chosen_internal_label == "目标图像": parsed_chosen_method, parsed_chosen_subject, parsed_chosen_filename = "TARGET", "GT", output_s_current_trial_data["data"]["image_id"]
450
+ else:
451
+ parts = chosen_internal_label.split('/');
452
+ if len(parts) == 3: parsed_chosen_method, parsed_chosen_subject, parsed_chosen_filename = parts[0].strip(), parts[1].strip(), parts[2].strip()
453
+ elif len(parts) == 2: parsed_chosen_method, parsed_chosen_subject = parts[0].strip(), parts[1].strip()
454
+ elif len(parts) == 1: parsed_chosen_method = parts[0].strip()
455
+ log_entry = {
456
+ "timestamp": datetime.now().isoformat(), "user_identifier": user_identifier_for_logging, "run_no": output_s_run_no,
457
+ "image_id": output_s_current_trial_data["data"]["image_id"],
458
+ "left_internal_label": output_s_current_trial_data["data"]["left_internal_label"],
459
+ "right_internal_label": output_s_current_trial_data["data"]["right_internal_label"],
460
+ "chosen_side": choice_value, "chosen_internal_label": chosen_internal_label,
461
+ "chosen_method": parsed_chosen_method, "chosen_subject": parsed_chosen_subject, "chosen_filename": parsed_chosen_filename,
462
+ "trial_sequence_in_run": output_s_current_trial_data["data"]["cur_no"],
463
+ "is_sentinel": output_s_current_trial_data["data"]["is_sentinel"]
464
+ }
465
+ output_s_user_logs.append(log_entry)
466
+ print(f"用户 {user_identifier_for_logging} 记录选择 (img: {log_entry['image_id']})。当前批次日志数: {len(output_s_user_logs)}")
467
+
468
+ if len(output_s_user_logs) >= LOG_BATCH_SIZE:
469
+ print(f"累积用户选择日志达到 {LOG_BATCH_SIZE} 条,准备批量保存...")
470
+ batch_id_for_filename = f"run{output_s_run_no}_trialidx{output_s_trial_idx}_logcount{len(output_s_user_logs)}"
471
+ user_logs_save_success = save_collected_logs_batch(list(output_s_user_logs), user_identifier_for_logging, batch_id_for_filename)
472
+ if user_logs_save_success:
473
+ print("批量用户选择日志已成功(或尝试)保存,将清空累积的用户选择日志列表。")
474
+ output_s_user_logs = []
475
+ else:
476
+ print("严重错误:批量用户选择日志保存失败。实验无法继续。")
477
+ error_message_ui = "错误:日志保存失败,可能是网络问题或API限流。实验已停止,请联系管理员。"
478
+ progress_message_ui = f"用户ID: {user_id_display_text} | 实验因错误停止在第 {output_s_run_no} 轮,试验 {output_s_trial_idx+1}"
479
+ error_ui_updates = create_ui_error_tuple(error_message_ui, progress_message_ui, stop_experiment=True)
480
+ return output_s_trial_idx, output_s_run_no, output_s_user_logs, output_s_current_trial_data, \
481
+ output_s_user_session_id, output_s_current_run_image_list, output_s_num_trials_this_run, *error_ui_updates
482
+
483
+ if global_history_has_unsaved_changes:
484
+ print("检测到全局图片对历史自上次保存后有更新,将一并保存...")
485
+ if not save_global_shown_pairs():
486
+ print("严重错误:全局图片对历史保存失败。实验无法继续。")
487
+ error_message_ui = "错误:全局历史数据保存失败。实验已停止,请联系管理员。"
488
+ progress_message_ui = f"用户ID: {user_id_display_text} | 实验因错误停止在第 {output_s_run_no} 轮,试验 {output_s_trial_idx+1}"
489
+ error_ui_updates = create_ui_error_tuple(error_message_ui, progress_message_ui, stop_experiment=True)
490
+ return output_s_trial_idx, output_s_run_no, output_s_user_logs, output_s_current_trial_data, \
491
+ output_s_user_session_id, output_s_current_run_image_list, output_s_num_trials_this_run, *error_ui_updates
492
+ else:
493
+ print(f"用户 {user_identifier_for_logging} 错误:记录选择时当前试验数据为空或缺少internal_label!")
494
+ error_ui_updates = create_ui_error_tuple("记录选择时内部错误。", f"用户ID: {user_id_display_text} | 进度:{output_s_trial_idx}/{output_s_num_trials_this_run}", stop_experiment=False)
495
+ return output_s_trial_idx, output_s_run_no, output_s_user_logs, output_s_current_trial_data, output_s_user_session_id, output_s_current_run_image_list, output_s_num_trials_this_run, *error_ui_updates
496
+
497
+ if action_type == "start_experiment":
498
+ is_first = (output_s_num_trials_this_run == 0 and output_s_trial_idx == 0 and output_s_run_no == 1)
499
+ is_completed_for_restart = (output_s_num_trials_this_run > 0 and output_s_trial_idx >= output_s_num_trials_this_run)
500
+
501
+ if is_completed_for_restart:
502
+ if output_s_user_logs:
503
+ print(f"轮次 {output_s_run_no-1} 结束,尝试保存剩余的 {len(output_s_user_logs)} 条用户选择日志...")
504
+ batch_id_for_filename = f"run{output_s_run_no-1}_final_logcount{len(output_s_user_logs)}"
505
+ if not save_collected_logs_batch(list(output_s_user_logs), user_identifier_for_logging, batch_id_for_filename):
506
+ print("严重错误:保存上一轮剩余用户选择日志失败。实验无法继续。")
507
+ error_message_ui = "错误:日志保存失败。实验已停止,请联系管理员。"
508
+ progress_message_ui = f"用户ID: {user_id_display_text} | 实验因错误停止"
509
+ error_ui_updates = create_ui_error_tuple(error_message_ui, progress_message_ui, stop_experiment=True)
510
+ return output_s_trial_idx, output_s_run_no-1, output_s_user_logs, output_s_current_trial_data, \
511
+ output_s_user_session_id, output_s_current_run_image_list, output_s_num_trials_this_run, *error_ui_updates
512
+ output_s_user_logs = []
513
+
514
+ if global_history_has_unsaved_changes:
515
+ print("轮次结束,尝试保存全局图片对历史...")
516
+ if not save_global_shown_pairs():
517
+ print("严重错误:全局历史数据保存失败。实验无法继续。")
518
+ error_message_ui = "错误:全局历史数据保存失败。实验已停止,请联系管理员。"
519
+ progress_message_ui = f"用户ID: {user_id_display_text} | 实验因错误停止"
520
+ error_ui_updates = create_ui_error_tuple(error_message_ui, progress_message_ui, stop_experiment=True)
521
+ return output_s_trial_idx, output_s_run_no-1, output_s_user_logs, output_s_current_trial_data, \
522
+ output_s_user_session_id, output_s_current_run_image_list, output_s_num_trials_this_run, *error_ui_updates
523
+
524
+ if is_first or is_completed_for_restart:
525
+ if is_completed_for_restart: output_s_run_no += 1
526
+ available_master_images = [img for img in master_image_list if img not in exhausted_target_images]
527
+ print(f"开始轮次 {output_s_run_no}: 从 {len(master_image_list)}个总目标图片中筛选,可用图片 {len(available_master_images)}个 (已排除 {len(exhausted_target_images)}个已耗尽图片).")
528
+ if not available_master_images:
529
+ msg = "所有目标图片的所有唯一图片对均已展示完毕!感谢您的参与。"
530
+ prog_text = f"用户ID: {user_id_display_text} | 实验完成!"
531
+ if output_s_user_logs:
532
+ print(f"最终轮次结束,尝试保存剩余的 {len(output_s_user_logs)} 条用户选择日志...")
533
+ batch_id_for_filename = f"run{output_s_run_no-1}_final_logcount{len(output_s_user_logs)}"
534
+ save_collected_logs_batch(list(output_s_user_logs), user_identifier_for_logging, batch_id_for_filename)
535
+ output_s_user_logs = []
536
+ if global_history_has_unsaved_changes:
537
+ print("实验最终结束,尝试保存全局图片对历史...")
538
+ save_global_shown_pairs()
539
+
540
+ ui_updates = list(create_ui_error_tuple(msg, prog_text, stop_experiment=True))
541
+ return 0, output_s_run_no, [], {}, output_s_user_session_id, [], 0, *tuple(ui_updates)
542
+
543
+ if REPEAT_SINGLE_TARGET_FOR_TESTING and available_master_images:
544
+ print(f"测试模式 (重复单一目标图) 已激活。")
545
+ single_image_to_repeat = available_master_images[0]
546
+ output_s_current_run_image_list = [single_image_to_repeat] * NUM_REPEATED_TRIALS_FOR_TESTING
547
+ output_s_num_trials_this_run = NUM_REPEATED_TRIALS_FOR_TESTING
548
+ print(f"测试模式:本轮将重复目标图片 '{single_image_to_repeat}' 共 {output_s_num_trials_this_run} 次。")
549
+ else:
550
+ num_really_avail = len(available_master_images)
551
+ current_run_max_trials = NUM_TRIALS_PER_RUN
552
+ run_size = min(num_really_avail, current_run_max_trials)
553
+ if run_size == 0:
554
+ error_ui = create_ui_error_tuple("错误: 可用图片采样数为0!", f"用户ID: {user_id_display_text} | 进度: 0/0", stop_experiment=False)
555
+ return 0, output_s_run_no, output_s_user_logs, {}, output_s_user_session_id, [], 0, *error_ui
556
+ output_s_current_run_image_list = random.sample(available_master_images, run_size)
557
+ output_s_num_trials_this_run = run_size
558
+
559
+ output_s_trial_idx = 0
560
+ output_s_current_trial_data = {}
561
+ if is_first:
562
+ timestamp_str = datetime.now().strftime('%Y%m%d%H%M%S%f'); random_val = random.randint(10000, 99999)
563
+ if not output_s_user_session_id:
564
+ output_s_user_session_id = f"user_{timestamp_str}_{random_val}"; user_identifier_for_logging = output_s_user_session_id
565
+ else:
566
+ user_identifier_for_logging = output_s_user_session_id
567
+ print(f"用户会话ID: {output_s_user_session_id}")
568
+ print(f"开始/继续轮次 {output_s_run_no} (用户ID: {output_s_user_session_id}). 本轮共 {output_s_num_trials_this_run} 个试验。")
569
+ else:
570
+ print(f"用户 {user_identifier_for_logging} 在第 {output_s_run_no} 轮,试验 {output_s_trial_idx} 点击开始,但轮次未完成。忽略。")
571
+ no_change_ui = create_no_change_tuple()
572
+ return output_s_trial_idx, output_s_run_no, output_s_user_logs, output_s_current_trial_data, output_s_user_session_id, output_s_current_run_image_list, output_s_num_trials_this_run, *no_change_ui
573
+
574
+ current_actual_trial_index_for_get_next = output_s_trial_idx
575
+
576
+ if current_actual_trial_index_for_get_next >= output_s_num_trials_this_run and output_s_num_trials_this_run > 0:
577
+ prog_text = f"用户ID: {output_s_user_session_id} | 进度:{output_s_num_trials_this_run}/{output_s_num_trials_this_run} | 第 {output_s_run_no} 轮 🎉"
578
+ ui_updates = list(create_ui_error_tuple(f"🎉 第 {output_s_run_no} 轮完成!请点击“开始试验 / 下一轮”继续。", prog_text, stop_experiment=False))
579
+ ui_updates[7]=gr.update(interactive=True); ui_updates[8]=gr.update(interactive=False); ui_updates[9]=gr.update(interactive=False)
580
+ ui_updates[0]=gr.update(value=None,visible=False); ui_updates[1]=gr.update(value=None,visible=False); ui_updates[2]=gr.update(value=None,visible=False)
581
+ yield output_s_trial_idx, output_s_run_no, output_s_user_logs, {"data": None}, output_s_user_session_id, output_s_current_run_image_list, output_s_num_trials_this_run, *ui_updates; return
582
+
583
+
584
+ if not output_s_current_run_image_list or output_s_num_trials_this_run == 0:
585
+ error_ui = create_ui_error_tuple("错误: 无法加载试验图片 (列表为空或试验数为0)", f"用户ID: {user_id_display_text} | 进度: N/A", stop_experiment=False)
586
+ return output_s_trial_idx, output_s_run_no, output_s_user_logs, {"data": None}, output_s_user_session_id, [], 0, *error_ui
587
+
588
+ trial_info = None
589
+ next_s_trial_idx_for_state_loop = current_actual_trial_index_for_get_next
590
+
591
+ while next_s_trial_idx_for_state_loop < output_s_num_trials_this_run:
592
+ current_target_image_for_trial = output_s_current_run_image_list[next_s_trial_idx_for_state_loop]
593
+ if current_target_image_for_trial in exhausted_target_images:
594
+ print(f"信息:目标图 '{current_target_image_for_trial}' 已在全局耗尽列表中,跳过此试验。")
595
+ next_s_trial_idx_for_state_loop += 1
596
+ output_s_trial_idx = next_s_trial_idx_for_state_loop
597
+ continue
598
+
599
+ _trial_info_candidate, _returned_next_idx = get_next_trial_info(next_s_trial_idx_for_state_loop, output_s_current_run_image_list, output_s_num_trials_this_run)
600
+
601
+ if _trial_info_candidate is not None:
602
+ trial_info = _trial_info_candidate
603
+ output_s_trial_idx = _returned_next_idx
604
+ break
605
+ else:
606
+ print(f"信息:目标图 '{current_target_image_for_trial}' 无法生成有效试验。尝试列表中的下一个。")
607
+ next_s_trial_idx_for_state_loop +=1
608
+ output_s_trial_idx = next_s_trial_idx_for_state_loop
609
+
610
+ if trial_info is None:
611
+ print(f"轮次 {output_s_run_no} 中没有更多可用的有效试验了。结束本轮。")
612
+ if output_s_user_logs:
613
+ print(f"轮次 {output_s_run_no} 无更多有效试验,尝试保存剩余 {len(output_s_user_logs)} 条日志...")
614
+ batch_id_for_filename = f"run{output_s_run_no}_no_more_trials_logcount{len(output_s_user_logs)}"
615
+ if not save_collected_logs_batch(list(output_s_user_logs), user_identifier_for_logging, batch_id_for_filename):
616
+ print("严重错误:保存剩余日志失败。实验可能需要停止。")
617
+ output_s_user_logs = []
618
+ if global_history_has_unsaved_changes:
619
+ print("轮次无更多有效试验,尝试保存全局图片对历史...")
620
+ if not save_global_shown_pairs():
621
+ print("严重错误:全局历史数据保存失败。实验可能需要停止。")
622
+
623
+ prog_text = f"用户ID: {output_s_user_session_id} | 进度:{output_s_num_trials_this_run}/{output_s_num_trials_this_run} | 第 {output_s_run_no} 轮 (无更多可用试验)"
624
+ ui_updates = list(create_ui_error_tuple(f"第 {output_s_run_no} 轮因无更多可用试验而结束。请点击“开始试验 / 下一轮”。", prog_text, stop_experiment=False))
625
+ ui_updates[7]=gr.update(interactive=True); ui_updates[8]=gr.update(interactive=False); ui_updates[9]=gr.update(interactive=False)
626
+ ui_updates[0]=gr.update(value=None,visible=False); ui_updates[1]=gr.update(value=None,visible=False); ui_updates[2]=gr.update(value=None,visible=False)
627
+ yield output_s_num_trials_this_run, output_s_run_no, output_s_user_logs, {"data": None}, output_s_user_session_id, output_s_current_run_image_list, output_s_num_trials_this_run, *ui_updates; return
628
+
629
+ output_s_current_trial_data = {"data": trial_info}
630
+ prog_text = f"用户ID: {output_s_user_session_id} | 进度:{trial_info['cur_no']}/{output_s_num_trials_this_run} | 第 {output_s_run_no} 轮"
631
+
632
+ ui_show_target_updates = list(create_no_change_tuple())
633
+ ui_show_target_updates[0]=gr.update(value=trial_info["target_path"],visible=True); ui_show_target_updates[1]=gr.update(value=None,visible=False); ui_show_target_updates[2]=gr.update(value=None,visible=False)
634
+ ui_show_target_updates[3]=""; ui_show_target_updates[4]=""; ui_show_target_updates[5]="请观察原图…"; ui_show_target_updates[6]=prog_text
635
+ ui_show_target_updates[7]=gr.update(interactive=False); ui_show_target_updates[8]=gr.update(interactive=False); ui_show_target_updates[9]=gr.update(interactive=False)
636
+ yield output_s_trial_idx, output_s_run_no, output_s_user_logs, output_s_current_trial_data, output_s_user_session_id, output_s_current_run_image_list, output_s_num_trials_this_run, *ui_show_target_updates
637
+
638
+ time.sleep(3)
639
+
640
+ ui_show_candidates_updates = list(create_no_change_tuple())
641
+ ui_show_candidates_updates[0]=gr.update(value=None,visible=False); ui_show_candidates_updates[1]=gr.update(value=trial_info["left_path"],visible=True); ui_show_candidates_updates[2]=gr.update(value=trial_info["right_path"],visible=True)
642
+ ui_show_candidates_updates[3]=gr.update(value=trial_info["left_display_label"], visible=True); ui_show_candidates_updates[4]=gr.update(value=trial_info["right_display_label"], visible=True)
643
+ ui_show_candidates_updates[5]="请选择更像原图的一张"; ui_show_candidates_updates[6]=prog_text
644
+ ui_show_candidates_updates[7]=gr.update(interactive=False); ui_show_candidates_updates[8]=gr.update(interactive=True); ui_show_candidates_updates[9]=gr.update(interactive=True)
645
+ yield output_s_trial_idx, output_s_run_no, output_s_user_logs, output_s_current_trial_data, output_s_user_session_id, output_s_current_run_image_list, output_s_num_trials_this_run, *ui_show_candidates_updates
646
+
647
+ # ==== Gradio UI 定义 和 程序入口 ====
648
+ def handle_download_history_file():
649
+ global GLOBAL_HISTORY_FILE
650
+ if os.path.exists(GLOBAL_HISTORY_FILE):
651
+ try:
652
+ if os.path.getsize(GLOBAL_HISTORY_FILE) > 0:
653
+ print(f"准备提供文件下载: {GLOBAL_HISTORY_FILE}")
654
+ return GLOBAL_HISTORY_FILE, gr.update(value=f"点击上面的链接下载 '{os.path.basename(GLOBAL_HISTORY_FILE)}'")
655
+ else:
656
+ print(f"历史文件 '{GLOBAL_HISTORY_FILE}' 为空,不提供下载。")
657
+ return None, gr.update(value=f"提示: 历史文件 '{os.path.basename(GLOBAL_HISTORY_FILE)}' 当前为空。")
658
+ except Exception as e:
659
+ print(f"检查历史文件大小时出错 '{GLOBAL_HISTORY_FILE}': {e}")
660
+ return None, gr.update(value=f"错误: 检查历史文件时出错。")
661
+ else:
662
+ print(f"请求下载历史文件,但文件 '{GLOBAL_HISTORY_FILE}' 未找到。")
663
+ return None, gr.update(value=f"错误: JSON历史文件 '{os.path.basename(GLOBAL_HISTORY_FILE)}' 未找到。请先运行实验以生成数据并触发保存。")
664
+
665
+ welcome_page_markdown = """
666
+ ## 欢迎加入实验!
667
+ 您好!非常感谢您抽出宝贵时间参与我们的视觉偏好评估实验。您的选择将帮助我们改进重建算法,让机器生成的图像更贴近人类视觉体验!
668
+ 1. **实验目的**:通过比较两幅 重建图像 与原始 目标图像 的相似��。
669
+ 2. **操作流程**:
670
+ * 点击下方的「我已阅读并同意开始实验」按钮。
671
+ * 然后点击主实验界面的「开始试验 / 下一轮」按钮。
672
+ * 系统先展示一张 **目标图像**,持续 3 秒。
673
+ * 随后自动切换到 **两张重建图像**。
674
+ * 根据刚才的观察记忆,选出您认为与目标图像最相似的一张。
675
+ * 选择后系统会自动进入下一轮比较。
676
+ 3. **温馨提示**:
677
+ * 请勿刷新或关闭页面,以免中断实验。
678
+ * 若图片加载稍有延迟,请耐心等待;持续异常可联系邮箱 yangminghan@bupt.edu.cn。
679
+ * 本实验将保护您的任何个人隐私信息,所有数据仅用于学术研究,请您认真选择和填写。
680
+ 4. **奖励说明**:
681
+ * 完成全部轮次后,请截图记录您所完成的实验总数(可累积,页面左下角将显示进度,请保证截取到为您分配的ID,轮次)。
682
+ * 将截图发送至邮箱 yangminghan@bupt.edu.cn,我们将在核验后发放奖励。
683
+ 再次感谢您的参与与支持!您每一次认真选择都对我们的研究意义重大。祝您一切顺利,实验愉快!
684
+ """
685
+ def handle_agree_and_start(name, gender, age, education, request: gr.Request):
686
+ error_messages_list = []
687
+ if not name or str(name).strip() == "": error_messages_list.append("姓名 不能为空。")
688
+ if gender is None or str(gender).strip() == "": error_messages_list.append("性别 必须选择。")
689
+ if age is None: error_messages_list.append("年龄 不能为空。")
690
+ elif not (isinstance(age, (int, float)) and 1 <= age <= 120):
691
+ try: num_age = float(age);
692
+ except (ValueError, TypeError): error_messages_list.append("年龄必须是一个有效的数字。")
693
+ else:
694
+ if not (1 <= num_age <= 120): error_messages_list.append("年龄必须在 1 到 120 之间。")
695
+ if education is None or str(education).strip() == "其他": error_messages_list.append("学历 必须选择。")
696
+ if error_messages_list:
697
+ full_error_message = "请修正以下错误:\n" + "\n".join([f"- {msg}" for msg in error_messages_list])
698
+ print(f"用户输入验证失败: {full_error_message}")
699
+ return gr.update(), False, gr.update(visible=True), gr.update(visible=False), full_error_message
700
+ s_name = str(name).strip().replace(" ","_").replace("/","_").replace("\\","_")
701
+ s_gender = str(gender).strip().replace(" ","_").replace("/","_").replace("\\","_")
702
+ s_age = str(int(float(age)))
703
+ s_education = str(education).strip().replace(" ","_").replace("/","_").replace("\\","_")
704
+ user_id_str = f"N-{s_name}_G-{s_gender}_A-{s_age}_E-{s_education}"
705
+ print(f"用户信息收集完毕,生成用户ID: {user_id_str}")
706
+ return user_id_str, True, gr.update(visible=False), gr.update(visible=True), ""
707
+
708
+ with gr.Blocks(css=CSS, title="图像重建主观评估") as demo:
709
+ s_show_experiment_ui = gr.State(False); s_trial_index = gr.State(0); s_run_no = gr.State(1)
710
+ s_user_logs = gr.State([]); s_current_trial_data = gr.State({}); s_user_session_id = gr.State(None)
711
+ s_current_run_image_list = gr.State([]); s_num_trials_this_run = gr.State(0)
712
+
713
+ welcome_container = gr.Column(visible=True)
714
+ experiment_container = gr.Column(visible=False)
715
+
716
+ with welcome_container:
717
+ gr.Markdown(welcome_page_markdown)
718
+ with gr.Row(): user_name_input = gr.Textbox(label="请输入您的姓名或代号 (例如 张三 或 User001)", placeholder="例如:张三 -> ZS"); user_gender_input = gr.Radio(label="性别", choices=["男", "女"])
719
+ with gr.Row(): user_age_input = gr.Number(label="年龄 (请输入1-120的整数)", minimum=1, maximum=120, step=1); user_education_input = gr.Dropdown(label="学历", choices=["其他","初中及以下","高中(含中专)", "大专(含在读)", "本科(含在读)", "硕士(含在读)", "博士(含在读)"])
720
+ welcome_error_msg = gr.Markdown(value="")
721
+ btn_agree_and_start = gr.Button("我已阅读上述说明并同意参与实验")
722
+
723
+ with experiment_container:
724
+ gr.Markdown("## 🧠 图像重建主观评估实验"); gr.Markdown(f"每轮实验大约有 {NUM_TRIALS_PER_RUN} 次比较。")
725
+ with gr.Row():
726
+ with gr.Column(scale=1, min_width=300): left_img = gr.Image(label="左候选图", visible=False, height=400, interactive=False); left_lbl = gr.Textbox(label="左图信息", value="", visible=True, interactive=False, max_lines=1); btn_left = gr.Button("选择左图 (更相似)", interactive=False, elem_classes="compact_button")
727
+ with gr.Column(scale=1, min_width=300): right_img = gr.Image(label="右候选图", visible=False, height=400, interactive=False); right_lbl = gr.Textbox(label="右图信息",value="", visible=True, interactive=False, max_lines=1); btn_right = gr.Button("选择右图 (更相似)", interactive=False, elem_classes="compact_button")
728
+ with gr.Row(): target_img = gr.Image(label="目标图像 (观察3秒后消失)", visible=False, height=400, interactive=False)
729
+ with gr.Row(): status_text = gr.Markdown(value="请点击“开始试验 / 下一轮”按钮。")
730
+ with gr.Row(): progress_text = gr.Markdown()
731
+ with gr.Row():
732
+ btn_start = gr.Button("开始试验 / 下一轮")
733
+ btn_download_json = gr.Button("下载JSON历史记录")
734
+ json_download_output = gr.File(label="下载的文件会在此处提供", interactive=False)
735
+ file_out_placeholder = gr.File(label=" ", visible=False, interactive=False)
736
+
737
+ outputs_ui_components_definition = [
738
+ target_img, left_img, right_img, left_lbl, right_lbl, status_text, progress_text,
739
+ btn_start, btn_left, btn_right, file_out_placeholder
740
+ ]
741
+ click_inputs_base = [
742
+ s_trial_index, s_run_no, s_user_logs, s_current_trial_data, s_user_session_id,
743
+ s_current_run_image_list, s_num_trials_this_run
744
+ ]
745
+ event_outputs = [
746
+ s_trial_index, s_run_no, s_user_logs, s_current_trial_data, s_user_session_id,
747
+ s_current_run_image_list, s_num_trials_this_run, *outputs_ui_components_definition
748
+ ]
749
+
750
+ btn_agree_and_start.click(fn=handle_agree_and_start, inputs=[user_name_input, user_gender_input, user_age_input, user_education_input], outputs=[s_user_session_id, s_show_experiment_ui, welcome_container, experiment_container, welcome_error_msg])
751
+ btn_start.click(fn=partial(process_experiment_step, action_type="start_experiment"), inputs=click_inputs_base, outputs=event_outputs, queue=True)
752
+ btn_left.click(fn=partial(process_experiment_step, action_type="record_choice", choice_value="left"), inputs=click_inputs_base, outputs=event_outputs, queue=True)
753
+ btn_right.click(fn=partial(process_experiment_step, action_type="record_choice", choice_value="right"), inputs=click_inputs_base, outputs=event_outputs, queue=True)
754
+ btn_download_json.click(fn=handle_download_history_file, inputs=None, outputs=[json_download_output, status_text])
755
+
756
+ if __name__ == "__main__":
757
+ if not master_image_list: print("\n关键错误:程序无法启动,因无目标图片。"); exit()
758
+ else:
759
+ print(f"从 '{TARGET_DIR}' 加载 {len(master_image_list)} 张目标图片。")
760
+ if not METHOD_ROOTS: print(f"警告: '{BASE_IMAGE_DIR}' 无候选方法子目录。")
761
+ if not SUBJECTS: print("警告: SUBJECTS 列表为空。")
762
+ print(f"用户选择日志保存到 Dataset: '{DATASET_REPO_ID}' 的 '{BATCH_LOG_FOLDER}/ 文件夹")
763
+ if not os.getenv("HF_TOKEN"): print("警告: HF_TOKEN 未设置。日志无法保存到Hugging Face Dataset。\n 请在 Space Secrets 中设置 HF_TOKEN。")
764
+ else: print("HF_TOKEN 已找到。")
765
+ print(f"全局图片对历史将从 '{GLOBAL_HISTORY_FILE}' 加载/保存到此文件。")
766
+
767
+ allowed_paths_list = []
768
+ image_base_dir_to_allow = BASE_IMAGE_DIR
769
+ if os.path.exists(image_base_dir_to_allow) and os.path.isdir(image_base_dir_to_allow):
770
+ allowed_paths_list.append(os.path.abspath(image_base_dir_to_allow))
771
+ else:
772
+ print(f"关键警告:图片基础目录 '{image_base_dir_to_allow}' 不存在或非目录。")
773
+
774
+ if os.path.exists(PERSISTENT_STORAGE_BASE) and os.path.isdir(PERSISTENT_STORAGE_BASE):
775
+ allowed_paths_list.append(os.path.abspath(PERSISTENT_STORAGE_BASE))
776
+ else:
777
+ print(f"警告:持久化存储基础目录 '{PERSISTENT_STORAGE_BASE}' 不存在。JSON历史文件下载可能受影响。")
778
+ try:
779
+ os.makedirs(PERSISTENT_STORAGE_BASE, exist_ok=True)
780
+ print(f"信息:已尝试创建目录 '{PERSISTENT_STORAGE_BASE}'。")
781
+ if os.path.exists(PERSISTENT_STORAGE_BASE) and os.path.isdir(PERSISTENT_STORAGE_BASE):
782
+ allowed_paths_list.append(os.path.abspath(PERSISTENT_STORAGE_BASE))
783
+ except Exception as e_mkdir_main:
784
+ print(f"错误:在 main 中创建目录 '{PERSISTENT_STORAGE_BASE}' 失败: {e_mkdir_main}")
785
+
786
+ final_allowed_paths = list(set(allowed_paths_list))
787
+ if final_allowed_paths:
788
+ print(f"Gradio demo.launch() 配置最终 allowed_paths: {final_allowed_paths}")
789
+ else:
790
+ print("警告:没有有效的 allowed_paths 被配置。Gradio文件访问可能受限。")
791
+
792
+ print("启动 Gradio 应用...")
793
+ if final_allowed_paths:
794
+ demo.launch(allowed_paths=final_allowed_paths)
795
+ else:
796
+ demo.launch()