DataWizard9742's picture
Update app.py
fde95e9 verified
raw
history blame
4.2 kB
import pandas as pd
import pickle
import gradio as gr
from sklearn.preprocessing import LabelEncoder
# --- 1. Load the original dataset to get categorical values and create encoders ---
# Make sure this path is correct if your dataset location has changed
df_original = pd.read_csv('AB_ELEVATORS_DATASET_final.csv')
categorical_cols_original = [
'BRAND NAME', 'LIFT_TYPE', 'DOOR_FRAME', 'DOOR_TYPE', 'CABIN_MATERIAL', 'MOTOR_TYPE'
]
numerical_cols_original = [
'PASSENGERS CAPACITY', 'SPEED_MPS', 'FLOORS'
]
# Store label encoders for each column
encoders = {}
for col in categorical_cols_original:
le = LabelEncoder()
le.fit(df_original[col])
encoders[col] = le
# Create mappings for Gradio dropdowns (original string values)
categorical_options = {
col: list(df_original[col].unique()) for col in categorical_cols_original
}
# --- 2. Load the trained model ---
with open("elevator_model(1).pkl", "rb") as f:
model = pickle.load(f)
# --- 3. Define the prediction function ---
def predict_price(
brand_name_str,
lift_type_str,
door_frame_str,
door_type_str,
cabin_material_str,
motor_type_str,
passengers_capacity,
speed_mps,
floors
):
# Encode categorical inputs using the fitted encoders
brand_name_encoded = encoders['BRAND NAME'].transform([brand_name_str])[0]
lift_type_encoded = encoders['LIFT_TYPE'].transform([lift_type_str])[0]
door_frame_encoded = encoders['DOOR_FRAME'].transform([door_frame_str])[0]
door_type_encoded = encoders['DOOR_TYPE'].transform([door_type_str])[0]
cabin_material_encoded = encoders['CABIN_MATERIAL'].transform([cabin_material_str])[0]
motor_type_encoded = encoders['MOTOR_TYPE'].transform([motor_type_str])[0]
# Create DataFrame with correct lowercased and snake_cased column names and order
input_data = pd.DataFrame([[
brand_name_encoded,
lift_type_encoded,
door_frame_encoded,
door_type_encoded,
cabin_material_encoded,
motor_type_encoded,
passengers_capacity,
speed_mps,
floors
]], columns=[
'brand_name', 'lift_type', 'door_frame', 'door_type', 'cabin_material',
'motor_type', 'passengers_capacity', 'speed_mps', 'floors'
])
prediction = model.predict(input_data)[0]
return f"Predicted Price: ₹{prediction:,.2f}", "VISIT AGAIN 😊"
# --- 4. Create Gradio Interface ---
inputs = [
gr.Dropdown(categorical_options['BRAND NAME'], label="Brand Name", value=categorical_options['BRAND NAME'][0]),
gr.Dropdown(categorical_options['LIFT_TYPE'], label="Lift Type", value=categorical_options['LIFT_TYPE'][0]),
gr.Dropdown(categorical_options['DOOR_FRAME'], label="Door Frame", value=categorical_options['DOOR_FRAME'][0]),
gr.Dropdown(categorical_options['DOOR_TYPE'], label="Door Type", value=categorical_options['DOOR_TYPE'][0]),
gr.Dropdown(categorical_options['CABIN_MATERIAL'], label="Cabin Material", value=categorical_options['CABIN_MATERIAL'][0]),
gr.Dropdown(categorical_options['MOTOR_TYPE'], label="Motor Type", value=categorical_options['MOTOR_TYPE'][0]),
gr.Slider(minimum=df_original['PASSENGERS CAPACITY'].min(), maximum=df_original['PASSENGERS CAPACITY'].max(), step=1, label="Passengers Capacity", value=int(df_original['PASSENGERS CAPACITY'].mean())),
gr.Slider(minimum=df_original['SPEED_MPS'].min(), maximum=df_original['SPEED_MPS'].max(), step=0.1, label="Speed (MPS)", value=df_original['SPEED_MPS'].mean()),
gr.Slider(minimum=df_original['FLOORS'].min(), maximum=df_original['FLOORS'].max(), step=1, label="Floors", value=int(df_original['FLOORS'].mean()))
]
outputs = [
gr.Textbox(label="Prediction Result"),
gr.Markdown()
]
iface = gr.Interface(
fn=predict_price,
inputs=inputs,
outputs=outputs,
title="AB ELEVATOR Company - Lift Quotation",
description="<h1 style='color: gold; border: 2px solid black; padding: 10px; text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;'>WELCOME TO AB ELEVATORS</h1>", # Styled welcome message
live=False
)
# Launch the interface
iface.launch()
iface.launch(server_name="0.0.0.0", server_port=7860)