SE333 / app.py
Nuhin23's picture
Update app.py
154dd49 verified
# ============================================
# 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()