import os import time import base64 import pickle import numpy as np import pandas as pd import streamlit as st import plotly.express as px from PIL import Image from collections import deque from urllib.request import urlopen # STEP 1 - DEFINE PATHS base_path = os.path.abspath(os.path.dirname(__file__)) model_path = os.path.join(base_path, 'model') img_path = os.path.join(base_path, 'img') # STEP 2 - LOAD MODEL model_filename = 'model_rating.pkl' scaler_filename = 'model_feat_scaling.pkl' encoder_filename = 'model_feat_enc.pkl' model_filepath = os.path.join(model_path, model_filename) scaler_filepath = os.path.join(model_path, scaler_filename) encoder_filepath = os.path.join(model_path, encoder_filename) with open(model_filepath, "rb") as filename: model_rating = pickle.load(filename) with open(scaler_filepath, "rb") as filename: scaler = pickle.load(filename) with open(encoder_filepath, "rb") as filename: encoder = pickle.load(filename) # STEP 3 - SET PAGE CONFIG st.set_page_config( page_title = 'FIFA 2022 Player Rating\'s Prediction', layout = 'wide', initial_sidebar_state = 'auto', menu_items = { 'About': ''' ## FIFA 2022 Player Rating\'s Prediction --- _Made by Danu Purnomo_ Predict rating of a football player based on FIFA 2022 players. ''' } ) # STEP 4 - CREATE BACKGROUND def convert_img_to_base64(img_path): with open(img_path, 'rb') as image_file: encoded_string = base64.b64encode(image_file.read()) return encoded_string img_background_path = os.path.join(img_path, '01 - background.jpg') # img_background_path = os.path.join(img_path, 'test-02.jpg') encoded_string = convert_img_to_base64(img_background_path) st.markdown( f""" """, unsafe_allow_html=True ) # STEP 5 - SET TITLE AND OPENER ## STEP 5.1 - SET TITLE text_title = '
Set Parameters
' st.markdown(text_style, unsafe_allow_html=True) # Attribute of a football player # 0 Name 19260 non-null object # 1 Age 19260 non-null int64 # 2 Height 19260 non-null int64 # 3 Weight 19260 non-null int64 # 4 Price 19260 non-null int64 # 5 AttackingWorkRate 19260 non-null object # 6 DefensiveWorkRate 19260 non-null object # 7 PaceTotal 19260 non-null int64 # 8 ShootingTotal 19260 non-null int64 # 9 PassingTotal 19260 non-null int64 # 10 DribblingTotal 19260 non-null int64 # 11 DefendingTotal 19260 non-null int64 # 12 PhysicalityTotal 19260 non-null int64 # 13 Rating 19260 non-null int64 with st.form(key='form_parameters'): ## STEP 6.1 : Section 1 header_section_1 = 'Personal Profile
' st.markdown(header_section_1, unsafe_allow_html=True) col1, col2, col3 = st.columns([1, 1, 1]) st.markdown(f'', unsafe_allow_html=True) with col1: img_personal_profile_path = os.path.join(img_path, '02 - personal profile.png') image = Image.open(img_personal_profile_path) st.image(image, width=350) with col2: col_name = st.text_input('Name', value='', help='Player\'s name') col_age = st.number_input('Age', min_value=14, max_value=60, value=22, step=1, help='Player\'s age. Default age is 22.') 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.') with col3: 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.') 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.') ## STEP 6.2 : Section 2 header_section_2 = 'Work Rate
' st.markdown('---') st.markdown(header_section_2, unsafe_allow_html=True) col1, col2, col3 = st.columns([1, 1, 1]) with col1: img_work_rate_path = os.path.join(img_path, '03 - work rate.png') image = Image.open(img_work_rate_path) st.image(image, width=250) with col2: col_attacking_work_rate = st.selectbox('Attacking Work Rate', ['-', 'Low', 'Medium', 'High'], index=0, help='Player\'s desire to attack.') col_defensive_work_rate = st.selectbox('Defensive Work Rate', ['-', 'Low', 'Medium', 'High'], index=0, help='Player\'s desire to defend.') ## STEP 6.3 : Section 3 header_section_3 = 'Ability
' st.markdown('---') st.markdown(header_section_3, unsafe_allow_html=True) col1, col2, col3 = st.columns([1, 1, 1]) with col1: img_work_rate_path = os.path.join(img_path, '04 - ability.png') image = Image.open(img_work_rate_path) st.image(image, width=350) with col2: col_pace_total = st.number_input('Pace Total', min_value=0, max_value=100, value=50, step=1, help='How fast is a player.') col_shooting_total = st.number_input('Shooting Total', min_value=0, max_value=100, value=50, step=1, help='How good at kicking.') col_passing_total = st.number_input('Passing Total', min_value=0, max_value=100, value=50, step=1, help='How good at passing.') with col3: col_dribbling_total = st.number_input('Dribbling Total', min_value=0, max_value=100, value=50, step=1, help='How good at dribbling.') col_defending_total = st.number_input('Defending Total', min_value=0, max_value=100, value=50, step=1, help='How good at defending.') 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.') ## STEP 6.4 : Section 4 st.markdown('' + str(int(y_pred_inf)) + '
' st.markdown(player_rating_pred, unsafe_allow_html=True) with col4: st.markdown('