File size: 21,499 Bytes
78e938e
aec300c
 
712e115
 
 
6961d71
75e7920
 
 
 
 
28848c6
41ca510
6a6f5a4
 
 
7f4d742
40b9f80
 
 
6a6f5a4
 
 
40b9f80
6a6f5a4
40b9f80
 
6a6f5a4
 
 
 
 
6961d71
a228e62
41ca510
6961d71
f31df03
f8fc69a
6961d71
 
 
59c1180
6961d71
 
333e4ee
b72cf26
 
 
 
 
 
 
 
6961d71
e13ce17
2578bcf
 
 
 
 
 
6961d71
4a1d123
 
f4d9d28
 
 
 
 
 
 
 
 
 
 
 
 
d0c2281
4a1d123
 
 
 
 
59c1180
 
6961d71
 
e13ce17
f8fc69a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
712e115
f8fc69a
 
 
 
2578bcf
f8fc69a
2578bcf
e13ce17
f8fc69a
 
 
 
 
 
4a1d123
 
 
 
 
7c64321
 
 
d6c7762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f601ac8
1095ff4
 
 
 
d6c7762
1095ff4
 
 
 
 
d6c7762
1095ff4
 
d6c7762
1095ff4
 
d6c7762
 
 
 
 
 
 
1095ff4
d6c7762
 
 
1095ff4
 
 
d6c7762
 
 
 
1095ff4
 
 
d6c7762
 
fca016e
1095ff4
 
d6c7762
fca016e
 
d6c7762
 
 
 
 
 
 
 
 
 
 
 
 
 
1095ff4
d6c7762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1095ff4
 
d6c7762
 
 
 
 
 
 
 
 
1095ff4
d6c7762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1095ff4
d6c7762
1095ff4
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
135
136
137
138
139
140
141
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import seaborn as sns
import matplotlib.pyplot as plt
import base64
import plotly.express as px
import joblib
import cv2
from PIL import Image
import tempfile
import numpy as np

# Set page config
st.set_page_config(page_title="Cricket Career Insights", layout="wide")

# Function to set background image
def set_background(image_path):
    with open(image_path, "rb") as image_file:
        encoded_image = base64.b64encode(image_file.read()).decode()
    page_bg_style = f"""
    <style>
    .stApp {{
        background-image: url("data:image/png;base64,{encoded_image}");
        background-size: cover;
        background-position: center;
        background-attachment: fixed;
    }}
    </style>
    """
    st.markdown(page_bg_style, unsafe_allow_html=True)

# Call function with local image path
set_background("cricket_background.jpg")

# Load data
data = pd.read_csv("International_cricket_data.csv")
gr_df = data.groupby("Player")

# Sidebar Navigation
st.sidebar.title("🏏 Navigation")
page = st.sidebar.radio("Go to", ["🏠 Home", "πŸ“ˆ Player Career Info","Player Comparision(πŸ§β€β™‚οΈ vs πŸ§β€β™‚οΈ)"])

