Britzzy commited on
Commit
d4b0483
·
verified ·
1 Parent(s): 0850b4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -18,10 +18,17 @@ def load_data_and_model():
18
  return None, None
19
  df = pd.read_csv(df_path)
20
 
 
 
 
 
 
 
 
 
21
  # Load model
22
  model = xgb.XGBRegressor()
23
  try:
24
- # Try both common casing to be safe on Linux/HuggingFace
25
  if os.path.exists("fairvalue_xgboost.json"):
26
  model.load_model("fairvalue_xgboost.json")
27
  elif os.path.exists("FairValue_xgboost.json"):
@@ -43,11 +50,6 @@ if df is None:
43
  st.stop()
44
 
45
  # === Data Sanity Gate ===
46
- # Standardize market value column name for the model
47
- for col in ['market_value_in_eur_x', 'market_value_in_eur_y']:
48
- if col in df.columns:
49
- df.rename(columns={col: 'market_value_in_eur'}, inplace=True)
50
-
51
  if 'Age' in df.columns:
52
  df['Age'] = df['Age'].clip(15, 45)
53
  if 'Contract_Years_Left' in df.columns:
@@ -60,7 +62,7 @@ st.sidebar.header("Player Transfer Profile")
60
 
61
  input_mode = st.sidebar.radio("Input Mode", ["Select Existing Player", "Create Custom Player"])
62
 
63
- name_col = next((c for c in ['name', 'name_x', 'Player_Name'] if c in df.columns), None)
64
 
65
  if input_mode == "Select Existing Player":
66
  if name_col:
@@ -76,7 +78,10 @@ if input_mode == "Select Existing Player":
76
 
77
  contract_default = max(0.5, min(6.0, float(player_data['Contract_Years_Left'].iloc[0])))
78
  age_default = max(16, min(40, int(player_data['Age'].iloc[0])))
79
- injury_default = max(0, min(500, int(player_data.get('Injury_Days_Total_24m', [10])[0])))
 
 
 
80
 
81
  contract_years = st.sidebar.slider("Contract Years Remaining", 0.5, 6.0, contract_default, 0.5)
82
  age = st.sidebar.slider("Age", 16, 40, age_default)
@@ -100,7 +105,8 @@ asking_price = st.sidebar.number_input("Selling Club Asking Price (£m)", 1.0, 2
100
  # Apply overrides
101
  player_data['Contract_Years_Left'] = contract_years
102
  player_data['Age'] = age
103
- player_data['Injury_Days_Total_24m'] = injuries if 'Injury_Days_Total_24m' in player_data.columns else injuries
 
104
 
105
  # Prepare inference payload
106
  expected_cols = model.feature_names_in_
 
18
  return None, None
19
  df = pd.read_csv(df_path)
20
 
21
+ # Standardize market value column name EARLY and DEDUPLICATE
22
+ for col in ['market_value_in_eur_x', 'market_value_in_eur_y', 'market_value_x', 'market_value_y']:
23
+ if col in df.columns:
24
+ df.rename(columns={col: 'market_value_in_eur'}, inplace=True)
25
+
26
+ # CRITICAL: Drop any duplicate columns created by renaming to avoid "reindex on duplicate labels"
27
+ df = df.loc[:, ~df.columns.duplicated()].copy()
28
+
29
  # Load model
30
  model = xgb.XGBRegressor()
31
  try:
 
32
  if os.path.exists("fairvalue_xgboost.json"):
33
  model.load_model("fairvalue_xgboost.json")
34
  elif os.path.exists("FairValue_xgboost.json"):
 
50
  st.stop()
51
 
52
  # === Data Sanity Gate ===
 
 
 
 
 
53
  if 'Age' in df.columns:
54
  df['Age'] = df['Age'].clip(15, 45)
55
  if 'Contract_Years_Left' in df.columns:
 
62
 
63
  input_mode = st.sidebar.radio("Input Mode", ["Select Existing Player", "Create Custom Player"])
64
 
65
+ name_col = next((c for c in ['name', 'name_x', 'Player_Name', 'Name'] if c in df.columns), None)
66
 
67
  if input_mode == "Select Existing Player":
68
  if name_col:
 
78
 
79
  contract_default = max(0.5, min(6.0, float(player_data['Contract_Years_Left'].iloc[0])))
80
  age_default = max(16, min(40, int(player_data['Age'].iloc[0])))
81
+ injury_col = next((c for c in ['Injury_Days_Total_24m', 'Injury_Days'] if c in player_data.columns), None)
82
+ injury_default = 0
83
+ if injury_col:
84
+ injury_default = max(0, min(500, int(player_data[injury_col].iloc[0])))
85
 
86
  contract_years = st.sidebar.slider("Contract Years Remaining", 0.5, 6.0, contract_default, 0.5)
87
  age = st.sidebar.slider("Age", 16, 40, age_default)
 
105
  # Apply overrides
106
  player_data['Contract_Years_Left'] = contract_years
107
  player_data['Age'] = age
108
+ if 'Injury_Days_Total_24m' in player_data.columns:
109
+ player_data['Injury_Days_Total_24m'] = injuries
110
 
111
  # Prepare inference payload
112
  expected_cols = model.feature_names_in_