Ilayr222 commited on
Commit
04ec689
·
verified ·
1 Parent(s): 6948a88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -38
app.py CHANGED
@@ -5,20 +5,13 @@ import gradio as gr
5
  import pycountry
6
 
7
  # === CONFIG ===
8
- DATA_FILE = "players.dataset.xlsx" # upload this after the app files
9
- POSITIONS = ["CB","RB","LB","CDM","CM","CAM","RW","ST","LW"]
10
 
11
- # User-specified exclusions for Nation values (exact/variant matches handled below)
12
  EXCLUDED_NATIONS = {
13
- "PALESTINE",
14
- "STATE OF PALESTINE",
15
- "PALESTINIAN TERRITORY",
16
- "PALESTINIAN TERRITORIES",
17
- "PSE",
18
- "PS",
19
  }
20
 
21
- # Canonical header mapping (tolerant to your Excel headers)
22
  CANON = {
23
  "name": ["name","player","player_name"],
24
  "age": ["age"],
@@ -53,8 +46,6 @@ def _to_number(x):
53
  def _to_full_country(n):
54
  if pd.isna(n): return None
55
  s = str(n).strip()
56
-
57
- # Convert alpha-3/alpha-2 codes to full names; otherwise keep as-is
58
  candidate = None
59
  if len(s) <= 3:
60
  c = pycountry.countries.get(alpha_3=s.upper())
@@ -64,8 +55,6 @@ def _to_full_country(n):
64
  if c: candidate = c.name
65
  if candidate is None:
66
  candidate = s
67
-
68
- # Apply user-specified exclusion
69
  norm = re.sub(r"\s+"," ", candidate).strip().upper()
70
  if norm in EXCLUDED_NATIONS:
71
  return None
@@ -77,7 +66,6 @@ def load_df():
77
  "Name","Age","Position","Nation","Club","Overall","Potential",
78
  "Height_cm","Weight_kg","Value","Wage"
79
  ])
80
-
81
  df = pd.read_excel(DATA_FILE, engine="openpyxl")
82
  df.columns = [str(c).strip() for c in df.columns]
83
  cmap = _canon_map(df.columns)
@@ -100,14 +88,13 @@ def load_df():
100
  out["Height_cm"] = df.get(cmap.get("height_cm"), pd.Series(dtype=object)).apply(_to_number)
101
  out["Weight_kg"] = df.get(cmap.get("weight_kg"), pd.Series(dtype=object)).apply(_to_number)
102
 
103
- # Drop excluded/empty nations
104
  out = out[~out["Nation"].isna()].reset_index(drop=True)
105
  return out
106
 
107
  DF = load_df()
108
 
109
  def positions_list():
110
- vals = set(POSITIONS)
111
  if "Position" in DF.columns:
112
  vals |= set(str(x).upper().strip() for x in DF["Position"].dropna().unique())
113
  return sorted(vals)
@@ -115,7 +102,6 @@ def positions_list():
115
  def nations_list():
116
  if "Nation" in DF.columns and not DF["Nation"].dropna().empty:
117
  return sorted(set(DF["Nation"].dropna().tolist()))
118
- # Fallback to all pycountry names (minus exclusions)
119
  vals = []
120
  for c in pycountry.countries:
121
  nm = c.name
@@ -131,7 +117,6 @@ def clubs_list():
131
  def filter_players(positions, nations, clubs, min_overall, min_potential,
132
  max_age, min_h, max_h, min_w, max_w, max_val, max_wage, query):
133
  df = DF.copy()
134
-
135
  if positions: df = df[df["Position"].isin(positions)]
136
  if nations: df = df[df["Nation"].isin(nations)]
137
  if clubs: df = df[df["Club"].isin(clubs)]
