mimo1972 commited on
Commit
fb67180
Β·
verified Β·
1 Parent(s): aeb982a

Update flightprice.py

Browse files
Files changed (1) hide show
  1. flightprice.py +27 -27
flightprice.py CHANGED
@@ -52,7 +52,7 @@ def get_day_quarter(hour):
52
  else:
53
  return 'Night'
54
 
55
- # --- 2. MAPPINGS (For UI Display Only) ---
56
  CITY_TO_CODE = {
57
  'Banglore': 'BLR', 'Bangalore': 'BLR',
58
  'Delhi': 'DEL', 'New Delhi': 'DEL',
@@ -94,19 +94,17 @@ if page == "πŸ’° Price Prediction":
94
  st.markdown("### Enter Flight Details")
95
 
96
  # ---------------------------------------------------------
97
- # INPUT SECTION (Reactive - No Form)
98
  # ---------------------------------------------------------
99
  c1, c2 = st.columns(2)
100
  with c1:
101
  st.subheader("Flight Info")
102
 
103
- # 1. Source Selection
104
  source = st.selectbox("Source", sorted(x_train['Source'].unique()))
105
- st.success(f"πŸ›« Code: **{get_code(source)}**")
106
 
107
- # 2. Destination Selection
108
  destination = st.selectbox("Destination", sorted(x_train['Destination'].unique()))
109
- st.error(f"πŸ›¬ Code: **{get_code(destination)}**")
110
 
111
  airline = st.selectbox("Airline", sorted(x_train['Airline'].unique()))
112
 
@@ -128,32 +126,36 @@ if page == "πŸ’° Price Prediction":
128
 
129
  if 'Route' in x_train.columns:
130
  # ---------------------------------------------------------
131
- # DOUBLE FILTER LOGIC (Source AND Destination)
132
  # ---------------------------------------------------------
133
- # We query the dataframe for rows matching BOTH selected Source and Destination
134
- filtered_routes = x_train[
135
- (x_train['Source'] == source) &
136
- (x_train['Destination'] == destination)
137
- ]['Route'].unique()
138
 
139
- filtered_routes = sorted(filtered_routes.astype(str))
 
140
 
141
- if len(filtered_routes) > 0:
142
- # If routes exist, user selects one
143
- selected_route = st.selectbox("Select Route", filtered_routes)
 
 
 
 
 
 
 
144
 
145
- # Lookup hidden features based on this valid route
146
  stops_val = route_lookup.get(selected_route, 0)
147
 
148
- # Display Feedback
149
  c_a, c_b = st.columns(2)
150
  c_a.metric("Total Stops", stops_val)
151
- c_b.success("βœ… Valid Route Found")
152
 
153
  else:
154
- # No routes connect these two cities in the training data
155
- st.error(f"🚫 No flights found from **{source}** to **{destination}** in the database.")
156
- st.info("Try changing the Destination or Source.")
157
  selected_route = None
158
  else:
159
  st.error("Route column missing.")
@@ -161,7 +163,7 @@ if page == "πŸ’° Price Prediction":
161
  st.markdown("<br>", unsafe_allow_html=True)
162
 
163
  # ---------------------------------------------------------
164
- # PREDICTION BUTTON
165
  # ---------------------------------------------------------
166
  if st.button("Predict Price", type="primary"):
167
  if selected_route:
@@ -177,12 +179,12 @@ if page == "πŸ’° Price Prediction":
177
  'Total_Stops': [stops_val]
178
  })
179
 
180
- # Align Columns (Fill missing with 0)
181
  final_input = pd.DataFrame(columns=x_train.columns)
182
  for col in x_train.columns:
183
  final_input.loc[0, col] = input_df.iloc[0].get(col, 0)
184
 
185
- # Ensure Types
186
  for col in final_input.columns:
187
  if x_train[col].dtype == 'object':
188
  final_input[col] = final_input[col].astype(str)
@@ -190,11 +192,9 @@ if page == "πŸ’° Price Prediction":
190
  final_input[col] = pd.to_numeric(final_input[col])
191
 
192
  try:
193
- # Predict
194
  pred = model.predict(final_input)[0]