if page == "🏠 Home":
    st.title("🏏 Step into the world of cricket –  scores and more await you!!!")

    st.subheader("What is Cricket?")
    st.markdown("""
    Cricket is the heartbeat of millions, a game where bat meets ball, and history is written in every boundary and wicket.  
    It’s a battlefield of strategy, where captains plot, bowlers scheme, and batsmen rise like warriors to defend their honor.  
    From the electrifying roar of a six soaring into the stands to the nail-biting tension of the final over,  
    cricket is more than a sportβ€”it’s a journey of emotions, uniting fans across the globe.  
    """)
    st.subheader("πŸ“Š About the Application")
    st.write("""
    Welcome to **Cricket Career Insights** – Your Ultimate Cricket Analytics Hub! πŸš€  
    - πŸ” **Player Stats** – Track career progression.  
    - πŸ† **Milestones** – First match & last match highlights.  
    - πŸ“Š **Performance Trends** – Runs, wickets, & strike rates across formats.  
    - πŸ”₯ **Record-Breaking Moments** – Highest scores & best bowling figures.  
    - ⚑ **Visual Insights** – Stunning charts & graphs.  
    """)
    st.subheader("πŸ‘©β€πŸ’» About the Author")
    st.markdown("""
    <p style="text-align: justify;">
    Hi, I’m Anshini Kumbhare! πŸ‘‹ I’m an AI πŸ’» enthusiast with a passion for exploring data and uncovering meaningful insights.  
    I thrive on solving real-world problems using data-driven approaches and have a deep interest in the field of data science.  

    With a curious mind and an eagerness to learn, I constantly explore advancements in AI and emerging technologies.  
    My love for cricket 🏏, combined with my technical skills, inspired me to create this applicationβ€”showcasing the perfect blend of passion and innovation.  

    When I’m not working on data projects, I enjoy staying updated with the latest trends in artificial intelligence,  
    contributing to AI communities, and collaborating on creative ideas to make technology accessible for everyone. βœ¨πŸ“Š  

    Let’s connect and explore the world of data together! πŸš€
    </p>
    """, unsafe_allow_html=True)

     # Social Media Links
    st.markdown("### πŸ“Œ Connect with Me:")
    st.markdown("[![LinkedIn](https://img.shields.io/badge/LinkedIn-Profile-blue)](https://www.linkedin.com/in/anshini-kumbhare)")
    st.markdown("[![GitHub](https://img.shields.io/badge/GitHub-Profile-lightgrey)](https://github.com/anshinii)")
    st.markdown('</div>', unsafe_allow_html=True)

    
elif page == "πŸ“ˆ Player Career Info":
    st.title("πŸ“ˆ Player Career Information")
    
    player_name = st.selectbox("🏏 Choose a Cricketer", list(gr_df.groups.keys()))
    player_data = gr_df.get_group(player_name).iloc[0]
    formats = ['Test', 'ODI', 'T20', 'IPL']

    # Batting Career Summary
    st.subheader("Batting Career Summary")
    batting_summary = [
        [fmt, player_data.get(f'Matches_{fmt}', 0), player_data.get(f'batting_Innings_{fmt}', 0),
         player_data.get(f'batting_Runs_{fmt}', 0), player_data.get(f'batting_Balls_{fmt}', 0),
         player_data.get(f'batting_Highest_{fmt}', 0), player_data.get(f'batting_Average_{fmt}', 0),
         player_data.get(f'batting_SR_{fmt}', 0), player_data.get(f'batting_Not Out_{fmt}', 0),
         player_data.get(f'batting_Fours_{fmt}', 0), player_data.get(f'batting_Sixes_{fmt}', 0),
         player_data.get(f'batting_50s_{fmt}', 0), player_data.get(f'batting_100s_{fmt}', 0),
         player_data.get(f'batting_200s_{fmt}', 0)] for fmt in formats
    ]
    batting_df = pd.DataFrame(batting_summary, columns=[
        'Format', 'Matches', 'Innings', 'Runs', 'Balls Faced', 'Highest', 'Average',
        'Strike Rate', 'Not Out', '4s', '6s', '50s', '100s', '200s'
    ])
    st.dataframe(batting_df.set_index("Format"))

    # Bowling Career Summary
    st.subheader("Bowling Career Summary")
    bowling_summary = [
        [fmt, player_data.get(f'bowling_{fmt}_Innings', 0), player_data.get(f'bowling_{fmt}_Balls', 0),
         player_data.get(f'bowling_{fmt}_Runs', 0), player_data.get(f'bowling_{fmt}_Wickets', 0),
         player_data.get(f'bowling_{fmt}_Avg', 0), player_data.get(f'bowling_{fmt}_Eco', 0),
         player_data.get(f'bowling_{fmt}_SR', 0), player_data.get(f'bowling_{fmt}_BBI', 0),
         player_data.get(f'bowling_{fmt}_5w', 0), player_data.get(f'bowling_{fmt}_10w', 0)] for fmt in formats
    ]
    bowling_df = pd.DataFrame(bowling_summary, columns=[
        'Format', 'Innings', 'Balls', 'Runs', 'Wickets', 'Avg', 'Economy',
        'Strike Rate', 'BBI', '5w', '10w'
    ])
    st.dataframe(bowling_df.set_index("Format"))

    # Matches per Format Pie Chart
    st.subheader("Matches Played per Format")
    match_counts = [player_data.get(f'Matches_{fmt}', 0) for fmt in formats]
    fig1 = px.pie(names=formats, values=match_counts, title="Match Distribution by Format", hole=0.4)
    st.plotly_chart(fig1)

    # Batting and Strike Rate Bar Charts
    st.subheader("Additional Visual Insights")
    fig2 = px.bar(batting_df, x='Format', y='Average', title='Batting Average by Format', text_auto=True, color='Format')
    st.plotly_chart(fig2)
    fig3 = px.bar(batting_df, x='Format', y='Strike Rate', title='Strike Rate by Format', text_auto=True, color='Format')
    st.plotly_chart(fig3)
    
    # 100s and 50s Bar Chart
    fig4 = go.Figure(data=[
        go.Bar(name='100s', x=formats, y=batting_df['100s']),
        go.Bar(name='50s', x=formats, y=batting_df['50s'])
    ])
    fig4.update_layout(title='Centuries and Fifties by Format', barmode='group')
    st.plotly_chart(fig4)

     # Line Chart for Runs Trend
    st.subheader("Runs Trend Across Formats")
    fig5 = px.line(batting_df, x='Format', y='Runs', title='Runs Scored Across Formats', markers=True)
    st.plotly_chart(fig5)


