Nicolas Wagner commited on
Commit
38308df
·
1 Parent(s): ebff827

add rate limit

Browse files
src/submission/submit_csv.py CHANGED
@@ -1,14 +1,14 @@
1
  import json
2
  import os
3
  import uuid
4
- from datetime import datetime, timezone
5
 
6
  from src.envs import API, SUBMISSIONS_PATH, SUBMISSIONS_REPO
7
  from src.evaluation.compute_metrics import compute_metrics
8
  from src.evaluation.load_labels import load_true_labels
9
  from src.submission.validate_csv import validate_csv
10
  from src.teams.auth import validate_token
11
- from src.teams.storage import hash_token
12
 
13
 
14
  def get_team_best_scores(team_name: str) -> dict | None:
@@ -105,6 +105,29 @@ def should_update_scores(new_scores: dict, best_scores: dict | None) -> bool:
105
  return False
106
 
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  def submit_csv(token: str, csv_content: str) -> tuple[bool, str]:
109
  team = validate_token(token)
110
  if not team:
@@ -121,8 +144,14 @@ def submit_csv(token: str, csv_content: str) -> tuple[bool, str]:
121
  if not is_valid:
122
  return False, f"CSV validation failed: {error_msg}"
123
 
 
 
 
 
124
  scores = compute_metrics(predictions_df, true_labels)
125
 
 
 
126
  best_scores = get_team_best_scores(team_name)
127
 
128
  if should_update_scores(scores, best_scores):
 
1
  import json
2
  import os
3
  import uuid
4
+ from datetime import datetime, timedelta, timezone
5
 
6
  from src.envs import API, SUBMISSIONS_PATH, SUBMISSIONS_REPO
7
  from src.evaluation.compute_metrics import compute_metrics
8
  from src.evaluation.load_labels import load_true_labels
9
  from src.submission.validate_csv import validate_csv
10
  from src.teams.auth import validate_token
11
+ from src.teams.storage import get_team_by_name, hash_token, update_last_valid_submission
12
 
13
 
14
  def get_team_best_scores(team_name: str) -> dict | None:
 
105
  return False
106
 
107
 
108
+ def check_rate_limit(team_name: str) -> tuple[bool, str]:
109
+ team_data = get_team_by_name(team_name)
110
+ if not team_data:
111
+ return True, ""
112
+
113
+ last_submission = team_data.get("last_valid_submission")
114
+ if not last_submission:
115
+ return True, ""
116
+
117
+ try:
118
+ last_time = datetime.strptime(last_submission, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc)
119
+ now = datetime.now(timezone.utc)
120
+ time_diff = now - last_time
121
+
122
+ if time_diff < timedelta(minutes=1):
123
+ remaining = 60 - time_diff.total_seconds()
124
+ return False, f"Rate limit exceeded. Please wait {int(remaining)} seconds before submitting again."
125
+
126
+ return True, ""
127
+ except Exception:
128
+ return True, ""
129
+
130
+
131
  def submit_csv(token: str, csv_content: str) -> tuple[bool, str]:
132
  team = validate_token(token)
133
  if not team:
 
144
  if not is_valid:
145
  return False, f"CSV validation failed: {error_msg}"
146
 
147
+ can_submit, rate_limit_msg = check_rate_limit(team_name)
148
+ if not can_submit:
149
+ return False, rate_limit_msg
150
+
151
  scores = compute_metrics(predictions_df, true_labels)
152
 
153
+ update_last_valid_submission(team_name)
154
+
155
  best_scores = get_team_best_scores(team_name)
156
 
157
  if should_update_scores(scores, best_scores):
src/teams/storage.py CHANGED
@@ -84,3 +84,44 @@ def get_all_teams() -> list[dict]:
84
  continue
85
 
86
  return teams
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  continue
85
 
86
  return teams
87
+
88
+
89
+ def update_last_valid_submission(team_name: str) -> None:
90
+ ensure_teams_dir()
91
+ team_file = os.path.join(TEAMS_PATH, f"{team_name}.json")
92
+
93
+ if not os.path.exists(team_file):
94
+ return
95
+
96
+ with open(team_file, "r") as f:
97
+ team_data = json.load(f)
98
+
99
+ team_data["last_valid_submission"] = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
100
+
101
+ with open(team_file, "w") as f:
102
+ json.dump(team_data, f)
103
+
104
+ try:
105
+ API.upload_file(
106
+ path_or_fileobj=team_file,
107
+ path_in_repo=f"{team_name}.json",
108
+ repo_id=TEAMS_REPO,
109
+ repo_type="dataset",
110
+ commit_message=f"Update last submission for team: {team_name}",
111
+ )
112
+ except Exception as e:
113
+ print(f"Warning: Could not upload team update to hub: {e}")
114
+
115
+
116
+ def get_team_by_name(team_name: str) -> dict | None:
117
+ ensure_teams_dir()
118
+ team_file = os.path.join(TEAMS_PATH, f"{team_name}.json")
119
+
120
+ if not os.path.exists(team_file):
121
+ return None
122
+
123
+ try:
124
+ with open(team_file, "r") as f:
125
+ return json.load(f)
126
+ except Exception:
127
+ return None