Spaces:
Sleeping
Sleeping
from sklearn.preprocessing import MinMaxScaler | |
import gradio as gr | |
import pandas as pd | |
import numpy as np | |
# Change to regression if needed | |
from pycaret.regression import load_model, predict_model, setup | |
# Load your dataframe | |
df = pd.read_csv("data.csv", index_col=0) # Replace with your actual data file | |
clf1 = setup(data=df, target='Mu (kN⋅m)', normalize=False) | |
# Use the same scaler that was used during training (important!) | |
scaler = MinMaxScaler() | |
input_columns = df.columns[:-1].tolist() # First 4 columns as input | |
target_column = df.columns[-1] # Last column as target | |
scaler.fit(df[input_columns]) | |
# Min-Max scaling per column | |
min_max_dict = { | |
col: (df[col].min(), df[col].max()) for col in input_columns | |
} | |
# Load your PyCaret model | |
# Replace with your actual saved model | |
def pycaret_predict_function(*inputs): | |
# Convert input tuple to DataFrame | |
dfx = pd.DataFrame([inputs], columns=input_columns) | |
dfx_scaled = pd.DataFrame(scaler.transform(dfx), columns=input_columns) | |
breakpoint() | |
print("Input DataFrame for prediction:", dfx_scaled) | |
model = load_model("TVAESynthesizer_best") | |
prediction = predict_model(model, data=dfx_scaled) | |
return prediction["prediction_label"].values[0] | |
try: | |
pass | |
except KeyError as e: | |
return 'Error: Prediction label not found. Please check the model output: ' + str(e) | |
# Create Gradio inputs dynamically based on scaled range | |
input_components = [ | |
gr.Slider( | |
minimum=min_max_dict[col][0], | |
maximum=min_max_dict[col][1], | |
step=(min_max_dict[col][1] - min_max_dict[col][0]) / 100, | |
label=col, | |
) for col in input_columns | |
] | |
# Gradio UI | |
with gr.Blocks() as demo: | |
gr.Markdown("## 🔮 Predict the Target Variable using PyCaret Model") | |
with gr.Row(): | |
inputs = [component.render() for component in input_components] | |
output = gr.Textbox(label=f"Predicted: {target_column}") | |
predict_btn = gr.Button("Predict") | |
predict_btn.click(fn=pycaret_predict_function, | |
inputs=inputs, outputs=output) | |
if __name__ == "__main__": | |
demo.launch() | |