Spaces:
Sleeping
Sleeping
| 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; | |
| } | |
| /* 平局选择样式 - 橙色 */ | |
| .tie-selected { | |
| border: 3px solid #FF9800 !important; | |
| background-color: #fff3e0 !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; | |
| } | |
| .progress-bar { | |
| background-color: #e0e0e0; | |
| border-radius: 10px; | |
| height: 8px; | |
| width: 200px; | |
| margin: 0 10px; | |
| position: relative; | |
| } | |
| .progress-fill { | |
| background-color: #4CAF50; | |
| height: 100%; | |
| border-radius: 10px; | |
| transition: width 0.3s ease; | |
| } | |
| """ | |
| def calculate_completion_stats(annotation_results): | |
| """计算任务完成统计信息""" | |
| if not annotation_results: | |
| return 0, 0, 0.0 | |
| total_tasks = get_total_tasks() | |
| completed_tasks = len(annotation_results) | |
| completion_rate = (completed_tasks / total_tasks * 100) if total_tasks > 0 else 0.0 | |
| return completed_tasks, total_tasks, completion_rate | |
| def get_initial_task_position(annotation_results): | |
| """根据用户的标注历史确定初始任务位置""" | |
| if not annotation_results: | |
| return 0 | |
| max_annotated = max(annotation_results.keys()) if annotation_results else -1 | |
| next_task = max_annotated + 1 | |
| total_tasks = get_total_tasks() | |
| return min(next_task, total_tasks - 1) | |
| def create_task_info_html(username, annotation_results, current_task_num): | |
| """创建包含用户信息和完成度的HTML""" | |
| completed_tasks, total_tasks, completion_rate = calculate_completion_stats(annotation_results) | |
| # 创建进度条HTML | |
| progress_bar_html = f""" | |
| <div class="progress-bar"> | |
| <div class="progress-fill" style="width: {completion_rate}%;"></div> | |
| </div> | |
| """ | |
| task_info_html = f""" | |
| <div class="user-task-info"> | |
| <span>👤 当前用户: {username}</span> | |
| <div style="display: flex; align-items: center;"> | |
| <span>完成度: {completed_tasks}/{total_tasks} ({completion_rate:.1f}%)</span> | |
| {progress_bar_html} | |
| </div> | |
| <span><strong>当前任务: {current_task_num}</strong></span> | |
| </div> | |
| """ | |
| return task_info_html | |
| 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}") | |
| user_current_task = get_initial_task_position(annotation_results) | |
| inst, text, audioA_update, audioB_update, prev_disabled, next_disabled, task_num = get_current_task_with_annotations( | |
| annotation_results, user_current_task) | |
| # 创建包含完成度信息的HTML | |
| task_info_html = create_task_info_html(username, annotation_results, task_num) | |
| return ( | |
| username, | |
| annotation_results, | |
| user_current_task, | |
| inst, | |
| text, | |
| audioA_update, | |
| audioB_update, | |
| gr.update(interactive=not prev_disabled), | |
| gr.update(interactive=not next_disabled), | |
| gr.update(value=task_info_html) | |
| ) | |
| def update_task_info_after_action(username, annotation_results, current_task_num): | |
| """在用户操作后更新任务信息显示""" | |
| return create_task_info_html(username, annotation_results, current_task_num) | |
| if __name__ == "__main__": | |
| print("启动应用...") | |
| with gr.Blocks(css=css) as demo: | |
| username = gr.State(value="unknown") | |
| annotation_results = gr.State(value={}) | |
| user_current_task = gr.State(value=0) | |
| ui_components = create_ui(get_current_task(), username, annotation_results) | |
| demo.load( | |
| set_user_info, | |
| inputs=None, | |
| outputs=[ | |
| username, | |
| annotation_results, | |
| user_current_task, | |
| 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_win"].click( | |
| ui_components["select_result"], | |
| inputs=[gr.State("win"), ui_components["audioA"], ui_components["audioB"], annotation_results, username, | |
| user_current_task], | |
| outputs=[ui_components["audioA"], ui_components["audioB"], annotation_results, ui_components["task_number"]] | |
| ) | |
| ui_components["btn_tie"].click( | |
| ui_components["select_result"], | |
| inputs=[gr.State("tie"), ui_components["audioA"], ui_components["audioB"], annotation_results, username, | |
| user_current_task], | |
| outputs=[ui_components["audioA"], ui_components["audioB"], annotation_results, ui_components["task_number"]] | |
| ) | |
| ui_components["btn_lose"].click( | |
| ui_components["select_result"], | |
| inputs=[gr.State("lose"), ui_components["audioA"], ui_components["audioB"], annotation_results, username, | |
| user_current_task], | |
| outputs=[ui_components["audioA"], ui_components["audioB"], annotation_results, ui_components["task_number"]] | |
| ) | |
| ui_components["btn_prev"].click( | |
| ui_components["change_task"], | |
| inputs=[gr.State("prev"), annotation_results, username, user_current_task], | |
| 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, user_current_task] | |
| ) | |
| ui_components["btn_next"].click( | |
| ui_components["change_task"], | |
| inputs=[gr.State("next"), annotation_results, username, user_current_task], | |
| 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, user_current_task] | |
| ) | |
| demo.launch(auth=config.ANNOTATOR,share=True,ssr_mode=False) |