SondosMB commited on
Commit
93bbadb
·
verified ·
1 Parent(s): 46227fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -321
app.py CHANGED
@@ -20,170 +20,9 @@ HF_TOKEN = os.getenv("HF_TOKEN")
20
  if not HF_TOKEN:
21
  raise ValueError("HF_TOKEN environment variable is not set or invalid.")
22
 
23
- # def initialize_leaderboard_file():
24
- # """
25
- # Ensure the leaderboard file exists and has the correct headers.
26
- # """
27
- # if not os.path.exists(LEADERBOARD_FILE):
28
- # pd.DataFrame(columns=[
29
- # "Model Name", "Overall Accuracy", "Valid Accuracy",
30
- # "Correct Predictions", "Total Questions", "Timestamp"
31
- # ]).to_csv(LEADERBOARD_FILE, index=False)
32
- # elif os.stat(LEADERBOARD_FILE).st_size == 0:
33
- # pd.DataFrame(columns=[
34
- # "Model Name", "Overall Accuracy", "Valid Accuracy",
35
- # "Correct Predictions", "Total Questions", "Timestamp"
36
- # ]).to_csv(LEADERBOARD_FILE, index=False)
37
-
38
- # def clean_answer(answer):
39
- # if pd.isna(answer):
40
- # return None
41
- # answer = str(answer)
42
- # clean = re.sub(r'[^A-Da-d]', '', answer)
43
- # return clean[0].upper() if clean else None
44
-
45
-
46
- # def update_leaderboard(results):
47
- # """
48
- # Append new submission results to the leaderboard file and push updates to the Hugging Face repository.
49
- # """
50
- # new_entry = {
51
- # "Model Name": results['model_name'],
52
- # "Overall Accuracy": round(results['overall_accuracy'] * 100, 2),
53
- # "Valid Accuracy": round(results['valid_accuracy'] * 100, 2),
54
- # "Correct Predictions": results['correct_predictions'],
55
- # "Total Questions": results['total_questions'],
56
- # "Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
57
- # }
58
-
59
- # try:
60
- # # Update the local leaderboard file
61
- # new_entry_df = pd.DataFrame([new_entry])
62
- # file_exists = os.path.exists(LEADERBOARD_FILE)
63
-
64
- # new_entry_df.to_csv(
65
- # LEADERBOARD_FILE,
66
- # mode='a', # Append mode
67
- # index=False,
68
- # header=not file_exists # Write header only if the file is new
69
- # )
70
- # print(f"Leaderboard updated successfully at {LEADERBOARD_FILE}")
71
-
72
- # # Push the updated file to the Hugging Face repository using HTTP API
73
- # api = HfApi()
74
- # token = HfFolder.get_token()
75
-
76
- # api.upload_file(
77
- # path_or_fileobj=LEADERBOARD_FILE,
78
- # path_in_repo="leaderboard.csv",
79
- # repo_id="SondosMB/ss", # Your Space repository
80
- # repo_type="space",
81
- # token=token
82
- # )
83
- # print("Leaderboard changes pushed to Hugging Face repository.")
84
-
85
- # except Exception as e:
86
- # print(f"Error updating leaderboard file: {e}")
87
-
88
-
89
 
90
- # def load_leaderboard():
91
- # if not os.path.exists(LEADERBOARD_FILE) or os.stat(LEADERBOARD_FILE).st_size == 0:
92
- # return pd.DataFrame({
93
- # "Model Name": [],
94
- # "Overall Accuracy": [],
95
- # "Valid Accuracy": [],
96
- # "Correct Predictions": [],
97
- # "Total Questions": [],
98
- # "Timestamp": [],
99
- # })
100
- # return pd.read_csv(LEADERBOARD_FILE)
101
 
