Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,110 +1,110 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import config
|
| 3 |
-
from ui_components import create_ui
|
| 4 |
-
from annotation import load_annotations
|
| 5 |
-
from task_manager import get_current_task, get_current_task_with_annotations, get_total_tasks
|
| 6 |
-
|
| 7 |
-
css = """
|
| 8 |
-
.center { text-align: center; }
|
| 9 |
-
.audio-container { margin: 10px; padding: 15px; }
|
| 10 |
-
.selected {
|
| 11 |
-
border: 3px solid #4CAF50 !important;
|
| 12 |
-
background-color: #e8f5e9 !important;
|
| 13 |
-
}
|
| 14 |
-
.user-task-info {
|
| 15 |
-
font-size: 16px;
|
| 16 |
-
color: #333;
|
| 17 |
-
padding: 10px;
|
| 18 |
-
background-color: #f0f0f0;
|
| 19 |
-
border-radius: 5px;
|
| 20 |
-
display: flex;
|
| 21 |
-
justify-content: space-between;
|
| 22 |
-
align-items: center;
|
| 23 |
-
}
|
| 24 |
-
"""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def set_user_info(request: gr.Request):
|
| 28 |
-
"""设置用户信息到 State 并加载用户特定的标注"""
|
| 29 |
-
username = request.username if hasattr(request, 'username') else "unknown"
|
| 30 |
-
annotation_results = load_annotations(username)
|
| 31 |
-
print(f"加载用户 {username} 的标注结果:{annotation_results}")
|
| 32 |
-
|
| 33 |
-
# 获取当前任务信息,并考虑已有的标注结果
|
| 34 |
-
inst, text, audioA_update, audioB_update, prev_disabled, next_disabled, task_num = get_current_task_with_annotations(
|
| 35 |
-
annotation_results)
|
| 36 |
-
|
| 37 |
-
# 获取总任务数
|
| 38 |
-
total_tasks = get_total_tasks()
|
| 39 |
-
|
| 40 |
-
# 合并用户信息和任务编号到任务编号位置
|
| 41 |
-
combined_task_info = f'<div class="user-task-info"><span>👤 当前用户: {username}</span><span><strong>任务编号: {task_num} / {total_tasks}</strong></span></div>'
|
| 42 |
-
|
| 43 |
-
# 返回所有需要初始化的组件值
|
| 44 |
-
return (
|
| 45 |
-
username,
|
| 46 |
-
annotation_results,
|
| 47 |
-
inst, # instruction
|
| 48 |
-
text, # text_box
|
| 49 |
-
audioA_update, # audioA
|
| 50 |
-
audioB_update, # audioB
|
| 51 |
-
gr.update(interactive=not prev_disabled), # btn_prev
|
| 52 |
-
gr.update(interactive=not next_disabled), # btn_next
|
| 53 |
-
gr.update(value=combined_task_info) # task_number 位置显示合并信息
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
if __name__ == "__main__":
|
| 58 |
-
print("启动应用...")
|
| 59 |
-
with gr.Blocks(css=css) as demo:
|
| 60 |
-
username = gr.State(value="unknown")
|
| 61 |
-
annotation_results = gr.State(value={})
|
| 62 |
-
ui_components = create_ui(get_current_task(), username, annotation_results)
|
| 63 |
-
|
| 64 |
-
# 修改 demo.load,移除 user_display,保留 task_number
|
| 65 |
-
demo.load(
|
| 66 |
-
set_user_info,
|
| 67 |
-
inputs=None,
|
| 68 |
-
outputs=[
|
| 69 |
-
username,
|
| 70 |
-
annotation_results,
|
| 71 |
-
ui_components["instruction"],
|
| 72 |
-
ui_components["text_box"],
|
| 73 |
-
ui_components["audioA"],
|
| 74 |
-
ui_components["audioB"],
|
| 75 |
-
ui_components["btn_prev"],
|
| 76 |
-
ui_components["btn_next"],
|
| 77 |
-
ui_components["task_number"] # 现在包含用户信息和任务编号
|
| 78 |
-
]
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
# 其余按钮绑定保持不变
|
| 82 |
-
ui_components["btn_A"].click(
|
| 83 |
-
ui_components["select_audio"],
|
| 84 |
-
inputs=[gr.State("A"), ui_components["audioA"], ui_components["audioB"], annotation_results, username],
|
| 85 |
-
outputs=[ui_components["audioA"], ui_components["audioB"], annotation_results]
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
ui_components["btn_B"].click(
|
| 89 |
-
ui_components["select_audio"],
|
| 90 |
-
inputs=[gr.State("B"), ui_components["audioA"], ui_components["audioB"], annotation_results, username],
|
| 91 |
-
outputs=[ui_components["audioA"], ui_components["audioB"], annotation_results]
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
ui_components["btn_prev"].click(
|
| 95 |
-
ui_components["change_task"],
|
| 96 |
-
inputs=[gr.State("prev"), annotation_results, username], # 添加 username 参数
|
| 97 |
-
outputs=[ui_components["instruction"], ui_components["text_box"], ui_components["audioA"],
|
| 98 |
-
ui_components["audioB"], ui_components["btn_prev"], ui_components["btn_next"],
|
| 99 |
-
ui_components["task_number"], annotation_results] # 保持 task_number
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
ui_components["btn_next"].click(
|
| 103 |
-
ui_components["change_task"],
|
| 104 |
-
inputs=[gr.State("next"), annotation_results, username], # 添加 username 参数
|
| 105 |
-
outputs=[ui_components["instruction"], ui_components["text_box"], ui_components["audioA"],
|
| 106 |
-
ui_components["audioB"], ui_components["btn_prev"], ui_components["btn_next"],
|
| 107 |
-
ui_components["task_number"], annotation_results] # 保持 task_number
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
demo.launch(auth=config.ANNOTATOR)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import config
|
| 3 |
+
from ui_components import create_ui
|
| 4 |
+
from annotation import load_annotations
|
| 5 |
+
from task_manager import get_current_task, get_current_task_with_annotations, get_total_tasks
|
| 6 |
+
|
| 7 |
+
css = """
|
| 8 |
+
.center { text-align: center; }
|
| 9 |
+
.audio-container { margin: 10px; padding: 15px; }
|
| 10 |
+
.selected {
|
| 11 |
+
border: 3px solid #4CAF50 !important;
|
| 12 |
+
background-color: #e8f5e9 !important;
|
| 13 |
+
}
|
| 14 |
+
.user-task-info {
|
| 15 |
+
font-size: 16px;
|
| 16 |
+
color: #333;
|
| 17 |
+
padding: 10px;
|
| 18 |
+
background-color: #f0f0f0;
|
| 19 |
+
border-radius: 5px;
|
| 20 |
+
display: flex;
|
| 21 |
+
justify-content: space-between;
|
| 22 |
+
align-items: center;
|
| 23 |
+
}
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def set_user_info(request: gr.Request):
|
| 28 |
+
"""设置用户信息到 State 并加载用户特定的标注"""
|
| 29 |
+
username = request.username if hasattr(request, 'username') else "unknown"
|
| 30 |
+
annotation_results = load_annotations(username)
|
| 31 |
+
print(f"加载用户 {username} 的标注结果:{annotation_results}")
|
| 32 |
+
|
| 33 |
+
# 获取当前任务信息,并考虑已有的标注结果
|
| 34 |
+
inst, text, audioA_update, audioB_update, prev_disabled, next_disabled, task_num = get_current_task_with_annotations(
|
| 35 |
+
annotation_results)
|
| 36 |
+
|
| 37 |
+
# 获取总任务数
|
| 38 |
+
total_tasks = get_total_tasks()
|
| 39 |
+
|
| 40 |
+
# 合并用户信息和任务编号到任务编号位置
|
| 41 |
+
combined_task_info = f'<div class="user-task-info"><span>👤 当前用户: {username}</span><span><strong>任务编号: {task_num} / {total_tasks}</strong></span></div>'
|
| 42 |
+
|
| 43 |
+
# 返回所有需要初始化的组件值
|
| 44 |
+
return (
|
| 45 |
+
username,
|
| 46 |
+
annotation_results,
|
| 47 |
+
inst, # instruction
|
| 48 |
+
text, # text_box
|
| 49 |
+
audioA_update, # audioA
|
| 50 |
+
audioB_update, # audioB
|
| 51 |
+
gr.update(interactive=not prev_disabled), # btn_prev
|
| 52 |
+
gr.update(interactive=not next_disabled), # btn_next
|
| 53 |
+
gr.update(value=combined_task_info) # task_number 位置显示合并信息
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
print("启动应用...")
|
| 59 |
+
with gr.Blocks(css=css) as demo:
|
| 60 |
+
username = gr.State(value="unknown")
|
| 61 |
+
annotation_results = gr.State(value={})
|
| 62 |
+
ui_components = create_ui(get_current_task(), username, annotation_results)
|
| 63 |
+
|
| 64 |
+
# 修改 demo.load,移除 user_display,保留 task_number
|
| 65 |
+
demo.load(
|
| 66 |
+
set_user_info,
|
| 67 |
+
inputs=None,
|
| 68 |
+
outputs=[
|
| 69 |
+
username,
|
| 70 |
+
annotation_results,
|
| 71 |
+
ui_components["instruction"],
|
| 72 |
+
ui_components["text_box"],
|
| 73 |
+
ui_components["audioA"],
|
| 74 |
+
ui_components["audioB"],
|
| 75 |
+
ui_components["btn_prev"],
|
| 76 |
+
ui_components["btn_next"],
|
| 77 |
+
ui_components["task_number"] # 现在包含用户信息和任务编号
|
| 78 |
+
]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# 其余按钮绑定保持不变
|
| 82 |
+
ui_components["btn_A"].click(
|
| 83 |
+
ui_components["select_audio"],
|
| 84 |
+
inputs=[gr.State("A"), ui_components["audioA"], ui_components["audioB"], annotation_results, username],
|
| 85 |
+
outputs=[ui_components["audioA"], ui_components["audioB"], annotation_results]
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
ui_components["btn_B"].click(
|
| 89 |
+
ui_components["select_audio"],
|
| 90 |
+
inputs=[gr.State("B"), ui_components["audioA"], ui_components["audioB"], annotation_results, username],
|
| 91 |
+
outputs=[ui_components["audioA"], ui_components["audioB"], annotation_results]
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
ui_components["btn_prev"].click(
|
| 95 |
+
ui_components["change_task"],
|
| 96 |
+
inputs=[gr.State("prev"), annotation_results, username], # 添加 username 参数
|
| 97 |
+
outputs=[ui_components["instruction"], ui_components["text_box"], ui_components["audioA"],
|
| 98 |
+
ui_components["audioB"], ui_components["btn_prev"], ui_components["btn_next"],
|
| 99 |
+
ui_components["task_number"], annotation_results] # 保持 task_number
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
ui_components["btn_next"].click(
|
| 103 |
+
ui_components["change_task"],
|
| 104 |
+
inputs=[gr.State("next"), annotation_results, username], # 添加 username 参数
|
| 105 |
+
outputs=[ui_components["instruction"], ui_components["text_box"], ui_components["audioA"],
|
| 106 |
+
ui_components["audioB"], ui_components["btn_prev"], ui_components["btn_next"],
|
| 107 |
+
ui_components["task_number"], annotation_results] # 保持 task_number
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
demo.launch(auth=config.ANNOTATOR, server_name=None, share=None)
|