mdkrasnow commited on
Commit
73fd9ce
·
1 Parent(s): b76f94b

Add gradio logging

Browse files
Files changed (1) hide show
  1. app.py +126 -11
app.py CHANGED
@@ -4,7 +4,7 @@ import json
4
  import uuid
5
  import datetime
6
  import os
7
- from typing import Dict, List, Any, Tuple
8
 
9
  # Import our modules
10
  import data_utils
@@ -32,7 +32,7 @@ def load_example(state):
32
  current_output = data_utils.all_outputs[state["current_example_index"]]
33
 
34
  # Return values in the same order as the outputs parameter in demo.load
35
- return current_output["low_inference_notes"], current_output["output"]["summary"]
36
  except (IndexError, KeyError) as e:
37
  # Handle errors by initializing a new cycle
38
  data_utils.initialize_cycle()
@@ -40,11 +40,12 @@ def load_example(state):
40
  current_output = data_utils.all_outputs[0]
41
 
42
  # Return values in the same order as the outputs parameter in demo.load
43
- return current_output["low_inference_notes"], current_output["output"]["summary"]
44
 
45
  def select_choice(choice, state):
46
- """Just set the choice in the state without submitting"""
47
  state["current_choice"] = choice
 
48
  return {
49
  "good_btn": gr.update(variant="primary" if choice == "good" else "secondary",
50
  elem_classes=["good-button", "selected-button"] if choice == "good" else ["good-button"]),
@@ -52,6 +53,7 @@ def select_choice(choice, state):
52
  elem_classes=["fine-button", "selected-button"] if choice == "fine" else ["fine-button"]),
53
  "bad_btn": gr.update(variant="primary" if choice == "bad" else "secondary",
54
  elem_classes=["bad-button", "selected-button"] if choice == "bad" else ["bad-button"]),
 
55
  "state": state
56
  }
57
 
@@ -61,26 +63,39 @@ def stage1_submit(explanation, state):
61
  if not state["current_choice"]:
62
  return {
63
  "error_message": gr.update(value="Please select a rating (Good, Fine, or Bad).", visible=True),
 
64
  "state": state
65
  }
66
 
67
  if not explanation.strip():
68
  return {
69
  "error_message": gr.update(value="Please provide an explanation for your choice.", visible=True),
 
70
  "state": state
71
  }
72
 
73
  # Get current output
74
  current_output = data_utils.all_outputs[state["current_example_index"]]
75
 
 
 
 
 
76
  # Store feedback in Argilla
77
- data_utils.store_feedback(
78
  output_id=current_output["id"],
79
  choice=state["current_choice"],
80
  explanation=explanation,
81
  stage="individual"
82
  )
83
 
 
 
 
 
 
 
 
84
  # Increment counter
85
  state["examples_completed"] += 1
86
 