102
- # def evaluate_predictions(prediction_file, model_name, add_to_leaderboard):
103
- # try:
104
- # ground_truth_path = hf_hub_download(
105
- # repo_id="SondosMB/ground-truth-dataset",
106
- # filename="ground_truth.csv",
107
- # repo_type="dataset",
108
- # use_auth_token=True
109
- # )
110
- # ground_truth_df = pd.read_csv(ground_truth_path)
111
- # except FileNotFoundError:
112
- # return "Ground truth file not found in the dataset repository.", load_leaderboard()
113
- # except Exception as e:
114
- # return f"Error loading ground truth: {e}", load_leaderboard()
115
-
116
- # if not prediction_file:
117
- # return "Prediction file not uploaded.", load_leaderboard()
118
-
119
- # try:
120
- # #load predition file
121
- # predictions_df = pd.read_csv(prediction_file.name)
122
- # # Validate required columns in prediction file
123
- # required_columns = ['question_id', 'predicted_answer']
124
- # missing_columns = [col for col in required_columns if col not in predictions_df.columns]
125
- # if missing_columns:
126
- # return (f"Error: Missing required columns in prediction file: {', '.join(missing_columns)}.",
127
- # load_leaderboard())
128
-
129
- # # Validate 'Answer' column in ground truth file
130
- # if 'Answer' not in ground_truth_df.columns:
131
- # return "Error: 'Answer' column is missing in the ground truth dataset.", load_leaderboard()
132
- # merged_df = pd.merge(predictions_df, ground_truth_df, on='question_id', how='inner')
133
- # merged_df['pred_answer'] = merged_df['predicted_answer'].apply(clean_answer)
134
-
135
- # valid_predictions = merged_df.dropna(subset=['pred_answer'])
136
- # correct_predictions = (valid_predictions['pred_answer'] == valid_predictions['Answer']).sum()
137
- # total_predictions = len(merged_df)
138
- # total_valid_predictions = len(valid_predictions)
139
-
140
- # overall_accuracy = correct_predictions / total_predictions if total_predictions > 0 else 0
141
- # valid_accuracy = correct_predictions / total_valid_predictions if total_valid_predictions > 0 else 0
142
-
143
- # results = {
144
- # 'model_name': model_name if model_name else "Unknown Model",
145
- # 'overall_accuracy': overall_accuracy,
146
- # }
147
-
148
- # if add_to_leaderboard:
149
- # update_leaderboard(results)
150
- # return "Evaluation completed and added to leaderboard.", load_leaderboard()
151
- # else:
152
- # return "Evaluation completed but not added to leaderboard.", load_leaderboard()
153
-
154
- # except Exception as e:
155
- # return f"Error during evaluation: {str(e)}", load_leaderboard()
156
-
157
- # initialize_leaderboard_file()
158
- # def initialize_leaderboard_file():
159
- # """
160
- # Ensure the leaderboard file exists and has the correct headers.
161
- # """
162
- # if not os.path.exists(LEADERBOARD_FILE):
163
- # pd.DataFrame(columns=[
164
- # "Model Name", "Overall Accuracy", "Valid Accuracy",
165
- # "Correct Predictions", "Total Questions", "Timestamp"
166
- # ]).to_csv(LEADERBOARD_FILE, index=False)
167
- # elif os.stat(LEADERBOARD_FILE).st_size == 0:
168
- # pd.DataFrame(columns=[
169
- # "Model Name", "Overall Accuracy", "Valid Accuracy",
170
- # "Correct Predictions", "Total Questions", "Timestamp"
171
- # ]).to_csv(LEADERBOARD_FILE, index=False)
172
-
173
- # def initialize_leaderboard_file():
174
- # """
175
- # Ensure the leaderboard file exists and has the correct headers.
176
- # """
177
- # if not os.path.exists(LEADERBOARD_FILE):
178
- # pd.DataFrame(columns=[
179
- # "Model Name", "Overall Accuracy", "Correct Predictions",
180
- # "Total Questions", "Timestamp", "Team Name"
181
- # ]).to_csv(LEADERBOARD_FILE, index=False)
182
- # elif os.stat(LEADERBOARD_FILE).st_size == 0:
183
- # pd.DataFrame(columns=[
184
- # "Model Name", "Overall Accuracy", "Correct Predictions",
185
- # "Total Questions", "Timestamp", "Team Name"
186
- # ]).to_csv(LEADERBOARD_FILE, index=False)
187
  def initialize_leaderboard_file():
