Mpavan45 commited on
Commit
4496d77
Β·
verified Β·
1 Parent(s): 3f6853f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -75
app.py CHANGED
@@ -140,38 +140,18 @@ from langchain_core.output_parsers import StrOutputParser
140
  # Set page config
141
  st.set_page_config(page_title="🏏 Ultimate Cricket Analytics", layout="wide")
142
 
143
- # Ensure data folder exists
144
- DATA_DIR = "data"
145
- os.makedirs(DATA_DIR, exist_ok=True)
146
-
147
- # Helper function to load Excel file with error handling
148
- @st.cache_data
149
- def load_excel_file(filename):
150
- filepath = os.path.join(DATA_DIR, filename)
151
- try:
152
- df = pd.read_excel(filepath, engine='xlrd') # Use xlrd for .xls files
153
- return df
154
- except FileNotFoundError:
155
- st.error(f"File `{filename}` not found in `{DATA_DIR}/`. Please upload it.")
156
- return pd.DataFrame()
157
- except ValueError:
158
- st.error(f"File `{filename}` is not a valid Excel file. Please check the format.")
159
- return pd.DataFrame()
160
- except Exception as e:
161
- st.error(f"Error loading `{filename}`: {e}")
162
- return pd.DataFrame()
163
 
164
- # Load all three formats
165
- odi_df = load_excel_file("odi.xls")
166
- t20_df = load_excel_file("t20.xls")
167
- test_df = load_excel_file("test.xls")
168
 
169
- # Check if all loaded correctly
170
- if not odi_df.empty and not t20_df.empty and not test_df.empty:
171
- st.success("All Excel files loaded successfully!")
172
- else:
173
- st.warning("Please ensure all Excel files (odi.xls, t20.xls, test.xls) are present in `data/` folder.")
174
 
 
175
 
176
  # Load CSV files
177
  batting_df = pd.read_csv("Batting_10_Teams_Final.csv")
@@ -236,67 +216,76 @@ with tab1:
236
  st.plotly_chart(px.line(player_bowling, x='Format', y='Eco'))
237
  st.plotly_chart(px.pie(player_bowling, values='Wickets', names='Format'))
238
 
 
239
  # ---------------- TAB 2: Team Comparison ----------------
240
  # ---------------- TAB 2: Team Comparison ----------------
241
  with tab2:
242
  st.title("πŸ“Š Team Comparison")
243
 
244
- # Add Format column to all datasets
245
  try:
 
246
  odi_df['Format'] = 'ODI'
247
  t20_df['Format'] = 'T20'
248
  test_df['Format'] = 'Test'
249
 
 
250
  combined_df = pd.concat([odi_df, t20_df, test_df], ignore_index=True)
251
 
252
- # Check team column name
253
- team_column = 'Country' if 'Country' in combined_df.columns else 'Team' if 'Team' in combined_df.columns else None
254
-
255
- if team_column:
256
- # Select teams and stat
257
- selected_teams = st.multiselect("Select Teams to Compare (up to 3)", combined_df[team_column].unique(), max_selections=3)
258
-
259
- if selected_teams:
260
- selected_data = combined_df[combined_df[team_column].isin(selected_teams)]
261
-
262
- stat = st.selectbox("Select Stat to Compare", ['Matches', 'Win%', 'Runs', 'Wickets', 'SR', 'Eco'])
263
-
264
- # Bar chart
265
- st.subheader(f"{stat} Comparison")
266
- fig = px.bar(
267
- selected_data,
268
- x=team_column,
269
- y=stat,
270
- color='Format',
271
- barmode='group',
272
- title=f"{stat} by Team and Format"
273
- )
274
- st.plotly_chart(fig, use_container_width=True)
275
-
276
- # Optional: Show raw comparison data
277
- if st.checkbox("Show Data Table"):
278
- st.dataframe(selected_data.reset_index(drop=True))
279
-
280
- # GenAI Team Bio
281
- st.subheader("πŸ“˜ GenAI Team Summary")
282
-
283
- for team in selected_teams:
284
- st.markdown(f"#### {team}")
285
- team_prompt = ChatPromptTemplate.from_messages([
286
- ("system", "You are an AI cricket historian. Provide a brief yet detailed overview of the team."),
287
- ("human", "{team_name}")
288
- ])
289
- team_chain = team_prompt | model | out_par
290
- team_bio = team_chain.invoke({"team_name": team})
291
- st.write(team_bio)
292
- else:
293
- st.info("Please select at least one team to compare.")
 
 
 
 
 
 
 
 
294
  else:
