Spaces:
Sleeping
Sleeping
Update pages/2player_comparison.py
Browse files- pages/2player_comparison.py +179 -127
pages/2player_comparison.py
CHANGED
@@ -1,133 +1,185 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
@st.cache_resource
|
10 |
-
def
|
11 |
-
with open(
|
12 |
model = pickle.load(f)
|
13 |
-
with open(
|
14 |
encoder = pickle.load(f)
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
st.
|
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 |
-
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
|
|
|
|
3 |
import numpy as np
|
4 |
+
import pickle
|
5 |
+
import plotly.express as px
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# ========== Custom CSS Styling ==========
|
9 |
+
st.markdown("""
|
10 |
+
<style>
|
11 |
+
.stApp {
|
12 |
+
background-color: #f0f4f8; /* Light clean background */
|
13 |
+
}
|
14 |
+
.title {
|
15 |
+
text-align: center;
|
16 |
+
color: #222222;
|
17 |
+
font-size: 40px;
|
18 |
+
font-family: 'Times New Roman';
|
19 |
+
font-weight: bold;
|
20 |
+
}
|
21 |
+
.header {
|
22 |
+
font-size: 32px;
|
23 |
+
font-family: 'Times New Roman';
|
24 |
+
color: #1a1a1a;
|
25 |
+
font-style: italic;
|
26 |
+
font-weight: bold;
|
27 |
+
}
|
28 |
+
.header1 {
|
29 |
+
font-size: 28px;
|
30 |
+
font-family: 'Times New Roman';
|
31 |
+
color: #333333;
|
32 |
+
font-style: italic;
|
33 |
+
font-weight: bold;
|
34 |
+
}
|
35 |
+
.header2 {
|
36 |
+
font-size: 20px;
|
37 |
+
font-family: 'Times New Roman';
|
38 |
+
color: #444444;
|
39 |
+
font-style: italic;
|
40 |
+
font-weight: bold;
|
41 |
+
}
|
42 |
+
.content {
|
43 |
+
font-size: 20px;
|
44 |
+
font-family: 'Times New Roman';
|
45 |
+
line-height: 1.6;
|
46 |
+
color: #222222;
|
47 |
+
}
|
48 |
+
.example {
|
49 |
+
font-size: 16px;
|
50 |
+
font-family: 'Times New Roman';
|
51 |
+
font-style: italic;
|
52 |
+
color: #666666;
|
53 |
+
}
|
54 |
+
</style>
|
55 |
+
""", unsafe_allow_html=True)
|
56 |
+
|
57 |
+
# ========== Load Model and Data ==========
|
58 |
@st.cache_resource
|
59 |
+
def load_model_and_data():
|
60 |
+
with open("best_rf_pipeline.pkl", "rb") as f:
|
61 |
model = pickle.load(f)
|
62 |
+
with open("label_encoder.pkl", "rb") as f:
|
63 |
encoder = pickle.load(f)
|
64 |
+
stats_df = pd.read_csv("Reduced_final_teams.csv")
|
65 |
+
return model, encoder, stats_df
|
66 |
+
|
67 |
+
pipeline, label_encoder, stats_df = load_model_and_data()
|
68 |
+
|
69 |
+
# ========== Plot Functions ==========
|
70 |
+
def plot_bar_comparison(x_labels, p1_values, p2_values, p1_name, p2_name, title, y_axis_label):
|
71 |
+
fig = px.bar(
|
72 |
+
x=x_labels * 2,
|
73 |
+
y=p1_values + p2_values,
|
74 |
+
color=[p1_name] * len(x_labels) + [p2_name] * len(x_labels),
|
75 |
+
barmode="group",
|
76 |
+
labels={"x": "Format", "y": y_axis_label, "color": "Player"},
|
77 |
+
title=title,
|
78 |
+
text=p1_values + p2_values
|
79 |
+
)
|
80 |
+
fig.update_traces(textposition='outside')
|
81 |
+
st.plotly_chart(fig, use_container_width=True)
|
82 |
+
|
83 |
+
def plot_line_comparison(x_labels, p1_values, p2_values, p1_name, p2_name, title, y_axis_label):
|
84 |
+
fig = px.line(
|
85 |
+
x=x_labels * 2,
|
86 |
+
y=p1_values + p2_values,
|
87 |
+
color=[p1_name] * len(x_labels) + [p2_name] * len(x_labels),
|
88 |
+
markers=True,
|
89 |
+
labels={"x": "Format", "y": y_axis_label, "color": "Player"},
|
90 |
+
title=title
|
91 |
+
)
|
92 |
+
st.plotly_chart(fig, use_container_width=True)
|
93 |
+
|
94 |
+
# ========== App Layout ==========
|
95 |
+
st.markdown("<div class='title'> Player Comparison📊 </div><br>", unsafe_allow_html=True)
|
96 |
+
|
97 |
+
col_img1, col_img2 = st.columns(2)
|
98 |
+
|
99 |
+
with col_img1:
|
100 |
+
st.markdown("<div class='header'> Upload Player Image 1 </div><br>", unsafe_allow_html=True)
|
101 |
+
uploaded_file1 = st.file_uploader("", type=["jpg", "png", "jpeg"], key="img1", label_visibility="collapsed")
|
102 |
+
if uploaded_file1 is not None:
|
103 |
+
display_img1 = Image.open(uploaded_file1).convert('RGB')
|
104 |
+
st.image(display_img1, caption="Player 1", use_container_width=True)
|
105 |
+
|
106 |
+
with col_img2:
|
107 |
+
st.markdown("<div class='header'> Upload Player Image 2 </div><br>", unsafe_allow_html=True)
|
108 |
+
uploaded_file2 = st.file_uploader("", type=["jpg", "png", "jpeg"], key="img2", label_visibility="collapsed")
|
109 |
+
if uploaded_file2 is not None:
|
110 |
+
display_img2 = Image.open(uploaded_file2).convert('RGB')
|
111 |
+
st.image(display_img2, caption="Player 2", use_container_width=True)
|
112 |
+
|
113 |
+
# ========== Predict and Compare ==========
|
114 |
+
if uploaded_file1 and uploaded_file2:
|
115 |
+
gray_img1 = Image.open(uploaded_file1).convert('L').resize((50, 50))
|
116 |
+
gray_img2 = Image.open(uploaded_file2).convert('L').resize((50, 50))
|
117 |
+
|
118 |
+
arr1 = np.array(gray_img1).flatten().reshape(1, -1)
|
119 |
+
arr2 = np.array(gray_img2).flatten().reshape(1, -1)
|
120 |
+
|
121 |
+
model, encoder, stats_df = load_model_and_data()
|
122 |
+
|
123 |
+
pred1 = model.predict(arr1)[0]
|
124 |
+
pred2 = model.predict(arr2)[0]
|
125 |
+
|
126 |
+
player_1 = encoder.inverse_transform([pred1])[0]
|
127 |
+
player_2 = encoder.inverse_transform([pred2])[0]
|
128 |
+
|
129 |
+
st.success(f"Player 1: {player_1}")
|
130 |
+
st.success(f"Player 2: {player_2}")
|
131 |
+
|
132 |
+
p1_data = stats_df[stats_df["Player"] == player_1].iloc[0]
|
133 |
+
p2_data = stats_df[stats_df["Player"] == player_2].iloc[0]
|
134 |
+
|
135 |
+
colA, colB = st.columns(2)
|
136 |
+
with colA:
|
137 |
+
show_batting = st.button("Show Batting Comparison 🏏")
|
138 |
+
with colB:
|
139 |
+
show_bowling = st.button("Show Bowling Comparison 🎯")
|
140 |
+
|
141 |
+
if show_batting:
|
142 |
+
st.markdown(f"<div class='header'>Batting Comparison: {player_1} vs {player_2} 🏏</div><br>", unsafe_allow_html=True)
|
143 |
+
formats = ["Test", "ODI", "T20", "IPL"]
|
144 |
+
|
145 |
+
batting_bar_metrics = {
|
146 |
+
"Runs": "batting_Runs_",
|
147 |
+
"50s": "batting_50s_",
|
148 |
+
"100s": "batting_100s_"
|
149 |
+
}
|
150 |
+
for metric_name, prefix in batting_bar_metrics.items():
|
151 |
+
p1_vals = [p1_data.get(f"{prefix}{fmt}", 0) for fmt in formats]
|
152 |
+
p2_vals = [p2_data.get(f"{prefix}{fmt}", 0) for fmt in formats]
|
153 |
+
plot_bar_comparison(formats, p1_vals, p2_vals, player_1, player_2, f"{metric_name} by Format", metric_name)
|
154 |
+
|
155 |
+
batting_line_metrics = {
|
156 |
+
"Batting Average": "batting_Average_",
|
157 |
+
"Strike Rate": "batting_SR_"
|
158 |
+
}
|
159 |
+
for metric_name, prefix in batting_line_metrics.items():
|
160 |
+
p1_vals = [p1_data.get(f"{prefix}{fmt}", 0) for fmt in formats]
|
161 |
+
p2_vals = [p2_data.get(f"{prefix}{fmt}", 0) for fmt in formats]
|
162 |
+
plot_line_comparison(formats, p1_vals, p2_vals, player_1, player_2, f"{metric_name} by Format", metric_name)
|
163 |
+
|
164 |
+
if show_bowling:
|
165 |
+
st.markdown(f"<div class='header'>Bowling Comparison: {player_1} vs {player_2} 🎯</div><br>", unsafe_allow_html=True)
|
166 |
+
formats = ["Test", "ODI", "T20", "IPL"]
|
167 |
+
|
168 |
+
bowling_bar_metrics = {
|
169 |
+
"Wickets": "_Wickets",
|
170 |
+
"Maidens": "_Maidens",
|
171 |
+
"Economy Rate": "_Eco"
|
172 |
+
}
|
173 |
+
for metric_name, suffix in bowling_bar_metrics.items():
|
174 |
+
p1_vals = [p1_data.get(f"bowling_{fmt}{suffix}", 0) for fmt in formats]
|
175 |
+
p2_vals = [p2_data.get(f"bowling_{fmt}{suffix}", 0) for fmt in formats]
|
176 |
+
plot_bar_comparison(formats, p1_vals, p2_vals, player_1, player_2, f"{metric_name} by Format", metric_name)
|
177 |
+
|
178 |
+
bowling_line_metrics = {
|
179 |
+
"Bowling Average": "_Avg",
|
180 |
+
"Strike Rate": "_SR"
|
181 |
+
}
|
182 |
+
for metric_name, suffix in bowling_line_metrics.items():
|
183 |
+
p1_vals = [p1_data.get(f"bowling_{fmt}{suffix}", 0) for fmt in formats]
|
184 |
+
p2_vals = [p2_data.get(f"bowling_{fmt}{suffix}", 0) for fmt in formats]
|
185 |
+
plot_line_comparison(formats, p1_vals, p2_vals, player_1, player_2, f"{metric_name} by Format", metric_name)
|