flen-crypto commited on
Commit
d9a94cd
·
verified ·
1 Parent(s): fc13bc8

Upload src/streamlit_app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +138 -40
src/streamlit_app.py CHANGED
@@ -1,40 +1,138 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
- import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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()