195
  real_price = np.expm1(pred)
196
 
197
- # Show Result
198
  st.markdown(f"""
199
  <div style="background-color:#d4edda;padding:10px;border-radius:10px;border:2px solid #c3e6cb">
200
  <h2 style="color:#155724;text-align:center;">🎫 Estimated Price: β‚Ή {real_price:,.2f}</h2>
 
52
  else:
53
  return 'Night'
54
 
55
+ # --- 2. Airport Codes (For Text Search) ---
56
  CITY_TO_CODE = {
57
  'Banglore': 'BLR', 'Bangalore': 'BLR',
58
  'Delhi': 'DEL', 'New Delhi': 'DEL',
 
94
  st.markdown("### Enter Flight Details")
95
 
96
  # ---------------------------------------------------------
97
+ # INPUTS
98
  # ---------------------------------------------------------
99
  c1, c2 = st.columns(2)
100
  with c1:
101
  st.subheader("Flight Info")
102
 
103
+ # Source
104
  source = st.selectbox("Source", sorted(x_train['Source'].unique()))
 
105
 
106
+ # Destination
107
  destination = st.selectbox("Destination", sorted(x_train['Destination'].unique()))
 
108
 
109
  airline = st.selectbox("Airline", sorted(x_train['Airline'].unique()))
110
 
 
126
 
127
  if 'Route' in x_train.columns:
128
  # ---------------------------------------------------------
129
+ # NEW FILTERING LOGIC
130
  # ---------------------------------------------------------
131
+ # 1. Get ALL routes that start from this Source (using Source column)
132
+ all_source_routes = x_train[x_train['Source'] == source]['Route'].unique()
133
+ all_source_routes = sorted(all_source_routes.astype(str))
 
 
134
 
135
+ # 2. Filter: Does the route string contain the Destination (Code or Name)?
136
+ dest_code = get_code(destination)
137
 
138
+ valid_routes = []
139
+ for r in all_source_routes:
140
+ # Check if 'BOM' or 'MUMBAI' is in 'DEL -> BOM -> COK'
141
+ # We use .upper() for case insensitive check
142
+ if (dest_code in r.upper()) or (destination.upper() in r.upper()):
143
+ valid_routes.append(r)
144
+
145
+ # 3. Display
146
+ if len(valid_routes) > 0:
147
+ selected_route = st.selectbox("Select Route", valid_routes)
148
 
149
+ # Lookup hidden features
150
  stops_val = route_lookup.get(selected_route, 0)
151
 
152
+ # Display Info
153
  c_a, c_b = st.columns(2)
154
  c_a.metric("Total Stops", stops_val)
155
+ c_b.success("βœ… Valid Route")
156
 
157
  else:
158
+ st.error(f"No routes found starting at **{source}** that contain **{destination}** ({dest_code}).")
 
 
159
  selected_route = None
160
  else:
161
  st.error("Route column missing.")
 
163
  st.markdown("<br>", unsafe_allow_html=True)
164
 
165
  # ---------------------------------------------------------
166
+ # PREDICT
167
  # ---------------------------------------------------------
168
  if st.button("Predict Price", type="primary"):
169
  if selected_route:
 
179
  'Total_Stops': [stops_val]
180
  })
181
 
182
+ # Align Columns
183
  final_input = pd.DataFrame(columns=x_train.columns)
184
  for col in x_train.columns:
185
  final_input.loc[0, col] = input_df.iloc[0].get(col, 0)
186
 
187
+ # Types
188
  for col in final_input.columns:
189
  if x_train[col].dtype == 'object':
190
  final_input[col] = final_input[col].astype(str)
 
192
  final_input[col] = pd.to_numeric(final_input[col])
193
 
194
  try:
 
195
  pred = model.predict(final_input)[0]
196
  real_price = np.expm1(pred)
197
 
 
198
  st.markdown(f"""
199
  <div style="background-color:#d4edda;padding:10px;border-radius:10px;border:2px solid #c3e6cb">
200
  <h2 style="color:#155724;text-align:center;">🎫 Estimated Price: β‚Ή {real_price:,.2f}</h2>