Harika22 commited on
Commit
167e85a
·
verified ·
1 Parent(s): c933ce3

Update pages/2_Players_Comparison.py

Browse files
Files changed (1) hide show
  1. pages/2_Players_Comparison.py +160 -0
pages/2_Players_Comparison.py CHANGED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import requests
8
+ import io
9
+
10
+ # Page config
11
+ st.set_page_config(page_title="HomePage", page_icon="🚀", layout="wide")
12
+
13
+ # Background + CSS styling
14
+ st.markdown("""
15
+ <style>
16
+ .stApp {
17
+ background-color: #E3F2FD;
18
+ font-family: 'Inter', sans-serif;
19
+ }
20
+ table {
21
+ width: 100%;
22
+ border-collapse: collapse;
23
+ font-size: 14px;
24
+ margin-top: 20px;
25
+ }
26
+ th, td {
27
+ border: 1px solid #ddd;
28
+ padding: 8px;
29
+ text-align: center;
30
+ }
31
+ thead {
32
+ background-color: #f2f2f2;
33
+ }
34
+ tbody tr:nth-child(even) {
35
+ background-color: #f9f9f9;
36
+ }
37
+ tbody tr:hover {
38
+ background-color: #f1f1f1;
39
+ }
40
+ </style>
41
+ """, unsafe_allow_html=True)
42
+
43
+ # Title
44
+ st.markdown("<h1 style='text-align: center; color: #c71585; font-size: 36px;'>Player Comparison</h1>", unsafe_allow_html=True)
45
+
46
+ # Load model and data from Hugging Face
47
+ @st.cache_resource
48
+ def load_model_and_data():
49
+ model_url = ""
50
+ encoder_url = "https://huggingface.co/spaces/LakshmiHarika/Cricket_Dashboard/resolve/main/pages/label_encoder.pkl"
51
+ csv_url = ""
52
+
53
+ model = pickle.load(io.BytesIO(requests.get(model_url).content))
54
+ encoder = pickle.load(io.BytesIO(requests.get(encoder_url).content))
55
+ df = pd.read_csv(csv_url)
56
+
57
+ return model, encoder, df
58
+
59
+ # Chart functions
60
+ def plot_bar_comparison(x, p1_vals, p2_vals, p1, p2, title, ylabel):
61
+ fig = px.bar(
62
+ x=x * 2,
63
+ y=p1_vals + p2_vals,
64
+ color=[p1] * len(x) + [p2] * len(x),
65
+ barmode="group",
66
+ labels={"x": "Format", "y": ylabel, "color": "Player"},
67
+ title=title,
68
+ text=p1_vals + p2_vals
69
+ )
70
+ fig.update_traces(textposition='outside')
71
+ st.plotly_chart(fig, use_container_width=True)
72
+
73
+ def plot_line_comparison(x, p1_vals, p2_vals, p1, p2, title, ylabel):
74
+ fig = px.line(
75
+ x=x * 2,
76
+ y=p1_vals + p2_vals,
77
+ color=[p1] * len(x) + [p2] * len(x),
78
+ markers=True,
79
+ labels={"x": "Format", "y": ylabel, "color": "Player"},
80
+ title=title
81
+ )
82
+ st.plotly_chart(fig, use_container_width=True)
83
+
84
+ # Upload images
85
+ col1, col2 = st.columns(2)
86
+ with col1:
87
+ uploaded_file1 = st.file_uploader("Upload Player Image 1", type=["jpg", "jpeg", "png"])
88
+ if uploaded_file1:
89
+ st.image(uploaded_file1, caption="Player 1 Image")
90
+ with col2:
91
+ uploaded_file2 = st.file_uploader("Upload Player Image 2", type=["jpg", "jpeg", "png"])
92
+ if uploaded_file2:
93
+ st.image(uploaded_file2, caption="Player 2 Image")
94
+
95
+ # Load model and data
96
+ model, encoder, stats_df = load_model_and_data()
97
+
98
+ # When both images are uploaded
99
+ if uploaded_file1 and uploaded_file2:
100
+ img1 = Image.open(uploaded_file1).convert("L").resize((50, 50))
101
+ img2 = Image.open(uploaded_file2).convert("L").resize((50, 50))
102
+
103
+ arr1 = np.array(img1).flatten().reshape(1, -1)
104
+ arr2 = np.array(img2).flatten().reshape(1, -1)
105
+
106
+ pred1 = model.predict(arr1)[0]
107
+ pred2 = model.predict(arr2)[0]
108
+
109
+ player1 = encoder.inverse_transform([pred1])[0]
110
+ player2 = encoder.inverse_transform([pred2])[0]
111
+
112
+ st.subheader(f"{player1} vs {player2}")
113
+
114
+ c1, c2 = st.columns(2)
115
+ show_bat = c1.button("Show Batting Comparison 🏏")
116
+ show_bowl = c2.button("Show Bowling Comparison 🎯")
117
+
118
+ p1_data = stats_df[stats_df["Player"] == player1].iloc[0]
119
+ p2_data = stats_df[stats_df["Player"] == player2].iloc[0]
120
+ formats = ["Test", "ODI", "T20", "IPL"]
121
+
122
+ # Batting section
123
+ if show_bat:
124
+ st.markdown(f"### Batting Comparison: {player1} vs {player2}")
125
+ for metric, prefix in {
126
+ "Runs": "batting_Runs_",
127
+ "50s": "batting_50s_",
128
+ "100s": "batting_100s_"
129
+ }.items():
130
+ p1_vals = [p1_data.get(f"{prefix}{fmt}", 0) for fmt in formats]
131
+ p2_vals = [p2_data.get(f"{prefix}{fmt}", 0) for fmt in formats]
132
+ plot_bar_comparison(formats, p1_vals, p2_vals, player1, player2, f"{metric} by Format", metric)
133
+
134
+ for metric, prefix in {
135
+ "Average": "batting_Average_",
136
+ "Strike Rate": "batting_SR_"
137
+ }.items():
138
+ p1_vals = [p1_data.get(f"{prefix}{fmt}", 0) for fmt in formats]
139
+ p2_vals = [p2_data.get(f"{prefix}{fmt}", 0) for fmt in formats]
140
+ plot_line_comparison(formats, p1_vals, p2_vals, player1, player2, f"{metric} by Format", metric)
141
+
142
+ # Bowling section
143
+ if show_bowl:
144
+ st.markdown(f"### Bowling Comparison: {player1} vs {player2}")
145
+ for metric, suffix in {
146
+ "Wickets": "_Wickets",
147
+ "Maidens": "_Maidens",
148
+ "Economy": "_Eco"
149
+ }.items():
150
+ p1_vals = [p1_data.get(f"bowling_{fmt}{suffix}", 0) for fmt in formats]
151
+ p2_vals = [p2_data.get(f"bowling_{fmt}{suffix}", 0) for fmt in formats]
152
+ plot_bar_comparison(formats, p1_vals, p2_vals, player1, player2, f"{metric} by Format", metric)
153
+
154
+ for metric, suffix in {
155
+ "Average": "_Avg",
156
+ "Strike Rate": "_SR"
157
+ }.items():
158
+ p1_vals = [p1_data.get(f"bowling_{fmt}{suffix}", 0) for fmt in formats]
159
+ p2_vals = [p2_data.get(f"bowling_{fmt}{suffix}", 0) for fmt in formats]
160
+ plot_line_comparison(formats, p1_vals, p2_vals, player1, player2, f"{metric} by Format", metric)