188
  """
189
  Ensure the leaderboard file exists and has the correct headers.
@@ -222,47 +61,7 @@ def clean_answer(answer):
222
  return clean[0].upper() if clean else None
223
 
224
 
225
- # def update_leaderboard(results):
226
- # """
227
- # Append new submission results to the leaderboard file and push updates to the Hugging Face repository.
228
- # """
229
- # new_entry = {
230
- # "Model Name": results['model_name'],
231
- # "Overall Accuracy": round(results['overall_accuracy'] * 100, 2),
232
- # "Valid Accuracy": round(results['valid_accuracy'] * 100, 2),
233
- # "Correct Predictions": results['correct_predictions'],
234
- # "Total Questions": results['total_questions'],
235
- # "Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
236
- # }
237
-
238
- # try:
239
- # # Update the local leaderboard file
240
- # new_entry_df = pd.DataFrame([new_entry])
241
- # file_exists = os.path.exists(LEADERBOARD_FILE)
242
-
243
- # new_entry_df.to_csv(
244
- # LEADERBOARD_FILE,
245
- # mode='a', # Append mode
246
- # index=False,
247
- # header=not file_exists # Write header only if the file is new
248
- # )
249
- # print(f"Leaderboard updated successfully at {LEADERBOARD_FILE}")
250
-
251
- # # Push the updated file to the Hugging Face repository using HTTP API
252
- # api = HfApi()
253
- # token = HfFolder.get_token()
254
-
255
- # api.upload_file(
256
- # path_or_fileobj=LEADERBOARD_FILE,
257
- # path_in_repo="leaderboard.csv",
258
- # repo_id="SondosMB/ss", # Your Space repository
259
- # repo_type="space",
260
- # token=token
261
- # )
262
- # print("Leaderboard changes pushed to Hugging Face repository.")
263
-
264
- # except Exception as e:
265
- # print(f"Error updating leaderboard file: {e}")
266
 
267
  def update_leaderboard(results):
