Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load your dataframe
|
| 7 |
-
df = pd.read_csv("data.csv") # Replace with your actual data file
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
input_columns = df.columns[:-1].tolist() # First 4 columns as input
|
| 11 |
target_column = df.columns[-1] # Last column as target
|
|
|
|
| 12 |
|
| 13 |
# Min-Max scaling per column
|
| 14 |
min_max_dict = {
|
|
@@ -16,15 +22,24 @@ min_max_dict = {
|
|
| 16 |
}
|
| 17 |
|
| 18 |
# Load your PyCaret model
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
# Predict using PyCaret model
|
| 26 |
-
prediction = predict_model(model, data=input_data)
|
| 27 |
-
return prediction[target_column].values[0]
|
| 28 |
|
| 29 |
# Create Gradio inputs dynamically based on scaled range
|
| 30 |
input_components = [
|
|
@@ -47,7 +62,8 @@ with gr.Blocks() as demo:
|
|
| 47 |
|
| 48 |
predict_btn = gr.Button("Predict")
|
| 49 |
|
| 50 |
-
predict_btn.click(fn=pycaret_predict_function,
|
|
|
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
demo.launch()
|
|
|
|
| 1 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
+
# Change to regression if needed
|
| 6 |
+
from pycaret.regression import load_model, predict_model, setup
|
| 7 |
+
|
| 8 |
|
| 9 |
# Load your dataframe
|
| 10 |
+
df = pd.read_csv("data.csv", index_col=0) # Replace with your actual data file
|
| 11 |
|
| 12 |
+
clf1 = setup(data=df, target='Mu (kN⋅m)', normalize=False)
|
| 13 |
+
# Use the same scaler that was used during training (important!)
|
| 14 |
+
scaler = MinMaxScaler()
|
| 15 |
input_columns = df.columns[:-1].tolist() # First 4 columns as input
|
| 16 |
target_column = df.columns[-1] # Last column as target
|
| 17 |
+
scaler.fit(df[input_columns])
|
| 18 |
|
| 19 |
# Min-Max scaling per column
|
| 20 |
min_max_dict = {
|
|
|
|
| 22 |
}
|
| 23 |
|
| 24 |
# Load your PyCaret model
|
| 25 |
+
# Replace with your actual saved model
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def pycaret_predict_function(*inputs):
|
| 29 |
+
# Convert input tuple to DataFrame
|
| 30 |
+
dfx = pd.DataFrame([inputs], columns=input_columns)
|
| 31 |
+
dfx_scaled = pd.DataFrame(scaler.transform(dfx), columns=input_columns)
|
| 32 |
+
breakpoint()
|
| 33 |
+
print("Input DataFrame for prediction:", dfx_scaled)
|
| 34 |
+
model = load_model("TVAESynthesizer_best")
|
| 35 |
+
prediction = predict_model(model, data=dfx_scaled)
|
| 36 |
+
return prediction["prediction_label"].values[0]
|
| 37 |
|
| 38 |
+
try:
|
| 39 |
+
pass
|
| 40 |
+
except KeyError as e:
|
| 41 |
+
return 'Error: Prediction label not found. Please check the model output: ' + str(e)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Create Gradio inputs dynamically based on scaled range
|
| 45 |
input_components = [
|
|
|
|
| 62 |
|
| 63 |
predict_btn = gr.Button("Predict")
|
| 64 |
|
| 65 |
+
predict_btn.click(fn=pycaret_predict_function,
|
| 66 |
+
inputs=inputs, outputs=output)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|