Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 144 |
-
|
| 145 |
-
os.makedirs(
|
| 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 |
-
#
|
| 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 |
-
#
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 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 |
-
#
|
| 253 |
-
team_column = '
|
| 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 |
else:
|
| 295 |
-
st.
|
| 296 |
|
| 297 |
except Exception as e:
|
| 298 |
-
st.error(f"Error
|
| 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:
|