CoolFace
Apppublic

Purnanand/Home_Price_Prediction_Python_ML_PanelHoloviz

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py229 linesDownload Raw Back to root
1import panel as pn2import pickle3import json4import numpy as np5import pandas as pd6import param7import base648import warnings9 10warnings.filterwarnings('ignore')11 12pn.extension('tabulator')13pn.extension(loading_spinner='petal', loading_color='#00aa41')14 15def image_to_base64(image_path):16    with open(image_path, "rb") as f:17        return base64.b64encode(f.read()).decode("utf-8")18 19class ImgBackground(pn.reactive.ReactiveHTML):20    object = param.Parameter()21    img_base64 = param.String()22    23    _template = """24    <div id="pn-component" style="height:100%; width:100%; position:relative;">25        <div id="background" style="background-image: url(data:image/jpeg;base64,${img_base64});26                                    background-repeat: no-repeat;27                                    background-attachment: scroll;28                                    background-position: center center;29                                    background-size: cover;30                                    filter: blur(2px);31                                    opacity: 0.8;32                                    height:100%;33                                    width:100%;34                                    position:absolute;35                                    top:0;36                                    left:0;37                                    z-index:-1;">38        </div>39        <div id="content" style="position:relative; z-index:1;">40            ${object}41        </div>42    </div>43    """44    45# Path to the local background image46background_image_path = "./background.jpg"47 48# Convert image to base6449img_base64 = image_to_base64(background_image_path)50 51# Load data and model52X = pd.read_excel("./data.xlsx")53X['total_sqft'] = X['total_sqft'].astype(int)54 55sqft_list = X['total_sqft'].unique().tolist()56bhk_list = X['bhk'].unique().tolist()57bath_list = X['bath'].unique().tolist()58 59# Define the threshold60threshold_sqft = 40061 62# Create a new list with values above the threshold63sqft_list = [x for x in sqft_list if x >= threshold_sqft]64 65minsqft = min(sqft_list)66maxsqft = max(sqft_list)67 68def load_saved_artifacts():69    global datacolumns, locations, model70    71    with open('columns.json', 'r') as f:72        datacolumns = json.load(f)['data_columns']73        locations = datacolumns[4:]74    75    with open('./banglore_home_prices_model.pickle', 'rb') as file:76        model = pickle.load(file)77 78load_saved_artifacts()79 80# Building widgets81location_select = pn.widgets.Select(name='Location', options=locations, width=300, value=locations[0], align='center')82bedroom_select = pn.widgets.Select(name='Bedrooms', options=[2,3,4], width=300, value=2, align='center')83bathroom_select = pn.widgets.Select(name='Bathrooms', options=[2,3], width=300, value=3, align='center')84sqft_slider = pn.widgets.Select(name='Square Feet', options=[2000,3000,4000], width=300, value=2000, align='center')85# sqft_slider = pn.widgets.IntSlider(name='Square Feet', start=minsqft, end=maxsqft, step=100, value=3000, width=300, align='center')86 87@pn.depends(location_select, bedroom_select, bathroom_select, sqft_slider)88def get_estimated_price(location, bhk, bath, sqft):89    try:90        loc_index = datacolumns.index(location.lower())91    except:92        loc_index = -193 94    x = np.zeros(len(datacolumns))95    x[0] = sqft96    x[1] = bath97    x[2] = bhk98    if loc_index >= 0:99        x[loc_index] = 1100    101    output = round(model.predict([x])[0], 2)102    103    return pn.indicators.Number(104        name="Estimated House Price",105        value=round(output/100,2),106        format='{value} Crore Rupees',107        title_size='24pt',108        font_size='36pt',109        styles={110            'background-color': 'rgba(95, 158, 160, 0.7)',111            'border': '',112            'color': 'white',113            'padding': '10px 20px',114            'text-align': 'center',115            'text-decoration': 'none',116            'font-family': 'tahoma',117            'margin': '20px auto',118            'cursor': 'default',119            'border-radius': '10px',120            'box-shadow': '0 4px 6px rgba(0, 0, 0, 0.1)',121            'width': '300px'122        }123    )124 125component1 = pn.Column(126    pn.Row(pn.Spacer(width=300),pn.pane.Markdown("# Housing Price Prediction Model", styles={127                'background-color': '#F0FFFF',128                'border': '',129                'color': 'black',130                'padding': '5px 5px',131                'text-align': 'center',132                'text-decoration': 'none',133                'font-family': 'tahoma',134                'margin': '10px auto',135                'cursor': 'default',136                'font-size': '20px',137                'font-weight': 'bold',138            })),139    pn.Row(140        pn.Column(141            pn.pane.Markdown("## Select a location", styles={142                'background-color': '#F0FFFF',143                'border': '',144                'color': 'black',145                'padding': '5px 5px',146                'text-align': 'center',147                'text-decoration': 'none',148                'font-family': 'tahoma',149                'margin': '10px auto',150                'cursor': 'default',151                'font-size': '10px',152                'font-weight': 'bold',153            }),154            pn.Row(location_select, width=300, align='center'),155            pn.Spacer(height=30),156            pn.pane.Markdown("## Select number of bedrooms", styles={157                'background-color': '#F0FFFF',158                'border': '',159                'color': 'black',160                'padding': '5px 5px',161                'text-align': 'center',162                'text-decoration': 'none',163                'font-family': 'tahoma',164                'margin': '10px auto',165                'cursor': 'default',166                'font-size': '10px',167                'font-weight': 'bold',168            }),169            pn.Row(bedroom_select, width=300, align='center'),170            pn.Spacer(height=30),171            pn.pane.Markdown("## Select number of bathrooms", styles={172                'background-color': '#F0FFFF',173                'border': '',174                'color': 'black',175                'padding': '5px 5px',176                'text-align': 'center',177                'text-decoration': 'none',178                'font-family': 'tahoma',179                'margin': '10px auto',180                'cursor': 'default',181                'font-size': '10px',182                'font-weight': 'bold',183            }),184            pn.Row(bathroom_select, width=300, align='center'),185            pn.Spacer(height=30),186            pn.pane.Markdown("## Select square feet using the slider", styles={187                'background-color': '#F0FFFF',188                'border': '',189                'color': 'black',190                'padding': '5px 5px',191                'text-align': 'center',192                'text-decoration': 'none',193                'font-family': 'tahoma',194                'margin': '10px auto',195                'cursor': 'default',196                'font-size': '10px',197                'font-weight': 'bold',198            }),199            pn.Row(sqft_slider, width=300, align='center'),200            align='center',201            width=600202        ),203        pn.Column(204            pn.pane.Markdown("## Predicted Price", styles={205                'background-color': '#F0FFFF',206                'border': '',207                'color': 'black',208                'padding': '5px 5px',209                'text-align': 'center',210                'text-decoration': 'none',211                'font-family': 'tahoma',212                'margin': '10px auto',213                'cursor': 'default',214                'font-size': '10px',215                'font-weight': 'bold',216            }),217            pn.Row(get_estimated_price, width=300, align='center'),218            align='center',219            width=600220        )221    ), 222    name="Housing Price Prediction"223)224 225svg_background = ImgBackground(object=component1, height=800, img_base64=img_base64)226 227svg_background.servable()228 229