268
  """
@@ -349,21 +148,23 @@ def update_leaderboard_pro(results):
349
  print(f"Error updating leaderboard file: {e}")
350
 
351
 
 
352
  # def load_leaderboard():
353
  # if not os.path.exists(LEADERBOARD_FILE) or os.stat(LEADERBOARD_FILE).st_size == 0:
354
  # return pd.DataFrame({
355
  # "Model Name": [],
356
  # "Overall Accuracy": [],
357
- # "Valid Accuracy": [],
358
  # "Correct Predictions": [],
359
  # "Total Questions": [],
360
  # "Timestamp": [],
 
 
361
  # })
362
  # return pd.read_csv(LEADERBOARD_FILE)
363
 
364
-
365
  def load_leaderboard():
366
  if not os.path.exists(LEADERBOARD_FILE) or os.stat(LEADERBOARD_FILE).st_size == 0:
 
367
  return pd.DataFrame({
368
  "Model Name": [],
369
  "Overall Accuracy": [],
@@ -371,9 +172,38 @@ def load_leaderboard():
371
  "Total Questions": [],
372
  "Timestamp": [],
373
  "Team Name": [],
374
-
375
  })
376
- return pd.read_csv(LEADERBOARD_FILE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
 
378
  def load_leaderboard_pro():
379
  if not os.path.exists(LEADERBOARD_FILE_pro) or os.stat(LEADERBOARD_FILE_pro).st_size == 0:
@@ -388,120 +218,7 @@ def load_leaderboard_pro():
388
  })
389
  return pd.read_csv(LEADERBOARD_FILE_pro)
390
 
391
- # def evaluate_predictions(prediction_file, model_name, add_to_leaderboard):
392
- # try:
393
- # ground_truth_path = hf_hub_download(
394
- # repo_id="SondosMB/ground-truth-dataset",
395
- # filename="ground_truth.csv",
396
- # repo_type="dataset",
397
- # use_auth_token=True
398
- # )
399
- # ground_truth_df = pd.read_csv(ground_truth_path)
400
- # except FileNotFoundError:
401
- # return "Ground truth file not found in the dataset repository.", load_leaderboard()
402
- # except Exception as e:
403
- # return f"Error loading ground truth: {e}", load_leaderboard()
404
-
405
- # if not prediction_file:
406
- # return "Prediction file not uploaded.", load_leaderboard()
407
-
408
- # try:
409
- # #load predition file
410
- # predictions_df = pd.read_csv(prediction_file.name)
411
- # # Validate required columns in prediction file
412
- # required_columns = ['question_id', 'predicted_answer']
413
- # missing_columns = [col for col in required_columns if col not in predictions_df.columns]
414
- # if missing_columns:
415
- # return (f"Error: Missing required columns in prediction file: {', '.join(missing_columns)}.",
416
- # load_leaderboard())
417
-
418
- # # Validate 'Answer' column in ground truth file
419
- # if 'Answer' not in ground_truth_df.columns:
420
- # return "Error: 'Answer' column is missing in the ground truth dataset.", load_leaderboard()
421
- # merged_df = pd.merge(predictions_df, ground_truth_df, on='question_id', how='inner')
422
- # merged_df['pred_answer'] = merged_df['predicted_answer'].apply(clean_answer)
423
-
424
- # valid_predictions = merged_df.dropna(subset=['pred_answer'])
425
- # correct_predictions = (valid_predictions['pred_answer'] == valid_predictions['Answer']).sum()
426
- # total_predictions = len(merged_df)
427
- # total_valid_predictions = len(valid_predictions)
428
-
429
- # overall_accuracy = correct_predictions / total_predictions if total_predictions > 0 else 0
430
- # valid_accuracy = correct_predictions / total_valid_predictions if total_valid_predictions > 0 else 0
431
-
432
- # results = {
433
- # 'model_name': model_name if model_name else "Unknown Model",
434
- # 'overall_accuracy': overall_accuracy,
435
- # 'valid_accuracy': valid_accuracy,
436
- # 'correct_predictions': correct_predictions,
437
- # 'total_questions': total_predictions,
438
- # }
439
-
440
- # if add_to_leaderboard:
441
- # update_leaderboard(results)
442
- # return "Evaluation completed and added to leaderboard.", load_leaderboard()
443
- # else:
444
- # return "Evaluation completed but not added to leaderboard.", load_leaderboard()
445
-
446
- # except Exception as e:
447
- # return f"Error during evaluation: {str(e)}", load_leaderboard()
448
-
449
- # def evaluate_predictions(prediction_file, model_name,Team_name ,add_to_leaderboard):
450
- # try:
451
- # ground_truth_path = hf_hub_download(
452
- # repo_id="SondosMB/ground-truth-dataset",
453
- # filename="ground_truth.csv",
454
- # repo_type="dataset",
455
- # use_auth_token=True
456
- # )
457
- # ground_truth_df = pd.read_csv(ground_truth_path)
458
- # except FileNotFoundError:
459
- # return "Ground truth file not found in the dataset repository.", load_leaderboard()
460
- # except Exception as e:
461
- # return f"Error loading ground truth: {e}", load_leaderboard()
462
-
463
- # if not prediction_file:
464
- # return "Prediction file not uploaded.", load_leaderboard()
465
-
466
- # try:
467
- # #load prediction file
468
- # predictions_df = pd.read_csv(prediction_file.name)
469
- # # Validate required columns in prediction file
470
- # required_columns = ['question_id', 'predicted_answer']
471
- # missing_columns = [col for col in required_columns if col not in predictions_df.columns]
472
- # if missing_columns:
473
- # return (f"Error: Missing required columns in prediction file: {', '.join(missing_columns)}.",
474
- # load_leaderboard())
475
-
476
- # # Validate 'Answer' column in ground truth file
477
- # if 'Answer' not in ground_truth_df.columns:
478
- # return "Error: 'Answer' column is missing in the ground truth dataset.", load_leaderboard()
479
- # merged_df = pd.merge(predictions_df, ground_truth_df, on='question_id', how='inner')
480
- # merged_df['pred_answer'] = merged_df['predicted_answer'].apply(clean_answer)
481
-
482
- # valid_predictions = merged_df.dropna(subset=['pred_answer'])
483
- # correct_predictions = (valid_predictions['pred_answer'] == valid_predictions['Answer']).sum()
484
- # total_predictions = len(merged_df)
485
-
486
- # overall_accuracy = correct_predictions / total_predictions if total_predictions > 0 else 0
487
-
488
- # results = {
489
- # 'model_name': model_name if model_name else "Unknown Model",
490
- # 'overall_accuracy': overall_accuracy,
491
- # 'correct_predictions': correct_predictions,
492
- # 'total_questions': total_predictions,
493
- # 'Team_name': Team_name if Team_name else "Unknown Team",
494
- # }
495
-
496
- # if add_to_leaderboard:
497
- # update_leaderboard(results)
498
- # return "Evaluation completed and added to leaderboard.", load_leaderboard()
499
- # else:
500
- # return "Evaluation completed but not added to leaderboard.", load_leaderboard()
501
-
502
- # except Exception as e:
503
- # return f"Error during evaluation: {str(e)}", load_leaderboard()
504
- # initialize_leaderboard_file()
505
 
506
  def evaluate_predictions(prediction_file, model_name,Team_name ,add_to_leaderboard):
507
  try:
 
20
  if not HF_TOKEN:
21
  raise ValueError("HF_TOKEN environment variable is not set or invalid.")
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+
 
 
 
 
 
 
 
 
 
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def initialize_leaderboard_file():
27
  """
