Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,113 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import json
|
3 |
-
import os
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import tempfile
|
5 |
+
|
6 |
+
DATA_FILE = "./test_data.json"
|
7 |
+
|
8 |
+
# 本地或 Render 环境下的保存目录
|
9 |
+
SAVE_DIR = "./annotations"
|
10 |
+
os.makedirs(SAVE_DIR, exist_ok=True)
|
11 |
+
|
12 |
+
# 读取样本数据
|
13 |
+
with open(DATA_FILE, "r", encoding="utf-8") as f:
|
14 |
+
data = json.load(f)
|
15 |
+
|
16 |
+
# 用于记录当前用户的所有打分(浏览器内存)
|
17 |
+
user_annotations = []
|
18 |
+
|
19 |
+
# 保存标注记录
|
20 |
+
def annotate(index, score, comment, annotator):
|
21 |
+
entry = data[index]
|
22 |
+
record = {
|
23 |
+
"index": index,
|
24 |
+
"annotator": annotator,
|
25 |
+
"source": entry["source"],
|
26 |
+
"hypothesis": entry["hypothesis"],
|
27 |
+
"score": score,
|
28 |
+
"comment": comment,
|
29 |
+
}
|
30 |
+
|
31 |
+
# 1. 保存到用户 session 记录
|
32 |
+
user_annotations.append(record)
|
33 |
+
|
34 |
+
# 2. 仍然保存到服务器端(可选)
|
35 |
+
# save_path = os.path.join(SAVE_DIR, f"annotations_{annotator}.jsonl")
|
36 |
+
# with open(save_path, "a", encoding="utf-8") as f:
|
37 |
+
# f.write(json.dumps(record, ensure_ascii=False) + "\n")
|
38 |
+
|
39 |
+
completed = index + 1
|
40 |
+
if completed >= len(data):
|
41 |
+
return (
|
42 |
+
"🎉 All samples annotated!",
|
43 |
+
index,
|
44 |
+
f"✅ Completed {completed}/{len(data)} samples.",
|
45 |
+
gr.update(interactive=False),
|
46 |
+
gr.update(interactive=False),
|
47 |
+
gr.update(interactive=False),
|
48 |
+
gr.update(interactive=False),
|
49 |
+
gr.update(visible=True) # 显示导出按钮
|
50 |
+
)
|
51 |
+
|
52 |
+
next_index = index + 1
|
53 |
+
progress = f"{completed}/{len(data)} annotated by {annotator}"
|
54 |
+
return (
|
55 |
+
"✅ Saved",
|
56 |
+
next_index,
|
57 |
+
progress,
|
58 |
+
gr.update(interactive=True),
|
59 |
+
gr.update(interactive=True),
|
60 |
+
gr.update(interactive=True),
|
61 |
+
gr.update(interactive=True),
|
62 |
+
gr.update(visible=False)
|
63 |
+
)
|
64 |
+
|
65 |
+
# 加载样本
|
66 |
+
def load_sample(i):
|
67 |
+
entry = data[i]
|
68 |
+
return entry["source"], entry["hypothesis"]
|
69 |
+
|
70 |
+
# 导出打分结果为 JSON 文件
|
71 |
+
def export_results():
|
72 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".json", mode="w", encoding="utf-8")
|
73 |
+
json.dump(user_annotations, tmp, ensure_ascii=False, indent=2)
|
74 |
+
tmp.close()
|
75 |
+
return tmp.name
|
76 |
+
|
77 |
+
with gr.Blocks() as demo:
|
78 |
+
gr.Markdown("## Direct Assessment Annotation")
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
annotator = gr.Textbox(
|
82 |
+
label="Annotator ID",
|
83 |
+
placeholder="Enter your name or ID",
|
84 |
+
value="annotator_1",
|
85 |
+
)
|
86 |
+
progress = gr.Textbox(label="Progress", interactive=False)
|
87 |
+
|
88 |
+
idx = gr.Number(value=0, visible=False)
|
89 |
+
source = gr.Textbox(label="Source Sentence", interactive=False)
|
90 |
+
hyp = gr.Textbox(label="Machine Translation", interactive=False)
|
91 |
+
score = gr.Slider(0, 100, step=1, label="Translation Quality Score")
|
92 |
+
comment = gr.Textbox(lines=2, placeholder="Optional comment...", label="Comment")
|
93 |
+
output = gr.Textbox(label="Status", interactive=False)
|
94 |
+
next_button = gr.Button("Submit and Next")
|
95 |
+
|
96 |
+
# 新增:导出按钮和文件下载组件
|
97 |
+
export_button = gr.Button("📥 Export My Results")
|
98 |
+
export_file = gr.File(label="Download your results", visible=False)
|
99 |
+
|
100 |
+
# 原打分按钮逻辑
|
101 |
+
next_button.click(
|
102 |
+
fn=annotate,
|
103 |
+
inputs=[idx, score, comment, annotator],
|
104 |
+
outputs=[output, idx, progress, score, comment, next_button, annotator, export_file],
|
105 |
+
)
|
106 |
+
|
107 |
+
# 新增导出逻辑
|
108 |
+
export_button.click(fn=export_results, outputs=export_file)
|
109 |
+
|
110 |
+
idx.change(fn=load_sample, inputs=idx, outputs=[source, hyp])
|
111 |
+
demo.load(fn=load_sample, inputs=[idx], outputs=[source, hyp])
|
112 |
+
|
113 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|