Mpavan45 commited on
Commit
18f8087
Β·
verified Β·
1 Parent(s): ae8dacc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +225 -122
app.py CHANGED
@@ -1,3 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
@@ -6,125 +137,97 @@ from langchain_google_genai import GoogleGenerativeAI
6
  from langchain_core.prompts import ChatPromptTemplate
7
  from langchain_core.output_parsers import StrOutputParser
8
 
9
- # Streamlit App
10
- st.set_page_config(page_title="Cricket Player Stats Dashboard", layout="wide")
11
-
12
- # Create a folder to save CSVs if not exists
13
- data_folder = "data"
14
- os.makedirs(data_folder, exist_ok=True)
15
-
16
- # File paths
17
- batting_path = "Batting_10_Teams_Final.csv"
18
- bowling_path = "Bowling_10_Teams_Final.csv"
19
-
20
- # Load saved CSVs
21
- batting_df = pd.read_csv(batting_path)
22
- bowling_df = pd.read_csv(bowling_path)
23
-
24
- # Extract unique teams and players
25
- teams = sorted(set(batting_df['Country'].unique()).union(bowling_df['Country'].unique()))
26
- team = st.sidebar.selectbox("Select Team", teams)
27
-
28
- # Filter players by team
29
- batting_players = batting_df[batting_df['Country'] == team]['player_name'].unique()
30
- bowling_players = bowling_df[bowling_df['Country'] == team]['player_name'].unique()
31
- players = sorted(set(batting_players).union(bowling_players))
32
- player = st.sidebar.selectbox("Select Player", players)
33
-
34
- # Filter data for the selected player
35
- player_batting = batting_df[(batting_df['player_name'] == player) & (batting_df['Country'] == team)]
36
- player_bowling = bowling_df[(bowling_df['player_name'] == player) & (bowling_df['Country'] == team)]
37
-
38
- st.title(f"{player}")
39
-
40
- # βœ… Integrating LLMs using Gen-AI
41
-
42
- # Loading API key and creating model
43
- api_key = st.secrets.get('genai_key')
44
- model = GoogleGenerativeAI(model="gemini-1.5-pro", google_api_key=api_key)
45
-
46
- # Creating output parser to generate output
47
- out_par = StrOutputParser()
48
-
49
- # Creating prompt template
50
- prompt = ChatPromptTemplate.from_messages([
51
- ("system", '''You are an AI cricket player information provider. Display the player's complete bio data in a
52
- detailed table format with rows and columns, including personal information. Below the table,
53
- include debut details for all formats. Additionally, provide a brief description of the player
54
- underneath. Only include player information, not their performance statistics.'''),
55
- ("human", "{player_name}")
56
- ])
57
-
58
- # Creating chain
59
- chain = prompt | model | out_par
60
-
61
- # Query GenAI with the player's name
62
- response = chain.invoke({"player_name": player})
63
- st.write(response)
64
-
65
- # Buttons for Batting and Bowling Cards with Toggle Feature
66
- col_card1, col_card2 = st.columns(2)
67
- if "batting_card" not in st.session_state:
68
- st.session_state["batting_card"] = False
69
- if "bowling_card" not in st.session_state:
70
- st.session_state["bowling_card"] = False
71
-
72
- with col_card1:
73
- if st.button("Batting Card"):
74
- st.session_state["batting_card"] = not st.session_state["batting_card"]
75
-
76
- if st.session_state["batting_card"]:
77
- if len(player_batting) > 0:
78
- st.write("### Batting Data")
79
- st.dataframe(player_batting.iloc[:, :16]) # Display first 16 columns
80
- else:
81
- st.write("No batting data available.")
82
-
83
- with col_card2:
84
- if st.button("Bowling Card"):
85
- st.session_state["bowling_card"] = not st.session_state["bowling_card"]
86
-
87
- if st.session_state["bowling_card"]:
88
- if len(player_bowling) > 0:
89
- st.write("### Bowling Data")
90
- st.dataframe(player_bowling.iloc[:, :15]) # Display first 15 columns
91
- else:
92
- st.write("No bowling data available.")
93
-
94
-
95
-
96
-
97
- # Batting Visualization
98
- if not player_batting.empty:
99
- st.subheader("Batting Stats")
100
- col1, col2 = st.columns(2)
101
- with col1:
102
- fig_bat_runs = px.bar(player_batting, x='Format', y='Runs', color='Format', title='Runs by Format')
103
- st.plotly_chart(fig_bat_runs, use_container_width=True)
104
- with col2:
105
- fig_bat_sr = px.line(player_batting, x='Format', y='SR', title='Strike Rate by Format')
106
- st.plotly_chart(fig_bat_sr, use_container_width=True)
107
-
108
- # Batting Pie Chart
109
- fig_bat_pie = px.pie(player_batting, values='Runs', names='Format',
110
- title='Runs Distribution Across Formats',
111
- color_discrete_map={"ODI": "red", "T20": "green", "Test": "blue"})
112
- st.plotly_chart(fig_bat_pie, use_container_width=True)
113
-
114
- # Bowling Visualization
115
- if not player_bowling.empty:
116
- st.subheader("Bowling Stats")
117
- col3, col4 = st.columns(2)
118
- with col3:
119
- fig_bowl_wickets = px.bar(player_bowling, x='Format', y='Wickets', color='Format', title='Wickets by Format')
120
- st.plotly_chart(fig_bowl_wickets, use_container_width=True)
121
- with col4:
122
- fig_bowl_eco = px.line(player_bowling, x='Format', y='Eco', title='Economy Rate by Format')
123
- st.plotly_chart(fig_bowl_eco, use_container_width=True)
124
-
125
- # Bowling Pie Chart
126
- fig_bowl_pie = px.pie(player_bowling, values='Wickets', names='Format',
127
- title='Wickets Distribution Across Formats',
128
- color_discrete_map={"ODI": "yellow", "T20": "purple", "Test": "brown"})
129
- st.plotly_chart(fig_bowl_pie, use_container_width=True)
130
-
 
