Spaces:
Sleeping
Sleeping
Commit
·
fb451d5
1
Parent(s):
69af549
adding redistribution bonus
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from pathlib import Path
|
|
| 8 |
import pandas as pd
|
| 9 |
import pytz
|
| 10 |
import streamlit as st
|
| 11 |
-
from datasets import Dataset, load_dataset
|
| 12 |
from huggingface_hub import CommitScheduler
|
| 13 |
|
| 14 |
# File paths as constants
|
|
@@ -303,7 +303,7 @@ def display_predictions():
|
|
| 303 |
def display_leaderboard():
|
| 304 |
if st.button("Show Leaderboard"):
|
| 305 |
try:
|
| 306 |
-
#
|
| 307 |
dataset = load_dataset("Jay-Rajput/DIS_IPL_Leads", split='train')
|
| 308 |
|
| 309 |
users_data = []
|
|
@@ -311,11 +311,11 @@ def display_leaderboard():
|
|
| 311 |
for user, points_dict in dataset[0].items():
|
| 312 |
points = points_dict.get("points", 0)
|
| 313 |
last_5_results = " ".join(points_dict.get("last_5_results", ["⚪"] * 5)) # Default: 5 white circles
|
| 314 |
-
|
| 315 |
users_data.append({
|
| 316 |
'User': user,
|
| 317 |
'Points': points,
|
| 318 |
-
|
| 319 |
'Last 5 Bids': last_5_results
|
| 320 |
})
|
| 321 |
else:
|
|
@@ -414,7 +414,6 @@ ADMIN_PASSPHRASE = "admin123"
|
|
| 414 |
|
| 415 |
|
| 416 |
def fetch_latest_predictions(match_id):
|
| 417 |
-
# Load the dataset. Adjust "split" to "train" or appropriate if "predictions" is a configuration.
|
| 418 |
dataset = load_dataset("Jay-Rajput/DIS_IPL_Preds", split="train")
|
| 419 |
# Convert the dataset to a pandas DataFrame
|
| 420 |
df = pd.DataFrame(dataset)
|
|
@@ -492,12 +491,10 @@ def redistribute_lost_points(match_id):
|
|
| 492 |
|
| 493 |
|
| 494 |
def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, outcome_only=False):
|
| 495 |
-
|
| 496 |
-
# Load existing match outcomes
|
| 497 |
outcomes = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split="train")
|
| 498 |
outcomes_df = pd.DataFrame(outcomes)
|
| 499 |
|
| 500 |
-
#
|
| 501 |
outcome_exists = False
|
| 502 |
for idx, outcome in outcomes_df.iterrows():
|
| 503 |
if outcome['match_id'] == match_id:
|
|
@@ -510,65 +507,191 @@ def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, ou
|
|
| 510 |
outcomes_df = pd.concat([outcomes_df, pd.DataFrame([new_outcome])], ignore_index=True)
|
| 511 |
outcomes = Dataset.from_pandas(outcomes_df)
|
| 512 |
|
| 513 |
-
if not outcome_only:
|
| 514 |
-
# Load predictions only if necessary
|
| 515 |
predictions = fetch_latest_predictions(match_id)
|
| 516 |
-
|
| 517 |
-
# Load users' data only if necessary
|
| 518 |
users = load_dataset("Jay-Rajput/DIS_IPL_Leads", split="train")
|
| 519 |
users_df = pd.DataFrame(users)
|
| 520 |
|
| 521 |
-
#
|
| 522 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
for user_name in users_df.columns:
|
| 524 |
-
|
| 525 |
-
|
|
|
|
|
|
|
|
|
|
| 526 |
prediction = predictions[predictions['user_name'] == user_name].iloc[0]
|
| 527 |
predicted_winner = prediction['predicted_winner']
|
| 528 |
predicted_motm = prediction['predicted_motm']
|
| 529 |
bid_points = prediction['bid_points']
|
| 530 |
|
| 531 |
-
# Update points based on prediction accuracy
|
| 532 |
if predicted_winner == winning_team:
|
| 533 |
user_points += 2000 + bid_points
|
| 534 |
-
result_indicator = "🟢"
|
| 535 |
if predicted_motm == man_of_the_match:
|
| 536 |
user_points += 500
|
| 537 |
else:
|
| 538 |
user_points -= 200 + bid_points
|
| 539 |
-
result_indicator = "🔴"
|
|
|
|
|
|
|
| 540 |
else:
|
| 541 |
-
# Deduct 200 points for not submitting a prediction
|
| 542 |
user_points -= 200
|
| 543 |
-
result_indicator = "⚪"
|
| 544 |
-
|
| 545 |
-
|
|
|
|
| 546 |
user_points = max(user_points, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
|
| 548 |
-
|
| 549 |
-
users_df[
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
if "last_5_results" not in users_df[
|
| 554 |
-
users_df[
|
| 555 |
-
users_df[
|
| 556 |
-
users_df[
|
| 557 |
-
|
| 558 |
-
if "last_5_results" not in users[user_name][0]:
|
| 559 |
-
users[user_name][0]["last_5_results"] = []
|
| 560 |
-
users[user_name][0]["last_5_results"].insert(0, result_indicator) # Insert at beginning
|
| 561 |
-
users[user_name][0]["last_5_results"] = users[user_name][0]["last_5_results"][:5] # Keep only last 5
|
| 562 |
|
|
|
|
| 563 |
users.to_json(USERS_JSON)
|
| 564 |
updated_dataset = Dataset.from_pandas(users_df)
|
| 565 |
updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
|
| 566 |
-
# redistribute_lost_points(match_id)
|
| 567 |
|
|
|
|
| 568 |
outcomes.to_json(OUTCOMES)
|
| 569 |
outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
|
| 570 |
|
| 571 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
# Function to fetch matches for a given date
|
| 573 |
def fetch_matches_by_date(matches, selected_date):
|
| 574 |
return [match for match in matches if datetime.strptime(match['date'], '%Y-%m-%d').date() == selected_date]
|
|
|
|
| 8 |
import pandas as pd
|
| 9 |
import pytz
|
| 10 |
import streamlit as st
|
| 11 |
+
from datasets import Dataset, load_dataset
|
| 12 |
from huggingface_hub import CommitScheduler
|
| 13 |
|
| 14 |
# File paths as constants
|
|
|
|
| 303 |
def display_leaderboard():
|
| 304 |
if st.button("Show Leaderboard"):
|
| 305 |
try:
|
| 306 |
+
# Load the 'leaders' configuration
|
| 307 |
dataset = load_dataset("Jay-Rajput/DIS_IPL_Leads", split='train')
|
| 308 |
|
| 309 |
users_data = []
|
|
|
|
| 311 |
for user, points_dict in dataset[0].items():
|
| 312 |
points = points_dict.get("points", 0)
|
| 313 |
last_5_results = " ".join(points_dict.get("last_5_results", ["⚪"] * 5)) # Default: 5 white circles
|
| 314 |
+
bonus = points_dict.get("redistributed_bonus", 0)
|
| 315 |
users_data.append({
|
| 316 |
'User': user,
|
| 317 |
'Points': points,
|
| 318 |
+
'Redistribution Bonus': bonus,
|
| 319 |
'Last 5 Bids': last_5_results
|
| 320 |
})
|
| 321 |
else:
|
|
|
|
| 414 |
|
| 415 |
|
| 416 |
def fetch_latest_predictions(match_id):
|
|
|
|
| 417 |
dataset = load_dataset("Jay-Rajput/DIS_IPL_Preds", split="train")
|
| 418 |
# Convert the dataset to a pandas DataFrame
|
| 419 |
df = pd.DataFrame(dataset)
|
|
|
|
| 491 |
|
| 492 |
|
| 493 |
def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, outcome_only=False):
|
|
|
|
|
|
|
| 494 |
outcomes = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split="train")
|
| 495 |
outcomes_df = pd.DataFrame(outcomes)
|
| 496 |
|
| 497 |
+
# Update or add match outcome
|
| 498 |
outcome_exists = False
|
| 499 |
for idx, outcome in outcomes_df.iterrows():
|
| 500 |
if outcome['match_id'] == match_id:
|
|
|
|
| 507 |
outcomes_df = pd.concat([outcomes_df, pd.DataFrame([new_outcome])], ignore_index=True)
|
| 508 |
outcomes = Dataset.from_pandas(outcomes_df)
|
| 509 |
|
| 510 |
+
if not outcome_only:
|
|
|
|
| 511 |
predictions = fetch_latest_predictions(match_id)
|
|
|
|
|
|
|
| 512 |
users = load_dataset("Jay-Rajput/DIS_IPL_Leads", split="train")
|
| 513 |
users_df = pd.DataFrame(users)
|
| 514 |
|
| 515 |
+
# Capture previous leaderboard (top 5 users and their points)
|
| 516 |
+
prev_scores = [(user, users_df[user][0]['points']) for user in users_df.columns]
|
| 517 |
+
prev_scores.sort(key=lambda x: x[1], reverse=True)
|
| 518 |
+
prev_top_5 = prev_scores[:5]
|
| 519 |
+
top5_usernames = [user for user, _ in prev_top_5]
|
| 520 |
+
|
| 521 |
+
lost_points_by_top5 = 0
|
| 522 |
+
user_outcomes = {}
|
| 523 |
+
|
| 524 |
+
# Step 1: Apply current match outcomes
|
| 525 |
for user_name in users_df.columns:
|
| 526 |
+
user_data = users_df[user_name][0]
|
| 527 |
+
user_points = user_data['points']
|
| 528 |
+
user_initial_points = user_points
|
| 529 |
+
|
| 530 |
+
if user_name in set(predictions['user_name']):
|
| 531 |
prediction = predictions[predictions['user_name'] == user_name].iloc[0]
|
| 532 |
predicted_winner = prediction['predicted_winner']
|
| 533 |
predicted_motm = prediction['predicted_motm']
|
| 534 |
bid_points = prediction['bid_points']
|
| 535 |
|
|
|
|
| 536 |
if predicted_winner == winning_team:
|
| 537 |
user_points += 2000 + bid_points
|
| 538 |
+
result_indicator = "🟢"
|
| 539 |
if predicted_motm == man_of_the_match:
|
| 540 |
user_points += 500
|
| 541 |
else:
|
| 542 |
user_points -= 200 + bid_points
|
| 543 |
+
result_indicator = "🔴"
|
| 544 |
+
if user_name in top5_usernames:
|
| 545 |
+
lost_points_by_top5 += (200 + bid_points)
|
| 546 |
else:
|
|
|
|
| 547 |
user_points -= 200
|
| 548 |
+
result_indicator = "⚪"
|
| 549 |
+
if user_name in top5_usernames:
|
| 550 |
+
lost_points_by_top5 += 200
|
| 551 |
+
|
| 552 |
user_points = max(user_points, 0)
|
| 553 |
+
user_outcomes[user_name] = {
|
| 554 |
+
"updated_points": user_points,
|
| 555 |
+
"result_indicator": result_indicator,
|
| 556 |
+
"initial_points": user_initial_points
|
| 557 |
+
}
|
| 558 |
+
|
| 559 |
+
# Step 2: Build new leaderboard after applying outcome
|
| 560 |
+
new_leaderboard = [(u, d["updated_points"]) for u, d in user_outcomes.items()]
|
| 561 |
+
new_leaderboard.sort(key=lambda x: x[1], reverse=True)
|
| 562 |
+
new_ranks = {user: rank for rank, (user, _) in enumerate(new_leaderboard)}
|
| 563 |
+
|
| 564 |
+
# Step 3: Try redistributing lost points to remaining users
|
| 565 |
+
remaining_users = [u for u in users_df.columns if u not in top5_usernames]
|
| 566 |
+
redistribution_possible = True
|
| 567 |
+
redistributed_outcomes = {}
|
| 568 |
+
|
| 569 |
+
if remaining_users and lost_points_by_top5 > 0:
|
| 570 |
+
share = lost_points_by_top5 // len(remaining_users)
|
| 571 |
+
|
| 572 |
+
# Simulate redistribution
|
| 573 |
+
for user in user_outcomes:
|
| 574 |
+
redistributed_outcomes[user] = user_outcomes[user]["updated_points"]
|
| 575 |
+
if user in remaining_users:
|
| 576 |
+
redistributed_outcomes[user] += share
|
| 577 |
+
|
| 578 |
+
# Check if new ranks remain same
|
| 579 |
+
redistributed_leaderboard = [(u, p) for u, p in redistributed_outcomes.items()]
|
| 580 |
+
redistributed_leaderboard.sort(key=lambda x: x[1], reverse=True)
|
| 581 |
+
redistributed_ranks = {user: rank for rank, (user, _) in enumerate(redistributed_leaderboard)}
|
| 582 |
+
|
| 583 |
+
for user in user_outcomes:
|
| 584 |
+
if new_ranks[user] != redistributed_ranks[user]:
|
| 585 |
+
redistribution_possible = False
|
| 586 |
+
break
|
| 587 |
+
|
| 588 |
+
# Step 4: Apply the appropriate update
|
| 589 |
+
for user in users_df.columns:
|
| 590 |
+
if redistribution_possible and user in redistributed_outcomes:
|
| 591 |
+
final_points = redistributed_outcomes[user]
|
| 592 |
+
bonus = final_points - user_outcomes[user]["updated_points"]
|
| 593 |
+
else:
|
| 594 |
+
final_points = user_outcomes[user]["updated_points"]
|
| 595 |
+
bonus = 0 # No redistribution received
|
| 596 |
|
| 597 |
+
users_df[user][0]['points'] = final_points
|
| 598 |
+
users_df[user][0]['redistributed_bonus'] = bonus # Track redistributed bonus
|
| 599 |
+
|
| 600 |
+
# Maintain last 5 results
|
| 601 |
+
result = user_outcomes[user]["result_indicator"]
|
| 602 |
+
if "last_5_results" not in users_df[user][0]:
|
| 603 |
+
users_df[user][0]["last_5_results"] = []
|
| 604 |
+
users_df[user][0]["last_5_results"].insert(0, result)
|
| 605 |
+
users_df[user][0]["last_5_results"] = users_df[user][0]["last_5_results"][:5]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 606 |
|
| 607 |
+
# Save updated leaderboard
|
| 608 |
users.to_json(USERS_JSON)
|
| 609 |
updated_dataset = Dataset.from_pandas(users_df)
|
| 610 |
updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
|
|
|
|
| 611 |
|
| 612 |
+
# Save match outcome
|
| 613 |
outcomes.to_json(OUTCOMES)
|
| 614 |
outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
|
| 615 |
|
| 616 |
|
| 617 |
+
# def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, outcome_only=False):
|
| 618 |
+
|
| 619 |
+
# # Load existing match outcomes
|
| 620 |
+
# outcomes = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split="train")
|
| 621 |
+
# outcomes_df = pd.DataFrame(outcomes)
|
| 622 |
+
|
| 623 |
+
# # Directly update or add the match outcome
|
| 624 |
+
# outcome_exists = False
|
| 625 |
+
# for idx, outcome in outcomes_df.iterrows():
|
| 626 |
+
# if outcome['match_id'] == match_id:
|
| 627 |
+
# outcomes_df.at[idx, 'winning_team'] = winning_team
|
| 628 |
+
# outcomes_df.at[idx, 'man_of_the_match'] = man_of_the_match
|
| 629 |
+
# outcome_exists = True
|
| 630 |
+
# break
|
| 631 |
+
# if not outcome_exists:
|
| 632 |
+
# new_outcome = {"match_id": match_id, "winning_team": winning_team, "man_of_the_match": man_of_the_match}
|
| 633 |
+
# outcomes_df = pd.concat([outcomes_df, pd.DataFrame([new_outcome])], ignore_index=True)
|
| 634 |
+
# outcomes = Dataset.from_pandas(outcomes_df)
|
| 635 |
+
|
| 636 |
+
# if not outcome_only: # Update user scores only if outcome_only is False
|
| 637 |
+
# # Load predictions only if necessary
|
| 638 |
+
# predictions = fetch_latest_predictions(match_id)
|
| 639 |
+
|
| 640 |
+
# # Load users' data only if necessary
|
| 641 |
+
# users = load_dataset("Jay-Rajput/DIS_IPL_Leads", split="train")
|
| 642 |
+
# users_df = pd.DataFrame(users)
|
| 643 |
+
|
| 644 |
+
# # Update user points based on prediction accuracy
|
| 645 |
+
# users_with_predictions = set(predictions['user_name'])
|
| 646 |
+
# for user_name in users_df.columns:
|
| 647 |
+
# user_points = users_df[user_name][0]['points']
|
| 648 |
+
# if user_name in users_with_predictions:
|
| 649 |
+
# prediction = predictions[predictions['user_name'] == user_name].iloc[0]
|
| 650 |
+
# predicted_winner = prediction['predicted_winner']
|
| 651 |
+
# predicted_motm = prediction['predicted_motm']
|
| 652 |
+
# bid_points = prediction['bid_points']
|
| 653 |
+
|
| 654 |
+
# # Update points based on prediction accuracy
|
| 655 |
+
# if predicted_winner == winning_team:
|
| 656 |
+
# user_points += 2000 + bid_points
|
| 657 |
+
# result_indicator = "🟢" # Correct Prediction
|
| 658 |
+
# if predicted_motm == man_of_the_match:
|
| 659 |
+
# user_points += 500
|
| 660 |
+
# else:
|
| 661 |
+
# user_points -= 200 + bid_points
|
| 662 |
+
# result_indicator = "🔴" # Wrong Prediction
|
| 663 |
+
# else:
|
| 664 |
+
# # Deduct 200 points for not submitting a prediction
|
| 665 |
+
# user_points -= 200
|
| 666 |
+
# result_indicator = "⚪" # No Prediction
|
| 667 |
+
|
| 668 |
+
# # Ensure user_points is never negative
|
| 669 |
+
# user_points = max(user_points, 0)
|
| 670 |
+
|
| 671 |
+
# # Update user's points in the DataFrame
|
| 672 |
+
# users_df[user_name][0]['points'] = user_points
|
| 673 |
+
# users[user_name][0]['points'] = user_points
|
| 674 |
+
|
| 675 |
+
# # Maintain last 5 prediction results
|
| 676 |
+
# if "last_5_results" not in users_df[user_name][0]:
|
| 677 |
+
# users_df[user_name][0]["last_5_results"] = []
|
| 678 |
+
# users_df[user_name][0]["last_5_results"].insert(0, result_indicator) # Insert at beginning
|
| 679 |
+
# users_df[user_name][0]["last_5_results"] = users_df[user_name][0]["last_5_results"][:5] # Keep only last 5
|
| 680 |
+
|
| 681 |
+
# if "last_5_results" not in users[user_name][0]:
|
| 682 |
+
# users[user_name][0]["last_5_results"] = []
|
| 683 |
+
# users[user_name][0]["last_5_results"].insert(0, result_indicator) # Insert at beginning
|
| 684 |
+
# users[user_name][0]["last_5_results"] = users[user_name][0]["last_5_results"][:5] # Keep only last 5
|
| 685 |
+
|
| 686 |
+
# users.to_json(USERS_JSON)
|
| 687 |
+
# updated_dataset = Dataset.from_pandas(users_df)
|
| 688 |
+
# updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
|
| 689 |
+
# # redistribute_lost_points(match_id)
|
| 690 |
+
|
| 691 |
+
# outcomes.to_json(OUTCOMES)
|
| 692 |
+
# outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
|
| 693 |
+
|
| 694 |
+
|
| 695 |
# Function to fetch matches for a given date
|
| 696 |
def fetch_matches_by_date(matches, selected_date):
|
| 697 |
return [match for match in matches if datetime.strptime(match['date'], '%Y-%m-%d').date() == selected_date]
|