Spaces:
Runtime error
Runtime error
Hugo Lindgren
commited on
Commit
·
d757d64
1
Parent(s):
bcf8053
start
Browse files- app.py +64 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import requests
|
| 4 |
+
import hopsworks
|
| 5 |
+
import joblib
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import datetime
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
year = datetime.date.today().year
|
| 11 |
+
|
| 12 |
+
project = hopsworks.login()
|
| 13 |
+
fs = project.get_feature_store()
|
| 14 |
+
mr = project.get_model_registry()
|
| 15 |
+
model_url = f"valuation_model_v{year-1}_{year}"
|
| 16 |
+
model = mr.get_model(model_url)
|
| 17 |
+
model_dir = model.download()
|
| 18 |
+
model = joblib.load(f"{model_dir}/valuation_model.pkl")
|
| 19 |
+
scaler = joblib.load(f"{model_dir}/scaler.pkl")
|
| 20 |
+
|
| 21 |
+
#Get data that has not been trained on
|
| 22 |
+
fg = fs.get_or_create_feature_group(
|
| 23 |
+
name='prediction_valuation_fg',
|
| 24 |
+
version=1,
|
| 25 |
+
description='Feature group containing X_test and y_test data for model performance measurement',
|
| 26 |
+
primary_key=['player_id', 'date'] # Replace with your primary key column name(s)
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
df = fg.read()
|
| 30 |
+
print(df)
|
| 31 |
+
|
| 32 |
+
def valuation_predictor(goals, assists, y_cards, r_cards,
|
| 33 |
+
m_played, height, age, mo_left,
|
| 34 |
+
t_made_goals, t_conceded_goals, t_clean_sheets,
|
| 35 |
+
pos, league):
|
| 36 |
+
|
| 37 |
+
features_for_prediction = [
|
| 38 |
+
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"
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
column_names = [
|
| 42 |
+
"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"
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Create DataFrame from the input features
|
| 47 |
+
X_predict = pd.DataFrame([features_for_prediction], columns=column_names)
|
| 48 |
+
# Apply the scaler to the relevant columns
|
| 49 |
+
scaled_columns = ['age', 'height_in_cm', 'minutes_played']
|
| 50 |
+
X_predict[scaled_columns] = scaler.transform(X_predict[scaled_columns])
|
| 51 |
+
estimated_valuation = model.predict(X_predict)
|
| 52 |
+
|
| 53 |
+
# Reformatting to the nearest thousand with commas
|
| 54 |
+
formatted_output_thousand = f"{round(estimated_valuation[0], -3):,} EUR"
|
| 55 |
+
return formatted_output_thousand
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 59 |
+
with gr.Row():
|
| 60 |
+
gr.Label(value="Latest predictions", show_label=False)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
hopsworks
|
| 4 |
+
joblib
|
| 5 |
+
pandas
|
| 6 |
+
scikit-learn
|