Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,169 +1,170 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
import numpy as np
|
4 |
-
import datetime
|
5 |
-
import os
|
6 |
-
import matplotlib.pyplot as plt
|
7 |
-
from tensorflow.keras.models import load_model
|
8 |
-
import joblib
|
9 |
-
|
10 |
-
# ===============================
|
11 |
-
# Sidebar Navigation
|
12 |
-
# ===============================
|
13 |
-
st.set_page_config(page_title="π IPL Unified Predictor App", layout="wide")
|
14 |
-
selected_tab = st.sidebar.selectbox("π Select Module", [
|
15 |
-
"Dashboard",
|
16 |
-
"Commentary",
|
17 |
-
"Scenario Simulator",
|
18 |
-
"Match Simulator",
|
19 |
-
"Live Match Predictor"
|
20 |
-
])
|
21 |
-
|
22 |
-
# ===============================
|
23 |
-
# 1. Dashboard Intro
|
24 |
-
# ===============================
|
25 |
-
if selected_tab == "Dashboard":
|
26 |
-
st.title("π IPL Unified Prediction & Simulation Dashboard")
|
27 |
-
st.markdown("""
|
28 |
-
Welcome to the complete IPL analytics suite. This unified app supports:
|
29 |
-
- π― Score Prediction using GRU
|
30 |
-
- π§ GPT-3.5 Commentary
|
31 |
-
- π² Match Scenario Simulator
|
32 |
-
- π Real-time Momentum & Points Table
|
33 |
-
- π§ͺ Live Match Score Prediction
|
34 |
-
|
35 |
-
Use the sidebar to navigate through modules.
|
36 |
-
""")
|
37 |
-
st.image("match_momentum_dashboard.gif", use_column_width=True)
|
38 |
-
|
39 |
-
# ===============================
|
40 |
-
# 2. Commentary Dashboard
|
41 |
-
# ===============================
|
42 |
-
elif selected_tab == "Commentary":
|
43 |
-
st.title("π§ Score Prediction & GPT Commentary")
|
44 |
-
try:
|
45 |
-
df = pd.read_csv("gru_match_simulation_commentary.csv")
|
46 |
-
st.dataframe(df)
|
47 |
-
st.markdown("""
|
48 |
-
**Commentary Summary:**
|
49 |
-
> The team started steadily, losing early wickets in the powerplay. However, they built partnerships during middle overs and accelerated towards the end with a flurry of boundaries.
|
50 |
-
""")
|
51 |
-
st.image("gru_match_simulation_plot.gif", caption="Score + Commentary Summary")
|
52 |
-
except:
|
53 |
-
st.warning("Upload commentary file or check dataset path.")
|
54 |
-
|
55 |
-
# ===============================
|
56 |
-
# 3. Scenario Simulator
|
57 |
-
# ===============================
|
58 |
-
elif selected_tab == "Scenario Simulator":
|
59 |
-
st.title("π― Match Scenario Score Predictor")
|
60 |
-
st.markdown("Enter cumulative runs over 20 overs to predict the final score.")
|
61 |
-
|
62 |
-
current_over = st.slider("Select Overs Completed", 1, 20, 10)
|
63 |
-
runs_input = []
|
64 |
-
|
65 |
-
for i in range(current_over):
|
66 |
-
run = st.number_input(f"Runs after Over {i+1}", min_value=0, step=1, key=f"r_{i}")
|
67 |
-
runs_input.append(run)
|
68 |
-
|
69 |
-
if st.button("Predict Score"):
|
70 |
-
if len(runs_input) == current_over:
|
71 |
-
padded = runs_input + [0]*(20 - len(runs_input))
|
72 |
-
arr = np.array(padded).reshape(-1, 1)
|
73 |
-
model = load_model("gru_score_predictor.keras", compile=False)
|
74 |
-
scaler_input = joblib.load("scaler_input.save")
|
75 |
-
scaler_output = joblib.load("scaler_output.save")
|
76 |
-
|
77 |
-
scaled = scaler_input.transform(arr).reshape(1, 20, 1)
|
78 |
-
pred = model.predict(scaled)
|
79 |
-
predicted_score = scaler_output.inverse_transform(pred)[0][0]
|
80 |
-
|
81 |
-
st.success(f"π Predicted Final Score: {predicted_score:.2f} runs")
|
82 |
-
|
83 |
-
# ===============================
|
84 |
-
# 4. Match Simulator + Points Table
|
85 |
-
# ===============================
|
86 |
-
elif selected_tab == "Match Simulator":
|
87 |
-
st.title("ποΈ IPL Match Simulator + Points Table")
|
88 |
-
teams = ["CSK", "MI", "RCB", "GT", "RR", "LSG", "KKR", "SRH"]
|
89 |
-
match_results = []
|
90 |
-
points = {team: 0 for team in teams}
|
91 |
-
|
92 |
-
for i in range(10):
|
93 |
-
t1, t2 = np.random.choice(teams, 2, replace=False)
|
94 |
-
winner = np.random.choice([t1, t2])
|
95 |
-
points[winner] += 2
|
96 |
-
match_results.append((t1, t2, winner))
|
97 |
-
|
98 |
-
df_matches = pd.DataFrame(match_results, columns=["Team A", "Team B", "Winner"])
|
99 |
-
df_points = pd.DataFrame(sorted(points.items(), key=lambda x: x[1], reverse=True), columns=["Team", "Points"])
|
100 |
-
|
101 |
-
st.subheader("π Simulated Match Results")
|
102 |
-
st.dataframe(df_matches)
|
103 |
-
|
104 |
-
st.subheader("π Points Table")
|
105 |
-
st.dataframe(df_points)
|
106 |
-
|
107 |
-
# ===============================
|
108 |
-
# 5. Live Match Predictor (Runs + Wickets)
|
109 |
-
# ===============================
|
110 |
-
elif selected_tab == "Live Match Predictor":
|
111 |
-
st.title("π‘ IPL Live Match Predictor")
|
112 |
-
|
113 |
-
col1, col2, col3 = st.columns(3)
|
114 |
-
with col1:
|
115 |
-
team_a = st.selectbox("Team A", ["MI", "RCB", "CSK", "GT"])
|
116 |
-
with col2:
|
117 |
-
team_b = st.selectbox("Team B", ["MI", "RCB", "CSK", "GT"])
|
118 |
-
with col3:
|
119 |
-
venue = st.selectbox("Venue", ["Wankhede", "Chinnaswamy", "Chepauk"])
|
120 |
-
|
121 |
-
date = st.date_input("Match Date", datetime.date.today())
|
122 |
-
status = st.radio("Match Status", ["In Progress", "Completed", "Scheduled"])
|
123 |
-
|
124 |
-
if st.button("Generate & Predict"):
|
125 |
-
runs = np.random.randint(0, 21, 20)
|
126 |
-
wickets = np.random.binomial(1, 0.3, 20)
|
127 |
-
|
128 |
-
df = pd.DataFrame({
|
129 |
-
"Over": list(range(1, 21)),
|
130 |
-
"Runs_This_Over": runs,
|
131 |
-
"Cumulative_Runs": np.cumsum(runs),
|
132 |
-
"Wickets_This_Over": wickets,
|
133 |
-
"Cumulative_Wickets": np.cumsum(wickets)
|
134 |
-
})
|
135 |
-
|
136 |
-
st.success(f"Match: {team_a} vs {team_b} at {venue} on {date}")
|
137 |
-
st.dataframe(df)
|
138 |
-
|
139 |
-
# Prediction logic
|
140 |
-
model = load_model("gru_score_predictor_rw.keras", compile=False)
|
141 |
-
scaler_input = joblib.load("
|
142 |
-
scaler_output = joblib.load("
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
ax1.
|
155 |
-
ax1.
|
156 |
-
|
157 |
-
ax2
|
158 |
-
ax2.
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
#
|
165 |
-
#
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import datetime
|
5 |
+
import os
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
from tensorflow.keras.models import load_model
|
8 |
+
import joblib
|
9 |
+
|
10 |
+
# ===============================
|
11 |
+
# Sidebar Navigation
|
12 |
+
# ===============================
|
13 |
+
st.set_page_config(page_title="π IPL Unified Predictor App", layout="wide")
|
14 |
+
selected_tab = st.sidebar.selectbox("π Select Module", [
|
15 |
+
"Dashboard",
|
16 |
+
"Commentary",
|
17 |
+
"Scenario Simulator",
|
18 |
+
"Match Simulator",
|
19 |
+
"Live Match Predictor"
|
20 |
+
])
|
21 |
+
|
22 |
+
# ===============================
|
23 |
+
# 1. Dashboard Intro
|
24 |
+
# ===============================
|
25 |
+
if selected_tab == "Dashboard":
|
26 |
+
st.title("π IPL Unified Prediction & Simulation Dashboard")
|
27 |
+
st.markdown("""
|
28 |
+
Welcome to the complete IPL analytics suite. This unified app supports:
|
29 |
+
- π― Score Prediction using GRU
|
30 |
+
- π§ GPT-3.5 Commentary
|
31 |
+
- π² Match Scenario Simulator
|
32 |
+
- π Real-time Momentum & Points Table
|
33 |
+
- π§ͺ Live Match Score Prediction
|
34 |
+
|
35 |
+
Use the sidebar to navigate through modules.
|
36 |
+
""")
|
37 |
+
st.image("match_momentum_dashboard.gif", use_column_width=True)
|
38 |
+
|
39 |
+
# ===============================
|
40 |
+
# 2. Commentary Dashboard
|
41 |
+
# ===============================
|
42 |
+
elif selected_tab == "Commentary":
|
43 |
+
st.title("π§ Score Prediction & GPT Commentary")
|
44 |
+
try:
|
45 |
+
df = pd.read_csv("gru_match_simulation_commentary.csv")
|
46 |
+
st.dataframe(df)
|
47 |
+
st.markdown("""
|
48 |
+
**Commentary Summary:**
|
49 |
+
> The team started steadily, losing early wickets in the powerplay. However, they built partnerships during middle overs and accelerated towards the end with a flurry of boundaries.
|
50 |
+
""")
|
51 |
+
st.image("gru_match_simulation_plot.gif", caption="Score + Commentary Summary")
|
52 |
+
except:
|
53 |
+
st.warning("Upload commentary file or check dataset path.")
|
54 |
+
|
55 |
+
# ===============================
|
56 |
+
# 3. Scenario Simulator
|
57 |
+
# ===============================
|
58 |
+
elif selected_tab == "Scenario Simulator":
|
59 |
+
st.title("π― Match Scenario Score Predictor")
|
60 |
+
st.markdown("Enter cumulative runs over 20 overs to predict the final score.")
|
61 |
+
|
62 |
+
current_over = st.slider("Select Overs Completed", 1, 20, 10)
|
63 |
+
runs_input = []
|
64 |
+
|
65 |
+
for i in range(current_over):
|
66 |
+
run = st.number_input(f"Runs after Over {i+1}", min_value=0, step=1, key=f"r_{i}")
|
67 |
+
runs_input.append(run)
|
68 |
+
|
69 |
+
if st.button("Predict Score"):
|
70 |
+
if len(runs_input) == current_over:
|
71 |
+
padded = runs_input + [0]*(20 - len(runs_input))
|
72 |
+
arr = np.array(padded).reshape(-1, 1)
|
73 |
+
model = load_model("gru_score_predictor.keras", compile=False)
|
74 |
+
scaler_input = joblib.load("scaler_input.save")
|
75 |
+
scaler_output = joblib.load("scaler_output.save")
|
76 |
+
|
77 |
+
scaled = scaler_input.transform(arr).reshape(1, 20, 1)
|
78 |
+
pred = model.predict(scaled)
|
79 |
+
predicted_score = scaler_output.inverse_transform(pred)[0][0]
|
80 |
+
|
81 |
+
st.success(f"π Predicted Final Score: {predicted_score:.2f} runs")
|
82 |
+
|
83 |
+
# ===============================
|
84 |
+
# 4. Match Simulator + Points Table
|
85 |
+
# ===============================
|
86 |
+
elif selected_tab == "Match Simulator":
|
87 |
+
st.title("ποΈ IPL Match Simulator + Points Table")
|
88 |
+
teams = ["CSK", "MI", "RCB", "GT", "RR", "LSG", "KKR", "SRH"]
|
89 |
+
match_results = []
|
90 |
+
points = {team: 0 for team in teams}
|
91 |
+
|
92 |
+
for i in range(10):
|
93 |
+
t1, t2 = np.random.choice(teams, 2, replace=False)
|
94 |
+
winner = np.random.choice([t1, t2])
|
95 |
+
points[winner] += 2
|
96 |
+
match_results.append((t1, t2, winner))
|
97 |
+
|
98 |
+
df_matches = pd.DataFrame(match_results, columns=["Team A", "Team B", "Winner"])
|
99 |
+
df_points = pd.DataFrame(sorted(points.items(), key=lambda x: x[1], reverse=True), columns=["Team", "Points"])
|
100 |
+
|
101 |
+
st.subheader("π Simulated Match Results")
|
102 |
+
st.dataframe(df_matches)
|
103 |
+
|
104 |
+
st.subheader("π Points Table")
|
105 |
+
st.dataframe(df_points)
|
106 |
+
|
107 |
+
# ===============================
|
108 |
+
# 5. Live Match Predictor (Runs + Wickets)
|
109 |
+
# ===============================
|
110 |
+
elif selected_tab == "Live Match Predictor":
|
111 |
+
st.title("π‘ IPL Live Match Predictor")
|
112 |
+
|
113 |
+
col1, col2, col3 = st.columns(3)
|
114 |
+
with col1:
|
115 |
+
team_a = st.selectbox("Team A", ["MI", "RCB", "CSK", "GT"])
|
116 |
+
with col2:
|
117 |
+
team_b = st.selectbox("Team B", ["MI", "RCB", "CSK", "GT"])
|
118 |
+
with col3:
|
119 |
+
venue = st.selectbox("Venue", ["Wankhede", "Chinnaswamy", "Chepauk"])
|
120 |
+
|
121 |
+
date = st.date_input("Match Date", datetime.date.today())
|
122 |
+
status = st.radio("Match Status", ["In Progress", "Completed", "Scheduled"])
|
123 |
+
|
124 |
+
if st.button("Generate & Predict"):
|
125 |
+
runs = np.random.randint(0, 21, 20)
|
126 |
+
wickets = np.random.binomial(1, 0.3, 20)
|
127 |
+
|
128 |
+
df = pd.DataFrame({
|
129 |
+
"Over": list(range(1, 21)),
|
130 |
+
"Runs_This_Over": runs,
|
131 |
+
"Cumulative_Runs": np.cumsum(runs),
|
132 |
+
"Wickets_This_Over": wickets,
|
133 |
+
"Cumulative_Wickets": np.cumsum(wickets)
|
134 |
+
})
|
135 |
+
|
136 |
+
st.success(f"Match: {team_a} vs {team_b} at {venue} on {date}")
|
137 |
+
st.dataframe(df)
|
138 |
+
|
139 |
+
# Prediction logic
|
140 |
+
model = load_model("match_live_predictor/gru_score_predictor_rw.keras", compile=False)
|
141 |
+
scaler_input = joblib.load("match_live_predictor/scaler_input_rw.save")
|
142 |
+
scaler_output = joblib.load("match_live_predictor/scaler_output_rw.save")
|
143 |
+
|
144 |
+
|
145 |
+
padded_input = np.pad(df[["Runs_This_Over", "Wickets_This_Over"]].values,
|
146 |
+
((0, 20 - df.shape[0]), (0, 0)), mode='constant')
|
147 |
+
scaled_input = scaler_input.transform(padded_input).reshape(1, 20, 2)
|
148 |
+
pred_scaled = model.predict(scaled_input)
|
149 |
+
predicted_score = scaler_output.inverse_transform(pred_scaled)[0][0]
|
150 |
+
|
151 |
+
st.success(f"π― Predicted Final Score: {predicted_score:.2f} runs")
|
152 |
+
|
153 |
+
fig, ax1 = plt.subplots()
|
154 |
+
ax1.plot(df["Over"], df["Cumulative_Runs"], marker='o', color='green', label='Runs')
|
155 |
+
ax1.set_xlabel("Over")
|
156 |
+
ax1.set_ylabel("Runs", color='green')
|
157 |
+
ax2 = ax1.twinx()
|
158 |
+
ax2.bar(df["Over"], df["Wickets_This_Over"], alpha=0.4, color='red', label='Wickets')
|
159 |
+
ax2.set_ylabel("Wickets", color='red')
|
160 |
+
plt.axhline(y=predicted_score, color='blue', linestyle='--', label='Predicted Score')
|
161 |
+
fig.legend(loc='upper left')
|
162 |
+
st.pyplot(fig)
|
163 |
+
|
164 |
+
# ===============================
|
165 |
+
# Footer
|
166 |
+
# ===============================
|
167 |
+
st.markdown("""
|
168 |
+
---
|
169 |
+
π¨βπ» Built by Dinesh Kumar | Powered by Streamlit, NumPy, Matplotlib, TensorFlow
|
170 |
+
""")
|