Spaces:
Sleeping
Sleeping
File size: 2,123 Bytes
d0da8d5 bf3366a 465b9da bf3366a d0da8d5 bf3366a 465b9da d0da8d5 bf3366a d0da8d5 465b9da d0da8d5 bf3366a 465b9da bf3366a 465b9da d0da8d5 465b9da d0da8d5 465b9da bf3366a 465b9da bf3366a 465b9da bf3366a 465b9da bf3366a 465b9da bf3366a d0da8d5 bf3366a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
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()
|