|
import gradio as gr |
|
import joblib |
|
import pandas as pd |
|
import numpy as np |
|
|
|
|
|
model_path = 'tiebreak_model_v1b.pkl' |
|
encoder_path = 'ordinal_encoder_tiebreak_model_v1b.pkl' |
|
|
|
model = joblib.load(model_path) |
|
encoder = joblib.load(encoder_path) |
|
|
|
|
|
def predict_tiebreak(df): |
|
def calculate_features(row): |
|
odds_min = min(row['Odds 1'], row['Odds 2']) |
|
odds_max = max(row['Odds 1'], row['Odds 2']) |
|
odds_ratio = odds_min / odds_max |
|
diff_log_odds = np.log(odds_max) / np.log(odds_min) |
|
sum_prob = (1 / odds_min) + (1 / odds_max) |
|
mean_log_odds = (np.log(odds_max) + np.log(odds_min)) / 2 |
|
|
|
return pd.Series([mean_log_odds, sum_prob, odds_ratio]) |
|
|
|
df[['Mean_Log_Odds', 'Sum_Prob', 'Ratio_Log_Odds']] = df.apply(calculate_features, axis=1) |
|
|
|
|
|
bin_columns = ['Mean_Log_Odds', 'Sum_Prob'] |
|
bin_transformed_columns = [f"{col}_bin" for col in bin_columns] |
|
|
|
for column in bin_columns: |
|
optb = joblib.load(f'{column}_binning_tiebreak_model_v1b.pkl') |
|
df[f"{column}_bin"] = optb.transform(df[column], metric="bins") |
|
|
|
|
|
df[bin_transformed_columns] = encoder.transform(df[bin_transformed_columns]) |
|
|
|
|
|
features = df[['Mean_Log_Odds_bin', 'Sum_Prob_bin', 'Ratio_Log_Odds']] |
|
df['Probability'] = model.predict_proba(features)[:, 1] |
|
|
|
|
|
best_threshold = 0.9420000000000005 |
|
df['entrada'] = df['Probability'] >= best_threshold |
|
df = df[df['entrada'] == True] |
|
|
|
return df |
|
|
|
|
|
def predict_from_excel(file): |
|
df = pd.read_excel(file) |
|
df_predictions = predict_tiebreak(df) |
|
|
|
|
|
output_file = "predictions.xlsx" |
|
df_predictions.to_excel(output_file, index=False) |
|
|
|
return df_predictions, output_file |
|
|
|
|
|
inputs = gr.File(label="Upload Excel File") |
|
outputs = [gr.DataFrame(label="Tabela de Previsões"), gr.File(label="Download Predictions Excel")] |
|
|
|
|
|
gr.Interface( |
|
fn=predict_from_excel, |
|
inputs=inputs, |
|
outputs=outputs, |
|
title="Previsão de Tiebreaks", |
|
description="Faça o upload de um arquivo Excel contendo dados no formato final_df para prever a probabilidade de menos de 1.5 tiebreaks e verificar se deve entrar na aposta." |
|
).launch() |
|
|