File size: 4,427 Bytes
a4c90b2
f028613
 
 
604f507
f028613
 
 
a4c90b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f028613
 
548b641
 
f028613
 
 
604f507
f028613
 
 
a4c90b2
 
 
 
f028613
 
 
 
 
 
a4c90b2
 
 
 
 
 
 
 
 
f028613
 
 
 
604f507
f028613
604f507
f028613
 
 
 
 
 
 
 
a4c90b2
 
 
 
 
f028613
 
 
604f507
f028613
604f507
 
f028613
 
 
 
604f507
 
f028613
 
 
 
604f507
 
f028613
 
604f507
f028613
 
 
604f507
 
f028613
 
604f507
f028613
 
604f507
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import gradio as gr
import config
from ui_components import create_ui
from annotation import load_annotations
from task_manager import get_current_task, get_current_task_with_annotations, get_total_tasks

css = """
.center { text-align: center; }
.audio-container { margin: 10px; padding: 15px; }
.selected {
    border: 3px solid #4CAF50 !important;
    background-color: #e8f5e9 !important;
}
.user-task-info {
    font-size: 16px;
    color: #333;
    padding: 10px;
    background-color: #f0f0f0;
    border-radius: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
"""


def set_user_info(request: gr.Request):
    """设置用户信息到 State 并加载用户特定的标注"""
    username = request.username if hasattr(request, 'username') else "unknown"
    annotation_results = load_annotations(username)
    print(f"加载用户 {username} 的标注结果:{annotation_results}")

    # 获取当前任务信息,并考虑已有的标注结果
    inst, text, audioA_update, audioB_update, prev_disabled, next_disabled, task_num = get_current_task_with_annotations(
        annotation_results)

    # 获取总任务数
    total_tasks = get_total_tasks()

    # 合并用户信息和任务编号到任务编号位置
    combined_task_info = f'<div class="user-task-info"><span>👤 当前用户: {username}</span><span><strong>任务编号: {task_num} / {total_tasks}</strong></span></div>'

    # 返回所有需要初始化的组件值
    return (
        username,
        annotation_results,
        inst,  # instruction
        text,  # text_box
        audioA_update,  # audioA
        audioB_update,  # audioB
        gr.update(interactive=not prev_disabled),  # btn_prev
        gr.update(interactive=not next_disabled),  # btn_next
        gr.update(value=combined_task_info)  # task_number 位置显示合并信息
    )


if __name__ == "__main__":
    print("启动应用...")
    with gr.Blocks(css=css) as demo:
        username = gr.State(value="unknown")
        annotation_results = gr.State(value={})
        ui_components = create_ui(get_current_task(), username, annotation_results)

        # 修改 demo.load,移除 user_display,保留 task_number
        demo.load(
            set_user_info,
            inputs=None,
            outputs=[
                username,
                annotation_results,
                ui_components["instruction"],
                ui_components["text_box"],
                ui_components["audioA"],
                ui_components["audioB"],
                ui_components["btn_prev"],
                ui_components["btn_next"],
                ui_components["task_number"]  # 现在包含用户信息和任务编号
            ]
        )

        # 其余按钮绑定保持不变
        ui_components["btn_A"].click(
            ui_components["select_audio"],
            inputs=[gr.State("A"), ui_components["audioA"], ui_components["audioB"], annotation_results, username],
            outputs=[ui_components["audioA"], ui_components["audioB"], annotation_results]
        )

        ui_components["btn_B"].click(
            ui_components["select_audio"],
            inputs=[gr.State("B"), ui_components["audioA"], ui_components["audioB"], annotation_results, username],
            outputs=[ui_components["audioA"], ui_components["audioB"], annotation_results]
        )

        ui_components["btn_prev"].click(
            ui_components["change_task"],
            inputs=[gr.State("prev"), annotation_results, username],  # 添加 username 参数
            outputs=[ui_components["instruction"], ui_components["text_box"], ui_components["audioA"],
                     ui_components["audioB"], ui_components["btn_prev"], ui_components["btn_next"],
                     ui_components["task_number"], annotation_results]  # 保持 task_number
        )

        ui_components["btn_next"].click(
            ui_components["change_task"],
            inputs=[gr.State("next"), annotation_results, username],  # 添加 username 参数
            outputs=[ui_components["instruction"], ui_components["text_box"], ui_components["audioA"],
                     ui_components["audioB"], ui_components["btn_prev"], ui_components["btn_next"],
                     ui_components["task_number"], annotation_results]  # 保持 task_number
        )

    demo.launch(auth=config.ANNOTATOR, share=True,ssr_mode=False)