# if page == "Player Comparision(πŸ§β€β™‚οΈ vs πŸ§β€β™‚οΈ)":
#     st.title("🏏 Player Recognition & Performance Analysis")
#     player_stats_df = pd.read_csv("final_cricket_dataset.csv")
#     model = joblib.load("svc_face_classifier.pkl")
#     label_encoder = joblib.load("label_encoder.pkl")
#     face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
    
#     # Streamlit Page Setup   
    
#     # Image Preprocessing + Face Detection Function
#     def detect_and_predict_face(image_file):
#         image = Image.open(image_file).convert("RGB")
#         img_np = np.array(image)
#         gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
        
#         faces = face_cascade.detectMultiScale(gray, 1.3, 5)
#         if len(faces) == 0:
#             return None, "No face detected!"
    
#         x, y, w, h = faces[0]
#         face = gray[y:y+h, x:x+w]
#         resized_face = cv2.resize(face, (64, 64))
#         flattened = resized_face.flatten().reshape(1, -1)
    
#         pred_label = model.predict(flattened)[0]
#         pred_name = label_encoder.inverse_transform([pred_label])[0]
#         return pred_name, None
    
#     # Upload images
#     col1, col2 = st.columns(2)
#     with col1:
#         img1 = st.file_uploader("Upload First Player Image", type=["jpg", "png", "jpeg"], key="img1")
#     with col2:
#         img2 = st.file_uploader("Upload Second Player Image", type=["jpg", "png", "jpeg"], key="img2")
    
#     if img1 and img2:
#         p1_name, err1 = detect_and_predict_face(img1)
#         p2_name, err2 = detect_and_predict_face(img2)
    
#         if err1:
#             st.error(f"Image 1: {err1}")
#         if err2:
#             st.error(f"Image 2: {err2}")
        
#         if not err1 and not err2:
#             st.success(f"βœ… Player 1 Detected: {p1_name}")
#             st.success(f"βœ… Player 2 Detected: {p2_name}")
    
#             formats = ['Test', 'ODI', 'T20', 'IPL']
#             df = player_stats_df
#             players = [p1_name, p2_name]
    
#             if p1_name not in df['Label'].values or p2_name not in df['Label'].values:
#                 st.error("One or both players not found in dataset.")
#                 st.stop()
    
#             p1_data = df[df['Label'] == p1_name].iloc[0]
#             p2_data = df[df['Label'] == p2_name].iloc[0]
    
