Spaces:
Runtime error
Runtime error
File size: 5,252 Bytes
d9a94cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import gradio as gr
# Sample player data
players = {
"Goalkeeper": [
{"name": "Alisson", "salary": 8, "team": "Liverpool"},
{"name": "Ederson", "salary": 7.5, "team": "Man City"},
{"name": "Mendy", "salary": 7, "team": "Chelsea"},
],
"Defender": [
{"name": "Van Dijk", "salary": 9, "team": "Liverpool"},
{"name": "Ruben Dias", "salary": 8.5, "team": "Man City"},
{"name": "Stones", "salary": 8, "team": "Man City"},
{"name": "Rudiger", "salary": 7.5, "team": "Real Madrid"},
],
"Midfielder": [
{"name": "KDB", "salary": 12, "team": "Man City"},
{"name": "Salah", "salary": 11.5, "team": "Liverpool"},
{"name": "Bruno Fernandes", "salary": 11, "team": "Man Utd"},
{"name": "Mount", "salary": 9, "team": "Chelsea"},
],
"Forward": [
{"name": "Haaland", "salary": 13, "team": "Man City"},
{"name": "Kane", "salary": 12.5, "team": "Bayern"},
{"name": "Foden", "salary": 10, "team": "Man City"},
{"name": "Saka", "salary": 9.5, "team": "Arsenal"},
]
}
# Position requirements and constraints
position_requirements = {
"Goalkeeper": 1,
"Defender": 3,
"Midfielder": 4,
"Forward": 3
}
total_salary_cap = 100
def draft_team(selected_players):
# Calculate total salary and validate team composition
total_salary = 0
team = []
position_counts = {pos: 0 for pos in position_requirements}
for player_str in selected_players:
if not player_str:
continue
pos, name, salary, team_name = player_str.split("|")
total_salary += float(salary)
position_counts[pos] += 1
team.append(f"{name} ({team_name}) - {salary}")
# Check if team is valid
valid = all(position_counts[pos] == req for pos, req in position_requirements.items())
salary_valid = total_salary <= total_salary_cap
if not valid:
return "Invalid team composition! Please select the correct number of players for each position.", team, total_salary
if not salary_valid:
return f"Over salary cap! Total salary: {total_salary} (Max: {total_salary_cap})", team, total_salary
return f"Valid team! Total salary: {total_salary}/{total_salary_cap}", team, total_salary
def create_player_dropdowns():
with gr.Row():
gk_dropdown = gr.Dropdown(
choices=[f"Goalkeeper|{p['name']}|{p['salary']}|{p['team']}" for p in players["Goalkeeper"]],
label="Goalkeeper",
allow_custom_value=False
)
with gr.Row():
def1 = gr.Dropdown(
choices=[f"Defender|{p['name']}|{p['salary']}|{p['team']}" for p in players["Defender"]],
label="Defender 1"
)
def2 = gr.Dropdown(
choices=[f"Defender|{p['name']}|{p['salary']}|{p['team']}" for p in players["Defender"]],
label="Defender 2"
)
def3 = gr.Dropdown(
choices=[f"Defender|{p['name']}|{p['salary']}|{p['team']}" for p in players["Defender"]],
label="Defender 3"
)
with gr.Row():
mid1 = gr.Dropdown(
choices=[f"Midfielder|{p['name']}|{p['salary']}|{p['team']}" for p in players["Midfielder"]],
label="Midfielder 1"
)
mid2 = gr.Dropdown(
choices=[f"Midfielder|{p['name']}|{p['salary']}|{p['team']}" for p in players["Midfielder"]],
label="Midfielder 2"
)
mid3 = gr.Dropdown(
choices=[f"Midfielder|{p['name']}|{p['salary']}|{p['team']}" for p in players["Midfielder"]],
label="Midfielder 3"
)
mid4 = gr.Dropdown(
choices=[f"Midfielder|{p['name']}|{p['salary']}|{p['team']}" for p in players["Midfielder"]],
label="Midfielder 4"
)
with gr.Row():
fwd1 = gr.Dropdown(
choices=[f"Forward|{p['name']}|{p['salary']}|{p['team']}" for p in players["Forward"]],
label="Forward 1"
)
fwd2 = gr.Dropdown(
choices=[f"Forward|{p['name']}|{p['salary']}|{p['team']}" for p in players["Forward"]],
label="Forward 2"
)
fwd3 = gr.Dropdown(
choices=[f"Forward|{p['name']}|{p['salary']}|{p['team']}" for p in players["Forward"]],
label="Forward 3"
)
return [gk_dropdown, def1, def2, def3, mid1, mid2, mid3, mid4, fwd1, fwd2, fwd3]
with gr.Blocks(title="CFS Fantasy Draft", theme=gr.themes.Soft()) as app:
gr.Markdown("# Crypto Fantasy Sports - Team Draft")
gr.Markdown("Select your players for each position. Stay under the salary cap!")
gr.Markdown(f"**Salary Cap: {total_salary_cap}**")
with gr.Row():
with gr.Column():
player_inputs = create_player_dropdowns()
submit_btn = gr.Button("Submit Team")
with gr.Column():
result = gr.Textbox(label="Result", interactive=False)
team_list = gr.Textbox(label="Your Team", lines=10, interactive=False)
salary_display = gr.Number(label="Total Salary", interactive=False)
submit_btn.click(
fn=draft_team,
inputs=player_inputs,
outputs=[result, team_list, salary_display]
)
app.launch() |