Spaces:
Sleeping
Sleeping
Update pages/2player_comparison.py
Browse files- pages/2player_comparison.py +127 -178
pages/2player_comparison.py
CHANGED
@@ -1,184 +1,133 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
import numpy as np
|
4 |
import pickle
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
.stApp {
|
11 |
-
background-color: #f0f4f8; /* Light clean background */
|
12 |
-
}
|
13 |
-
.title {
|
14 |
-
text-align: center;
|
15 |
-
color: #222222;
|
16 |
-
font-size: 40px;
|
17 |
-
font-family: 'Times New Roman';
|
18 |
-
font-weight: bold;
|
19 |
-
}
|
20 |
-
.header {
|
21 |
-
font-size: 32px;
|
22 |
-
font-family: 'Times New Roman';
|
23 |
-
color: #1a1a1a;
|
24 |
-
font-style: italic;
|
25 |
-
font-weight: bold;
|
26 |
-
}
|
27 |
-
.header1 {
|
28 |
-
font-size: 28px;
|
29 |
-
font-family: 'Times New Roman';
|
30 |
-
color: #333333;
|
31 |
-
font-style: italic;
|
32 |
-
font-weight: bold;
|
33 |
-
}
|
34 |
-
.header2 {
|
35 |
-
font-size: 20px;
|
36 |
-
font-family: 'Times New Roman';
|
37 |
-
color: #444444;
|
38 |
-
font-style: italic;
|
39 |
-
font-weight: bold;
|
40 |
-
}
|
41 |
-
.content {
|
42 |
-
font-size: 20px;
|
43 |
-
font-family: 'Times New Roman';
|
44 |
-
line-height: 1.6;
|
45 |
-
color: #222222;
|
46 |
-
}
|
47 |
-
.example {
|
48 |
-
font-size: 16px;
|
49 |
-
font-family: 'Times New Roman';
|
50 |
-
font-style: italic;
|
51 |
-
color: #666666;
|
52 |
-
}
|
53 |
-
</style>
|
54 |
-
""", unsafe_allow_html=True)
|
55 |
-
|
56 |
-
# ========== Load Model and Data ==========
|
57 |
@st.cache_resource
|
58 |
-
def
|
59 |
-
with open(
|
60 |
model = pickle.load(f)
|
61 |
-
with open(
|
62 |
encoder = pickle.load(f)
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
st.
|
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 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
st.
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
"
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
p1_vals = [p1_data.get(f"bowling_{fmt}{suffix}", 0) for fmt in formats]
|
183 |
-
p2_vals = [p2_data.get(f"bowling_{fmt}{suffix}", 0) for fmt in formats]
|
184 |
-
plot_line_comparison(formats, p1_vals, p2_vals, player_1, player_2, f"{metric_name} by Format", metric_name)
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
3 |
import pickle
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
# Load model and encoder
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
@st.cache_resource
|
10 |
+
def load_model_and_encoder():
|
11 |
+
with open('best_rf_pipeline.pkl', 'rb') as f:
|
12 |
model = pickle.load(f)
|
13 |
+
with open('label_encoder.pkl', 'rb') as f:
|
14 |
encoder = pickle.load(f)
|
15 |
+
return model, encoder
|
16 |
+
|
17 |
+
# Load player dataset
|
18 |
+
@st.cache_data
|
19 |
+
def load_data():
|
20 |
+
return pd.read_csv('Reduced_final_teams.csv')
|
21 |
+
|
22 |
+
# Fuzzy match player name
|
23 |
+
def get_matching_player(name_from_file, player_list):
|
24 |
+
name_lower = name_from_file.lower()
|
25 |
+
for player in player_list:
|
26 |
+
if player.lower() == name_lower:
|
27 |
+
return player
|
28 |
+
return None
|
29 |
+
|
30 |
+
# Horizontal bar chart
|
31 |
+
def plot_horizontal_bar(df, player1, player2):
|
32 |
+
st.subheader("π Stat Comparison - Horizontal Bar Chart")
|
33 |
+
num_cols = df.select_dtypes(include='number').columns
|
34 |
+
df_num = df[num_cols].T
|
35 |
+
df_num.columns = [player1, player2]
|
36 |
+
df_num = df_num.sort_values(by=player1, ascending=False).head(15)
|
37 |
+
|
38 |
+
fig, ax = plt.subplots(figsize=(10, 7))
|
39 |
+
df_num.plot(kind='barh', ax=ax)
|
40 |
+
ax.set_title(f"{player1} vs {player2} - Key Stats")
|
41 |
+
ax.set_xlabel("Value")
|
42 |
+
ax.set_ylabel("Metric")
|
43 |
+
ax.legend(loc="lower right")
|
44 |
+
st.pyplot(fig)
|
45 |
+
|
46 |
+
# Pie chart comparison
|
47 |
+
def plot_pie_charts(player1_data, player2_data, player1, player2):
|
48 |
+
st.subheader("π₯§ Batting vs Bowling Contribution")
|
49 |
+
col1, col2 = st.columns(2)
|
50 |
+
|
51 |
+
for col, player_data, player_name in zip([col1, col2], [player1_data, player2_data], [player1, player2]):
|
52 |
+
batting_total = player_data.get('Runs_ODI', 0) + player_data.get('Runs_T20', 0) + player_data.get('Runs_Test', 0)
|
53 |
+
bowling_total = player_data.get('Wickets_ODI', 0) + player_data.get('Wickets_T20', 0) + player_data.get('Wickets_Test', 0)
|
54 |
+
|
55 |
+
labels = ['Batting', 'Bowling']
|
56 |
+
sizes = [batting_total, bowling_total]
|
57 |
+
|
58 |
+
fig, ax = plt.subplots()
|
59 |
+
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90, colors=['#4CAF50', '#2196F3'])
|
60 |
+
ax.axis('equal')
|
61 |
+
col.pyplot(fig)
|
62 |
+
col.caption(f"{player_name}'s Batting vs Bowling")
|
63 |
+
|
64 |
+
# Bowling metric bar chart
|
65 |
+
def plot_bowling_comparison(df, player1, player2):
|
66 |
+
st.subheader("π― Bowling Metrics Comparison")
|
67 |
+
|
68 |
+
bowling_cols = [col for col in df.columns if 'Wickets' in col or 'Economy' in col or 'Bowling_Average' in col]
|
69 |
+
df_bowling = df[bowling_cols].T
|
70 |
+
df_bowling.columns = [player1, player2]
|
71 |
+
df_bowling = df_bowling.dropna().sort_values(by=player1, ascending=False)
|
72 |
+
|
73 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
74 |
+
df_bowling.plot(kind='bar', ax=ax)
|
75 |
+
ax.set_title("Bowling Stats")
|
76 |
+
ax.set_ylabel("Value")
|
77 |
+
ax.set_xticklabels(df_bowling.index, rotation=45, ha='right')
|
78 |
+
ax.legend(loc="upper right")
|
79 |
+
st.pyplot(fig)
|
80 |
+
|
81 |
+
# Main app
|
82 |
+
def main():
|
83 |
+
st.set_page_config(layout="wide")
|
84 |
+
st.title("Cricket Player Comparison Tool π")
|
85 |
+
|
86 |
+
df = load_data()
|
87 |
+
model, encoder = load_model_and_encoder()
|
88 |
+
player_list = df['Player'].tolist()
|
89 |
+
|
90 |
+
# Upload images
|
91 |
+
col1, col2 = st.columns(2)
|
92 |
+
with col1:
|
93 |
+
img1 = st.file_uploader("Upload Image for Player 1", type=['png', 'jpg', 'jpeg'], key='img1')
|
94 |
+
with col2:
|
95 |
+
img2 = st.file_uploader("Upload Image for Player 2", type=['png', 'jpg', 'jpeg'], key='img2')
|
96 |
+
|
97 |
+
if img1 and img2:
|
98 |
+
name1_raw = img1.name.rsplit('.', 1)[0]
|
99 |
+
name2_raw = img2.name.rsplit('.', 1)[0]
|
100 |
+
|
101 |
+
player1_name = get_matching_player(name1_raw, player_list)
|
102 |
+
player2_name = get_matching_player(name2_raw, player_list)
|
103 |
+
|
104 |
+
if player1_name and player2_name and player1_name != player2_name:
|
105 |
+
player1_data = df[df['Player'].str.lower() == player1_name.lower()].squeeze()
|
106 |
+
player2_data = df[df['Player'].str.lower() == player2_name.lower()].squeeze()
|
107 |
+
|
108 |
+
st.success(f"Comparing **{player1_name}** vs **{player2_name}**")
|
109 |
+
|
110 |
+
col3, col4 = st.columns(2)
|
111 |
+
with col3:
|
112 |
+
st.image(img1, caption=player1_name, use_container_width=True)
|
113 |
+
with col4:
|
114 |
+
st.image(img2, caption=player2_name, use_container_width=True)
|
115 |
+
|
116 |
+
# Display comparison table
|
117 |
+
comparison_df = pd.DataFrame([player1_data, player2_data])
|
118 |
+
comparison_df.set_index('Player', inplace=True)
|
119 |
+
st.subheader("π Full Stats Table")
|
120 |
+
st.dataframe(comparison_df.T)
|
121 |
+
|
122 |
+
# Visualizations
|
123 |
+
plot_horizontal_bar(comparison_df, player1_name, player2_name)
|
124 |
+
plot_pie_charts(player1_data, player2_data, player1_name, player2_name)
|
125 |
+
plot_bowling_comparison(comparison_df, player1_name, player2_name)
|
126 |
+
|
127 |
+
else:
|
128 |
+
st.error("β Player names from image files don't match or are the same. Please check file names.")
|
129 |
+
else:
|
130 |
+
st.info("πΈ Please upload two player images to continue.")
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
main()
|
|
|
|
|
|