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

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +82 -0
  2. requirements.txt +1 -0
  3. test_data.json +10 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio==5.23.3
test_data.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "source": "The weather is nice today.",
4
+ "hypothesis": "Il fait beau aujourd'hui."
5
+ },
6
+ {
7
+ "source": "I would like a cup of coffee.",
8
+ "hypothesis": "Je veux une tasse de café."
9
+ }
10
+ ]