Jay-Rajput commited on
Commit
d6f23b0
·
1 Parent(s): 5825c65

enhanced admin panel

Browse files
Files changed (1) hide show
  1. app.py +63 -30
app.py CHANGED
@@ -13,7 +13,6 @@ from huggingface_hub import CommitScheduler, HfApi
13
 
14
  # File paths as constants
15
  PREDICTIONS_CSV = 'dis_predictions.csv'
16
- PREDICTIONS_FOLDER = "ipl_predictions" # Folder where JSON files are saved locally
17
  USERS_JSON = 'leaders/users.json'
18
  MATCHES_JSON = 'matches.json'
19
  OUTCOMES_JSON = 'match_outcomes.json'
@@ -21,13 +20,10 @@ PLAYERS_JSON = 'players.json'
21
  image_path = 'ipl_image.png'
22
 
23
 
24
- prediction_id = uuid.uuid4().hex
25
- # Save prediction to a local JSON file for CommitScheduler to handle
26
- prediction_file = Path("ipl_predictions/") / f"prediction_{prediction_id}.json"
27
- PREDICTIONS_FOLDER = prediction_file.parent
28
  PREDICTIONS_FOLDER.mkdir(parents=True, exist_ok=True)
29
 
30
- users_file = Path("leaders/") / f"users.json"
31
  USERS_FOLDER = users_file.parent
32
  USERS_FOLDER.mkdir(parents=True, exist_ok=True)
33
 
@@ -36,7 +32,7 @@ scheduler = CommitScheduler(
36
  repo_id="DIS_IPL_Dataset",
37
  repo_type="dataset",
38
  folder_path=PREDICTIONS_FOLDER, # Local folder where predictions are saved temporarily
39
- path_in_repo="predictions", # Path in dataset repo where predictions will be saved
40
  every=2, # Push every 240 minutes (4 hours)
41
  )
42
 
@@ -44,7 +40,7 @@ scheduler = CommitScheduler(
44
  scheduler = CommitScheduler(
45
  repo_id="DIS_IPL_Dataset",
46
  repo_type="dataset",
47
- folder_path=USERS_FOLDER, # Local folder where predictions are saved temporarily
48
  path_in_repo="leaders", # Path in dataset repo where predictions will be saved
49
  every=2, # Push every 240 minutes (4 hours)
50
  )
@@ -157,6 +153,7 @@ def submit_prediction(
157
  st.error(f"Your bid points exceed the 20% limit of your total points. Maximum allowed bid points: {max_bid_points}")
158
  return
159
 
 
160
  prediction_date = datetime.now().strftime('%Y-%m-%d')
161
 
162
  prediction_data = {
@@ -168,6 +165,10 @@ def submit_prediction(
168
  'bid_points': bid_points,
169
  'prediction_date': prediction_date # Include the prediction date
170
  }
 
 
 
 
171
 
172
  with scheduler.lock:
173
  with prediction_file.open("a") as file:
@@ -346,9 +347,25 @@ with st.expander("Leaderboard 🏆"):
346
  ############################# Admin Panel ##################################
347
  ADMIN_PASSPHRASE = "admin123"
348
 
349
- def save_users(users):
350
- with open(USERS_JSON, 'w') as file:
351
- json.dump(users, file, indent=4)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352
 
353
 
354
  def save_match_outcomes(outcomes):
@@ -357,9 +374,13 @@ def save_match_outcomes(outcomes):
357
 
358
 
359
  def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match):
 
 
 
360
  outcomes = load_data(OUTCOMES_JSON) # Load existing match outcomes
361
- predictions = load_predictions(PREDICTIONS_CSV) # Load existing predictions
362
- users = load_users(USERS_JSON) # Load existing user points
 
363
 
364
  # Directly update or add the match outcome
365
  outcome_exists = False
@@ -372,22 +393,28 @@ def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match):
372
  outcomes.append({"match_id": match_id, "winning_team": winning_team, "man_of_the_match": man_of_the_match})
373
 
374
  # Update user points based on prediction accuracy
375
- for _, prediction in predictions.iterrows():
376
- if prediction['match_id'] == match_id:
377
- user_name = prediction['user_name']
378
- users[user_name] = users.get(user_name, 0) # Initialize user points if not present
379
-
380
- # Update points based on prediction accuracy
381
- if prediction['predicted_winner'] == winning_team:
382
- users[user_name] += 1000
383
- users[user_name] += prediction['bid_points']
384
- if prediction['predicted_motm'] == man_of_the_match:
385
- users[user_name] += 400 # Bonus for both correct predictions
386
- else:
387
- users[user_name] -= 200 + prediction['bid_points'] # Penalty for wrong team prediction
388
 
389
  save_match_outcomes(outcomes)
390
- save_users(users)
 
 
 
 
 
 
 
391
 
392
 
393
  with st.sidebar:
@@ -396,11 +423,17 @@ with st.sidebar:
396
 
397
  if admin_pass == ADMIN_PASSPHRASE:
398
  expander.success("Authenticated")
399
- matches = get_today_matches() # This function fetches today's matches
 
 
 
400
 
 
 
 
401
  # If matches are available, let the admin select one
402
- if matches:
403
- match_selection = expander.selectbox("Select Match", matches, format_func=lambda match: f"{match['teams'][0]} vs {match['teams'][1]}", key="match_selection")
404
  selected_match_id = match_selection['match_id']
405
  teams = match_selection['teams']
406
 
 
13
 
14
  # File paths as constants
15
  PREDICTIONS_CSV = 'dis_predictions.csv'
 
16
  USERS_JSON = 'leaders/users.json'
17
  MATCHES_JSON = 'matches.json'
18
  OUTCOMES_JSON = 'match_outcomes.json'
 
20
  image_path = 'ipl_image.png'
21
 
22
 
23
+ PREDICTIONS_FOLDER = Path("ipl_predictions")
 
 
 
24
  PREDICTIONS_FOLDER.mkdir(parents=True, exist_ok=True)
25
 
26
+ users_file = Path("leaders") / f"users.json"
27
  USERS_FOLDER = users_file.parent
28
  USERS_FOLDER.mkdir(parents=True, exist_ok=True)
29
 
 
32
  repo_id="DIS_IPL_Dataset",
33
  repo_type="dataset",
34
  folder_path=PREDICTIONS_FOLDER, # Local folder where predictions are saved temporarily
35
+ path_in_repo="ipl_predictions", # Path in dataset repo where predictions will be saved
36
  every=2, # Push every 240 minutes (4 hours)
37
  )