@@ -144,14 +129,12 @@ def filter_players(positions, nations, clubs, min_overall, min_potential,
144
  if not math.isnan(max_w): df = df[df["Weight_kg"] <= max_w]
145
  if not math.isnan(max_val): df = df[df["Value"] <= max_val]
146
  if not math.isnan(max_wage): df = df[df["Wage"] <= max_wage]
147
-
148
  if query:
149
  q = query.strip().lower()
150
  df = df[df.apply(lambda r: any(
151
  q in str(r.get(c, "")).lower()
152
  for c in ["Name","Club","Position","Nation"]
153
  ), axis=1)]
154
-
155
  cols = [c for c in ["Name","Age","Position","Nation","Club","Overall","Potential",
156
  "Height_cm","Weight_kg","Value","Wage"] if c in df.columns]
157
  return df[cols].reset_index(drop=True)
@@ -161,7 +144,7 @@ def to_csv_bytes(df):
161
  df.to_csv(buf, index=False, encoding="utf-8")
162
  return buf.getvalue().encode("utf-8")
163
 
164
- # === UI ===
165
  THEME = gr.themes.Soft(primary_hue="blue")
166
  CSS = """
167
  #title { text-align:center; }
@@ -171,28 +154,37 @@ CSS = """
171
 
172
  with gr.Blocks(theme=THEME, css=CSS) as demo:
173
  gr.Markdown("<h1 id='title'>ProScout — Player Finder</h1>")
 
 
174
  with gr.Row():
 
175
  with gr.Column(scale=1, elem_classes="card"):
176
- pos = gr.CheckboxGroup(positions_list(), label="Positions", info="Pick one or more")
177
- nat = gr.Dropdown(nations_list(), multiselect=True, label="Nations", filterable=True)
178
- clu = gr.Dropdown(clubs_list(), multiselect=True, label="Clubs", filterable=True)
179
- query = gr.Textbox(label="Search (Name / Club / Nation / Position)")
180
-
181
- with gr.Accordion("Advanced filters", open=False):
182
- min_ovr = gr.Slider(0, 99, value=70, step=1, label="Min Overall")
183
- min_pot = gr.Slider(0, 99, value=70, step=1, label="Min Potential")
184
- max_age = gr.Slider(15, 45, value=30, step=1, label="Max Age")
185
- min_h = gr.Slider(140, 210, value=140, step=1, label="Min Height (cm)")
186
- max_h = gr.Slider(140, 210, value=210, step=1, label="Max Height (cm)")
187
- min_w = gr.Slider(45, 120, value=45, step=1, label="Min Weight (kg)")
188
- max_w = gr.Slider(45, 120, value=120, step=1, label="Max Weight (kg)")
189
- max_val = gr.Number(value=np.nan, label="Max Value (EUR)")
190
- max_wage = gr.Number(value=np.nan, label="Max Wage (EUR)")
191
 
 
 
 
 
 
 
 
192
  btn = gr.Button("Search", variant="primary")
193
 
 
 
 
 
 
 
 
 
 
 
194
  with gr.Column(scale=2, elem_classes="card"):
195
- results = gr.Dataframe(row_count=(10,"dynamic"), wrap=True, interactive=False, label="Results")
196
  with gr.Row():
197
  count_box = gr.Markdown("", elem_classes="stat")
198
  avg_ovr_box = gr.Markdown("", elem_classes="stat")
@@ -209,6 +201,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
209
  csv_bytes = to_csv_bytes(df)
210
  return df, f"**Players:** {count}", f"**Avg OVR:** {avg_ovr}", f"**Avg Age:** {avg_age}", csv_bytes
211
 
 
212
  demo.load(
213
  _run,
214
  inputs=[pos,nat,clu,min_ovr,min_pot,max_age,min_h,max_h,min_w,max_w,max_val,max_wage,query],
 
5
  import pycountry
6
 
7
  # === CONFIG ===
8
+ DATA_FILE = "players.dataset.xlsx"
9
+ BASE_POSITIONS = ["CB","RB","LB","CDM","CM","CAM","RW","ST","LW"]
10
 
 
11
  EXCLUDED_NATIONS = {
12
+ "PALESTINE","STATE OF PALESTINE","PALESTINIAN TERRITORY","PALESTINIAN TERRITORIES","PSE","PS"
 
 
 
 
 
13
  }
14
 
 
15
  CANON = {
16
  "name": ["name","player","player_name"],
17
  "age": ["age"],
 
46
  def _to_full_country(n):
47
  if pd.isna(n): return None
48
  s = str(n).strip()
 
 
49
  candidate = None
50
  if len(s) <= 3:
51
  c = pycountry.countries.get(alpha_3=s.upper())
 
55
  if c: candidate = c.name
56
  if candidate is None:
57
  candidate = s
 
 
58
  norm = re.sub(r"\s+"," ", candidate).strip().upper()
59
  if norm in EXCLUDED_NATIONS:
60
  return None
 
66
  "Name","Age","Position","Nation","Club","Overall","Potential",
67
  "Height_cm","Weight_kg","Value","Wage"
68
  ])
 
69
  df = pd.read_excel(DATA_FILE, engine="openpyxl")
70
  df.columns = [str(c).strip() for c in df.columns]
71
  cmap = _canon_map(df.columns)
 
88
  out["Height_cm"] = df.get(cmap.get("height_cm"), pd.Series(dtype=object)).apply(_to_number)
89
  out["Weight_kg"] = df.get(cmap.get("weight_kg"), pd.Series(dtype=object)).apply(_to_number)
90
 
 
91
  out = out[~out["Nation"].isna()].reset_index(drop=True)
92
  return out
93
 
94
  DF = load_df()
95
 
96
  def positions_list():
97
+ vals = set(BASE_POSITIONS)
98
  if "Position" in DF.columns:
99
  vals |= set(str(x).upper().strip() for x in DF["Position"].dropna().unique())
100
  return sorted(vals)
 
102
  def nations_list():
103
  if "Nation" in DF.columns and not DF["Nation"].dropna().empty:
104
  return sorted(set(DF["Nation"].dropna().tolist()))
 
105
  vals = []
106
  for c in pycountry.countries:
107
  nm = c.name
 
117
  def filter_players(positions, nations, clubs, min_overall, min_potential,
118
  max_age, min_h, max_h, min_w, max_w, max_val, max_wage, query):
119
  df = DF.copy()
 
120
  if positions: df = df[df["Position"].isin(positions)]
121
  if nations: df = df[df["Nation"].isin(nations)]
122
  if clubs: df = df[df["Club"].isin(clubs)]
 
129
  if not math.isnan(max_w): df = df[df["Weight_kg"] <= max_w]
130
  if not math.isnan(max_val): df = df[df["Value"] <= max_val]
131
  if not math.isnan(max_wage): df = df[df["Wage"] <= max_wage]
 
132
  if query:
133
  q = query.strip().lower()
134
  df = df[df.apply(lambda r: any(
135
  q in str(r.get(c, "")).lower()
136
  for c in ["Name","Club","Position","Nation"]
137
  ), axis=1)]
 
138
  cols = [c for c in ["Name","Age","Position","Nation","Club","Overall","Potential",
139
  "Height_cm","Weight_kg","Value","Wage"] if c in df.columns]
140
  return df[cols].reset_index(drop=True)
 
144
  df.to_csv(buf, index=False, encoding="utf-8")
145
  return buf.getvalue().encode("utf-8")
146
 
147
+ # === UI (spread out, no accordion) ===
148
  THEME = gr.themes.Soft(primary_hue="blue")
149
  CSS = """
150
  #title { text-align:center; }
 
154
 
155
  with gr.Blocks(theme=THEME, css=CSS) as demo:
156
  gr.Markdown("<h1 id='title'>ProScout — Player Finder</h1>")
157
+
158
+ # ROW: filters (3 columns) + results (wide on the right)
159
  with gr.Row():
160
+ # Column 1: categorical filters + search
161
  with gr.Column(scale=1, elem_classes="card"):
162
+ pos = gr.CheckboxGroup(positions_list(), label="Positions", value=[], info="Pick one or more")
163
+ nat = gr.Dropdown(nations_list(), multiselect=True, label="Nations", value=[], filterable=True)
164
+ clu = gr.Dropdown(clubs_list(), multiselect=True, label="Clubs", value=[], filterable=True)
165
+ query = gr.Textbox(label="Search (Name / Club / Nation / Position)", placeholder="e.g., Maccabi, Brazil, CAM")
 
 
 
 
 
 
 
 
 
 
 
166
 
167
+ # Column 2: ratings & age
168
+ with gr.Column(scale=1, elem_classes="card"):
169
+ min_ovr = gr.Slider(0, 99, value=0, step=1, label="Min Overall")
170
+ min_pot = gr.Slider(0, 99, value=0, step=1, label="Min Potential")
171
+ max_age = gr.Slider(15, 45, value=45, step=1, label="Max Age")
172
+
173
+ gr.Markdown("") # spacer
174
  btn = gr.Button("Search", variant="primary")
175
 
176
+ # Column 3: physical & financial
177
+ with gr.Column(scale=1, elem_classes="card"):
178
+ min_h = gr.Slider(140, 210, value=140, step=1, label="Min Height (cm)")
179
+ max_h = gr.Slider(140, 210, value=210, step=1, label="Max Height (cm)")
180
+ min_w = gr.Slider(45, 120, value=45, step=1, label="Min Weight (kg)")
181
+ max_w = gr.Slider(45, 120, value=120, step=1, label="Max Weight (kg)")
182
+ max_val = gr.Number(value=np.nan, label="Max Value (EUR)")
183
+ max_wage = gr.Number(value=np.nan, label="Max Wage (EUR)")
184
+
185
+ # Results panel
186
  with gr.Column(scale=2, elem_classes="card"):
187
+ results = gr.Dataframe(row_count=(12,"dynamic"), wrap=True, interactive=False, label="Results")
188
  with gr.Row():
189
  count_box = gr.Markdown("", elem_classes="stat")
190
  avg_ovr_box = gr.Markdown("", elem_classes="stat")
 
201
  csv_bytes = to_csv_bytes(df)
202
  return df, f"**Players:** {count}", f"**Avg OVR:** {avg_ovr}", f"**Avg Age:** {avg_age}", csv_bytes
203
 
204
+ # Show data immediately on load (no empty screen)
205
  demo.load(
206
  _run,
207
  inputs=[pos,nat,clu,min_ovr,min_pot,max_age,min_h,max_h,min_w,max_w,max_val,max_wage,query],