Zihao-Li commited on
Commit
c08865b
·
verified ·
1 Parent(s): 029f30f

Upload app.py

Browse files

support previous button

Files changed (1) hide show
  1. app.py +75 -6
app.py CHANGED
@@ -79,6 +79,7 @@ def annotate(index, score, comment, annotator):
79
  global current_lang
80
  index = int(index)
81
  entry = data[index]
 
82
  record = {
83
  "index": index,
84
  "annotator": annotator,
@@ -88,6 +89,13 @@ def annotate(index, score, comment, annotator):
88
  "score": score,
89
  "comment": comment,
90
  }
 
 
 
 
 
 
 
91
  user_annotations.append(record)
92
 
93
  # 保存到服务器端(可选)
@@ -109,19 +117,65 @@ def annotate(index, score, comment, annotator):
109
  )
110
 
111
  next_index = index + 1
 
 
 
 
 
 
 
 
 
112
  progress = f"{completed}/{len(data)} annotated by {annotator}"
 
113
  return (
114
  "✅ Saved",
115
  next_index,
116
  progress,
117
- gr.update(interactive=True),
118
- gr.update(interactive=True),
119
- gr.update(interactive=True),
120
- gr.update(interactive=True),
121
  gr.update(visible=False),
122
  )
123
 
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  def export_results():
126
  if not user_annotations:
127
  raise ValueError("No annotations to export.")
@@ -165,7 +219,8 @@ with gr.Blocks() as demo:
165
  score = gr.Slider(0, 100, step=1, label="Translation Quality Score")
166
  comment = gr.Textbox(lines=2, placeholder="Optional comment...", label="Comment")
167
  output = gr.Textbox(label="Status", interactive=False)
168
- next_button = gr.Button("Submit and Next")
 
169
  export_file = gr.File(label="Download your results", visible=False)
170
 
171
  load_button.click(
@@ -187,11 +242,25 @@ with gr.Blocks() as demo:
187
  progress,
188
  score,
189
  comment,
 
190
  next_button,
191
- annotator,
192
  export_file,
193
  ],
194
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  export_button.click(
196
  fn=export_results,
197
  inputs=[],
 
79
  global current_lang
80
  index = int(index)
81
  entry = data[index]
82
+
83
  record = {
84
  "index": index,
85
  "annotator": annotator,
 
89
  "score": score,
90
  "comment": comment,
91
  }
92
+
93
+ global user_annotations
94
+ user_annotations = [
95
+ rec
96
+ for rec in user_annotations
97
+ if rec["index"] != index or rec["annotator"] != annotator
98
+ ]
99
  user_annotations.append(record)
100
 
101
  # 保存到服务器端(可选)
 
117
  )
118
 
119
  next_index = index + 1
120
+ next_entry = data[next_index]
121
+
122
+ prev_score, prev_comment = 0, ""
123
+ for rec in user_annotations:
124
+ if rec["index"] == next_index and rec["annotator"] == annotator:
125
+ prev_score = rec["score"]
126
+ prev_comment = rec["comment"]
127
+ break
128
+
129
  progress = f"{completed}/{len(data)} annotated by {annotator}"
130
+ is_at_start = next_index == 0
131
  return (
132
  "✅ Saved",
133
  next_index,
134
  progress,
135
+ gr.update(value=prev_score, interactive=True),
136
+ gr.update(value=prev_comment, interactive=True),
137
+ gr.update(interactive=not is_at_start), # Previous button
138
+ gr.update(interactive=True), # Next button
139
  gr.update(visible=False),
140
  )
141
 
142
 
143
+ def go_previous(index, annotator):
144
+ index = int(index)
145
+ if index <= 0:
146
+ return (
147
+ 0,
148
+ data[0]["source"],
149
+ data[0]["hypothesis"],
150
+ "",
151
+ 0,
152
+ "",
153
+ f"At the beginning. 0/{len(data)}",
154
+ )
155
+
156
+ prev_index = index - 1
157
+ entry = data[prev_index]
158
+ prev_score, prev_comment = 0, ""
159
+ for rec in user_annotations:
160
+ if rec["index"] == prev_index and rec["annotator"] == annotator:
161
+ prev_score = rec["score"]
162
+ prev_comment = rec["comment"]
163
+ break
164
+ is_at_start = prev_index == 0
165
+ progress = f"{prev_index}/{len(data)} annotated by {annotator}"
166
+
167
+ return (
168
+ prev_index,
169
+ entry["source"],
170
+ entry["hypothesis"],
171
+ prev_score,
172
+ prev_comment,
173
+ progress,
174
+ gr.update(interactive=not is_at_start), # Previous button
175
+ gr.update(interactive=True), # Next button
176
+ )
177
+
178
+
179
  def export_results():
180
  if not user_annotations:
181
  raise ValueError("No annotations to export.")
 
219
  score = gr.Slider(0, 100, step=1, label="Translation Quality Score")
220
  comment = gr.Textbox(lines=2, placeholder="Optional comment...", label="Comment")
221
  output = gr.Textbox(label="Status", interactive=False)
222
+ previous_button = gr.Button("⏪Previous")
223
+ next_button = gr.Button("⏩Next")
224
  export_file = gr.File(label="Download your results", visible=False)
225
 
226
  load_button.click(
 
242
  progress,
243
  score,
244
  comment,
245
+ previous_button,
246
  next_button,
 
247
  export_file,
248
  ],
249
  )
250
+ previous_button.click(
251
+ fn=go_previous,
252
+ inputs=[idx, annotator],
253
+ outputs=[
254
+ idx,
255
+ source,
256
+ hyp,
257
+ score,
258
+ comment,
259
+ progress,
260
+ previous_button,
261
+ next_button,
262
+ ],
263
+ )
264
  export_button.click(
265
  fn=export_results,
266
  inputs=[],