295
- st.error("Could not find 'Team' or 'Country' column in the Excel data.")
296
 
297
  except Exception as e:
298
- st.error(f"Error processing data: {e}")
299
-
300
 
301
  # ---------------- TAB 3: Team GenAI Bio ----------------
302
  with tab3:
 
140
  # Set page config
141
  st.set_page_config(page_title="🏏 Ultimate Cricket Analytics", layout="wide")
142
 
143
+ # Create a folder to save CSVs if not exists
144
+ data_folder = "data"
145
+ os.makedirs(data_folder, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
+ #]
 
 
 
148
 
149
+ # Load all three formats
150
+ odi_df = pd.read_csv("odi.xls")
151
+ t20_df = pd.read_csv("t20.xls")
152
+ test_df = pd.read_csv("test.xls")
 
153
 
154
+ ]
155
 
156
  # Load CSV files
157
  batting_df = pd.read_csv("Batting_10_Teams_Final.csv")
 
216
  st.plotly_chart(px.line(player_bowling, x='Format', y='Eco'))
217
  st.plotly_chart(px.pie(player_bowling, values='Wickets', names='Format'))
218
 
219
+ # ---------------- TAB 2: Team Comparison ----------------
220
  # ---------------- TAB 2: Team Comparison ----------------
221
  # ---------------- TAB 2: Team Comparison ----------------
222
  with tab2:
223
  st.title("πŸ“Š Team Comparison")
224
 
 
225
  try:
226
+ # Tag the format for clarity
227
  odi_df['Format'] = 'ODI'
228
  t20_df['Format'] = 'T20'
229
  test_df['Format'] = 'Test'
230
 
231
+ # Combine all formats
232
  combined_df = pd.concat([odi_df, t20_df, test_df], ignore_index=True)
233
 
234
+ # Columns as per your data
235
+ team_column = 'Team'
236
+
237
+ # Team selection
238
+ selected_teams = st.multiselect("Select Teams to Compare (up to 3)", combined_df[team_column].unique(), max_selections=3)
239
+
240
+ if selected_teams:
241
+ selected_data = combined_df[combined_df[team_column].isin(selected_teams)]
242
+
243
+ # Map column names to user-friendly labels
244
+ stat_options = {
245
+ 'Mat': 'Matches',
246
+ 'Won': 'Wins',
247
+ 'Lost': 'Losses',
248
+ 'Draw': 'Draws',
249
+ 'Tied': 'Ties',
250
+ 'W/L': 'Win/Loss Ratio',
251
+ '%W': 'Win %',
252
+ '%L': 'Loss %',
253
+ '%D': 'Draw %'
254
+ }
255
+
256
+ stat_choice = st.selectbox("Select Stat to Compare", list(stat_options.keys()), format_func=lambda x: stat_options[x])
257
+
258
+ st.subheader(f"{stat_options[stat_choice]} Comparison")
259
+
260
+ fig = px.bar(
261
+ selected_data,
262
+ x='Team',
263
+ y=stat_choice,
264
+ color='Format',
265
+ barmode='group',
266
+ title=f"{stat_options[stat_choice]} by Team and Format"
267
+ )
268
+ st.plotly_chart(fig, use_container_width=True)
269
+
270
+ if st.checkbox("Show Raw Team Data Table"):
271
+ st.dataframe(selected_data.reset_index(drop=True))
272
+
273
+ # GenAI Team Summary
274
+ st.subheader("πŸ“˜ GenAI Team Summary")
275
+ for team in selected_teams:
276
+ st.markdown(f"#### {team}")
277
+ team_prompt = ChatPromptTemplate.from_messages([
278
+ ("system", "You are an AI cricket historian. Provide a brief yet detailed overview of the team."),
279
+ ("human", "{team_name}")
280
+ ])
281
+ team_chain = team_prompt | model | out_par
282
+ bio = team_chain.invoke({"team_name": team})
283
+ st.write(bio)
284
  else:
285
+ st.info("Please select at least one team to compare.")
286
 
287
  except Exception as e:
288
+ st.error(f"Error loading team data: {e}")
 
289
 
290
  # ---------------- TAB 3: Team GenAI Bio ----------------
291
  with tab3: