File size: 2,927 Bytes
154dd49 | 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 | # ============================================
# AI Apartment Rent Predictor for Dhaka
# Single Codebase (Colab + Hugging Face Ready)
# ============================================
# Install required libraries
import pandas as pd
import numpy as np
import gradio as gr
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestRegressor
# -----------------------------
# 1. CREATE SAMPLE DATASET
# -----------------------------
data = {
"area_sqft": [800, 1000, 1200, 1500, 1800, 900, 1100, 1400, 1600, 2000],
"bedrooms": [2, 3, 3, 4, 4, 2, 3, 3, 4, 5],
"bathrooms": [2, 2, 3, 3, 4, 2, 2, 3, 3, 4],
"location": [
"Dhanmondi", "Mirpur", "Gulshan", "Banani", "Uttara",
"Mohammadpur", "Mirpur", "Gulshan", "Banani", "Uttara"
],
"furnished": ["No", "No", "Yes", "Yes", "Yes", "No", "No", "Yes", "Yes", "Yes"],
"rent": [25000, 18000, 50000, 55000, 40000, 22000, 20000, 52000, 56000, 45000]
}
df = pd.DataFrame(data)
# -----------------------------
# 2. DATA PREPROCESSING
# -----------------------------
location_encoder = LabelEncoder()
furnished_encoder = LabelEncoder()
df["location"] = location_encoder.fit_transform(df["location"])
df["furnished"] = furnished_encoder.fit_transform(df["furnished"])
X = df.drop("rent", axis=1)
y = df["rent"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# -----------------------------
# 3. TRAIN AI MODEL
# -----------------------------
model = RandomForestRegressor(n_estimators=200, random_state=42)
model.fit(X_train, y_train)
# -----------------------------
# 4. PREDICTION FUNCTION
# -----------------------------
def predict_rent(area_sqft, bedrooms, bathrooms, location, furnished):
location_encoded = location_encoder.transform([location])[0]
furnished_encoded = furnished_encoder.transform([furnished])[0]
input_data = np.array([
area_sqft, bedrooms, bathrooms, location_encoded, furnished_encoded
]).reshape(1, -1)
prediction = model.predict(input_data)[0]
return f"Estimated Monthly Rent: ৳ {int(prediction)}"
# -----------------------------
# 5. GRADIO WEB APP
# -----------------------------
interface = gr.Interface(
fn=predict_rent,
inputs=[
gr.Number(label="Apartment Size (sqft)", value=1000),
gr.Number(label="Bedrooms", value=3),
gr.Number(label="Bathrooms", value=2),
gr.Dropdown(
choices=["Dhanmondi", "Mirpur", "Gulshan", "Banani", "Uttara", "Mohammadpur"],
label="Location"
),
gr.Dropdown(
choices=["Yes", "No"],
label="Furnished"
),
],
outputs="text",
title="🏙️ Dhaka Apartment Rent Predictor (AI)",
description="An AI-based system to predict apartment rents in Dhaka for newcomers."
)
interface.launch() |