38
 
 
40
  scheduler = CommitScheduler(
41
  repo_id="DIS_IPL_Dataset",
42
  repo_type="dataset",
43
+ folder_path=USERS_FOLDER, # Local folder where users are saved temporarily
44
  path_in_repo="leaders", # Path in dataset repo where predictions will be saved
45
  every=2, # Push every 240 minutes (4 hours)
46
  )
 
153
  st.error(f"Your bid points exceed the 20% limit of your total points. Maximum allowed bid points: {max_bid_points}")
154
  return
155
 
156
+ prediction_id = uuid.uuid4().hex
157
  prediction_date = datetime.now().strftime('%Y-%m-%d')
158
 
159
  prediction_data = {
 
165
  'bid_points': bid_points,
166
  'prediction_date': prediction_date # Include the prediction date
167
  }
168
+
169
+ # Construct the filename to include match_id for easier retrieval
170
+ prediction_file_name = f"prediction_{match_id}_{prediction_id}.json"
171
+ prediction_file = PREDICTIONS_FOLDER / prediction_file_name
172
 
173
  with scheduler.lock:
174
  with prediction_file.open("a") as file:
 
347
  ############################# Admin Panel ##################################
348
  ADMIN_PASSPHRASE = "admin123"
349
 
350
+ from huggingface_hub import Repository
351
+
352
+ # Define the local path to clone the repository
353
+ local_repo_path = "DIS_IPL_Dataset"
354
+ # Define your dataset repository name on Hugging Face Hub
355
+ repo_id = "Jay-Rajput/DIS_IPL_Dataset"
356
+ # Clone the repository (this only needs to be done once)
357
+ repo = Repository(local_dir=local_repo_path, clone_from=repo_id)
358
+
359
+
360
+ def fetch_latest_predictions(match_id):
361
+ # Assuming predictions are stored in the "ipl_predictions" directory in your dataset repo
362
+ predictions_path = Path("ipl_predictions")
363
+ latest_predictions = []
364
+ for prediction_file in predictions_path.glob(f"prediction_{match_id}_*.json"):
365
+ with open(prediction_file, 'r') as file:
366
+ prediction_data = json.load(file)
367
+ latest_predictions.append(prediction_data)
368
+ return latest_predictions
369
 
370
 
371
  def save_match_outcomes(outcomes):
 
374
 
375
 
376
  def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match):
377
+ # Fetch latest predictions from the dataset repo
378
+ predictions = fetch_latest_predictions(match_id)
379
+
380
  outcomes = load_data(OUTCOMES_JSON) # Load existing match outcomes
381
+ users_file_path = repo.git_pull("leaders/users.json")
382
+ with open(users_file_path, 'r') as file:
383
+ users = json.load(file)
384
 
385
  # Directly update or add the match outcome
386
  outcome_exists = False
 
393
  outcomes.append({"match_id": match_id, "winning_team": winning_team, "man_of_the_match": man_of_the_match})
394
 
395
  # Update user points based on prediction accuracy
396
+ for prediction in predictions:
397
+ user_name = prediction['user_name']
398
+ users[user_name] = users.get(user_name, 0) # Initialize user points if not present
399
+
400
+ # Update points based on prediction accuracy
401
+ if prediction['predicted_winner'] == winning_team:
402
+ users[user_name] += 1000
403
+ users[user_name] += prediction['bid_points']
404
+ if prediction['predicted_motm'] == man_of_the_match:
405
+ users[user_name] += 400 # Bonus for both correct predictions
406
+ else:
407
+ users[user_name] -= 200 + prediction['bid_points'] # Penalty for wrong team prediction
 
408
 
409
  save_match_outcomes(outcomes)
410
+ # Save updated users data back to the dataset repo
411
+ with open(users_file_path, 'w') as file:
412
+ json.dump(users, file)
413
+
414
+ # Commit changes to the dataset repo
415
+ repo.git_add(users_file_path)
416
+ repo.git_commit("Update leaderboard")
417
+ repo.git_push()
418
 
419
 
420
  with st.sidebar:
 
423
 
424
  if admin_pass == ADMIN_PASSPHRASE:
425
  expander.success("Authenticated")
426
+
427
+ all_matches = load_data(MATCHES_JSON)
428
+ match_outcomes = load_data(OUTCOMES_JSON)
429
+ submitted_match_ids = [outcome["match_id"] for outcome in match_outcomes]
430
 
431
+ # Filter matches to those that do not have outcomes submitted yet
432
+ matches_without_outcomes = [match for match in all_matches if match["match_id"] not in submitted_match_ids]
433
+
434
  # If matches are available, let the admin select one
435
+ if matches_without_outcomes:
436
+ match_selection = expander.selectbox("Select Match", matches_without_outcomes, format_func=lambda match: f"{match['teams'][0]} vs {match['teams'][1]}", key="match_selection")
437
  selected_match_id = match_selection['match_id']
438
  teams = match_selection['teams']
439