Spaces:
Sleeping
Sleeping
jarajpu commited on
Commit ·
d89cb7d
1
Parent(s): abc1d97
DIS IPL App - Base
Browse filesFiles changed (6)
- app.pyAdded+246
- match_outcomes.jsonAdded
- matches.jsonAdded+156
- predictions.csvAdded+2
- requirements.txtAdded+3
- users.jsonAdded+17
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246import jsonfrom datetime import datetime
import pandas as pdimport pytzimport streamlit as st
# File pathspredictions_csv = 'predictions.csv'users_json = 'users.json'matches_json = 'matches.json'outcomes_json = 'match_outcomes.json'
# Initialize CSV and JSON files if they don't existdef initialize_files(): # Initialize predictions CSV try: pd.read_csv(predictions_csv) except FileNotFoundError: df = pd.DataFrame(columns=['user_name', 'match_id', 'predicted_winner', 'predicted_motm', 'bid_points']) df.to_csv(predictions_csv, index=False)
# Load users from JSONdef get_users(): try: with open(users_json, 'r') as file: users = json.load(file) return list(users.keys()) except FileNotFoundError: return []
# Load matches from JSONdef load_matches(): try: with open(matches_json, 'r') as f: return json.load(f) except FileNotFoundError: return []
def load_match_outcomes(): try: with open(outcomes_json, 'r') as file: return json.load(file) except FileNotFoundError: return []
# Get today's date in IST to load today's matchdef get_current_date_ist(): tz_IST = pytz.timezone('Asia/Kolkata') datetime_ist = datetime.now(tz_IST) return datetime_ist.strftime('%Y-%m-%d')
# Function to get matches for todaydef get_today_matches(): today = get_current_date_ist() matches = load_matches() today_matches = [match for match in matches if match['date'] == today] return today_matches
# Submit prediction functiondef submit_prediction(user_name, match_id, predicted_winner, predicted_motm, bid_points): # Ensure predictions DataFrame is loaded or initialized correctly try: predictions = pd.read_csv(predictions_csv) # Check if all expected columns are present, if not, reinitialize the DataFrame expected_columns = ['user_name', 'match_id', 'predicted_winner', 'predicted_motm', 'bid_points'] if not all(column in predictions.columns for column in expected_columns): raise ValueError("CSV file missing one or more columns; Reinitializing.") except (FileNotFoundError, ValueError) as e: predictions = pd.DataFrame(columns=expected_columns)
# Check for duplicate prediction for the same match by the same user if user_name == "Select a user...": st.warning("Please select a valid user.") return else: existing_predictions = predictions[(predictions['user_name'] == user_name) & (predictions['match_id'] == match_id)] if not existing_predictions.empty: st.error("You've already submitted a prediction for this match.") return
# Append new prediction new_prediction = { 'user_name': user_name, 'match_id': match_id, 'predicted_winner': predicted_winner, 'predicted_motm': predicted_motm, 'bid_points': bid_points }
predictions = pd.concat([predictions, pd.DataFrame([new_prediction])], ignore_index=True) predictions.to_csv(predictions_csv, index=False) st.success("Prediction submitted successfully!")
# Streamlit UIst.title("DIS IPL Match Predictions")
# Prediction formwith st.expander("Submit Prediction"):
# User selection user_name = st.selectbox("Select User", ["Select a user..."] + get_users())
# Match selection matches = get_today_matches() if matches: match_choice = st.selectbox("Select Today's Match", matches, format_func=lambda match: f"{match['teams'][0]} vs {match['teams'][1]}") match_id = match_choice['match_id'] teams = match_choice['teams'] else: st.write("No matches are scheduled for today.") st.stop()
# Predictions predicted_winner = st.selectbox("Predicted Winner", teams) predicted_motm = st.text_input("Predicted Man of the Match") bid_points = st.number_input("Bid Points", min_value=1, value=100)
# Submit button if st.button("Submit Prediction"): submit_prediction(user_name, match_id, predicted_winner, predicted_motm, bid_points)
# Show predictionswith st.expander("Predictions"):
# Display predictions if st.button("Show Predictions"): try: df = pd.read_csv(predictions_csv) st.dataframe(df) except FileNotFoundError: st.write("No predictions have been submitted yet.")
# Show leaderboardwith st.expander("Leaderboard"):
# Display leaderboard if st.button("Show Leaderboard"): try: with open(users_json, 'r') as f: users = json.load(f) leaderboard = sorted(users.items(), key=lambda x: x[1], reverse=True) df_leaderboard = pd.DataFrame(leaderboard, columns=['User', 'Points'])
# Add a 'Rank' column starting from 1 df_leaderboard['Rank'] = range(1, len(df_leaderboard) + 1)
# Reorder DataFrame columns so 'Rank' is first df_leaderboard = df_leaderboard[['Rank', 'User', 'Points']]
st.dataframe(df_leaderboard) except FileNotFoundError: st.write("Leaderboard data not available.")
ADMIN_PASSPHRASE = "admin123"
def load_predictions(): # loading predictions from 'predictions.csv' try: return pd.read_csv(predictions_csv) except FileNotFoundError: return pd.DataFrame(columns=['user_name', 'match_id', 'predicted_winner', 'predicted_motm', 'bid_points'])
def load_users(): with open(users_json, 'r') as file: return json.load(file)
def save_users(users): with open(users_json, 'w') as file: json.dump(users, file, indent=4)
def save_match_outcomes(outcomes): with open(outcomes_json, 'w') as file: json.dump(outcomes, file, indent=4)
def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match): outcomes = load_match_outcomes() # Load existing match outcomes predictions = load_predictions() # Load existing predictions users = load_users() # Load existing user points
# Update match outcomes match_outcome = next((outcome for outcome in outcomes if outcome['match_id'] == match_id), None) if match_outcome: match_outcome['winning_team'] = winning_team match_outcome['man_of_the_match'] = man_of_the_match else: outcomes.append({ "match_id": match_id, "winning_team": winning_team, "man_of_the_match": man_of_the_match })
# Update user points based on prediction accuracy match_predictions = predictions[predictions['match_id'] == match_id] for _, prediction in match_predictions.iterrows(): user_name = prediction['user_name'] # Initialize user points if not present users[user_name] = users.get(user_name, 1000)
if prediction['predicted_winner'] == winning_team: users[user_name] += prediction['bid_points'] * 2 # Correct team prediction else: users[user_name] -= prediction['bid_points'] # Deduct points for incorrect prediction
if prediction['predicted_motm'] == man_of_the_match: users[user_name] += 100 # Correct man of the match prediction
# Save updated outcomes and user points save_match_outcomes(outcomes) save_users(users)
with st.sidebar.expander("Admin Panel", expanded=False): admin_pass = st.text_input("Enter admin passphrase:", type="password")
if admin_pass == ADMIN_PASSPHRASE: st.success("Authenticated") matches = get_today_matches() # this function gets matches for today
match_id_selection = st.selectbox("Select Match ID", [match['match_id'] for match in matches]) winning_team = st.text_input("Winning Team") man_of_the_match = st.text_input("Man of the Match")
if st.button("Submit Match Outcome"): update_leaderboard_and_outcomes(match_id_selection, winning_team, man_of_the_match) st.success("Match outcome submitted and leaderboard updated!") else: st.error("Not authenticated")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156[ { "match_id": "20240321_1", "date": "2024-03-21", "time": "7:30 PM", "teams": ["CSK", "RCB"], "venue": "Chennai" }, { "match_id": "20240322_1", "date": "2024-03-22", "time": "7:30 PM", "teams": ["CSK", "RCB"], "venue": "Chennai" }, { "match_id": "20240323_2", "date": "2024-03-23", "time": "3:30 PM", "teams": ["PBKS", "DC"], "venue": "Mohali" }, { "match_id": "20240323_3", "date": "2024-03-23", "time": "7:30 PM", "teams": ["KKR", "SRH"], "venue": "Kolkata" }, { "match_id": "20240324_4", "date": "2024-03-24", "time": "3:30 PM", "teams": ["RR", "LSG"], "venue": "Jaipur" }, { "match_id": "20240324_5", "date": "2024-03-24", "time": "7:30 PM", "teams": ["GT", "MI"], "venue": "Ahmedabad" }, { "match_id": "20240325_6", "date": "2024-03-25", "time": "7:30 PM", "teams": ["RCB", "PBKS"], "venue": "Bengaluru" }, { "match_id": "20240326_7", "date": "2024-03-26", "time": "7:30 PM", "teams": ["CSK", "GT"], "venue": "Chennai" }, { "match_id": "20240327_8", "date": "2024-03-27", "time": "7:30 PM", "teams": ["SRH", "MI"], "venue": "Hyderabad" }, { "match_id": "20240328_9", "date": "2024-03-28", "time": "7:30 PM", "teams": ["RR", "DC"], "venue": "Jaipur" }, { "match_id": "20240329_10", "date": "2024-03-29", "time": "7:30 PM", "teams": ["RCB", "KKR"], "venue": "Bengaluru" }, { "match_id": "20240330_11", "date": "2024-03-30", "time": "7:30 PM", "teams": ["LSG", "PBKS"], "venue": "Lucknow" }, { "match_id": "20240331_12", "date": "2024-03-31", "time": "3:30 PM", "teams": ["GT", "SRH"], "venue": "Ahmedabad" }, { "match_id": "20240331_13", "date": "2024-03-31", "time": "7:30 PM", "teams": ["DC", "CSK"], "venue": "Visakhapatnam" }, { "match_id": "20240401_14", "date": "2024-04-01", "time": "7:30 PM", "teams": ["MI", "RR"], "venue": "Mumbai" }, { "match_id": "20240402_15", "date": "2024-04-02", "time": "7:30 PM", "teams": ["RCB", "LSG"], "venue": "Bengaluru" }, { "match_id": "20240403_16", "date": "2024-04-03", "time": "7:30 PM", "teams": ["DC", "KKR"], "venue": "Visakhapatnam" }, { "match_id": "20240404_17", "date": "2024-04-04", "time": "7:30 PM", "teams": ["GT", "PBKS"], "venue": "Ahmedabad" }, { "match_id": "20240405_18", "date": "2024-04-05", "time": "7:30 PM", "teams": ["SRH", "CSK"], "venue": "Hyderabad" }, { "match_id": "20240406_19", "date": "2024-04-06", "time": "7:30 PM", "teams": ["RR", "RCB"], "venue": "Jaipur" }, { "match_id": "20240407_20", "date": "2024-04-07", "time": "3:30 PM", "teams": ["MI", "DC"], "venue": "Mumbai" }, { "match_id": "20240407_21", "date": "2024-04-07", "time": "7:30 PM", "teams": ["LSG", "GT"], "venue": "Lucknow" }]12user_name,match_id,predicted_winner,predicted_motm,bid_pointsJay,20240321_1,CSK,Dhoni,105123pandaspytzstreamlit1234567891011121314151617{ "Archana": 1000, "Arpit": 1000, "Ganesh": 1000, "Haaris": 1000, "Harshit": 1000, "Jay": 1000, "Kichu": 1000, "Megha": 1000, "Naveein": 1000, "Neha": 1000, "Praveen": 1000, "Rakesh": 1000, "Sunil": 1000, "Vaibhav": 1000, "Vinay": 1000}app.pyAdded+246
match_outcomes.jsonAdded
matches.jsonAdded+156
predictions.csvAdded+2
requirements.txtAdded+3
users.jsonAdded+17