3koozy's picture
Update app.py
49bc541
import pandas as pd
import numpy as np
from IPython.display import display
from sklearn import preprocessing
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import OneHotEncoder
from pickle import dump, load
from sklearn.metrics import top_k_accuracy_score
# load the model
mlp_model = load(open('mlp_classifier.pkl', 'rb'))
# load the scaler
my_scaler = load(open('scaler.pkl', 'rb'))
hot_enc_scaler = load(open('hot_enc.pkl', 'rb'))
my_label_enc = load(open('label_enc.pkl', 'rb'))
import gradio as gr
team_list = ["China" , "Saudi Arabia", "United States", "Finland"]
description = '''
This small prototype uses Big Data and AI to guide beginner athletes in choosing the most suitable sport based on their bio info.
'''
def classify_sport(Sex,Age,Height,Weight,Team):
#pre-processing:
numerical_features = [[Age,Height,Weight]]
catagorical_features = [[Sex,Team]]
numerical_features = my_scaler.transform(numerical_features)
catagorical_features = hot_enc_scaler.transform(catagorical_features).toarray()
sample_player = np.concatenate((numerical_features[0], catagorical_features[0]), axis=0)
#predict:
mlp_predicted = mlp_model.predict_proba(sample_player.reshape(1, -1))
k = 5
mlp_predicted_topk_proba = np.sort(mlp_predicted[0])[-k:]
top_k_indicies = np.array(mlp_predicted[0].argsort()[-k:])
top_k_classes = my_label_enc.inverse_transform(top_k_indicies)
output_dict = {top_k_classes[i]: float(mlp_predicted_topk_proba[i]) for i in range(k)}
#advice:
advice = "Your Profile looks very promising!\nBased on your Bio, we suggest pursuing {} as a professional player.\nWe also believe that the following sports are very suitable for you: {}"\
.format(top_k_classes[-1], top_k_classes[:-1])
return output_dict, advice
demo = gr.Interface(
fn=classify_sport,
inputs=[gr.inputs.Dropdown(["M" , "F"]),gr.Slider(15, 80),gr.Slider(100, 200),gr.Slider(30, 200),
gr.inputs.Dropdown(team_list)],
outputs=[gr.outputs.Label(num_top_classes=5), gr.Text(label='Advice')],
title= "TalentAI - Suggest Suitable Sport",
description= description,
article= "Abdulaziz Alakooz developed this prototype as part of Thkaa AI in sports contest - August 2022.")
demo.launch()