danupurnomo
commited on
Commit
Β·
b495898
1
Parent(s):
16e92b4
Add files and images
Browse files- README.md +3 -3
- app.py +267 -0
- img/.DS_Store +0 -0
- img/01 - background.jpg +0 -0
- img/02 - personal profile.png +0 -0
- img/03 - work rate.png +0 -0
- img/04 - ability.png +0 -0
- model/model_feat_enc.pkl +0 -0
- model/model_feat_scaling.pkl +0 -0
- model/model_rating.pkl +0 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: Fifa 2022 Rating Prediction
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
sdk_version: 1.10.0
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
title: Fifa 2022 Rating Prediction
|
3 |
+
emoji: π
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: red
|
6 |
sdk: streamlit
|
7 |
sdk_version: 1.10.0
|
8 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,267 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import base64
|
4 |
+
import pickle
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
import streamlit as st
|
8 |
+
import plotly.express as px
|
9 |
+
from PIL import Image
|
10 |
+
from collections import deque
|
11 |
+
from urllib.request import urlopen
|
12 |
+
|
13 |
+
|
14 |
+
# STEP 1 - DEFINE PATHS
|
15 |
+
base_path = os.path.abspath(os.path.dirname(__file__))
|
16 |
+
model_path = os.path.join(base_path, 'model')
|
17 |
+
img_path = os.path.join(base_path, 'img')
|
18 |
+
|
19 |
+
# STEP 2 - LOAD MODEL
|
20 |
+
model_filename = 'model_rating.pkl'
|
21 |
+
scaler_filename = 'model_feat_scaling.pkl'
|
22 |
+
encoder_filename = 'model_feat_enc.pkl'
|
23 |
+
|
24 |
+
model_filepath = os.path.join(model_path, model_filename)
|
25 |
+
scaler_filepath = os.path.join(model_path, scaler_filename)
|
26 |
+
encoder_filepath = os.path.join(model_path, encoder_filename)
|
27 |
+
|
28 |
+
with open(model_filepath, "rb") as filename:
|
29 |
+
model_rating = pickle.load(filename)
|
30 |
+
|
31 |
+
with open(scaler_filepath, "rb") as filename:
|
32 |
+
scaler = pickle.load(filename)
|
33 |
+
|
34 |
+
with open(encoder_filepath, "rb") as filename:
|
35 |
+
encoder = pickle.load(filename)
|
36 |
+
|
37 |
+
# STEP 3 - SET PAGE CONFIG
|
38 |
+
st.set_page_config(
|
39 |
+
page_title = 'FIFA 2022 Player Rating\'s Prediction',
|
40 |
+
layout = 'wide',
|
41 |
+
initial_sidebar_state = 'expanded',
|
42 |
+
menu_items = {
|
43 |
+
'About': '''
|
44 |
+
## FIFA 2022 Player Rating\'s Prediction
|
45 |
+
---
|
46 |
+
_Made by Danu Purnomo_
|
47 |
+
|
48 |
+
Predict rating of a football player based on FIFA 2022 players.
|
49 |
+
'''
|
50 |
+
}
|
51 |
+
)
|
52 |
+
|
53 |
+
# STEP 4 - CREATE BACKGROUND
|
54 |
+
def convert_img_to_base64(img_path):
|
55 |
+
with open(img_path, 'rb') as image_file:
|
56 |
+
encoded_string = base64.b64encode(image_file.read())
|
57 |
+
return encoded_string
|
58 |
+
|
59 |
+
img_background_path = os.path.join(img_path, '01 - background.jpg')
|
60 |
+
encoded_string = convert_img_to_base64(img_background_path)
|
61 |
+
st.markdown(
|
62 |
+
f"""
|
63 |
+
<style>
|
64 |
+
.stApp {{
|
65 |
+
background-image: url(data:image/{"jpg"};base64,{encoded_string.decode()});
|
66 |
+
background-size: cover;
|
67 |
+
}}
|
68 |
+
|
69 |
+
</style>
|
70 |
+
""",
|
71 |
+
unsafe_allow_html=True
|
72 |
+
)
|
73 |
+
|
74 |
+
# STEP 5 - SET TITLE AND OPENER
|
75 |
+
## STEP 5.1 - SET TITLE
|
76 |
+
text_title = '<h1 style="font-family:sans-serif; color:#cbd5e7; text-align:center;">FIFA 2022 Player\'s Rating Predictions</h1>'
|
77 |
+
st.markdown(text_title, unsafe_allow_html=True)
|
78 |
+
|
79 |
+
## STEP 5.2 - SET OPENER
|
80 |
+
gif0 = '<div style="width:1080px"><iframe allow="fullscreen" align="center" frameBorder="0" height="720" src="https://giphy.com/embed/ICE7YmNTU9MatWIgxi/video" width="1440"></iframe></div>'
|
81 |
+
st.markdown(gif0, unsafe_allow_html=True)
|
82 |
+
|
83 |
+
# STEP 6 - SET PARAMETERS
|
84 |
+
st.markdown('---')
|
85 |
+
text_style = '<p style="font-family:sans-serif; color:#b41ff0; font-size: 30px;">Set Parameters</p>'
|
86 |
+
st.markdown(text_style, unsafe_allow_html=True)
|
87 |
+
|
88 |
+
# Attribute of a football player
|
89 |
+
# 0 Name 19260 non-null object
|
90 |
+
# 1 Age 19260 non-null int64
|
91 |
+
# 2 Height 19260 non-null int64
|
92 |
+
# 3 Weight 19260 non-null int64
|
93 |
+
# 4 Price 19260 non-null int64
|
94 |
+
# 5 AttackingWorkRate 19260 non-null object
|
95 |
+
# 6 DefensiveWorkRate 19260 non-null object
|
96 |
+
# 7 PaceTotal 19260 non-null int64
|
97 |
+
# 8 ShootingTotal 19260 non-null int64
|
98 |
+
# 9 PassingTotal 19260 non-null int64
|
99 |
+
# 10 DribblingTotal 19260 non-null int64
|
100 |
+
# 11 DefendingTotal 19260 non-null int64
|
101 |
+
# 12 PhysicalityTotal 19260 non-null int64
|
102 |
+
# 13 Rating 19260 non-null int64
|
103 |
+
|
104 |
+
with st.form(key='form_parameters'):
|
105 |
+
|
106 |
+
## STEP 6.1 : Section 1
|
107 |
+
header_section_1 = '<p style="font-family:sans-serif; color:#67b8f8; font-size: 20px;"> Personal Profile </p>'
|
108 |
+
st.markdown(header_section_1, unsafe_allow_html=True)
|
109 |
+
|
110 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
111 |
+
st.markdown(f'<p style="background-color:#0066cc;color:#33ff33;font-size:24px;border-radius:2%;"></p>', unsafe_allow_html=True)
|
112 |
+
with col1:
|
113 |
+
img_personal_profile_path = os.path.join(img_path, '02 - personal profile.png')
|
114 |
+
image = Image.open(img_personal_profile_path)
|
115 |
+
st.image(image, width=350)
|
116 |
+
|
117 |
+
with col2:
|
118 |
+
col_name = st.text_input('Name', value='', help='Player\'s name')
|
119 |
+
col_age = st.number_input('Age', min_value=14, max_value=60, value=22, step=1, help='Player\'s age. Default age is 22.')
|
120 |
+
col_price = st.number_input('Price (EUR)', min_value=0, value=1000000, step=1, format='%d', help='Player\'s price. Default price is EUR 1,000,000.')
|
121 |
+
|
122 |
+
with col3:
|
123 |
+
col_height = st.number_input('Height (cm)', min_value=140, max_value=220, value=180, step=1, help='Player\'s height. Default height is 180 cm.')
|
124 |
+
col_weight = st.number_input('Weight (kg)', min_value=40, max_value=120, value=70, step=1, help='Player\'s weight. Default weight is 70 kg.')
|
125 |
+
|
126 |
+
## STEP 6.2 : Section 2
|
127 |
+
header_section_2 = '<p style="font-family:sans-serif; color:#67b8f8; font-size: 20px;"> Work Rate </p>'
|
128 |
+
st.markdown('---')
|
129 |
+
st.markdown(header_section_2, unsafe_allow_html=True)
|
130 |
+
|
131 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
132 |
+
with col1:
|
133 |
+
img_work_rate_path = os.path.join(img_path, '03 - work rate.png')
|
134 |
+
image = Image.open(img_work_rate_path)
|
135 |
+
st.image(image, width=250)
|
136 |
+
|
137 |
+
with col2:
|
138 |
+
col_attacking_work_rate = st.selectbox('Attacking Work Rate', ['-', 'Low', 'Medium', 'High'], index=0, help='Player\'s desire to attack.')
|
139 |
+
col_defensive_work_rate = st.selectbox('Defensive Work Rate', ['-', 'Low', 'Medium', 'High'], index=0, help='Player\'s desire to defend.')
|
140 |
+
|
141 |
+
## STEP 6.3 : Section 3
|
142 |
+
header_section_3 = '<p style="font-family:sans-serif; color:#67b8f8; font-size: 20px;"> Ability </p>'
|
143 |
+
st.markdown('---')
|
144 |
+
st.markdown(header_section_3, unsafe_allow_html=True)
|
145 |
+
|
146 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
147 |
+
with col1:
|
148 |
+
img_work_rate_path = os.path.join(img_path, '04 - ability.png')
|
149 |
+
image = Image.open(img_work_rate_path)
|
150 |
+
st.image(image, width=350)
|
151 |
+
|
152 |
+
with col2:
|
153 |
+
col_pace_total = st.number_input('Pace Total', min_value=0, max_value=100, value=50, step=1, help='How fast is a player.')
|
154 |
+
col_shooting_total = st.number_input('Shooting Total', min_value=0, max_value=100, value=50, step=1, help='How good at kicking.')
|
155 |
+
col_passing_total = st.number_input('Passing Total', min_value=0, max_value=100, value=50, step=1, help='How good at passing.')
|
156 |
+
|
157 |
+
with col3:
|
158 |
+
col_dribbling_total = st.number_input('Dribbling Total', min_value=0, max_value=100, value=50, step=1, help='How good at dribbling.')
|
159 |
+
col_defending_total = st.number_input('Defending Total', min_value=0, max_value=100, value=50, step=1, help='How good at defending.')
|
160 |
+
col_physicality_total = st.number_input('Physicality Total', min_value=0, max_value=100, value=50, step=1, help='How good is a player\'s physique.')
|
161 |
+
|
162 |
+
## STEP 6.4 : Section 4
|
163 |
+
st.markdown('<br><br>', unsafe_allow_html=True)
|
164 |
+
col1, col2, col3 = st.columns([3, 1, 3])
|
165 |
+
with col2:
|
166 |
+
submitted = st.form_submit_button('Predict')
|
167 |
+
|
168 |
+
# STEP 7 - PREDICT NEW DATA
|
169 |
+
## STEP 7.1 - Create DataFrame for New Data
|
170 |
+
## `new_data` is for inference meanwhile `new_data_for_radar_plot` is for plot line_polar.
|
171 |
+
|
172 |
+
new_data = {
|
173 |
+
'Name': [col_name],
|
174 |
+
'Age': [col_age],
|
175 |
+
'Height': [col_height],
|
176 |
+
'Weight': [col_weight],
|
177 |
+
'Price': [col_price],
|
178 |
+
'AttackingWorkRate': [col_attacking_work_rate],
|
179 |
+
'DefensiveWorkRate': [col_defensive_work_rate],
|
180 |
+
'PaceTotal': [col_pace_total],
|
181 |
+
'ShootingTotal': [col_shooting_total],
|
182 |
+
'PassingTotal': [col_passing_total],
|
183 |
+
'DribblingTotal': [col_dribbling_total],
|
184 |
+
'DefendingTotal': [col_defending_total],
|
185 |
+
'PhysicalityTotal': [col_physicality_total]
|
186 |
+
}
|
187 |
+
|
188 |
+
new_data_for_radar_plot = {
|
189 |
+
'PaceTotal': [col_pace_total],
|
190 |
+
'ShootingTotal': [col_shooting_total],
|
191 |
+
'PassingTotal': [col_passing_total],
|
192 |
+
'DribblingTotal': [col_dribbling_total],
|
193 |
+
'DefendingTotal': [col_defending_total],
|
194 |
+
'PhysicalityTotal': [col_physicality_total]
|
195 |
+
}
|
196 |
+
|
197 |
+
new_data = pd.DataFrame.from_dict(new_data)
|
198 |
+
new_data_for_radar_plot = pd.DataFrame.from_dict(new_data_for_radar_plot)
|
199 |
+
# st.write(new_data)
|
200 |
+
print('New Data : ', new_data)
|
201 |
+
|
202 |
+
result_section = st.empty()
|
203 |
+
if submitted :
|
204 |
+
## STEP 7.2 - Split Numerical Columns and Categorical Columns
|
205 |
+
num_columns = ['Age', 'Height', 'Weight', 'Price', 'PaceTotal', 'ShootingTotal', 'PassingTotal', 'DribblingTotal', 'DefendingTotal', 'PhysicalityTotal']
|
206 |
+
cat_columns = ['AttackingWorkRate', 'DefensiveWorkRate']
|
207 |
+
|
208 |
+
new_data_num = new_data[num_columns]
|
209 |
+
new_data_cat = new_data[cat_columns]
|
210 |
+
|
211 |
+
## STEP 7.3 - Feature Scaling and Feature Encoding
|
212 |
+
new_data_num_scaled = scaler.transform(new_data_num)
|
213 |
+
new_data_cat_encoded = encoder.transform(new_data_cat)
|
214 |
+
|
215 |
+
## STEP 7.4 - Concatenate between Numerical Columns and Categorical Columns
|
216 |
+
new_data_final = np.concatenate([new_data_num_scaled, new_data_cat_encoded], axis=1)
|
217 |
+
|
218 |
+
## STEP 7.5 - Predict using Linear Regression
|
219 |
+
y_pred_inf = model_rating.predict(new_data_final)
|
220 |
+
print(type(y_pred_inf))
|
221 |
+
|
222 |
+
## STEP 7.6 - Display Prediction
|
223 |
+
result_section.empty()
|
224 |
+
bar = st.progress(0)
|
225 |
+
for i in range(100):
|
226 |
+
bar.progress(i + 1)
|
227 |
+
time.sleep(0.01)
|
228 |
+
bar.empty()
|
229 |
+
|
230 |
+
with result_section.container():
|
231 |
+
st.markdown('<br><br>', unsafe_allow_html=True)
|
232 |
+
|
233 |
+
col1, col2, col3, col4, col5 = st.columns([0.5, 2, 1, 2, 1])
|
234 |
+
with col2:
|
235 |
+
st.markdown("")
|
236 |
+
|
237 |
+
with col3:
|
238 |
+
st.markdown('<br><br><br><br>', unsafe_allow_html=True)
|
239 |
+
player_name = '<h1 style="font-family:helvetica; color:#fff9ac; text-align:center"> ' + col_name + ' </h1>'
|
240 |
+
st.markdown(player_name, unsafe_allow_html=True)
|
241 |
+
|
242 |
+
player_rating_pred = '<p style="font-family:helvetica; color:#fff9ac; font-size:100px; text-align:center"> <b>' + str(int(y_pred_inf)) + ' </p>'
|
243 |
+
st.markdown(player_rating_pred, unsafe_allow_html=True)
|
244 |
+
|
245 |
+
with col4:
|
246 |
+
st.markdown('<br><br>', unsafe_allow_html=True)
|
247 |
+
skill_total_fig = px.line_polar(
|
248 |
+
r = new_data_for_radar_plot.loc[0].values,
|
249 |
+
theta = new_data_for_radar_plot.columns,
|
250 |
+
line_close = True,
|
251 |
+
range_r = [0, 100],
|
252 |
+
color_discrete_sequence = ['#FFF9AC'],
|
253 |
+
# hover_name=['PaceTotal', '1', '2', '4', '5', '6'],
|
254 |
+
template='plotly_dark')
|
255 |
+
skill_total_fig.update_traces(fill='toself')
|
256 |
+
skill_total_fig.update_layout({
|
257 |
+
'plot_bgcolor': 'rgba(255, 0, 0, 0)',
|
258 |
+
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
|
259 |
+
'font_size': 19
|
260 |
+
})
|
261 |
+
st.write(skill_total_fig)
|
262 |
+
|
263 |
+
st.write('''\n\n\n\n Source images :
|
264 |
+
[link](https://www.vecteezy.com/vector-art/5129950-football-player-figure-line-art-human-action-on-motion-lines-controlling-the-ball-with-chest),
|
265 |
+
[link](https://www.vecteezy.com/vector-art/5939693-football-player-figure-line-art-human-action-on-motion-lines-kicking-ball),
|
266 |
+
[link](https://www.vecteezy.com/vector-art/5129956-football-player-figure-line-art-human-action-on-motion-lines-kicking-ball)
|
267 |
+
''')
|
img/.DS_Store
ADDED
Binary file (10.2 kB). View file
|
|
img/01 - background.jpg
ADDED
![]() |
img/02 - personal profile.png
ADDED
![]() |
img/03 - work rate.png
ADDED
![]() |
img/04 - ability.png
ADDED
![]() |
model/model_feat_enc.pkl
ADDED
Binary file (646 Bytes). View file
|
|
model/model_feat_scaling.pkl
ADDED
Binary file (1.24 kB). View file
|
|
model/model_rating.pkl
ADDED
Binary file (743 Bytes). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
pandas
|
3 |
+
plotly
|
4 |
+
scikit-learn
|