Spaces:
Sleeping
Sleeping
File size: 4,967 Bytes
659dfad 4ce3f06 659dfad 1df9a8f 659dfad 1df9a8f 4ce3f06 1df9a8f 4ce3f06 1df9a8f 4ce3f06 1df9a8f 4ce3f06 1df9a8f 4ce3f06 1df9a8f 659dfad 4ce3f06 659dfad 4ce3f06 659dfad 4ce3f06 659dfad 4ce3f06 1df9a8f 4ce3f06 1df9a8f 4ce3f06 1df9a8f 4ce3f06 1df9a8f 4ce3f06 1df9a8f 4ce3f06 1df9a8f 659dfad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import streamlit as st
import pandas as pd
import pickle
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Load model and encoder
@st.cache_resource
def load_model_and_encoder():
with open('best_rf_pipeline.pkl', 'rb') as f:
model = pickle.load(f)
with open('label_encoder.pkl', 'rb') as f:
encoder = pickle.load(f)
return model, encoder
# Load player dataset
@st.cache_data
def load_data():
return pd.read_csv('Reduced_final_teams.csv')
# Fuzzy match player name
def get_matching_player(name_from_file, player_list):
name_lower = name_from_file.lower()
for player in player_list:
if player.lower() == name_lower:
return player
return None
# Horizontal bar chart
def plot_horizontal_bar(df, player1, player2):
st.subheader("π Stat Comparison - Horizontal Bar Chart")
num_cols = df.select_dtypes(include='number').columns
df_num = df[num_cols].T
df_num.columns = [player1, player2]
df_num = df_num.sort_values(by=player1, ascending=False).head(15)
fig, ax = plt.subplots(figsize=(10, 7))
df_num.plot(kind='barh', ax=ax)
ax.set_title(f"{player1} vs {player2} - Key Stats")
ax.set_xlabel("Value")
ax.set_ylabel("Metric")
ax.legend(loc="lower right")
st.pyplot(fig)
# Pie chart comparison
def plot_pie_charts(player1_data, player2_data, player1, player2):
st.subheader("π₯§ Batting vs Bowling Contribution")
col1, col2 = st.columns(2)
for col, player_data, player_name in zip([col1, col2], [player1_data, player2_data], [player1, player2]):
batting_total = player_data.get('Runs_ODI', 0) + player_data.get('Runs_T20', 0) + player_data.get('Runs_Test', 0)
bowling_total = player_data.get('Wickets_ODI', 0) + player_data.get('Wickets_T20', 0) + player_data.get('Wickets_Test', 0)
labels = ['Batting', 'Bowling']
sizes = [batting_total, bowling_total]
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90, colors=['#4CAF50', '#2196F3'])
ax.axis('equal')
col.pyplot(fig)
col.caption(f"{player_name}'s Batting vs Bowling")
# Bowling metric bar chart
def plot_bowling_comparison(df, player1, player2):
st.subheader("π― Bowling Metrics Comparison")
bowling_cols = [col for col in df.columns if 'Wickets' in col or 'Economy' in col or 'Bowling_Average' in col]
df_bowling = df[bowling_cols].T
df_bowling.columns = [player1, player2]
df_bowling = df_bowling.dropna().sort_values(by=player1, ascending=False)
fig, ax = plt.subplots(figsize=(10, 6))
df_bowling.plot(kind='bar', ax=ax)
ax.set_title("Bowling Stats")
ax.set_ylabel("Value")
ax.set_xticklabels(df_bowling.index, rotation=45, ha='right')
ax.legend(loc="upper right")
st.pyplot(fig)
# Main app
def main():
st.set_page_config(layout="wide")
st.title("Cricket Player Comparison Tool π")
df = load_data()
model, encoder = load_model_and_encoder()
player_list = df['Player'].tolist()
# Upload images
col1, col2 = st.columns(2)
with col1:
img1 = st.file_uploader("Upload Image for Player 1", type=['png', 'jpg', 'jpeg'], key='img1')
with col2:
img2 = st.file_uploader("Upload Image for Player 2", type=['png', 'jpg', 'jpeg'], key='img2')
if img1 and img2:
name1_raw = img1.name.rsplit('.', 1)[0]
name2_raw = img2.name.rsplit('.', 1)[0]
player1_name = get_matching_player(name1_raw, player_list)
player2_name = get_matching_player(name2_raw, player_list)
if player1_name and player2_name and player1_name != player2_name:
player1_data = df[df['Player'].str.lower() == player1_name.lower()].squeeze()
player2_data = df[df['Player'].str.lower() == player2_name.lower()].squeeze()
st.success(f"Comparing **{player1_name}** vs **{player2_name}**")
col3, col4 = st.columns(2)
with col3:
st.image(img1, caption=player1_name, use_container_width=True)
with col4:
st.image(img2, caption=player2_name, use_container_width=True)
# Display comparison table
comparison_df = pd.DataFrame([player1_data, player2_data])
comparison_df.set_index('Player', inplace=True)
st.subheader("π Full Stats Table")
st.dataframe(comparison_df.T)
# Visualizations
plot_horizontal_bar(comparison_df, player1_name, player2_name)
plot_pie_charts(player1_data, player2_data, player1_name, player2_name)
plot_bowling_comparison(comparison_df, player1_name, player2_name)
else:
st.error("β Player names from image files don't match or are the same. Please check file names.")
else:
st.info("πΈ Please upload two player images to continue.")
if __name__ == "__main__":
main()
|