| import streamlit as st |
| import pandas as pd |
| import numpy as np |
| import joblib |
| import os |
|
|
| |
| st.set_page_config( |
| page_title="Air Passengers Prediction Dashboard", |
| page_icon="✈️", |
| layout="centered" |
| ) |
|
|
| |
| @st.cache_resource |
| def load_ml_artifacts(): |
| try: |
| |
| if not os.path.exists("model.pkl"): |
| raise FileNotFoundError("Core model file ('model.pkl') is missing from the directory.") |
| if not os.path.exists("scaler.pkl"): |
| raise FileNotFoundError("Scaler artifact ('scaler.pkl') is missing from the directory.") |
| if not os.path.exists("columns.pkl"): |
| raise FileNotFoundError("Columns layout file ('columns.pkl') is missing from the directory.") |
| |
| |
| loaded_model = joblib.load("model.pkl") |
| loaded_scaler = joblib.load("scaler.pkl") |
| loaded_columns = joblib.load("columns.pkl") |
| |
| return loaded_model, loaded_scaler, loaded_columns, None |
| |
| except Exception as e: |
| |
| return None, None, None, str(e) |
|
|
| |
| model, scaler, expected_columns, error_message = load_ml_artifacts() |
|
|
| |
| if error_message: |
| st.error("🚨 **Initialization Error!** Failed to load the Machine Learning files.") |
| st.info(f"**Details:** {error_message}") |
| st.warning("Please ensure 'model.pkl', 'scaler.pkl', and 'columns.pkl' are uploaded to the main root folder.") |
| st.stop() |
|
|
|
|
| |
| st.title("✈️ Air Passengers Prediction System") |
| st.write("Predict the estimated number of passengers based on the year and month using our trained high-accuracy model.") |
| st.markdown("---") |
|
|
| st.subheader("📊 Enter Details for Prediction") |
|
|
| |
| |
| input_year = st.number_input( |
| "Select Year", |
| min_value=1900, |
| max_value=2100, |
| value=1950, |
| step=1, |
| help="Enter the target year for passenger prediction." |
| ) |
|
|
| |
| months_list = [ |
| "January", "February", "March", "April", "May", "June", |
| "July", "August", "September", "October", "November", "December" |
| ] |
| selected_month = st.selectbox("Select Month", options=months_list) |
|
|
| |
| st.markdown("###") |
| if st.button("🚀 Predict Passenger Count", use_container_width=True): |
| with st.spinner("Processing data & generating prediction..."): |
| try: |
| |
| |
| |
| |
| scaled_year = scaler.transform([[input_year]])[0][0] |
| |
| |
| |
| input_dict = {col: 0 for col in expected_columns} |
| |
| |
| if 'year' in input_dict: |
| input_dict['year'] = scaled_year |
| |
| |
| |
| month_feature_name = f"month_{selected_month}" |
| if month_feature_name in input_dict: |
| input_dict[month_feature_name] = 1 |
| else: |
| |
| alt_name = selected_month.lower() |
| if alt_name in input_dict: |
| input_dict[alt_name] = 1 |
| |
| |
| final_features_df = pd.DataFrame([input_dict], columns=expected_columns) |
| |
| |
| prediction = model.predict(final_features_df) |
| |
| |
| |
| |
| |
| final_result = int(np.round(prediction[0])) |
| |
| |
| st.success("🎯 **Prediction Computed Successfully!**") |
| st.metric(label="Estimated Passenger Count", value=f"{final_result:,} Passengers") |
| |
| except Exception as prediction_error: |
| st.error("⚠️ **Prediction pipeline failed!** Check feature matching.") |
| st.code(f"Error logs: {str(prediction_error)}") |
|
|
| st.markdown("---") |
| st.caption("Powered by Streamlit & Scikit-Learn | Accuracy Rating: ~98%") |