@@ -106,10 +121,12 @@ def stage1_submit(explanation, state):
106
  return {
107
  "stage1_group": gr.update(visible=True),
108
  "stage2_group": gr.update(visible=False),
 
109
  "notes_display": current_output["low_inference_notes"],
110
  "output_display": current_output["output"]["summary"],
111
  "explanation_input": gr.update(value=""),
112
  "error_message": gr.update(value="", visible=False),
 
113
  "good_btn": gr.update(variant="secondary"),
114
  "fine_btn": gr.update(variant="secondary"),
115
  "bad_btn": gr.update(variant="secondary"),
@@ -127,6 +144,7 @@ def stage1_submit(explanation, state):
127
  "explanation_input": gr.update(value=""),
128
  "comparison_explanation": gr.update(value=""),
129
  "error_message": gr.update(value="", visible=False),
 
130
  "good_btn": gr.update(variant="secondary"),
131
  "fine_btn": gr.update(variant="secondary"),
132
  "bad_btn": gr.update(variant="secondary"),
@@ -140,10 +158,12 @@ def stage1_submit(explanation, state):
140
  return {
141
  "stage1_group": gr.update(visible=True),
142
  "stage2_group": gr.update(visible=False),
 
143
  "notes_display": current_output["low_inference_notes"],
144
  "output_display": current_output["output"]["summary"],
145
  "explanation_input": gr.update(value=""),
146
  "error_message": gr.update(value="", visible=False),
 
147
  "good_btn": gr.update(variant="secondary"),
148
  "fine_btn": gr.update(variant="secondary"),
149
  "bad_btn": gr.update(variant="secondary"),
@@ -151,13 +171,15 @@ def stage1_submit(explanation, state):
151
  }
152
 
153
  def select_comparison_choice(choice, state):
154
- """Just set the comparison choice in the state without submitting"""
155
  state["current_choice"] = choice
 
156
  return {
157
  "choose_a_btn": gr.update(variant="primary" if choice == "a" else "secondary",
158
  elem_classes=["selected-button"] if choice == "a" else []),
159
  "choose_b_btn": gr.update(variant="primary" if choice == "b" else "secondary",
160
  elem_classes=["selected-button"] if choice == "b" else []),
 
161
  "state": state
162
  }
163
 
@@ -167,12 +189,14 @@ def stage2_submit(explanation, state):
167
  if not state["current_choice"]:
168
  return {
169
  "error_message": gr.update(value="Please select which output is better (A or B).", visible=True),
 
170
  "state": state
171
  }
172
 
173
  if not explanation.strip():
174
  return {
175
  "error_message": gr.update(value="Please provide an explanation for your choice.", visible=True),
 
176
  "state": state
177
  }
178
 
@@ -183,17 +207,35 @@ def stage2_submit(explanation, state):
183
  # Determine which output was chosen
184
  a_wins = (state["current_choice"] == "a")
185
 
 
 
 
 
186
  # Update ELO ratings
187
- elo.update_ratings(output_a["id"], output_b["id"], a_wins)
 
 
 
 
 
 
 
188
 
189
  # Store feedback
190
- data_utils.store_feedback(
191
  output_id=output_a["id"] if a_wins else output_b["id"],
192
  choice=f"preferred_over_{output_b['id'] if a_wins else output_a['id']}",
193
  explanation=explanation,
194
  stage="comparison"
195
  )
196
 
 
 
 
 
 
 
 
197
  # Reset current choice
198
  state["current_choice"] = None
199
 
@@ -210,11 +252,13 @@ def stage2_submit(explanation, state):
210
  return {
211
  "stage1_group": gr.update(visible=True),
212
  "stage2_group": gr.update(visible=False),
 
213
  "notes_display": current_output["low_inference_notes"],
214
  "output_display": current_output["output"]["summary"],
215
  "explanation_input": gr.update(value=""),
216
  "comparison_explanation": gr.update(value=""),
217
  "error_message": gr.update(value="", visible=False),
 
218
  "choose_a_btn": gr.update(variant="secondary"),
219
  "choose_b_btn": gr.update(variant="secondary"),
220
  "state": state
@@ -232,11 +276,13 @@ def stage2_submit(explanation, state):
232
  return {
233
  "stage1_group": gr.update(visible=True),
234
  "stage2_group": gr.update(visible=False),
 
235
  "notes_display": current_output["low_inference_notes"],
236
  "output_display": current_output["output"]["summary"],
237
  "explanation_input": gr.update(value=""),
238
  "comparison_explanation": gr.update(value=""),
239
  "error_message": gr.update(value="", visible=False),
 
240
  "choose_a_btn": gr.update(variant="secondary"),
241
  "choose_b_btn": gr.update(variant="secondary"),
242
  "state": state
@@ -252,6 +298,7 @@ def stage2_submit(explanation, state):
252
  "output_b": output_b["output"]["summary"],
253
  "comparison_explanation": gr.update(value=""),
254
  "error_message": gr.update(value="", visible=False),
 
255
  "choose_a_btn": gr.update(variant="secondary"),
256
  "choose_b_btn": gr.update(variant="secondary"),
257
  "state": state
@@ -302,12 +349,20 @@ with gr.Blocks(theme=gr.themes.Soft(
302
  opacity: 0.8;
303
  box-shadow: 0 0 5px rgba(0,0,0,0.5);
304
  }
 
 
 
 
 
 
 
305
  """) as demo:
306
  # Initialize state
307
  state = gr.State(initialize_state())
308
 
309
  # Error message area
310
  error_message = gr.Markdown(visible=False)
 
311
 
312
  with gr.Tabs():
313
  with gr.TabItem("Data Labeling"):
@@ -316,6 +371,9 @@ with gr.Blocks(theme=gr.themes.Soft(
316
  gr.Markdown("# Output Evaluation")
317
  gr.Markdown("### Please rate the quality of this output")
318
 
 
 
 
319
  with gr.Row():
320
  # Left column for notes - make it scrollable and smaller
321
  with gr.Column(scale=1):
@@ -374,7 +432,7 @@ with gr.Blocks(theme=gr.themes.Soft(
374
  label="Output A",
375
  lines=8,
376
  interactive=False
377
- )
378
  choose_a_btn = gr.Button("Output A is better", variant="secondary")
379
 
380
  with gr.Column():
@@ -402,7 +460,7 @@ with gr.Blocks(theme=gr.themes.Soft(
402
  demo.load(
403
  fn=load_example,
404
  inputs=[state],
405
- outputs=[notes_display, output_display]
406
  )
407
 
408
  # Also load the leaderboard on startup
@@ -411,6 +469,62 @@ with gr.Blocks(theme=gr.themes.Soft(
411
  inputs=[],
412
  outputs=[leaderboard_display]
413
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
414
 
415
  # For Hugging Face Spaces deployment
416
  if __name__ == "__main__":
@@ -418,4 +532,5 @@ if __name__ == "__main__":
418
  data_utils.init_argilla()
419
 
420
  # Launch the app
421
- demo.launch()
 
 
4
  import uuid
5
  import datetime
6
  import os
7
+ from typing import Dict, List, Any, Tuple, Optional
8
 
9
  # Import our modules
10
  import data_utils
 
32
  current_output = data_utils.all_outputs[state["current_example_index"]]
33
 
34
  # Return values in the same order as the outputs parameter in demo.load
35
+ return current_output["component_id"], current_output["low_inference_notes"], current_output["output"]["summary"]
36
  except (IndexError, KeyError) as e:
37
  # Handle errors by initializing a new cycle
38
  data_utils.initialize_cycle()
 
40
  current_output = data_utils.all_outputs[0]
41
 
42
  # Return values in the same order as the outputs parameter in demo.load
43
+ return current_output["component_id"], current_output["low_inference_notes"], current_output["output"]["summary"]
44
 
45
  def select_choice(choice, state):
46
+ """Set the choice in the state and provide visual feedback"""
47
  state["current_choice"] = choice
48
+ print(f"User selected: {choice}")
49
  return {
50
  "good_btn": gr.update(variant="primary" if choice == "good" else "secondary",
51
  elem_classes=["good-button", "selected-button"] if choice == "good" else ["good-button"]),
 
53
  elem_classes=["fine-button", "selected-button"] if choice == "fine" else ["fine-button"]),
54
  "bad_btn": gr.update(variant="primary" if choice == "bad" else "secondary",
55
  elem_classes=["bad-button", "selected-button"] if choice == "bad" else ["bad-button"]),
56
+ "log_message": gr.update(value=f"Selected rating: {choice.upper()}", visible=True),
57
  "state": state
58
  }
59
 
 
63
  if not state["current_choice"]:
64
  return {
65
  "error_message": gr.update(value="Please select a rating (Good, Fine, or Bad).", visible=True),
66
+ "log_message": gr.update(value="Submission failed: No rating selected", visible=True),
67
  "state": state
68
  }
69
 
70
  if not explanation.strip():
71
  return {
72
  "error_message": gr.update(value="Please provide an explanation for your choice.", visible=True),
73
+ "log_message": gr.update(value="Submission failed: No explanation provided", visible=True),
74
  "state": state
75
  }
76
 
77
  # Get current output
78
  current_output = data_utils.all_outputs[state["current_example_index"]]
79
 
80
+ # Log the action
81
+ print(f"Submitting feedback for output ID: {current_output['id']}")
82
+ print(f"Choice: {state['current_choice']}, Explanation: {explanation}")
83
+
84
  # Store feedback in Argilla
85
+ success = data_utils.store_feedback(
86
  output_id=current_output["id"],
87
  choice=state["current_choice"],
88
  explanation=explanation,
89
  stage="individual"
90
  )
91
 
92
+ if not success:
93
+ return {
94
+ "error_message": gr.update(value="Failed to store feedback in Argilla.", visible=True),
95
+ "log_message": gr.update(value="Submission failed: Error storing in Argilla", visible=True),
96
+ "state": state
97
+ }
98
+
99
  # Increment counter
100
  state["examples_completed"] += 1
101
 
 
121
  return {
122
  "stage1_group": gr.update(visible=True),
123
  "stage2_group": gr.update(visible=False),
124
+ "component_id_display": current_output["component_id"],
125
  "notes_display": current_output["low_inference_notes"],
126
  "output_display": current_output["output"]["summary"],
127
  "explanation_input": gr.update(value=""),
128
  "error_message": gr.update(value="", visible=False),
129
+ "log_message": gr.update(value="Started new cycle - not enough rated outputs for comparison", visible=True),
130
  "good_btn": gr.update(variant="secondary"),
131
  "fine_btn": gr.update(variant="secondary"),
132
  "bad_btn": gr.update(variant="secondary"),
 
144
  "explanation_input": gr.update(value=""),
145
  "comparison_explanation": gr.update(value=""),
146
  "error_message": gr.update(value="", visible=False),
147
+ "log_message": gr.update(value="Moving to comparison stage", visible=True),
148
  "good_btn": gr.update(variant="secondary"),
149
  "fine_btn": gr.update(variant="secondary"),
150
  "bad_btn": gr.update(variant="secondary"),
 
158
  return {
159
  "stage1_group": gr.update(visible=True),
160
  "stage2_group": gr.update(visible=False),
161
+ "component_id_display": current_output["component_id"],
162
  "notes_display": current_output["low_inference_notes"],
163
  "output_display": current_output["output"]["summary"],
164
  "explanation_input": gr.update(value=""),
165
  "error_message": gr.update(value="", visible=False),
166
+ "log_message": gr.update(value=f"Submission successful! Moving to next example ({state['examples_completed']+1}/{len(data_utils.all_outputs)})", visible=True),
167
  "good_btn": gr.update(variant="secondary"),
168
  "fine_btn": gr.update(variant="secondary"),
169
  "bad_btn": gr.update(variant="secondary"),
 
171
  }
172
 
173
  def select_comparison_choice(choice, state):
174
+ """Set the comparison choice in the state and provide visual feedback"""
175
  state["current_choice"] = choice
176
+ print(f"User selected output: {choice}")
177
  return {
178
  "choose_a_btn": gr.update(variant="primary" if choice == "a" else "secondary",
179
  elem_classes=["selected-button"] if choice == "a" else []),
180
  "choose_b_btn": gr.update(variant="primary" if choice == "b" else "secondary",
181
  elem_classes=["selected-button"] if choice == "b" else []),
182
+ "log_message": gr.update(value=f"Selected output: {choice.upper()}", visible=True),
183
  "state": state
184
  }
185
 
 
189
  if not state["current_choice"]:
190
  return {
191
  "error_message": gr.update(value="Please select which output is better (A or B).", visible=True),
192
+ "log_message": gr.update(value="Submission failed: No output selected", visible=True),
193
  "state": state
194
  }
195
 
196
  if not explanation.strip():
197
  return {
198
  "error_message": gr.update(value="Please provide an explanation for your choice.", visible=True),
199
+ "log_message": gr.update(value="Submission failed: No explanation provided", visible=True),
200
  "state": state
201
  }
202
 
 
207
  # Determine which output was chosen
208
  a_wins = (state["current_choice"] == "a")
209
 
210
+ # Log the action
211
+ print(f"Comparing outputs: {output_a['id']} vs {output_b['id']}")
212
+ print(f"User chose: {'A' if a_wins else 'B'}, Explanation: {explanation}")
213
+
214
  # Update ELO ratings
215
+ new_rating_a, new_rating_b = elo.update_ratings(output_a["id"], output_b["id"], a_wins)
216
+
217
+ if new_rating_a is None or new_rating_b is None:
218
+ return {
219
+ "error_message": gr.update(value="Failed to update ELO ratings.", visible=True),
220
+ "log_message": gr.update(value="Submission failed: Error updating ELO ratings", visible=True),
221
+ "state": state
222
+ }
223
 
224
  # Store feedback
225
+ success = data_utils.store_feedback(
226
  output_id=output_a["id"] if a_wins else output_b["id"],
227
  choice=f"preferred_over_{output_b['id'] if a_wins else output_a['id']}",
228
  explanation=explanation,
229
  stage="comparison"
230
  )
231
 
232
+ if not success:
233
+ return {
234
+ "error_message": gr.update(value="Failed to store comparison feedback in Argilla.", visible=True),
235
+ "log_message": gr.update(value="Submission failed: Error storing comparison in Argilla", visible=True),
236
+ "state": state
237
+ }
238
+
239
  # Reset current choice
240
  state["current_choice"] = None
241
 
 
252
  return {
253
  "stage1_group": gr.update(visible=True),
254
  "stage2_group": gr.update(visible=False),
255
+ "component_id_display": current_output["component_id"],
256
  "notes_display": current_output["low_inference_notes"],
257
  "output_display": current_output["output"]["summary"],
258
  "explanation_input": gr.update(value=""),
259
  "comparison_explanation": gr.update(value=""),
260
  "error_message": gr.update(value="", visible=False),
261
+ "log_message": gr.update(value="Completed comparison cycle. Starting new evaluation cycle.", visible=True),
262
  "choose_a_btn": gr.update(variant="secondary"),
263
  "choose_b_btn": gr.update(variant="secondary"),
264
  "state": state
 
276
  return {
277
  "stage1_group": gr.update(visible=True),
278
  "stage2_group": gr.update(visible=False),
279
+ "component_id_display": current_output["component_id"],
280
  "notes_display": current_output["low_inference_notes"],
281
  "output_display": current_output["output"]["summary"],
282
  "explanation_input": gr.update(value=""),
283
  "comparison_explanation": gr.update(value=""),
284
  "error_message": gr.update(value="", visible=False),
285
+ "log_message": gr.update(value="Not enough rated outputs for comparison. Starting new cycle.", visible=True),
286
  "choose_a_btn": gr.update(variant="secondary"),
287
  "choose_b_btn": gr.update(variant="secondary"),
288
  "state": state
 
298
  "output_b": output_b["output"]["summary"],
299
  "comparison_explanation": gr.update(value=""),
300
  "error_message": gr.update(value="", visible=False),
301
+ "log_message": gr.update(value=f"Comparison submitted successfully! Moving to next comparison ({state['elo_comparisons_done']+1}/5)", visible=True),
302
  "choose_a_btn": gr.update(variant="secondary"),
303
  "choose_b_btn": gr.update(variant="secondary"),
304
  "state": state
 
349
  opacity: 0.8;
350
  box-shadow: 0 0 5px rgba(0,0,0,0.5);
351
  }
352
+ .log-message {
353
+ padding: 8px;
354
+ margin-top: 10px;
355
+ border-radius: 4px;
356
+ background-color: #f0f0f0;
357
+ font-size: 14px;
358
+ }
359
  """) as demo:
360
  # Initialize state
361
  state = gr.State(initialize_state())
362
 
363
  # Error message area
364
  error_message = gr.Markdown(visible=False)
365
+ log_message = gr.Markdown(visible=False, elem_classes=["log-message"])
366
 
367
  with gr.Tabs():
368
  with gr.TabItem("Data Labeling"):
 
371
  gr.Markdown("# Output Evaluation")
372
  gr.Markdown("### Please rate the quality of this output")
373
 
374
+ # Component ID display
375
+ component_id_display = gr.Markdown("### Component ID: Loading...")
376
+
377
  with gr.Row():
378
  # Left column for notes - make it scrollable and smaller
379
  with gr.Column(scale=1):
 
432
  label="Output A",
433
  lines=8,
434
  interactive=False
435
+ )
436
  choose_a_btn = gr.Button("Output A is better", variant="secondary")
437
 
438
  with gr.Column():
 
460
  demo.load(
461
  fn=load_example,
462
  inputs=[state],
463
+ outputs=[component_id_display, notes_display, output_display]
464
  )
465
 
466
  # Also load the leaderboard on startup
 
469
  inputs=[],
470
  outputs=[leaderboard_display]
471
  )
472
+
473
+ # Connect the buttons to their functions
474
+ good_btn.click(
475
+ fn=select_choice,
476
+ inputs=["good", state],
477
+ outputs=[good_btn, fine_btn, bad_btn, log_message, state]
478
+ )
479
+
480
+ fine_btn.click(
481
+ fn=select_choice,
482
+ inputs=["fine", state],
483
+ outputs=[good_btn, fine_btn, bad_btn, log_message, state]
484
+ )
485
+
486
+ bad_btn.click(
487
+ fn=select_choice,
488
+ inputs=["bad", state],
489
+ outputs=[good_btn, fine_btn, bad_btn, log_message, state]
490
+ )
491
+
492
+ submit_btn.click(
493
+ fn=stage1_submit,
494
+ inputs=[explanation_input, state],
495
+ outputs=[
496
+ stage1_group, stage2_group, component_id_display, notes_display, output_display,
497
+ explanation_input, error_message, log_message, good_btn, fine_btn, bad_btn, state
498
+ ]
499
+ )
500
+
501
+ choose_a_btn.click(
502
+ fn=select_comparison_choice,
503
+ inputs=["a", state],
504
+ outputs=[choose_a_btn, choose_b_btn, log_message, state]
505
+ )
506
+
507
+ choose_b_btn.click(
508
+ fn=select_comparison_choice,
509
+ inputs=["b", state],
510
+ outputs=[choose_a_btn, choose_b_btn, log_message, state]
511
+ )
512
+
513
+ compare_submit_btn.click(
514
+ fn=stage2_submit,
515
+ inputs=[comparison_explanation, state],
516
+ outputs=[
517
+ stage1_group, stage2_group, component_id_display, notes_display, output_display,
518
+ notes_display_elo, output_a, output_b, comparison_explanation,
519
+ error_message, log_message, choose_a_btn, choose_b_btn, state
520
+ ]
521
+ )
522
+
523
+ refresh_btn.click(
524
+ fn=get_leaderboard_display,
525
+ inputs=[],
526
+ outputs=[leaderboard_display]
527
+ )
528
 
529
  # For Hugging Face Spaces deployment
530
  if __name__ == "__main__":
 
532
  data_utils.init_argilla()
533
 
534
  # Launch the app
535
+ demo.launch()
536
+