oliver-aizip kai-aizip commited on
Commit
a24f6cf
·
verified ·
1 Parent(s): 9781f99

Component to store vote details to a csv (#8)

Browse files

- Component to store vote details to a csv (bd9b954f2fea7eab00e1bc7c3b484255b95f6c04)


Co-authored-by: Kai <[email protected]>

Files changed (1) hide show
  1. utils/vote_logger.py +121 -0
utils/vote_logger.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import csv
3
+ import json
4
+ from datetime import datetime
5
+ import pandas as pd
6
+
7
+ def save_vote_details(example, model_a, model_b, winner, feedback, summary_a, summary_b):
8
+ """
9
+ Save detailed vote information to CSV file for future analysis.
10
+
11
+ Parameters:
12
+ - example: The question and context information
13
+ - model_a, model_b: Names of models being compared
14
+ - winner: 'left', 'right', 'tie', or 'neither' indicating the vote result
15
+ - feedback: List of feedback options selected by the user
16
+ - summary_a, summary_b: The model outputs (summaries)
17
+ """
18
+ # Prepare the vote details record
19
+ vote_record = {
20
+ "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
21
+ "model_a": model_a,
22
+ "model_b": model_b,
23
+ "winner": winner,
24
+ "feedback": json.dumps(feedback),
25
+ "question_id": example.get("id", "unknown"),
26
+ "question": example.get("question", ""),
27
+ "insufficient_context": example.get("insufficient", False),
28
+ "summary_a": summary_a,
29
+ "summary_b": summary_b
30
+ }
31
+
32
+ # Define the path to the CSV file
33
+ csv_path = os.path.join('utils', 'vote_details.csv')
34
+ file_exists = os.path.exists(csv_path)
35
+
36
+ try:
37
+ # Open the file in append mode
38
+ with open(csv_path, 'a', newline='', encoding='utf-8') as f:
39
+ writer = csv.DictWriter(f, fieldnames=vote_record.keys())
40
+
41
+ # Write header if file doesn't exist
42
+ if not file_exists:
43
+ writer.writeheader()
44
+
45
+ # Write the vote record
46
+ writer.writerow(vote_record)
47
+
48
+ print(f"Vote details saved to {csv_path}")
49
+ except Exception as e:
50
+ print(f"Error saving vote details: {e}")
51
+
52
+ # Create a backup copy every 10 votes
53
+ try:
54
+ if os.path.exists(csv_path):
55
+ with open(csv_path, 'r', encoding='utf-8') as f:
56
+ num_votes = sum(1 for _ in f) - 1 # Subtract 1 for header
57
+
58
+ if num_votes % 10 == 0:
59
+ backup_path = os.path.join('utils', f'vote_details_backup_{datetime.now().strftime("%Y%m%d_%H%M%S")}.csv')
60
+ with open(csv_path, 'r', encoding='utf-8') as src, open(backup_path, 'w', encoding='utf-8') as dst:
61
+ dst.write(src.read())
62
+ print(f"Created backup at {backup_path}")
63
+ except Exception as e:
64
+ print(f"Error creating backup: {e}")
65
+
66
+ def get_vote_statistics():
67
+ """
68
+ Analyze vote details and provide statistics.
69
+
70
+ Returns:
71
+ - Dictionary of statistics about votes
72
+ """
73
+ csv_path = os.path.join('utils', 'vote_details.csv')
74
+
75
+ if not os.path.exists(csv_path):
76
+ return {"error": "No vote data available"}
77
+
78
+ try:
79
+ # Read the CSV into a DataFrame
80
+ df = pd.read_csv(csv_path)
81
+
82
+ # Basic statistics
83
+ stats = {
84
+ "total_votes": len(df),
85
+ "winner_distribution": {
86
+ "left": len(df[df['winner'] == 'left']),
87
+ "right": len(df[df['winner'] == 'right']),
88
+ "tie": len(df[df['winner'] == 'tie']),
89
+ "neither": len(df[df['winner'] == 'neither'])
90
+ },
91
+ "model_appearances": {},
92
+ "model_wins": {},
93
+ "feedback_frequency": {}
94
+ }
95
+
96
+ # Count model appearances and wins
97
+ for model in set(list(df['model_a']) + list(df['model_b'])):
98
+ a_appearances = len(df[df['model_a'] == model])
99
+ b_appearances = len(df[df['model_b'] == model])
100
+ stats["model_appearances"][model] = a_appearances + b_appearances
101
+
102
+ a_wins = len(df[(df['model_a'] == model) & (df['winner'] == 'left')])
103
+ b_wins = len(df[(df['model_b'] == model) & (df['winner'] == 'right')])
104
+ stats["model_wins"][model] = a_wins + b_wins
105
+
106
+ # Process feedback
107
+ all_feedback = []
108
+ for feedback_json in df['feedback']:
109
+ try:
110
+ feedback_list = json.loads(feedback_json)
111
+ all_feedback.extend(feedback_list)
112
+ except:
113
+ pass
114
+
115
+ for feedback in all_feedback:
116
+ stats["feedback_frequency"][feedback] = stats["feedback_frequency"].get(feedback, 0) + 1
117
+
118
+ return stats
119
+
120
+ except Exception as e:
121
+ return {"error": f"Error analyzing vote data: {e}"}