1
+ # import streamlit as st
2
+ # import pandas as pd
3
+ # import plotly.express as px
4
+ # import os
5
+ # from langchain_google_genai import GoogleGenerativeAI
6
+ # from langchain_core.prompts import ChatPromptTemplate
7
+ # from langchain_core.output_parsers import StrOutputParser
8
+
9
+ # # Streamlit App
10
+ # st.set_page_config(page_title="Cricket Player Stats Dashboard", layout="wide")
11
+
12
+ # # Create a folder to save CSVs if not exists
13
+ # data_folder = "data"
14
+ # os.makedirs(data_folder, exist_ok=True)
15
+
16
+ # # File paths
17
+ # batting_path = "Batting_10_Teams_Final.csv"
18
+ # bowling_path = "Bowling_10_Teams_Final.csv"
19
+
20
+ # # Load saved CSVs
21
+ # batting_df = pd.read_csv(batting_path)
22
+ # bowling_df = pd.read_csv(bowling_path)
23
+
24
+ # # Extract unique teams and players
25
+ # teams = sorted(set(batting_df['Country'].unique()).union(bowling_df['Country'].unique()))
26
+ # team = st.sidebar.selectbox("Select Team", teams)
27
+
28
+ # # Filter players by team
29
+ # batting_players = batting_df[batting_df['Country'] == team]['player_name'].unique()
30
+ # bowling_players = bowling_df[bowling_df['Country'] == team]['player_name'].unique()
31
+ # players = sorted(set(batting_players).union(bowling_players))
32
+ # player = st.sidebar.selectbox("Select Player", players)
33
+
34
+ # # Filter data for the selected player
35
+ # player_batting = batting_df[(batting_df['player_name'] == player) & (batting_df['Country'] == team)]
36
+ # player_bowling = bowling_df[(bowling_df['player_name'] == player) & (bowling_df['Country'] == team)]
37
+
38
+ # st.title(f"{player}")
39
+
40
+ # # βœ… Integrating LLMs using Gen-AI
41
+
42
+ # # Loading API key and creating model
43
+ # api_key = st.secrets.get('genai_key')
44
+ # model = GoogleGenerativeAI(model="gemini-1.5-pro", google_api_key=api_key)
45
+
46
+ # # Creating output parser to generate output
47
+ # out_par = StrOutputParser()
48
+
49
+ # # Creating prompt template
50
+ # prompt = ChatPromptTemplate.from_messages([
51
+ # ("system", '''You are an AI cricket player information provider. Display the player's complete bio data in a
52
+ # detailed table format with rows and columns, including personal information. Below the table,
53
+ # include debut details for all formats. Additionally, provide a brief description of the player
54
+ # underneath. Only include player information, not their performance statistics.'''),
55
+ # ("human", "{player_name}")
56
+ # ])
57
+
58
+ # # Creating chain
59
+ # chain = prompt | model | out_par
60
+
61
+ # # Query GenAI with the player's name
62
+ # response = chain.invoke({"player_name": player})
63
+ # st.write(response)
64
+
65
+ # # Buttons for Batting and Bowling Cards with Toggle Feature
66
+ # col_card1, col_card2 = st.columns(2)
67
+ # if "batting_card" not in st.session_state:
68
+ # st.session_state["batting_card"] = False
69
+ # if "bowling_card" not in st.session_state:
70
+ # st.session_state["bowling_card"] = False
71
+
72
+ # with col_card1:
73
+ # if st.button("Batting Card"):
74
+ # st.session_state["batting_card"] = not st.session_state["batting_card"]
75
+
76
+ # if st.session_state["batting_card"]:
77
+ # if len(player_batting) > 0:
78
+ # st.write("### Batting Data")
79
+ # st.dataframe(player_batting.iloc[:, :16]) # Display first 16 columns
80
+ # else:
81
+ # st.write("No batting data available.")
82
+
83
+ # with col_card2:
84
+ # if st.button("Bowling Card"):
85
+ # st.session_state["bowling_card"] = not st.session_state["bowling_card"]
86
+
87
+ # if st.session_state["bowling_card"]:
88
+ # if len(player_bowling) > 0:
89
+ # st.write("### Bowling Data")
90
+ # st.dataframe(player_bowling.iloc[:, :15]) # Display first 15 columns
91
+ # else:
92
+ # st.write("No bowling data available.")
93
+
94
+
95
+
96
+
97
+ # # Batting Visualization
98
+ # if not player_batting.empty:
99
+ # st.subheader("Batting Stats")
100
+ # col1, col2 = st.columns(2)
101
+ # with col1:
102
+ # fig_bat_runs = px.bar(player_batting, x='Format', y='Runs', color='Format', title='Runs by Format')
103
+ # st.plotly_chart(fig_bat_runs, use_container_width=True)
104
+ # with col2:
105
+ # fig_bat_sr = px.line(player_batting, x='Format', y='SR', title='Strike Rate by Format')
106
+ # st.plotly_chart(fig_bat_sr, use_container_width=True)
107
+
108
+ # # Batting Pie Chart
109
+ # fig_bat_pie = px.pie(player_batting, values='Runs', names='Format',
110
+ # title='Runs Distribution Across Formats',
111
+ # color_discrete_map={"ODI": "red", "T20": "green", "Test": "blue"})
112
+ # st.plotly_chart(fig_bat_pie, use_container_width=True)
113
+
114
+ # # Bowling Visualization
115
+ # if not player_bowling.empty:
116
+ # st.subheader("Bowling Stats")
117
+ # col3, col4 = st.columns(2)
118
+ # with col3:
119
+ # fig_bowl_wickets = px.bar(player_bowling, x='Format', y='Wickets', color='Format', title='Wickets by Format')
120
+ # st.plotly_chart(fig_bowl_wickets, use_container_width=True)
121
+ # with col4:
122
+ # fig_bowl_eco = px.line(player_bowling, x='Format', y='Eco', title='Economy Rate by Format')
123
+ # st.plotly_chart(fig_bowl_eco, use_container_width=True)
124
+
125
+ # # Bowling Pie Chart
126
+ # fig_bowl_pie = px.pie(player_bowling, values='Wickets', names='Format',
127
+ # title='Wickets Distribution Across Formats',
128
+ # color_discrete_map={"ODI": "yellow", "T20": "purple", "Test": "brown"})
129
+ # st.plotly_chart(fig_bowl_pie, use_container_width=True)
130
+
131
+
132
  import streamlit as st
