Zihao-Li commited on
Commit
71df68e
·
verified ·
1 Parent(s): 2da8baa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -82
app.py CHANGED
@@ -1,82 +1,113 @@
1
- import gradio as gr
2
- import json
3
- import os
4
-
5
- DATA_FILE = "./test_data.json"
6
- SAVE_FILE = "./annotations.jsonl"
7
-
8
- with open(DATA_FILE, "r", encoding="utf-8") as f:
9
- data = json.load(f)
10
-
11
- if not os.path.exists(SAVE_FILE):
12
- with open(SAVE_FILE, "w", encoding="utf-8") as f:
13
- pass
14
-
15
- def annotate(index, score, comment, annotator):
16
- entry = data[index]
17
- record = {
18
- "index": index,
19
- "annotator": annotator,
20
- "source": entry["source"],
21
- "hypothesis": entry["hypothesis"],
22
- "score": score,
23
- "comment": comment
24
- }
25
-
26
- save_path = f"./annotations_{annotator}.jsonl"
27
- with open(save_path, "a", encoding="utf-8") as f:
28
- f.write(json.dumps(record, ensure_ascii=False) + "\n")
29
-
30
- completed = index + 1
31
- if completed >= len(data):
32
- return (
33
- "🎉 All samples annotated!",
34
- index,
35
- f"✅ Completed {completed}/{len(data)} samples.",
36
- gr.update(interactive=False),
37
- gr.update(interactive=False),
38
- gr.update(interactive=False),
39
- gr.update(interactive=False)
40
- )
41
-
42
- next_index = index + 1
43
- progress = f"{completed}/{len(data)} annotated by {annotator}"
44
- return (
45
- "✅ Saved",
46
- next_index,
47
- progress,
48
- gr.update(interactive=True),
49
- gr.update(interactive=True),
50
- gr.update(interactive=True),
51
- gr.update(interactive=True)
52
- )
53
-
54
-
55
- def load_sample(i):
56
- entry = data[i]
57
- return entry["source"], entry["hypothesis"]
58
-
59
- with gr.Blocks() as demo:
60
- gr.Markdown("## Direct Assessment Annotation")
61
-
62
- with gr.Row():
63
- annotator = gr.Textbox(label="Annotator ID", placeholder="Enter your name or ID", value="annotator_1")
64
- progress = gr.Textbox(label="Progress", interactive=False)
65
-
66
- idx = gr.Number(value=0, visible=False)
67
- source = gr.Textbox(label="Source Sentence", interactive=False)
68
- hyp = gr.Textbox(label="Machine Translation", interactive=False)
69
- score = gr.Slider(0, 100, step=1, label="Translation Quality Score")
70
- comment = gr.Textbox(lines=2, placeholder="Optional comment...", label="Comment")
71
- output = gr.Textbox(label="Status", interactive=False)
72
- next_button = gr.Button("Submit and Next")
73
-
74
- next_button.click(
75
- fn=annotate,
76
- inputs=[idx, score, comment, annotator],
77
- outputs=[output, idx, progress, score, comment, next_button, annotator]
78
- )
79
- idx.change(fn=load_sample, inputs=idx, outputs=[source, hyp])
80
- demo.load(fn=load_sample, inputs=[idx], outputs=[source, hyp])
81
-
82
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)))