File size: 4,628 Bytes
aa8a219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from sklearn.pipeline import Pipeline 
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
import streamlit as st
import pandas as pd
import joblib

# Load data and model
df = pd.read_csv('House_Rent_Dataset.csv')
df['Extracted Floor'] = df['Floor'].str.extract(r'^(\d{1,2}|[A-Za-z]+)', expand=False)

def map_floors(floor):
    if floor.startswith('Ground'):
        return 0
    elif floor.startswith('Upper'):
        return 0
    elif floor.startswith('Lower'):
        return -1
    else:
        return floor

df['Extracted Floor'] = df['Extracted Floor'].apply(map_floors)

# Load the trained model
model = joblib.load('best_regression_model.pkl')

# Define the preprocessor and pipeline
preprocessor = ColumnTransformer(
    transformers=[
        ("num", StandardScaler(), ["BHK", "Size", 'Bathroom', 'Extracted Floor']),
        ("cat", OneHotEncoder(), ["Area Type", "Area Locality", "City", 'Furnishing Status', 'Tenant Preferred', 'Point of Contact'])
    ]
)
pipeline = Pipeline(steps=[("preprocessor", preprocessor), ("regressor", model)])

# Fit the pipeline on the data (optional, if you want to refit with the data)
pipeline.fit(df[['BHK', 'Size', 'Area Type', 'Area Locality', 'City',
                 'Furnishing Status', 'Tenant Preferred', 'Bathroom', 'Point of Contact', 'Extracted Floor']], df["Rent"])

# Function to predict the price
def price_prediction(bhk, size, area_type, area_locality, city, furnishing_status, tenant_preferred, bathroom, point_of_contact, floor):
    input_data = pd.DataFrame({
        "BHK": [bhk],
        "Size": [size],
        "Area Type": [area_type],
        "Area Locality": [area_locality],
        "City": [city],
        "Furnishing Status": [furnishing_status],
        "Tenant Preferred": [tenant_preferred],
        "Bathroom": [bathroom],
        "Point of Contact": [point_of_contact],
        "Extracted Floor": [floor]
    })
    prediction = pipeline.predict(input_data)[0]
    return prediction

# Main function to render the Streamlit app
def main():
    st.set_page_config(page_title="House Rent Prediction in India", layout="wide")
    
    # App title and description
    st.title("🏠 House Rent Prediction in India")
    st.markdown("""
    **Enter the house features** below to predict the rent. 
    Adjust the inputs to see how different characteristics affect the rent.
    """)
    
    # Add custom CSS for styling
    st.markdown("""
    <style>
    .stButton>button {
        background-color: #28a745;
        color: white;
        font-size: 18px;
        border-radius: 5px;
        padding: 10px 20px;
        margin-top: 20px;
    }
    .stButton>button:hover {
        background-color: #218838;
    }
    .stText {
        font-size: 16px;
        color: #333;
    }
    .stTitle {
        color: #007bff;
    }
    </style>
    """, unsafe_allow_html=True)

    # Side bar inputs (Better structure and user-friendly)
    st.sidebar.header("Enter House Details")
    
    city = st.sidebar.selectbox("City", df["City"].unique())
    bhk = st.sidebar.number_input("Number of Bedrooms, Hall, Kitchen", int(df["BHK"].min()), int(df["BHK"].max()))
    size = st.sidebar.number_input("Size of the House in Square Feet", min_value=float(df["Size"].min()), max_value=float(df["Size"].max()), step=10.0)
    bathroom = st.sidebar.number_input("Bathroom Number", 0, step=1)
    floor = st.sidebar.number_input("Extracted Floor", -1, step=1)

    # Dynamic options based on city selection
    area_type = st.sidebar.selectbox("Area Type", df[df['City'] == city]['Area Type'].unique())
    area_locality = st.sidebar.selectbox("Area Locality", df[df['City'] == city]['Area Locality'].unique())
    tenant_preferred = st.sidebar.selectbox("Tenant Preferred", df["Tenant Preferred"].unique())
    furnishing_status = st.sidebar.selectbox("Furnishing Status", df["Furnishing Status"].unique())
    point_of_contact = st.sidebar.selectbox("Point of Contact", df["Point of Contact"].unique())
    
    # Prediction button
    if st.sidebar.button("Predict Rent"):
        price = price_prediction(bhk, size, area_type, area_locality, city, furnishing_status, tenant_preferred, bathroom, point_of_contact, floor)
        price = float(price)
        
        # Display the result with enhanced visualization
        st.subheader("Predicted Rent: 💲 **${:,.2f}**".format(price))
        st.markdown("""
        This is the estimated price based on the characteristics you provided. 
        Please note that the actual market rent may vary.
        """)

if __name__ == "__main__":
    main()