133
  import pandas as pd
134
  import plotly.express as px
 
137
  from langchain_core.prompts import ChatPromptTemplate
138
  from langchain_core.output_parsers import StrOutputParser
139
 
140
+ # Set page config
141
+ st.set_page_config(page_title="🏏 Ultimate Cricket Analytics", layout="wide")
142
+
143
+ # Load CSV files
144
+ batting_df = pd.read_csv("Batting_10_Teams_Final.csv")
145
+ bowling_df = pd.read_csv("Bowling_10_Teams_Final.csv")
146
+ odi_df = pd.read_excel("odi.xls")
147
+ t20_df = pd.read_excel("t20.xls")
148
+ test_df = pd.read_excel("test.xls")
149
+
150
+ # Tabs for navigation
151
+ tab1, tab2, tab3 = st.tabs(["🏏 Player Dashboard", "πŸ“Š Team Comparison", "πŸ“š GenAI Team Bio"])
152
+
153
+ # ---------------- TAB 1: Player Dashboard ----------------
154
+ with tab1:
155
+ st.title("🏏 Player Dashboard")
156
+ teams = sorted(set(batting_df['Country'].unique()).union(bowling_df['Country'].unique()))
157
+ team = st.sidebar.selectbox("Select Team", teams)
158
+
159
+ batting_players = batting_df[batting_df['Country'] == team]['player_name'].unique()
160
+ bowling_players = bowling_df[bowling_df['Country'] == team]['player_name'].unique()
161
+ players = sorted(set(batting_players).union(bowling_players))
162
+ player = st.sidebar.selectbox("Select Player", players)
163
+
164
+ player_batting = batting_df[(batting_df['player_name'] == player) & (batting_df['Country'] == team)]
165
+ player_bowling = bowling_df[(bowling_df['player_name'] == player) & (bowling_df['Country'] == team)]
166
+
167
+ st.header(player)
168
+
169
+ # GenAI Player Info
170
+ api_key = st.secrets.get('genai_key')
171
+ model = GoogleGenerativeAI(model="gemini-1.5-pro", google_api_key=api_key)
172
+ out_par = StrOutputParser()
173
+ prompt = ChatPromptTemplate.from_messages([
174
+ ("system", '''You are an AI cricket player information provider. Display the player's complete bio data in a
175
+ detailed table format with rows and columns, including personal information. Below the table,
176
+ include debut details for all formats. Additionally, provide a brief description of the player
177
+ underneath. Only include player information, not their performance statistics.'''),
178
+ ("human", "{player_name}")
179
+ ])
180
+ chain = prompt | model | out_par
181
+ st.write(chain.invoke({"player_name": player}))
182
+
183
+ # Toggle Cards
184
+ if st.button("Show Batting Card"):
185
+ st.dataframe(player_batting.iloc[:, :16])
186
+ if st.button("Show Bowling Card"):
187
+ st.dataframe(player_bowling.iloc[:, :15])
188
+
189
+ # Visuals
190
+ if not player_batting.empty:
191
+ st.subheader("Batting Visualizations")
192
+ col1, col2 = st.columns(2)
193
+ with col1:
194
+ st.plotly_chart(px.bar(player_batting, x='Format', y='Runs', color='Format'))
195
+ with col2:
196
+ st.plotly_chart(px.line(player_batting, x='Format', y='SR'))
197
+ st.plotly_chart(px.pie(player_batting, values='Runs', names='Format'))
198
+
199
+ if not player_bowling.empty:
200
+ st.subheader("Bowling Visualizations")
201
+ col3, col4 = st.columns(2)
202
+ with col3:
203
+ st.plotly_chart(px.bar(player_bowling, x='Format', y='Wickets', color='Format'))
204
+ with col4:
205
+ st.plotly_chart(px.line(player_bowling, x='Format', y='Eco'))
206
+ st.plotly_chart(px.pie(player_bowling, values='Wickets', names='Format'))
207
+
208
+ # ---------------- TAB 2: Team Comparison ----------------
209
+ with tab2:
210
+ st.title("πŸ“Š Team Comparison")
211
+ format_option = st.selectbox("Choose Format", ["ODI", "T20", "Test"])
212
+ data = odi_df if format_option == "ODI" else t20_df if format_option == "T20" else test_df
213
+
214
+ selected_teams = st.multiselect("Select Teams to Compare (up to 3)", data['Team'].unique(), max_selections=3)
215
+ if selected_teams:
216
+ comparison_df = data[data['Team'].isin(selected_teams)]
217
+ st.dataframe(comparison_df.reset_index(drop=True))
218
+
219
+ # Example visuals
220
+ st.plotly_chart(px.bar(comparison_df, x='Team', y='Matches', color='Team', title="Matches Played"))
221
+ st.plotly_chart(px.bar(comparison_df, x='Team', y='Win%', color='Team', title="Win Percentage"))
222
+
223
+ # ---------------- TAB 3: Team GenAI Bio ----------------
224
+ with tab3:
225
+ st.title("πŸ“š Team GenAI Bio")
226
+ team_input = st.text_input("Enter Team Name (e.g., India, Australia)")
227
+ if team_input:
228
+ team_prompt = ChatPromptTemplate.from_messages([
229
+ ("system", "You are an AI cricket historian. Provide a brief yet detailed overview of the team."),
230
+ ("human", "{team_name}")
231
+ ])
232
+ team_chain = team_prompt | model | out_par
233
+ st.write(team_chain.invoke({"team_name": team_input}))