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