#             st.markdown("## πŸ“Š Comparative Stats")
    
#             # Batting Summary
#             st.markdown("### 🏏 Batting Career Summary")
#             batting_summary = []
#             for fmt in formats:
#                 batting_summary.append({
#                     "Format": fmt,
#                     p1_name: p1_data.get(f'batting_Runs_{fmt}', 0),
#                     p2_name: p2_data.get(f'batting_Runs_{fmt}', 0)
#                 })
#             batting_df = pd.DataFrame(batting_summary)
#             fig = px.bar(batting_df, x="Format", y=[p1_name, p2_name], barmode="group", title="Total Runs by Format")
#             st.plotly_chart(fig, use_container_width=True)
    
#             # Bowling Summary
#             st.markdown("### 🎯 Bowling Career Summary")
#             bowling_summary = []
#             for fmt in formats:
#                 bowling_summary.append({
#                     "Format": fmt,
#                     p1_name: p1_data.get(f'bowling_{fmt}_Wickets', 0),
#                     p2_name: p2_data.get(f'bowling_{fmt}_Wickets', 0)
#                 })
#             bowling_df = pd.DataFrame(bowling_summary)
#             fig = px.bar(bowling_df, x="Format", y=[p1_name, p2_name], barmode="group", title="Total Wickets by Format")
#             st.plotly_chart(fig, use_container_width=True)
    
#             # Strike Rate vs Runs
#             st.markdown("### ⚑ Strike Rate vs Runs")
#             data = {
#                 "Format": formats,
#                 f"{p1_name} Runs": [p1_data.get(f'batting_Runs_{fmt}', 0) for fmt in formats],
#                 f"{p1_name} SR": [p1_data.get(f'batting_SR_{fmt}', 0) for fmt in formats],
#                 f"{p2_name} Runs": [p2_data.get(f'batting_Runs_{fmt}', 0) for fmt in formats],
#                 f"{p2_name} SR": [p2_data.get(f'batting_SR_{fmt}', 0) for fmt in formats],
#             }
    
#             fig = px.scatter(x=data[f"{p1_name} Runs"] + data[f"{p2_name} Runs"],
#                              y=data[f"{p1_name} SR"] + data[f"{p2_name} SR"],
#                              color=["Player 1"] * 4 + ["Player 2"] * 4,
#                              text=formats * 2,
#                              labels={"x": "Runs", "y": "Strike Rate"},
#                              title="Runs vs Strike Rate Comparison")
#             st.plotly_chart(fig, use_container_width=True)
    
#             # Milestones
#             st.markdown("### πŸ† Milestone Comparison")
#             milestone_df = pd.DataFrame({
#                 "Format": formats * 2,
#                 "Player": [p1_name] * 4 + [p2_name] * 4,
#                 "50s": [p1_data.get(f"batting_50s_{fmt}", 0) for fmt in formats] +
#                        [p2_data.get(f"batting_50s_{fmt}", 0) for fmt in formats],
#                 "100s": [p1_data.get(f"batting_100s_{fmt}", 0) for fmt in formats] +
#                         [p2_data.get(f"batting_100s_{fmt}", 0) for fmt in formats],
#                 "200s": [p1_data.get(f"batting_200s_{fmt}", 0) for fmt in formats] +
#                         [p2_data.get(f"batting_200s_{fmt}", 0) for fmt in formats]
#             })
    
#             for stat in ['50s', '100s', '200s']:
#                 fig = px.bar(milestone_df, x="Format", y=stat, color="Player", barmode="group",
#                              title=f"{stat} Achievements Comparison")
#                 st.plotly_chart(fig, use_container_width=True)
    
#             # Pie Chart - Matches Played
#             st.markdown("### 🧩 Matches Played by Format")
#             for i, data_player in enumerate([p1_data, p2_data]):
#                 match_data = {
#                     fmt: data_player.get(f"Matches_{fmt}", 0) for fmt in formats
#                 }
#                 fig = px.pie(values=list(match_data.values()), names=list(match_data.keys()), 
#                              title=f"{players[i]} Matches Distribution")
#                 st.plotly_chart(fig, use_container_width=True)
    
