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