rairo commited on
Commit
a22e862
·
verified ·
1 Parent(s): b76a9a4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import pandas as pd
4
+
5
+ st.set_page_config(page_title="MLB Prospect Pulse", layout="wide")
6
+
7
+ # Title and description
8
+ st.title("⚾ MLB Prospect Prediction Toolkit")
9
+ st.markdown("""
10
+ **Challenge 5 Solution**
11
+ Predict prospect potential, compare players, and explore scenarios using MLB's GUMBO data.
12
+ """)
13
+
14
+ @st.cache_data
15
+ def get_schedule(season=2024):
16
+ url = f"https://statsapi.mlb.com/api/v1/schedule?sportId=1&season={season}"
17
+ return requests.get(url).json()
18
+
19
+ @st.cache_data
20
+ def get_roster(team_id=119, season=2024): # Default: LA Dodgers
21
+ url = f"https://statsapi.mlb.com/api/v1/teams/{team_id}/roster?season={season}"
22
+ return requests.get(url).json()
23
+
24
+ @st.cache_data
25
+ def get_player_stats(player_id):
26
+ url = f"https://statsapi.mlb.com/api/v1/people/{player_id}?hydrate=stats"
27
+ return requests.get(url).json()
28
+
29
+
30
+ def prediction_section():
31
+ st.header("🧠 Prospect Potential Prediction")
32
+
33
+ # Get available teams
34
+ schedule = get_schedule()
35
+ teams = {team['team']['id']: team['team']['name']
36
+ for game in schedule['dates'][0]['games']
37
+ for team in [game['teams']['home']['team'], game['teams']['away']['team']]}
38
+
39
+ col1, col2 = st.columns(2)
40
+ with col1:
41
+ team_id = st.selectbox("Select Team", options=teams.keys(), format_func=lambda x: teams[x])
42
+
43
+ with col2:
44
+ roster = get_roster(team_id)['roster']
45
+ players = {p['person']['id']: p['person']['fullName'] for p in roster}
46
+ player_id = st.selectbox("Select Prospect", options=players.keys(), format_func=lambda x: players[x])
47
+
48
+ if st.button("Analyze Prospect"):
49
+ stats = get_player_stats(player_id)
50
+ # Add your ML model here (placeholder example)
51
+ st.success(f"Predicted WAR in 3 years: 4.2 (Sample Output)")
52
+ st.json(stats) # Show raw data for inspection
53
+
54
+
55
+ def comparison_tool():
56
+ st.header("🔍 Player Comparison")
57
+
58
+ # Get 2 players to compare
59
+ roster = get_roster()['roster']
60
+ players = {p['person']['id']: p['person']['fullName'] for p in roster}
61
+
62
+ col1, col2 = st.columns(2)
63
+ with col1:
64
+ p1 = st.selectbox("Player 1", options=players.keys(), format_func=lambda x: players[x])
65
+ with col2:
66
+ p2 = st.selectbox("Player 2", options=players.keys(), format_func=lambda x: players[x])
67
+
68
+ if p1 and p2:
69
+ stats1 = get_player_stats(p1)
70
+ stats2 = get_player_stats(p2)
71
+
72
+ # Create comparison table (customize with actual metrics)
73
+ comparison = pd.DataFrame({
74
+ "Metric": ["Age", "BA", "HR", "SO"],
75
+ players[p1]: [25, 0.285, 32, 110], # Sample data
76
+ players[p2]: [23, 0.265, 28, 95]
77
+ }).set_index("Metric")
78
+
79
+ st.dataframe(comparison, use_container_width=True)
80
+
81
+
82
+ def what_if_scenarios():
83
+ st.header("🎮 What-If Analysis")
84
+
85
+ # Interactive sliders
86
+ col1, col2, col3 = st.columns(3)
87
+ with col1:
88
+ age = st.slider("Age", 18, 35, 22)
89
+ with col2:
90
+ ba = st.slider("Batting Average", 0.150, 0.400, 0.275)
91
+ with col3:
92
+ hr = st.slider("Projected HR", 0, 60, 25)
93
+
94
+ # Add your model simulation here
95
+ simulated_war = age * 0.1 + ba * 10 + hr * 0.2 # Example calculation
96
+ st.metric("Simulated Future WAR", f"{simulated_war:.1f}")
97
+
98
+
99
+ def main():
100
+ tabs = {
101
+ "Prediction": prediction_section,
102
+ "Comparison": comparison_tool,
103
+ "What-If": what_if_scenarios
104
+ }
105
+
106
+ current_tab = st.sidebar.radio("Navigation", list(tabs.keys()))
107
+ tabs[current_tab]()
108
+
109
+ if __name__ == "__main__":
110
+ main()