gamaba commited on
Commit
ac41ad0
Β·
verified Β·
1 Parent(s): 9b7ab28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -64
app.py CHANGED
@@ -3,34 +3,29 @@ import pickle
3
  import gradio as gr
4
  from sklearn.preprocessing import LabelEncoder
5
 
6
- # --- 1. Load the original dataset to get categorical values and create encoders ---
7
- # Make sure this path is correct if your dataset location has changed
8
  df_original = pd.read_csv('AB_ELEVATORS_DATASET_final.csv')
9
 
10
  categorical_cols_original = [
11
  'BRAND NAME', 'LIFT_TYPE', 'DOOR_FRAME', 'DOOR_TYPE', 'CABIN_MATERIAL', 'MOTOR_TYPE'
12
  ]
13
- numerical_cols_original = [
14
- 'PASSENGERS CAPACITY', 'SPEED_MPS', 'FLOORS'
15
- ]
16
 
17
- # Store label encoders for each column
18
  encoders = {}
19
  for col in categorical_cols_original:
20
  le = LabelEncoder()
21
  le.fit(df_original[col])
22
  encoders[col] = le
23
 
24
- # Create mappings for Gradio dropdowns (original string values)
25
  categorical_options = {
26
  col: list(df_original[col].unique()) for col in categorical_cols_original
27
  }
28
 
29
- # --- 2. Load the trained model ---
30
  with open("elevator_model.pkl", "rb") as f:
31
  model = pickle.load(f)
32
 
33
- # --- 3. Define the prediction function ---
34
  def predict_price(
35
  brand_name_str,
36
  lift_type_str,
@@ -42,60 +37,104 @@ def predict_price(
42
  speed_mps,
43
  floors
44
  ):
45
- # Encode categorical inputs using the fitted encoders
46
- brand_name_encoded = encoders['BRAND NAME'].transform([brand_name_str])[0]
47
- lift_type_encoded = encoders['LIFT_TYPE'].transform([lift_type_str])[0]
48
- door_frame_encoded = encoders['DOOR_FRAME'].transform([door_frame_str])[0]
49
- door_type_encoded = encoders['DOOR_TYPE'].transform([door_type_str])[0]
50
- cabin_material_encoded = encoders['CABIN_MATERIAL'].transform([cabin_material_str])[0]
51
- motor_type_encoded = encoders['MOTOR_TYPE'].transform([motor_type_str])[0]
52
-
53
- # Create DataFrame with correct lowercased and snake_cased column names and order
54
- input_data = pd.DataFrame([[
55
- brand_name_encoded,
56
- lift_type_encoded,
57
- door_frame_encoded,
58
- door_type_encoded,
59
- cabin_material_encoded,
60
- motor_type_encoded,
61
- passengers_capacity,
62
- speed_mps,
63
- floors
64
- ]], columns=[
65
- 'brand_name', 'lift_type', 'door_frame', 'door_type', 'cabin_material',
66
- 'motor_type', 'passengers_capacity', 'speed_mps', 'floors'
67
- ])
68
-
69
- prediction = model.predict(input_data)[0]
70
- prediction = int(prediction)
71
- return f"Predicted Price: β‚Ή{prediction:,.2f}", "VISIT AGAIN 😊"
72
-
73
- # --- 4. Create Gradio Interface ---
74
- inputs = [
75
- gr.Dropdown(categorical_options['BRAND NAME'], label="Brand Name", value=categorical_options['BRAND NAME'][0]),
76
- gr.Dropdown(categorical_options['LIFT_TYPE'], label="Lift Type", value=categorical_options['LIFT_TYPE'][0]),
77
- gr.Dropdown(categorical_options['DOOR_FRAME'], label="Door Frame", value=categorical_options['DOOR_FRAME'][0]),
78
- gr.Dropdown(categorical_options['DOOR_TYPE'], label="Door Type", value=categorical_options['DOOR_TYPE'][0]),
79
- gr.Dropdown(categorical_options['CABIN_MATERIAL'], label="Cabin Material", value=categorical_options['CABIN_MATERIAL'][0]),
80
- gr.Dropdown(categorical_options['MOTOR_TYPE'], label="Motor Type", value=categorical_options['MOTOR_TYPE'][0]),
81
- 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())),
82
- 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()),
83
- gr.Slider(minimum=df_original['FLOORS'].min(), maximum=df_original['FLOORS'].max(), step=1, label="Floors", value=int(df_original['FLOORS'].mean()))
84
- ]
85
 
