YAMITEK's picture
Upload 6 files
6a81a2d verified
import streamlit as st
import pickle
import numpy as np
import pandas as pd
# Custom CSS for background and white button
css = """
<style>
.stForm {
background-image: url("https://images.rawpixel.com/image_800/cHJpdmF0ZS9sci9pbWFnZXMvd2Vic2l0ZS8yMDI0LTAxL2ZyZWVpbWFnZXNjb21wYW55X2JsYWNrX2FuZF93aGl0ZV9hZXN0aGV0aWNfcGhvdG9ncmFwaHlfb2ZfYWlycF83ZWM0ZmM0YS03NzA5LTRkZjYtOTNjZC0xZWJlNmJkZmZiZTVfMS5qcGc.jpg");
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
margin: 0;
padding: 0;
}
.stButton > button {
background-color: white;
color: black;
}
</style>
"""
# Inject custom CSS
st.markdown(css, unsafe_allow_html=True)
# Model loading
model_path = 'model.pkl'
with open(model_path, 'rb') as file:
model = pickle.load(file)
# Options
sourceOptions = ['Banglore', 'Chennai', 'Delhi', 'Kolkata', 'Mumbai']
destinationOptions = ['Banglore', 'Cochin', 'Delhi', 'Hyderabad', 'Kolkata', 'New Delhi']
airlineOptions = {0: 'Air Asia', 1: 'Air India', 2: 'GoAir', 3: 'IndiGo', 4: 'Jet Airways',
5: 'Jet Airways Business', 6: 'Multiple carriers',
7: 'Multiple carriers Premium economy', 8: 'SpiceJet', 9: 'Trujet',
10: 'Vistara', 11: 'Vistara Premium economy'}
totalStopsOptions = {0: 'non stop', 1: '1 stop', 2: '2 stop', 3: '3 stop', 4: '4 stop'}
def prepare_input_data(
source, destination, airline, totalStops, dateOfJourney, depTime, arrivalTime,
inFlightMealNotIncluded, noCheckInBaggageIncluded
):
day_of_journey = dateOfJourney.day
month_of_journey = dateOfJourney.month
dep_hour = depTime.hour
dep_min = depTime.minute
arrival_hour = arrivalTime.hour
arrival_min = arrivalTime.minute
duration_minutes = (arrival_hour * 60 + arrival_min) - (dep_hour * 60 + dep_min)
if duration_minutes < 0:
duration_minutes += 24 * 60
source_encoded = [1 if source == s else 0 for s in sourceOptions]
destination_encoded = [1 if destination == d else 0 for d in destinationOptions]
airline_encoded = [key for key, value in airlineOptions.items() if value == airline][0]
total_stops_encoded = [key for key, value in totalStopsOptions.items() if value == totalStops][0]
in_flight_meal = 1 if inFlightMealNotIncluded == "Yes" else 0
no_check_in_baggage = 1 if noCheckInBaggageIncluded == "Yes" else 0
input_features = (
[day_of_journey, month_of_journey, dep_hour, dep_min, arrival_hour, arrival_min, duration_minutes]
+ source_encoded
+ destination_encoded
+ [total_stops_encoded, airline_encoded, in_flight_meal, no_check_in_baggage]
)
return np.array([input_features])
# Create a form
with st.form(key="my_form"):
st.markdown("<h1 style='text-align: center; background-color: #32bbef;'>Flight Fare Prediction ML App</h1>", unsafe_allow_html=True)
source = st.selectbox("Source", options=sourceOptions)
destination = st.selectbox("Destination", options=destinationOptions)
airline = st.selectbox("Airline", options=list(airlineOptions.values()))
totalStops = st.selectbox("Total Stops", options=list(totalStopsOptions.values()))
dateOfJourney = st.date_input("Date of Journey")
depTime = st.time_input("Dep Time (HH:MM):")
arrivalTime = st.time_input("Arrival Time (HH:MM)")
inFlightMealNotIncluded = st.selectbox("In Flight Meal Not Included", options=["Select", "Yes", "No"])
noCheckInBaggageIncluded = st.selectbox("No check-in Baggage Included", options=["Select", "Yes", "No"])
submit_button = st.form_submit_button(label="Predict Flight Price")
# Handle form submission
if submit_button:
# Prepare input data
input_data = prepare_input_data(
source, destination, airline, totalStops, dateOfJourney, depTime, arrivalTime,
inFlightMealNotIncluded, noCheckInBaggageIncluded
)
# Predict flight price
try:
prediction = model.predict(input_data)
st.success(f"Expected Flight Fare is: ₹{prediction[0]:,.2f}")
except Exception as e:
st.error(f"Error in prediction: {e}")