import gradio as gr import pandas as pd import numpy as np import os from tensorflow.keras.models import load_model from tensorflow.keras.losses import MeanSquaredError from joblib import load import joblib # FEATURES (same as training) CATEGORICAL_FEATURES = ['District', 'Settlement_Type', 'Land_Type', 'Building_Type'] NUMERICAL_FEATURES = ['Area_sft', 'Floor_Count', 'Design_Complexity', 'BBS_kg_per_sft'] # Required artifact filenames (upload these to the Space repo) required_files = [ 'y_scaler_manual.pkl', 'scaler_manual.pkl', 'encoder_manual.pkl', 'ann_model_manual.h5' ] missing_files = [f for f in required_files if not os.path.exists(f)] if missing_files: raise FileNotFoundError( f"Missing files: {', '.join(missing_files)}. Upload these model files into the Space repository." ) # Load artifacts y_scaler = load('y_scaler_manual.pkl') scaler = load('scaler_manual.pkl') encoder = joblib.load('encoder_manual.pkl') custom_objects = {"mse": MeanSquaredError()} model = load_model("ann_model_manual.h5", custom_objects=custom_objects) def predict_estimate( District, Settlement_Type, Area_sft, Land_Type, Building_Type, Floor_Count, Design_Complexity, BBS_kg_per_sft ): input_df = pd.DataFrame({ 'District': [District], 'Settlement_Type': [Settlement_Type], 'Area_sft': [Area_sft], 'Land_Type': [Land_Type], 'Building_Type': [Building_Type], 'Floor_Count': [Floor_Count], 'Design_Complexity': [Design_Complexity], 'BBS_kg_per_sft': [BBS_kg_per_sft] }) # Preprocess X_num = input_df[NUMERICAL_FEATURES] X_cat = input_df[CATEGORICAL_FEATURES] X_num_scaled = scaler.transform(X_num) X_cat_encoded = encoder.transform(X_cat) final_input = np.hstack([X_num_scaled, X_cat_encoded]) # Predict (scaled) pred_scaled = model.predict(final_input, verbose=0)[0][0] # Inverse scale pred_pkr = y_scaler.inverse_transform([[pred_scaled]])[0][0] pred_pkr = max(pred_pkr, 0) return f"Estimated Total Cost: PKR {pred_pkr:,.2f}" # UI options (same options used during training) DISTRICTS = ['Ghanche', 'Ghizer', 'Nagar', 'Astore', 'Diamer', 'Skardu', 'Shigar', 'Hunza', 'Gilgit', 'Roundu'] SETTLEMENT_TYPES = ['Rural', 'Sub-Rural', 'Urban'] LAND_TYPES = ['Commercial', 'Uncultivated', 'Cultivated'] BUILDING_TYPES = ['Commercial', 'Road', 'Residential'] inputs = [ gr.Dropdown(DISTRICTS, label="District", value="Gilgit"), gr.Dropdown(SETTLEMENT_TYPES, label="Settlement Type", value="Urban"), gr.Number(label="Area (sq ft)", value=2500), gr.Dropdown(LAND_TYPES, label="Land Type", value="Commercial"), gr.Dropdown(BUILDING_TYPES, label="Building Type", value="Residential"), gr.Number(label="Floor Count", value=2), gr.Number(label="Design Complexity (1–5)", value=3), gr.Number(label="BBS kg per sq ft", value=0) ] iface = gr.Interface( fn=predict_estimate, inputs=inputs, outputs=gr.Text(label="Predicted Total Estimate"), title="Construction Cost Predictor (ANN Model)", description="Predicts estimated construction cost using 8 core input features.", allow_flagging="never" ) if __name__ == "__main__": iface.launch()