Spaces:
Sleeping
Sleeping
Wenjiawang0312
commited on
Commit
·
05f2d77
1
Parent(s):
85c101d
store
Browse files
app.py
CHANGED
|
@@ -171,6 +171,7 @@ def create_video_survey_app():
|
|
| 171 |
# 状态变量
|
| 172 |
current_question_idx = gr.State(0)
|
| 173 |
current_method_mapping = gr.State({})
|
|
|
|
| 174 |
|
| 175 |
# 进度显示
|
| 176 |
with gr.Row():
|
|
@@ -217,18 +218,21 @@ def create_video_survey_app():
|
|
| 217 |
})
|
| 218 |
|
| 219 |
# 更新问题显示
|
| 220 |
-
def update_question(question_idx, save_previous=False, prev_ratings=None, prev_mapping=None):
|
| 221 |
if question_idx < 0:
|
| 222 |
question_idx = 0
|
| 223 |
if question_idx >= len(question_folders):
|
| 224 |
question_idx = len(question_folders) - 1
|
| 225 |
|
| 226 |
-
#
|
| 227 |
save_msg = ""
|
| 228 |
if save_previous and prev_ratings and prev_mapping:
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
scene_name = question_folders[question_idx]
|
| 234 |
videos, method_mapping = get_videos_for_question(scene_name)
|
|
@@ -242,13 +246,27 @@ def create_video_survey_app():
|
|
| 242 |
else:
|
| 243 |
video_updates.append(gr.update(value=None))
|
| 244 |
|
| 245 |
-
#
|
| 246 |
-
rating_updates = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
question_markdown = f"**场景 {question_idx + 1} / {len(question_folders)}**: `{scene_name}`"
|
| 249 |
|
| 250 |
return (
|
| 251 |
-
[question_idx, method_mapping, question_markdown, save_msg] +
|
| 252 |
video_updates +
|
| 253 |
rating_updates
|
| 254 |
)
|
|
@@ -267,32 +285,48 @@ def create_video_survey_app():
|
|
| 267 |
return ratings
|
| 268 |
|
| 269 |
# 下一题按钮
|
| 270 |
-
def on_next(question_idx, method_mapping, *rating_values):
|
| 271 |
# 收集当前评分
|
| 272 |
current_ratings = collect_ratings(*rating_values)
|
| 273 |
|
| 274 |
-
#
|
| 275 |
scene_name = question_folders[question_idx]
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
# 移动到下一题
|
| 279 |
new_idx = question_idx + 1
|
| 280 |
if new_idx >= len(question_folders):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
return [
|
| 282 |
question_idx,
|
| 283 |
method_mapping,
|
|
|
|
| 284 |
f"**✅ 所有场景已完成!/ All scenes completed!**",
|
| 285 |
-
|
| 286 |
] + [gr.update(value=None)] * 4 + [gr.update(value=3)] * 12
|
| 287 |
|
| 288 |
-
return update_question(new_idx)
|
| 289 |
|
| 290 |
# 上一题按钮
|
| 291 |
-
def on_prev(question_idx, *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
new_idx = question_idx - 1
|
| 293 |
if new_idx < 0:
|
| 294 |
new_idx = 0
|
| 295 |
-
return update_question(new_idx)
|
| 296 |
|
| 297 |
# 收集所有评分组件
|
| 298 |
all_rating_inputs = []
|
|
@@ -302,10 +336,11 @@ def create_video_survey_app():
|
|
| 302 |
# 绑定事件
|
| 303 |
next_btn.click(
|
| 304 |
on_next,
|
| 305 |
-
inputs=[current_question_idx, current_method_mapping] + all_rating_inputs,
|
| 306 |
outputs=[
|
| 307 |
current_question_idx,
|
| 308 |
current_method_mapping,
|
|
|
|
| 309 |
question_text,
|
| 310 |
status_text
|
| 311 |
] + video_components + all_rating_inputs
|
|
@@ -313,10 +348,11 @@ def create_video_survey_app():
|
|
| 313 |
|
| 314 |
prev_btn.click(
|
| 315 |
on_prev,
|
| 316 |
-
inputs=[current_question_idx] + all_rating_inputs,
|
| 317 |
outputs=[
|
| 318 |
current_question_idx,
|
| 319 |
current_method_mapping,
|
|
|
|
| 320 |
question_text,
|
| 321 |
status_text
|
| 322 |
] + video_components + all_rating_inputs
|
|
@@ -324,10 +360,11 @@ def create_video_survey_app():
|
|
| 324 |
|
| 325 |
# 初始化第一个问题
|
| 326 |
demo.load(
|
| 327 |
-
lambda: update_question(0),
|
| 328 |
outputs=[
|
| 329 |
current_question_idx,
|
| 330 |
current_method_mapping,
|
|
|
|
| 331 |
question_text,
|
| 332 |
status_text
|
| 333 |
] + video_components + all_rating_inputs
|
|
|
|
| 171 |
# 状态变量
|
| 172 |
current_question_idx = gr.State(0)
|
| 173 |
current_method_mapping = gr.State({})
|
| 174 |
+
all_ratings = gr.State({}) # 存储所有场景的评分
|
| 175 |
|
| 176 |
# 进度显示
|
| 177 |
with gr.Row():
|
|
|
|
| 218 |
})
|
| 219 |
|
| 220 |
# 更新问题显示
|
| 221 |
+
def update_question(question_idx, all_ratings_dict, save_previous=False, prev_ratings=None, prev_mapping=None):
|
| 222 |
if question_idx < 0:
|
| 223 |
question_idx = 0
|
| 224 |
if question_idx >= len(question_folders):
|
| 225 |
question_idx = len(question_folders) - 1
|
| 226 |
|
| 227 |
+
# 如果需要,保存上一题的评分到字典
|
| 228 |
save_msg = ""
|
| 229 |
if save_previous and prev_ratings and prev_mapping:
|
| 230 |
+
prev_idx = question_idx - 1 if question_idx > 0 else question_idx
|
| 231 |
+
prev_scene = question_folders[prev_idx]
|
| 232 |
+
all_ratings_dict[prev_scene] = {
|
| 233 |
+
'ratings': prev_ratings,
|
| 234 |
+
'mapping': prev_mapping
|
| 235 |
+
}
|
| 236 |
|
| 237 |
scene_name = question_folders[question_idx]
|
| 238 |
videos, method_mapping = get_videos_for_question(scene_name)
|
|
|
|
| 246 |
else:
|
| 247 |
video_updates.append(gr.update(value=None))
|
| 248 |
|
| 249 |
+
# 检查是否有已保存的评分
|
| 250 |
+
rating_updates = []
|
| 251 |
+
if scene_name in all_ratings_dict:
|
| 252 |
+
# 恢复之前的评分
|
| 253 |
+
saved_ratings = all_ratings_dict[scene_name]['ratings']
|
| 254 |
+
for i in range(4):
|
| 255 |
+
method_name = f"Method {chr(65+i)}"
|
| 256 |
+
if method_name in saved_ratings:
|
| 257 |
+
rating_updates.append(gr.update(value=saved_ratings[method_name]['dynamic_quality']))
|
| 258 |
+
rating_updates.append(gr.update(value=saved_ratings[method_name]['static_consistency']))
|
| 259 |
+
rating_updates.append(gr.update(value=saved_ratings[method_name]['overall_quality']))
|
| 260 |
+
else:
|
| 261 |
+
rating_updates.extend([gr.update(value=3), gr.update(value=3), gr.update(value=3)])
|
| 262 |
+
else:
|
| 263 |
+
# 重置为默认评分
|
| 264 |
+
rating_updates = [gr.update(value=3) for _ in range(12)] # 4个视频 x 3个评分,每个默认值为3
|
| 265 |
|
| 266 |
question_markdown = f"**场景 {question_idx + 1} / {len(question_folders)}**: `{scene_name}`"
|
| 267 |
|
| 268 |
return (
|
| 269 |
+
[question_idx, method_mapping, all_ratings_dict, question_markdown, save_msg] +
|
| 270 |
video_updates +
|
| 271 |
rating_updates
|
| 272 |
)
|
|
|
|
| 285 |
return ratings
|
| 286 |
|
| 287 |
# 下一题按钮
|
| 288 |
+
def on_next(question_idx, method_mapping, all_ratings_dict, *rating_values):
|
| 289 |
# 收集当前评分
|
| 290 |
current_ratings = collect_ratings(*rating_values)
|
| 291 |
|
| 292 |
+
# 保存当前评分到字典
|
| 293 |
scene_name = question_folders[question_idx]
|
| 294 |
+
all_ratings_dict[scene_name] = {
|
| 295 |
+
'ratings': current_ratings,
|
| 296 |
+
'mapping': method_mapping
|
| 297 |
+
}
|
| 298 |
|
| 299 |
# 移动到下一题
|
| 300 |
new_idx = question_idx + 1
|
| 301 |
if new_idx >= len(question_folders):
|
| 302 |
+
# 所有题目完成,保存到文件
|
| 303 |
+
for scene, data in all_ratings_dict.items():
|
| 304 |
+
save_ratings(scene, data['ratings'], data['mapping'])
|
| 305 |
+
|
| 306 |
return [
|
| 307 |
question_idx,
|
| 308 |
method_mapping,
|
| 309 |
+
all_ratings_dict,
|
| 310 |
f"**✅ 所有场景已完成!/ All scenes completed!**",
|
| 311 |
+
"🎉 感谢参与!所有评分已保存 / Thank you! All ratings saved"
|
| 312 |
] + [gr.update(value=None)] * 4 + [gr.update(value=3)] * 12
|
| 313 |
|
| 314 |
+
return update_question(new_idx, all_ratings_dict)
|
| 315 |
|
| 316 |
# 上一题按钮
|
| 317 |
+
def on_prev(question_idx, method_mapping, all_ratings_dict, *rating_values):
|
| 318 |
+
# 收集当前评分并保存
|
| 319 |
+
current_ratings = collect_ratings(*rating_values)
|
| 320 |
+
scene_name = question_folders[question_idx]
|
| 321 |
+
all_ratings_dict[scene_name] = {
|
| 322 |
+
'ratings': current_ratings,
|
| 323 |
+
'mapping': method_mapping
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
new_idx = question_idx - 1
|
| 327 |
if new_idx < 0:
|
| 328 |
new_idx = 0
|
| 329 |
+
return update_question(new_idx, all_ratings_dict)
|
| 330 |
|
| 331 |
# 收集所有评分组件
|
| 332 |
all_rating_inputs = []
|
|
|
|
| 336 |
# 绑定事件
|
| 337 |
next_btn.click(
|
| 338 |
on_next,
|
| 339 |
+
inputs=[current_question_idx, current_method_mapping, all_ratings] + all_rating_inputs,
|
| 340 |
outputs=[
|
| 341 |
current_question_idx,
|
| 342 |
current_method_mapping,
|
| 343 |
+
all_ratings,
|
| 344 |
question_text,
|
| 345 |
status_text
|
| 346 |
] + video_components + all_rating_inputs
|
|
|
|
| 348 |
|
| 349 |
prev_btn.click(
|
| 350 |
on_prev,
|
| 351 |
+
inputs=[current_question_idx, current_method_mapping, all_ratings] + all_rating_inputs,
|
| 352 |
outputs=[
|
| 353 |
current_question_idx,
|
| 354 |
current_method_mapping,
|
| 355 |
+
all_ratings,
|
| 356 |
question_text,
|
| 357 |
status_text
|
| 358 |
] + video_components + all_rating_inputs
|
|
|
|
| 360 |
|
| 361 |
# 初始化第一个问题
|
| 362 |
demo.load(
|
| 363 |
+
lambda: update_question(0, {}),
|
| 364 |
outputs=[
|
| 365 |
current_question_idx,
|
| 366 |
current_method_mapping,
|
| 367 |
+
all_ratings,
|
| 368 |
question_text,
|
| 369 |
status_text
|
| 370 |
] + video_components + all_rating_inputs
|