Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from PIL import Image | |
| import requests | |
| import hopsworks | |
| import joblib | |
| import pandas as pd | |
| import datetime | |
| import os | |
| year = datetime.date.today().year - 1 | |
| project = hopsworks.login() | |
| fs = project.get_feature_store() | |
| mr = project.get_model_registry() | |
| model_url = f"valuation_model_v{year-1}_{year}" | |
| model = mr.get_model(model_url) | |
| model_dir = model.download() | |
| model = joblib.load(f"{model_dir}/valuation_model.pkl") | |
| scaler = joblib.load(f"{model_dir}/scaler.pkl") | |
| print("Model downloaded") | |
| def valuation_predictor(goals, assists, y_cards, r_cards, | |
| m_played, height, age, mo_left, | |
| t_made_goals, t_conceded_goals, t_clean_sheets, | |
| pos, league): | |
| features_for_prediction = [ | |
| y_cards, r_cards, goals, assists, m_played, height, age, mo_left, t_made_goals, t_conceded_goals, t_clean_sheets, league=="La Liga", league=="League 1", league=="Premier league", league=="Serie A", league=="Bundesliga", pos=="Attack", pos=="Defender", pos=="Goalkeeper", pos=="Midfield" | |
| ] | |
| column_names = [ | |
| "yellow_cards", "red_cards", "goals", "assists", "minutes_played", "height_in_cm", "age", "months_left", "own_goals", "opponent_goals", "clean_sheets", "league_es1","league_fr1", "league_gb1","league_it1","league_l1", "position_attack","position_defender", "position_goalkeeper","position_midfield" | |
| ] | |
| # Create DataFrame from the input features | |
| X_predict = pd.DataFrame([features_for_prediction], columns=column_names) | |
| # Apply the scaler to the relevant columns | |
| scaled_columns = ['age', 'height_in_cm', 'minutes_played'] | |
| X_predict[scaled_columns] = scaler.transform(X_predict[scaled_columns]) | |
| estimated_valuation = model.predict(X_predict) | |
| # Reformatting to the nearest thousand with commas | |
| formatted_output_thousand = f"{round(estimated_valuation[0], -3):,} EUR" | |
| return formatted_output_thousand | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Label(value="Player specific stats", show_label=False) | |
| goals = gr.Number(value=7.0, label="Goals") | |
| assists = gr.Number(value=5, label="Assists") | |
| y_cards = gr.Number(value=5, label="Yellow cards") | |
| r_cards = gr.Number(value=0, label="Red cards") | |
| m_played = gr.Number(value=1500, label="Minutes played") | |
| height = gr.Number(value=175, label="Height (cm)") | |
| age = gr.Number(value=25, label="Age") | |
| mo_left = gr.Number(value=36, label="Months left on contract") | |
| with gr.Column(): | |
| gr.Label(value="Team specific stats", show_label=False) | |
| t_made_goals = gr.Number(value=60, label="Goals made by player's team during the season") | |
| t_conceded_goals = gr.Number(value=43, label="Goals conceded by player's team during the season") | |
| t_clean_sheets = gr.Number(value=15, label="Number of times player's team has let in 0 goals during a game") | |
| gr.Label(value="League and position data", show_label=False) | |
| pos = gr.Dropdown(choices=["Attack", "Defender", "Goalkeeper", "Midfield"]) | |
| league = gr.Dropdown(choices=["La Liga","League 1", "Premier league", "Serie A", "Bundesliga"]) | |
| with gr.Row(): | |
| btn = gr.Button("Predict player value") | |
| estimated_player_value = gr.Textbox(label="Estimated player value...") | |
| btn.click(fn=valuation_predictor, | |
| inputs=[ | |
| goals, assists, y_cards, r_cards, | |
| m_played, height, age, mo_left, | |
| t_made_goals, t_conceded_goals, t_clean_sheets, | |
| pos, league], | |
| outputs=estimated_player_value | |
| ) | |
| demo.launch() |