86
- outputs = [
87
- gr.Textbox(label="Prediction Result"),
88
- gr.Markdown()
89
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- iface = gr.Interface(
92
- fn=predict_price,
93
- inputs=inputs,
94
- outputs=outputs,
95
- title="AB ELEVATOR Company - Lift Quotation",
96
- 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
97
- live=False
98
- )
99
-
100
- # Launch the interface
101
- iface.launch()
 
3
  import gradio as gr
4
  from sklearn.preprocessing import LabelEncoder
5
 
6
+ # --- Load dataset ---
 
7
  df_original = pd.read_csv('AB_ELEVATORS_DATASET_final.csv')
8
 
9
  categorical_cols_original = [
10
  'BRAND NAME', 'LIFT_TYPE', 'DOOR_FRAME', 'DOOR_TYPE', 'CABIN_MATERIAL', 'MOTOR_TYPE'
11
  ]
 
 
 
12
 
13
+ # --- Create encoders ---
14
  encoders = {}
15
  for col in categorical_cols_original:
16
  le = LabelEncoder()
17
  le.fit(df_original[col])
18
  encoders[col] = le
19
 
 
20
  categorical_options = {
21
  col: list(df_original[col].unique()) for col in categorical_cols_original
22
  }
23
 
24
+ # --- Load model ---
25
  with open("elevator_model.pkl", "rb") as f:
26
  model = pickle.load(f)
27
 
28
+ # --- Prediction function ---
29
  def predict_price(
30
  brand_name_str,
31
  lift_type_str,
 
37
  speed_mps,
38
  floors
39
  ):
40
+ try:
41
+ # Encode inputs
42
+ input_data = pd.DataFrame([[
43
+ encoders['BRAND NAME'].transform([brand_name_str])[0],
44
+ encoders['LIFT_TYPE'].transform([lift_type_str])[0],
45
+ encoders['DOOR_FRAME'].transform([door_frame_str])[0],
46
+ encoders['DOOR_TYPE'].transform([door_type_str])[0],
47
+ encoders['CABIN_MATERIAL'].transform([cabin_material_str])[0],
48
+ encoders['MOTOR_TYPE'].transform([motor_type_str])[0],
49
+ int(passengers_capacity),
50
+ float(speed_mps),
51
+ int(floors)
52
+ ]], columns=[
53
+ 'brand_name', 'lift_type', 'door_frame', 'door_type',
54
+ 'cabin_material', 'motor_type',
55
+ 'passengers_capacity', 'speed_mps', 'floors'
56
+ ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
+ prediction = model.predict(input_data)[0]
59
+ price = float(prediction)
60
+
61
+ return (
62
+ f"### πŸ’° Estimated Price: β‚Ή {price:,.2f}",
63
+ "βœ… Quote generated successfully"
64
+ )
65
+
66
+ except Exception as e:
67
+ return f"❌ Error: {str(e)}", "⚠️ Please check inputs"
68
+
69
+ # --- Custom UI using Blocks ---
70
+ with gr.Blocks(theme=gr.themes.Soft(), title="AB Elevators") as demo:
71
+
72
+ gr.Markdown("""
73
+ <div style="text-align:center; padding:20px;">
74
+ <h1 style="color:gold; text-shadow:2px 2px 5px black;">
75
+ πŸš€ AB ELEVATORS
76
+ </h1>
77
+ <h3>Smart Lift Price Prediction System</h3>
78
+ <p>Get instant quotations based on your requirements</p>
79
+ </div>
80
+ """)
81
+
82
+ with gr.Row():
83
+ with gr.Column():
84
+ brand = gr.Dropdown(categorical_options['BRAND NAME'], label="🏒 Brand")
85
+ lift = gr.Dropdown(categorical_options['LIFT_TYPE'], label="πŸ›— Lift Type")
86
+ door_frame = gr.Dropdown(categorical_options['DOOR_FRAME'], label="πŸšͺ Door Frame")
87
+
88
+ with gr.Column():
89
+ door_type = gr.Dropdown(categorical_options['DOOR_TYPE'], label="πŸšͺ Door Type")
90
+ cabin = gr.Dropdown(categorical_options['CABIN_MATERIAL'], label="🧱 Cabin Material")
91
+ motor = gr.Dropdown(categorical_options['MOTOR_TYPE'], label="βš™οΈ Motor Type")
92
+
93
+ gr.Markdown("### πŸ”’ Configuration")
94
+
95
+ with gr.Row():
96
+ passengers = gr.Slider(
97
+ minimum=int(df_original['PASSENGERS CAPACITY'].min()),
98
+ maximum=int(df_original['PASSENGERS CAPACITY'].max()),
99
+ step=1,
100
+ label="πŸ‘₯ Passengers Capacity",
101
+ value=int(df_original['PASSENGERS CAPACITY'].mean())
102
+ )
103
+
104
+ speed = gr.Slider(
105
+ minimum=float(df_original['SPEED_MPS'].min()),
106
+ maximum=float(df_original['SPEED_MPS'].max()),
107
+ step=0.1,
108
+ label="⚑ Speed (MPS)",
109
+ value=float(df_original['SPEED_MPS'].mean())
110
+ )
111
+
112
+ floors = gr.Slider(
113
+ minimum=int(df_original['FLOORS'].min()),
114
+ maximum=int(df_original['FLOORS'].max()),
115
+ step=1,
116
+ label="🏒 Floors",
117
+ value=int(df_original['FLOORS'].mean())
118
+ )
119
+
120
+ predict_btn = gr.Button("πŸ’‘ Generate Quote", variant="primary")
121
+
122
+ output_price = gr.Markdown()
123
+ output_status = gr.Markdown()
124
+
125
+ predict_btn.click(
126
+ fn=predict_price,
127
+ inputs=[brand, lift, door_frame, door_type, cabin, motor,
128
+ passengers, speed, floors],
129
+ outputs=[output_price, output_status]
130
+ )
131
+
132
+ gr.Markdown("""
133
+ <hr>
134
+ <div style="text-align:center;">
135
+ <p>✨ Designed for AB Elevators | AI Powered Pricing</p>
136
+ </div>
137
+ """)
138
 
139
+ # --- Launch ---
140
+ demo.launch()