28
  Ensure the leaderboard file exists and has the correct headers.
 
61
  return clean[0].upper() if clean else None
62
 
63
 
64
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  def update_leaderboard(results):
67
  """
 
148
  print(f"Error updating leaderboard file: {e}")
149
 
150
 
151
+
152
  # def load_leaderboard():
153
  # if not os.path.exists(LEADERBOARD_FILE) or os.stat(LEADERBOARD_FILE).st_size == 0:
154
  # return pd.DataFrame({
155
  # "Model Name": [],
156
  # "Overall Accuracy": [],
 
157
  # "Correct Predictions": [],
158
  # "Total Questions": [],
159
  # "Timestamp": [],
160
+ # "Team Name": [],
161
+
162
  # })
163
  # return pd.read_csv(LEADERBOARD_FILE)
164
 
 
165
  def load_leaderboard():
166
  if not os.path.exists(LEADERBOARD_FILE) or os.stat(LEADERBOARD_FILE).st_size == 0:
167
+ # Create an empty DataFrame with all expected columns
168
  return pd.DataFrame({
169
  "Model Name": [],
170
  "Overall Accuracy": [],
 
172
  "Total Questions": [],
173
  "Timestamp": [],
174
  "Team Name": [],
 
175
  })
176
+
177
+ # Read the CSV file
178
+ df = pd.read_csv(LEADERBOARD_FILE)
179
+
180
+ # Ensure all columns exist
181
+ expected_columns = [
182
+ "Model Name",
183
+ "Overall Accuracy",
184
+ "Correct Predictions",
185
+ "Total Questions",
186
+ "Timestamp",
187
+ "Team Name"
188
+ ]
189
+
190
+ # Add missing columns with default values
191
+ for col in expected_columns:
192
+ if col not in df.columns:
193
+ if col == "Timestamp":
194
+ df[col] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
195
+ elif col == "Team Name":
196
+ df[col] = "Unknown Team"
197
+ else:
198
+ df[col] = None
199
+
200
+ # Remove duplicate entries based on Model Name
201
+ df = df.drop_duplicates(subset="Model Name", keep='last')
202
+
203
+ # Reorder columns to match expected structure
204
+ df = df[expected_columns]
205
+
206
+ return df
207
 
208
  def load_leaderboard_pro():
209
  if not os.path.exists(LEADERBOARD_FILE_pro) or os.stat(LEADERBOARD_FILE_pro).st_size == 0:
 
218
  })
219
  return pd.read_csv(LEADERBOARD_FILE_pro)
220
 
221
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
 
223
  def evaluate_predictions(prediction_file, model_name,Team_name ,add_to_leaderboard):
224
  try: