Update src/streamlit_app.py
#2
by musk12 - opened
- src/streamlit_app.py +67 -48
src/streamlit_app.py
CHANGED
|
@@ -27,6 +27,23 @@ def get_api_key():
|
|
| 27 |
# 2. If that fails, look in the Hugging Face Environment variables
|
| 28 |
return os.environ.get("GROQ_API_KEY")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Now, use this variable throughout your app
|
| 31 |
GROQ_API_KEY = get_api_key()
|
| 32 |
|
|
@@ -86,12 +103,7 @@ def get_image_base64(fig):
|
|
| 86 |
return base64.b64encode(buf.getvalue()).decode('utf-8')
|
| 87 |
|
| 88 |
|
| 89 |
-
|
| 90 |
-
def render_chart_section(title, fig, key_id):
|
| 91 |
-
with st.container(border=True):
|
| 92 |
-
st.subheader(title)
|
| 93 |
-
st.pyplot(fig)
|
| 94 |
-
ai_explainer_ui(fig, key_id)
|
| 95 |
|
| 96 |
def ai_explainer_ui(fig, key_id):
|
| 97 |
state_key = f"explanation_{key_id}"
|
|
@@ -140,8 +152,7 @@ def ai_explainer_ui(fig, key_id):
|
|
| 140 |
except Exception as e:
|
| 141 |
st.error(f"Connection Error: {e}")
|
| 142 |
|
| 143 |
-
|
| 144 |
-
st.info(st.session_state[state_key])
|
| 145 |
|
| 146 |
st.title(f"π {analysis_mode}")
|
| 147 |
if analysis_mode == "Overview":
|
|
@@ -188,14 +199,17 @@ if analysis_mode == "Overview":
|
|
| 188 |
)
|
| 189 |
|
| 190 |
fig1, ax1 = plt.subplots(figsize=(8, 5))
|
| 191 |
-
ax1.plot(season_counts.index.astype(str), season_counts.values,
|
| 192 |
-
|
| 193 |
-
ax1.set_xlabel("Season", fontsize=11)
|
| 194 |
-
ax1.set_ylabel("Matches Played", fontsize=11)
|
| 195 |
-
ax1.tick_params(axis='x', rotation=45)
|
| 196 |
-
ax1.grid(axis='y', alpha=0.3)
|
| 197 |
-
fig1.tight_layout() # β instance method, not plt.tight_layout()
|
| 198 |
-
st.pyplot(fig1)
|
|
|
|
|
|
|
|
|
|
| 199 |
plt.close(fig1) # β free memory immediately
|
| 200 |
ai_explainer_ui(fig1, "overview_season")
|
| 201 |
st.divider()
|
|
@@ -211,18 +225,20 @@ if analysis_mode == "Overview":
|
|
| 211 |
)
|
| 212 |
|
| 213 |
fig2, ax2 = plt.subplots(figsize=(8, 5)) # β plt.subplots, NOT Figure()
|
| 214 |
-
colors = plt.cm.viridis_r(
|
| 215 |
-
|
| 216 |
-
)
|
| 217 |
-
ax2.barh(team_runs.index[::-1], team_runs.values[::-1], color=colors[::1])
|
| 218 |
-
ax2.set_xlabel("Total Runs", fontsize=11)
|
| 219 |
-
ax2.set_ylabel("Team", fontsize=11)
|
| 220 |
-
ax2.xaxis.set_major_formatter(
|
| 221 |
-
|
| 222 |
-
)
|
| 223 |
-
ax2.grid(axis='x', alpha=0.3)
|
| 224 |
-
fig2.tight_layout()
|
| 225 |
-
st.pyplot(fig2)
|
|
|
|
|
|
|
| 226 |
plt.close(fig2)
|
| 227 |
ai_explainer_ui(fig2, "overview_teams")
|
| 228 |
|
|
@@ -312,19 +328,20 @@ elif analysis_mode == "IPL Team Analysis (2008-2025)":
|
|
| 312 |
|
| 313 |
|
| 314 |
for i, (title, func) in enumerate(plots):
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
| 320 |
|
| 321 |
-
|
| 322 |
-
|
| 323 |
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
with col2:
|
| 327 |
-
|
| 328 |
st.divider()
|
| 329 |
|
| 330 |
elif analysis_mode == "Ball-by-Ball Analysis":
|
|
@@ -367,13 +384,15 @@ elif analysis_mode == "Ball-by-Ball Analysis":
|
|
| 367 |
]
|
| 368 |
|
| 369 |
for i, (title, func) in enumerate(bb_plots):
|
| 370 |
-
st.subheader(f"{i+1}. {title}")
|
| 371 |
-
col1, col2 = st.columns([2, 1])
|
| 372 |
-
with col1:
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
with col2:
|
| 378 |
-
|
|
|
|
|
|
|
| 379 |
st.divider()
|
|
|
|
| 27 |
# 2. If that fails, look in the Hugging Face Environment variables
|
| 28 |
return os.environ.get("GROQ_API_KEY")
|
| 29 |
|
| 30 |
+
|
| 31 |
+
@st.fragment
|
| 32 |
+
def render_chart_section(title, func, key_id):
|
| 33 |
+
with st.container(border=True):
|
| 34 |
+
st.subheader(title)
|
| 35 |
+
col1, col2 = st.columns([2, 1])
|
| 36 |
+
with col1:
|
| 37 |
+
fig, ax = plt.subplots()
|
| 38 |
+
func() # Ye plotting function execute karega
|
| 39 |
+
if any(x in title for x in ["Venue", "Season", "City"]):
|
| 40 |
+
plt.xticks(rotation=45)
|
| 41 |
+
st.pyplot(fig, use_container_width=True) # Container width se jumping rukegi
|
| 42 |
+
with col2:
|
| 43 |
+
ai_explainer_ui(fig, key_id)
|
| 44 |
+
plt.close(fig)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
# Now, use this variable throughout your app
|
| 48 |
GROQ_API_KEY = get_api_key()
|
| 49 |
|
|
|
|
| 103 |
return base64.b64encode(buf.getvalue()).decode('utf-8')
|
| 104 |
|
| 105 |
|
| 106 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
def ai_explainer_ui(fig, key_id):
|
| 109 |
state_key = f"explanation_{key_id}"
|
|
|
|
| 152 |
except Exception as e:
|
| 153 |
st.error(f"Connection Error: {e}")
|
| 154 |
|
| 155 |
+
|
|
|
|
| 156 |
|
| 157 |
st.title(f"π {analysis_mode}")
|
| 158 |
if analysis_mode == "Overview":
|
|
|
|
| 199 |
)
|
| 200 |
|
| 201 |
fig1, ax1 = plt.subplots(figsize=(8, 5))
|
| 202 |
+
# ax1.plot(season_counts.index.astype(str), season_counts.values,
|
| 203 |
+
# marker='o', color='#1f77b4', linewidth=2)
|
| 204 |
+
# ax1.set_xlabel("Season", fontsize=11)
|
| 205 |
+
# ax1.set_ylabel("Matches Played", fontsize=11)
|
| 206 |
+
# ax1.tick_params(axis='x', rotation=45)
|
| 207 |
+
# ax1.grid(axis='y', alpha=0.3)
|
| 208 |
+
# fig1.tight_layout() # β instance method, not plt.tight_layout()
|
| 209 |
+
# st.pyplot(fig1)
|
| 210 |
+
|
| 211 |
+
render_chart_section("Matches per Season", fig1, "overview_season")
|
| 212 |
+
|
| 213 |
plt.close(fig1) # β free memory immediately
|
| 214 |
ai_explainer_ui(fig1, "overview_season")
|
| 215 |
st.divider()
|
|
|
|
| 225 |
)
|
| 226 |
|
| 227 |
fig2, ax2 = plt.subplots(figsize=(8, 5)) # β plt.subplots, NOT Figure()
|
| 228 |
+
# colors = plt.cm.viridis_r(
|
| 229 |
+
# [i / max(len(team_runs) - 1, 1) for i in range(len(team_runs))]
|
| 230 |
+
# )
|
| 231 |
+
# ax2.barh(team_runs.index[::-1], team_runs.values[::-1], color=colors[::1])
|
| 232 |
+
# ax2.set_xlabel("Total Runs", fontsize=11)
|
| 233 |
+
# ax2.set_ylabel("Team", fontsize=11)
|
| 234 |
+
# ax2.xaxis.set_major_formatter(
|
| 235 |
+
# plt.FuncFormatter(lambda x, _: f"{int(x/1000)}k" if x >= 1000 else str(int(x)))
|
| 236 |
+
# )
|
| 237 |
+
# ax2.grid(axis='x', alpha=0.3)
|
| 238 |
+
# fig2.tight_layout()
|
| 239 |
+
# st.pyplot(fig2)
|
| 240 |
+
|
| 241 |
+
render_chart_section("Top 10 Run Scoring Teams", fig2, "overview_teams")
|
| 242 |
plt.close(fig2)
|
| 243 |
ai_explainer_ui(fig2, "overview_teams")
|
| 244 |
|
|
|
|
| 328 |
|
| 329 |
|
| 330 |
for i, (title, func) in enumerate(plots):
|
| 331 |
+
render_chart_section(f"{i+1}. {title}", func, f"team_btn_{i}")
|
| 332 |
+
# st.subheader(f"{i+1}. {title}")
|
| 333 |
+
# col1, col2 = st.columns([2, 1])
|
| 334 |
+
# with col1:
|
| 335 |
+
# fig, ax = plt.subplots()
|
| 336 |
+
# func()
|
| 337 |
|
| 338 |
+
# if "Venue" in title or "Season" in title or "City" in title:
|
| 339 |
+
# plt.xticks(rotation=45)
|
| 340 |
|
| 341 |
+
# st.pyplot(fig)
|
| 342 |
+
# plt.close(fig)
|
| 343 |
+
# with col2:
|
| 344 |
+
# ai_explainer_ui(fig, f"team_btn_{i}")
|
| 345 |
st.divider()
|
| 346 |
|
| 347 |
elif analysis_mode == "Ball-by-Ball Analysis":
|
|
|
|
| 384 |
]
|
| 385 |
|
| 386 |
for i, (title, func) in enumerate(bb_plots):
|
| 387 |
+
# st.subheader(f"{i+1}. {title}")
|
| 388 |
+
# col1, col2 = st.columns([2, 1])
|
| 389 |
+
# with col1:
|
| 390 |
+
# fig, ax = plt.subplots()
|
| 391 |
+
# func()
|
| 392 |
+
# st.pyplot(fig)
|
| 393 |
+
# plt.close(fig)
|
| 394 |
+
# with col2:
|
| 395 |
+
# ai_explainer_ui(fig, f"bb_btn_{i}")
|
| 396 |
+
|
| 397 |
+
render_chart_section(f"{i+1}. {title}", func, f"bb_btn_{i}")
|
| 398 |
st.divider()
|