gamaba commited on
Commit
48fed9c
·
verified ·
1 Parent(s): e10f985

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ 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,
37
+ door_frame_str,
38
+ door_type_str,
39
+ cabin_material_str,
40
+ motor_type_str,
41
+ passengers_capacity,
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
+ return f"Predicted Price: ₹{prediction:,.2f}", "VISIT AGAIN 😊"
71
+
72
+ # --- 4. Create Gradio Interface ---
73
+ inputs = [
74
+ gr.Dropdown(categorical_options['BRAND NAME'], label="Brand Name", value=categorical_options['BRAND NAME'][0]),
75
+ gr.Dropdown(categorical_options['LIFT_TYPE'], label="Lift Type", value=categorical_options['LIFT_TYPE'][0]),
76
+ gr.Dropdown(categorical_options['DOOR_FRAME'], label="Door Frame", value=categorical_options['DOOR_FRAME'][0]),
77
+ gr.Dropdown(categorical_options['DOOR_TYPE'], label="Door Type", value=categorical_options['DOOR_TYPE'][0]),
78
+ gr.Dropdown(categorical_options['CABIN_MATERIAL'], label="Cabin Material", value=categorical_options['CABIN_MATERIAL'][0]),
79
+ gr.Dropdown(categorical_options['MOTOR_TYPE'], label="Motor Type", value=categorical_options['MOTOR_TYPE'][0]),
80
+ 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())),
81
+ 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()),
82
+ gr.Slider(minimum=df_original['FLOORS'].min(), maximum=df_original['FLOORS'].max(), step=1, label="Floors", value=int(df_original['FLOORS'].mean()))
83
+ ]
84
+
85
+ outputs = [
86
+ gr.Textbox(label="Prediction Result"),
87
+ gr.Markdown()
88
+ ]
89
+
90
+ iface = gr.Interface(
91
+ fn=predict_price,
92
+ inputs=inputs,
93
+ outputs=outputs,
94
+ title="AB ELEVATOR Company - Lift Quotation",
95
+ 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
96
+ live=False
97
+ )
98
+
99
+ # Launch the interface
100
+ iface.launch()