#             # Final Trend Overview
#             st.markdown("### πŸ“‰ Trend Overview")
#             fig, ax = plt.subplots(1, 2, figsize=(14, 5))
#             sns.barplot(x=formats, y=[p1_data.get(f'batting_Runs_{fmt}', 0) for fmt in formats], ax=ax[0], label=p1_name)
#             sns.barplot(x=formats, y=[p2_data.get(f'batting_Runs_{fmt}', 0) for fmt in formats], ax=ax[0], label=p2_name)
#             ax[0].set_title("Batting Runs Trend")
#             ax[0].legend()
    
#             sns.barplot(x=formats, y=[p1_data.get(f'bowling_{fmt}_Wickets', 0) for fmt in formats], ax=ax[1], label=p1_name)
#             sns.barplot(x=formats, y=[p2_data.get(f'bowling_{fmt}_Wickets', 0) for fmt in formats], ax=ax[1], label=p2_name)
#             ax[1].set_title("Bowling Wickets Trend")
#             ax[1].legend()
#             st.pyplot(fig)
    
elif page == "Player Comparision(πŸ§β€β™‚οΈ vs πŸ§β€β™‚οΈ)":
    st.title("🏏 Player Recognition & Performance Analysis")
    player_stats_df = pd.read_csv("final_cricket_dataset.csv")
    model = joblib.load("svc_face_classifier.pkl")
    label_encoder = joblib.load("label_encoder.pkl")
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

    def detect_and_predict_face(image_file):
        image = Image.open(image_file).convert("RGB")
        img_np = np.array(image)
        gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)

        if len(faces) == 0:
            return None, "No face detected!"

        x, y, w, h = faces[0]
        face = gray[y:y+h, x:x+w]
        resized = cv2.resize(face, (64, 64))
        flat = resized.flatten().reshape(1, -1)

        pred = model.predict(flat)[0]
        name = label_encoder.inverse_transform([pred])[0]
        return name, None

    col1, col2 = st.columns(2)
    img1 = col1.file_uploader("Upload First Player Image", type=["jpg", "jpeg", "png"], key="img1")
    img2 = col2.file_uploader("Upload Second Player Image", type=["jpg", "jpeg", "png"], key="img2")

    if img1 and img2:
        p1_name, err1 = detect_and_predict_face(img1)
        p2_name, err2 = detect_and_predict_face(img2)

        if err1: st.error(f"Image 1: {err1}")
        if err2: st.error(f"Image 2: {err2}")

        if not err1 and not err2:
            st.success(f"βœ… Player 1 Detected: {p1_name}")
            st.success(f"βœ… Player 2 Detected: {p2_name}")

            df = player_stats_df.copy()
            if p1_name not in df['Label'].values or p2_name not in df['Label'].values:
                st.error("One or both players not found in dataset.")
                st.stop()

            p1_data = df[df['Label'] == p1_name].iloc[0]
            p2_data = df[df['Label'] == p2_name].iloc[0]

            st.write("### πŸ“‹ Player Data Preview")
            st.write(p1_data)
            st.write(p2_data)

            formats = ['Test', 'ODI', 'T20', 'IPL']

            def plot_bar(compare_col, title, y_label):
                vals1 = [p1_data.get(f"{compare_col}_{fmt}", 0) for fmt in formats]
                vals2 = [p2_data.get(f"{compare_col}_{fmt}", 0) for fmt in formats]
                df_plot = pd.DataFrame({
                    "Format": formats,
                    p1_name: vals1,
                    p2_name: vals2
                })
                if df_plot[[p1_name, p2_name]].sum().sum() == 0:
                    st.warning(f"No data to plot for {title}.")
                    return
                try:
                    fig = px.bar(df_plot, x="Format", y=[p1_name, p2_name], barmode="group", title=title)
                    st.plotly_chart(fig, use_container_width=True)
                except Exception as e:
                    st.error(f"Could not render {title}: {e}")

            st.markdown("## πŸ“Š Comparative Stats")
            st.subheader("πŸ›‘οΈ Batting – Total Runs by Format")
            plot_bar("batting_Runs", "Total Runs by Format", "Runs")
            st.subheader("🎯 Bowling – Total Wickets by Format")
            plot_bar("bowling_Wickets", "Total Wickets by Format", "Wickets")

            st.subheader("⚑ Runs vs Strike Rate Scatter")

            data_pts = []
            for player, pdata in [(p1_name, p1_data), (p2_name, p2_data)]:
                runs = [pdata.get(f"batting_Runs_{fmt}", 0) for fmt in formats]
                sr = [pdata.get(f"batting_SR_{fmt}", 0) for fmt in formats]
                for fmt, r, s in zip(formats, runs, sr):
                    data_pts.append({
                        "Player": player,
                        "Format": fmt,
                        "Runs": r,
                        "Strike Rate": s
                    })
            scatter_df = pd.DataFrame(data_pts)
            if scatter_df[['Runs', 'Strike Rate']].sum().sum() == 0:
                st.warning("Not enough data for scatter plot.")
            else:
                fig = px.scatter(scatter_df, x="Runs", y="Strike Rate",
                                 color="Player", text="Format",
                                 title="Runs vs Strike Rate Comparison",
                                 labels={"Runs": "Runs", "Strike Rate": "Strike Rate"})
                fig.update_traces(textposition='top center')
                st.plotly_chart(fig, use_container_width=True)

            st.subheader("πŸ† Milestone Comparison")
            milestone_data = []
            for player, pdata in [(p1_name, p1_data), (p2_name, p2_data)]:
                for fmt in formats:
                    milestone_data.append({
                        "Player": player,
                        "Format": fmt,
                        "50s": pdata.get(f"batting_50s_{fmt}", 0),
                        "100s": pdata.get(f"batting_100s_{fmt}", 0),
                        "200s": pdata.get(f"batting_200s_{fmt}", 0)
                    })
            ms_df = pd.DataFrame(milestone_data)
            for stat in ["50s", "100s", "200s"]:
                fig = px.bar(ms_df, x="Format", y=stat,
                             color="Player", barmode="group",
                             title=f"{stat} Achievements Comparison")
                st.plotly_chart(fig, use_container_width=True)

            st.subheader("🧩 Matches Distribution by Format")
            for player, pdata in [(p1_name, p1_data), (p2_name, p2_data)]:
                match_counts = [pdata.get(f"Matches_{fmt}", 0) for fmt in formats]
                if sum(match_counts) == 0:
                    st.warning(f"No match data for {player}.")
                    continue
                fig = px.pie(values=match_counts, names=formats,
                             title=f"{player} – Matches Distribution")
                st.plotly_chart(fig, use_container_width=True)

            st.subheader("πŸ“‰ Final Trends Overview")
            fig, axes = plt.subplots(1, 2, figsize=(14, 5))
            sns.set_theme(style="whitegrid")

            sns.barplot(x=formats,
                        y=[p1_data.get(f"batting_Runs_{fmt}", 0) for fmt in formats],
                        ax=axes[0], label=p1_name, color="b", alpha=0.6)
            sns.barplot(x=formats,
                        y=[p2_data.get(f"batting_Runs_{fmt}", 0) for fmt in formats],
                        ax=axes[0], label=p2_name, color="r", alpha=0.6)
            axes[0].set_title("Batting Runs Trend")
            axes[0].legend()

            sns.barplot(x=formats,
                        y=[p1_data.get(f"bowling_Test_Wickets", 0) for fmt in formats],
                        ax=axes[1], label=p1_name, color="b", alpha=0.6)
            sns.barplot(x=formats,
                        y=[p2_data.get(f"bowling_Test_Wickets", 0) for fmt in formats],
                        ax=axes[1], label=p2_name, color="r", alpha=0.6)
            axes[1].set_title("Bowling Wickets Trend")
            axes[1].legend()

            st.pyplot(fig)