-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
170 lines (138 loc) · 5.79 KB
/
app.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import streamlit as st
import pandas as pd
import pickle
from pathlib import Path
import os
st.set_page_config(
page_title="Player Recommender",
page_icon=":soccer:"
)
@st.cache_data(show_spinner=False)
def getData():
# loading outfield players' cleaned data and engine
player_df = pd.read_pickle(r'data/outfield.pkl')
with open(r'data/player_ID.pickle', 'rb') as file:
player_ID = pickle.load(file)
with open(r'data/engine.pickle', 'rb') as file:
engine = pickle.load(file)
# loading gk players' cleaned data and engine
gk_df = pd.read_pickle(r'data/gk.pkl')
with open(r'data/gk_ID.pickle', 'rb') as file:
gk_ID = pickle.load(file)
with open(r'data/gk_engine.pickle', 'rb') as file:
gk_engine = pickle.load(file)
return [player_df, player_ID, engine], [gk_df, gk_ID, gk_engine]
outfield_data, gk_data = getData()
header = st.container()
params = st.container()
result = st.container()
teams = st.container()
credit = st.container()
with header:
st.title('Player Recommendation Tool ')
with params:
st.text(' \n')
st.text(' \n')
st.header('Get recommendations')
col1, col2, col3 = st.columns([1, 2.2, 0.8])
with col1:
radio = st.radio('Player type', ['Outfield players', 'Goal Keepers'])
with col2:
if radio == 'Outfield players':
df, player_ID, engine = outfield_data
else:
df, player_ID, engine = gk_data
players = sorted(list(player_ID.keys()))
age_default = (min(df['Age']), max(df['Age']))
query = st.selectbox('Player name', players,
help='Type without deleting a character. To search from a specific team, just type in the club\'s name.')
with col3:
foot = st.selectbox('Preferred foot', ['All', 'Right', 'Left'])
col4, col5, col6, col7 = st.columns([0.7, 1, 1, 1])
with col4:
if radio == 'Outfield players':
res, val, step = (5, 20), 10, 5
else:
res, val, step = (3, 10), 5, 1
count = st.slider(
'Number of results', min_value=res[0], max_value=res[1], value=val, step=step)
with col5:
comp = st.selectbox('League', ['All', 'Premier League', 'La Liga', 'Serie A', 'Bundesliga', 'Ligue 1'],
help='Leagues to get recommendations from. \'All\' leagues by default.')
with col6:
comparison = st.selectbox('Comparison with', ['All positions', 'Same position'],
help='Whether to compare the selected player with all positions or just the same defined position in the dataset. \'All \
positions\' by default.')
with col7:
age = st.slider('Age bracket', min_value=age_default[0], max_value=age_default[1], value=age_default,
help='Age range to get recommendations from. Drag the sliders on either side. \'All\' ages by default.')
with result:
st.text(' \n')
st.text(' \n')
st.text(' \n')
st.markdown('_showing recommendations for_ **{}**'.format(query))
def getRecommendations(metric, df_type, league='All', foot='All', comparison='All positions', age=age_default, count=val):
if df_type == 'outfield':
df_res = df.iloc[:, [1, 3, 5, 6, 11, -1]].copy()
else:
df_res = df.iloc[:, [1, 3, 5, 6, 11]].copy()
df_res['Player'] = list(player_ID.keys())
df_res.insert(1, 'Similarity', metric)
df_res = df_res.sort_values(by=['Similarity'], ascending=False)
metric = [str(num) + '%' for num in df_res['Similarity']]
df_res['Similarity'] = metric
df_res = df_res.iloc[1:, :]
if comparison == 'Same position' and df_type == 'outfield':
q_pos = list(df[df['Player'] == query.split(' (')[0]].Pos)[0]
df_res = df_res[df_res['Pos'] == q_pos]
if league == 'All':
pass
else:
df_res = df_res[df_res['Comp'] == league]
if age == age_default:
pass
else:
df_res = df_res[(df_res['Age'] >= age[0]) &
(df_res['Age'] <= age[1])]
if foot == 'All' or df_type == 'gk':
pass
elif foot == 'Left':
df_res = df_res[df_res['Foot'] == 'left']
else:
df_res = df_res[df_res['Foot'] == 'right']
df_res = df_res.iloc[:count, :].reset_index(drop=True)
df_res.index = df_res.index + 1
if len(df) == 2040:
mp90 = [str(round(num, 1)) for num in df_res['90s']]
df_res['90s'] = mp90
df_res.rename(columns={'Pos': 'Position',
'Comp': 'League'}, inplace=True)
return df_res
sims = engine[query]
df_type = 'outfield' if len(df) == 2040 else 'gk'
recoms = getRecommendations(sims, df_type=df_type, foot=foot,
league=comp, comparison=comparison, age=age, count=count)
st.table(recoms)
with teams:
st.text(' \n')
st.header("Team")
col1, col2, col3, col4 = st.columns([1, 1, 1, 1])
with col1:
st.image('images/bijay.jpg', width=150)
st.write('**Bijay Sapkota**')
with col2:
st.image('images/ishan.jpg', width=150)
st.write('**Ishan Panta**')
with col3:
st.image('images/manish.png', width=150)
st.write('**Manish Shivabhakti**')
with col4:
st.image('images/priyanshu.jpg', width=150)
st.write('**Priyanshu Sharma**')
with credit:
st.text(' \n')
st.text(' \n')
@st.cache_data()
def read_info(path):
return Path(path).read_text(encoding='utf8')
st.markdown(read_info('info.md'), unsafe_allow_html=True)