Spaces:
Sleeping
Sleeping
Commit
·
c7ca941
1
Parent(s):
39a530e
new changes
Browse files
app.py
CHANGED
|
@@ -212,10 +212,16 @@ def calculate_max_bid_points(user_name):
|
|
| 212 |
|
| 213 |
|
| 214 |
def load_users(USERS_JSON):
|
|
|
|
| 215 |
try:
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
return {}
|
| 220 |
|
| 221 |
|
|
@@ -290,21 +296,22 @@ def display_predictions():
|
|
| 290 |
def display_leaderboard():
|
| 291 |
if st.button("Show Leaderboard"):
|
| 292 |
try:
|
| 293 |
-
#
|
| 294 |
-
dataset = load_dataset("Jay-Rajput/DIS_IPL_Leads", split='train')
|
| 295 |
-
|
| 296 |
users_data = []
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
leaderboard = pd.DataFrame(users_data)
|
| 310 |
|
|
@@ -318,8 +325,9 @@ def display_leaderboard():
|
|
| 318 |
leaderboard = leaderboard[['Rank', 'User', 'Points', 'Last 5 Bids']]
|
| 319 |
|
| 320 |
st.dataframe(leaderboard, hide_index=True)
|
|
|
|
| 321 |
except Exception as e:
|
| 322 |
-
st.
|
| 323 |
|
| 324 |
|
| 325 |
# Streamlit UI
|
|
@@ -414,86 +422,108 @@ def fetch_latest_predictions(match_id):
|
|
| 414 |
return pd.DataFrame()
|
| 415 |
|
| 416 |
|
| 417 |
-
def
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
new_outcome = {"match_id": match_id, "winning_team": winning_team, "man_of_the_match": man_of_the_match}
|
| 438 |
-
outcomes_df = pd.concat([outcomes_df, pd.DataFrame([new_outcome])], ignore_index=True)
|
| 439 |
-
outcomes = Dataset.from_pandas(outcomes_df)
|
| 440 |
|
| 441 |
-
if not outcome_only: # Update user scores only if outcome_only is False
|
| 442 |
-
# Load predictions only if necessary
|
| 443 |
-
predictions = fetch_latest_predictions(match_id)
|
| 444 |
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
else:
|
| 466 |
-
|
| 467 |
-
result_indicator = "
|
| 468 |
-
else:
|
| 469 |
-
# Deduct 1000 points for not submitting a prediction
|
| 470 |
-
user_points -= 1000
|
| 471 |
-
result_indicator = "⚪" # No Prediction
|
| 472 |
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
users_df[user_name][0]["last_5_results"] = users_df[user_name][0]["last_5_results"][:5] # Keep only last 5
|
| 485 |
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
users[user_name][0]["last_5_results"] = users[user_name][0]["last_5_results"][:5] # Keep only last 5
|
| 490 |
-
|
| 491 |
-
users.to_json(USERS_JSON)
|
| 492 |
-
updated_dataset = Dataset.from_pandas(users_df)
|
| 493 |
-
updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
|
| 494 |
|
| 495 |
-
|
| 496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
|
| 498 |
|
| 499 |
# Function to fetch matches for a given date
|
|
@@ -553,6 +583,7 @@ with st.sidebar:
|
|
| 553 |
# Submit match outcome and update user scores
|
| 554 |
update_leaderboard_and_outcomes(selected_match_id, winning_team, man_of_the_match)
|
| 555 |
expander.success("Match outcome submitted and leaderboard updated!")
|
|
|
|
| 556 |
else:
|
| 557 |
expander.write("No matches available for the selected date.")
|
| 558 |
else:
|
|
|
|
| 212 |
|
| 213 |
|
| 214 |
def load_users(USERS_JSON):
|
| 215 |
+
"""Load users data with fallback to Hugging Face"""
|
| 216 |
try:
|
| 217 |
+
# First try local file
|
| 218 |
+
if os.path.exists(USERS_JSON):
|
| 219 |
+
with open(USERS_JSON, 'r', encoding='utf-8') as file:
|
| 220 |
+
return json.load(file)
|
| 221 |
+
# If not found, load from Hugging Face
|
| 222 |
+
return load_and_process_users()
|
| 223 |
+
except Exception as e:
|
| 224 |
+
st.error(f"Error loading users data: {e}")
|
| 225 |
return {}
|
| 226 |
|
| 227 |
|
|
|
|
| 296 |
def display_leaderboard():
|
| 297 |
if st.button("Show Leaderboard"):
|
| 298 |
try:
|
| 299 |
+
# Load users data using our centralized function
|
|
|
|
|
|
|
| 300 |
users_data = []
|
| 301 |
+
data = load_users(USERS_JSON)
|
| 302 |
+
|
| 303 |
+
for user, user_info in data.items():
|
| 304 |
+
points = user_info.get("points", 0)
|
| 305 |
+
last_5_results = " ".join(user_info.get("last_5_results", ["⚪"] * 5))
|
| 306 |
+
users_data.append({
|
| 307 |
+
'User': user,
|
| 308 |
+
'Points': points,
|
| 309 |
+
"Last 5 Bids": last_5_results
|
| 310 |
+
})
|
| 311 |
+
|
| 312 |
+
if not users_data:
|
| 313 |
+
st.warning("No user data found!")
|
| 314 |
+
return
|
| 315 |
|
| 316 |
leaderboard = pd.DataFrame(users_data)
|
| 317 |
|
|
|
|
| 325 |
leaderboard = leaderboard[['Rank', 'User', 'Points', 'Last 5 Bids']]
|
| 326 |
|
| 327 |
st.dataframe(leaderboard, hide_index=True)
|
| 328 |
+
|
| 329 |
except Exception as e:
|
| 330 |
+
st.error(f"Failed to load leaderboard data: {str(e)}")
|
| 331 |
|
| 332 |
|
| 333 |
# Streamlit UI
|
|
|
|
| 422 |
return pd.DataFrame()
|
| 423 |
|
| 424 |
|
| 425 |
+
def load_and_process_users():
|
| 426 |
+
"""Load and process users data from Hugging Face"""
|
| 427 |
+
try:
|
| 428 |
+
# Load dataset from Hugging Face
|
| 429 |
+
users = load_dataset("Jay-Rajput/DIS_IPL_Leads", split="train")
|
| 430 |
+
users_df = users.to_dict()
|
| 431 |
+
|
| 432 |
+
# Process into the correct format
|
| 433 |
+
users_dict = {}
|
| 434 |
+
for user_name, user_data in users_df.items():
|
| 435 |
+
users_dict[user_name] = user_data[0] # Extract the first (and only) item
|
| 436 |
+
|
| 437 |
+
# Save to local JSON file for faster access
|
| 438 |
+
with open(USERS_JSON, 'w', encoding='utf-8') as f:
|
| 439 |
+
json.dump(users_dict, f, ensure_ascii=False, indent=4)
|
| 440 |
+
|
| 441 |
+
return users_dict
|
| 442 |
+
except Exception as e:
|
| 443 |
+
st.error(f"Error loading users data: {e}")
|
| 444 |
+
return {}
|
|
|
|
|
|
|
|
|
|
| 445 |
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
+
def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, outcome_only=False):
|
| 448 |
+
try:
|
| 449 |
+
# First load and process the latest users data
|
| 450 |
+
users_dict = load_and_process_users()
|
| 451 |
+
|
| 452 |
+
# Rest of your existing code for updating outcomes...
|
| 453 |
+
features = Features({
|
| 454 |
+
'match_id': Value('string'),
|
| 455 |
+
'man_of_the_match': Value('string'),
|
| 456 |
+
'winning_team': Value('string'),
|
| 457 |
+
})
|
| 458 |
+
|
| 459 |
+
# Load existing match outcomes
|
| 460 |
+
outcomes = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split="train")
|
| 461 |
+
outcomes_df = pd.DataFrame(outcomes)
|
| 462 |
+
|
| 463 |
+
# Update or add the match outcome
|
| 464 |
+
outcome_exists = False
|
| 465 |
+
for idx, outcome in outcomes_df.iterrows():
|
| 466 |
+
if outcome['match_id'] == match_id:
|
| 467 |
+
outcomes_df.at[idx, 'winning_team'] = winning_team
|
| 468 |
+
outcomes_df.at[idx, 'man_of_the_match'] = man_of_the_match
|
| 469 |
+
outcome_exists = True
|
| 470 |
+
break
|
| 471 |
+
|
| 472 |
+
if not outcome_exists:
|
| 473 |
+
new_outcome = {"match_id": match_id, "winning_team": winning_team, "man_of_the_match": man_of_the_match}
|
| 474 |
+
outcomes_df = pd.concat([outcomes_df, pd.DataFrame([new_outcome])], ignore_index=True)
|
| 475 |
+
outcomes = Dataset.from_pandas(outcomes_df)
|
| 476 |
+
|
| 477 |
+
if not outcome_only:
|
| 478 |
+
# Load predictions
|
| 479 |
+
predictions = fetch_latest_predictions(match_id)
|
| 480 |
+
|
| 481 |
+
# Update user points based on prediction accuracy
|
| 482 |
+
users_with_predictions = set(predictions['user_name'])
|
| 483 |
+
for user_name, user_data in users_dict.items():
|
| 484 |
+
if user_name in users_with_predictions:
|
| 485 |
+
prediction = predictions[predictions['user_name'] == user_name].iloc[0]
|
| 486 |
+
predicted_winner = prediction['predicted_winner']
|
| 487 |
+
predicted_motm = prediction['predicted_motm']
|
| 488 |
+
bid_points = prediction['bid_points']
|
| 489 |
+
|
| 490 |
+
# Update points based on prediction accuracy
|
| 491 |
+
if predicted_winner == winning_team:
|
| 492 |
+
user_data['points'] += 2000 + bid_points
|
| 493 |
+
result_indicator = "🟢"
|
| 494 |
+
if predicted_motm == man_of_the_match:
|
| 495 |
+
user_data['points'] += 500
|
| 496 |
+
else:
|
| 497 |
+
user_data['points'] -= 200 + bid_points
|
| 498 |
+
result_indicator = "🔴"
|
| 499 |
else:
|
| 500 |
+
user_data['points'] -= 1000
|
| 501 |
+
result_indicator = "⚪"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
|
| 503 |
+
user_data['points'] = max(user_data['points'], 0)
|
| 504 |
+
|
| 505 |
+
# Update last 5 results
|
| 506 |
+
if "last_5_results" not in user_data:
|
| 507 |
+
user_data["last_5_results"] = []
|
| 508 |
+
user_data["last_5_results"].insert(0, result_indicator)
|
| 509 |
+
user_data["last_5_results"] = user_data["last_5_results"][:5]
|
| 510 |
+
|
| 511 |
+
# Save updated users data back to Hugging Face
|
| 512 |
+
users_dataset = Dataset.from_dict({k: [v] for k, v in users_dict.items()})
|
| 513 |
+
users_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads")
|
|
|
|
| 514 |
|
| 515 |
+
# Also save locally
|
| 516 |
+
with open(USERS_JSON, 'w', encoding='utf-8') as f:
|
| 517 |
+
json.dump(users_dict, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 518 |
|
| 519 |
+
# Save outcomes
|
| 520 |
+
outcomes.to_json(OUTCOMES)
|
| 521 |
+
outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
|
| 522 |
+
|
| 523 |
+
return True
|
| 524 |
+
except Exception as e:
|
| 525 |
+
st.error(f"Error updating leaderboard: {e}")
|
| 526 |
+
return False
|
| 527 |
|
| 528 |
|
| 529 |
# Function to fetch matches for a given date
|
|
|
|
| 583 |
# Submit match outcome and update user scores
|
| 584 |
update_leaderboard_and_outcomes(selected_match_id, winning_team, man_of_the_match)
|
| 585 |
expander.success("Match outcome submitted and leaderboard updated!")
|
| 586 |
+
st.experimental_rerun()
|
| 587 |
else:
|
| 588 |
expander.write("No matches available for the selected date.")
|
| 589 |
else:
|