Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================
|
| 2 |
+
# AI Apartment Rent Predictor for Dhaka
|
| 3 |
+
# Single Codebase (Colab + Hugging Face Ready)
|
| 4 |
+
# ============================================
|
| 5 |
+
|
| 6 |
+
# Install required libraries
|
| 7 |
+
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import numpy as np
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
from sklearn.model_selection import train_test_split
|
| 13 |
+
from sklearn.preprocessing import LabelEncoder
|
| 14 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 15 |
+
|
| 16 |
+
# -----------------------------
|
| 17 |
+
# 1. CREATE SAMPLE DATASET
|
| 18 |
+
# -----------------------------
|
| 19 |
+
|
| 20 |
+
data = {
|
| 21 |
+
"area_sqft": [800, 1000, 1200, 1500, 1800, 900, 1100, 1400, 1600, 2000],
|
| 22 |
+
"bedrooms": [2, 3, 3, 4, 4, 2, 3, 3, 4, 5],
|
| 23 |
+
"bathrooms": [2, 2, 3, 3, 4, 2, 2, 3, 3, 4],
|
| 24 |
+
"location": [
|
| 25 |
+
"Dhanmondi", "Mirpur", "Gulshan", "Banani", "Uttara",
|
| 26 |
+
"Mohammadpur", "Mirpur", "Gulshan", "Banani", "Uttara"
|
| 27 |
+
],
|
| 28 |
+
"furnished": ["No", "No", "Yes", "Yes", "Yes", "No", "No", "Yes", "Yes", "Yes"],
|
| 29 |
+
"rent": [25000, 18000, 50000, 55000, 40000, 22000, 20000, 52000, 56000, 45000]
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
df = pd.DataFrame(data)
|
| 33 |
+
|
| 34 |
+
# -----------------------------
|
| 35 |
+
# 2. DATA PREPROCESSING
|
| 36 |
+
# -----------------------------
|
| 37 |
+
|
| 38 |
+
location_encoder = LabelEncoder()
|
| 39 |
+
furnished_encoder = LabelEncoder()
|
| 40 |
+
|
| 41 |
+
df["location"] = location_encoder.fit_transform(df["location"])
|
| 42 |
+
df["furnished"] = furnished_encoder.fit_transform(df["furnished"])
|
| 43 |
+
|
| 44 |
+
X = df.drop("rent", axis=1)
|
| 45 |
+
y = df["rent"]
|
| 46 |
+
|
| 47 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 48 |
+
X, y, test_size=0.2, random_state=42
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# -----------------------------
|
| 52 |
+
# 3. TRAIN AI MODEL
|
| 53 |
+
# -----------------------------
|
| 54 |
+
|
| 55 |
+
model = RandomForestRegressor(n_estimators=200, random_state=42)
|
| 56 |
+
model.fit(X_train, y_train)
|
| 57 |
+
|
| 58 |
+
# -----------------------------
|
| 59 |
+
# 4. PREDICTION FUNCTION
|
| 60 |
+
# -----------------------------
|
| 61 |
+
|
| 62 |
+
def predict_rent(area_sqft, bedrooms, bathrooms, location, furnished):
|
| 63 |
+
location_encoded = location_encoder.transform([location])[0]
|
| 64 |
+
furnished_encoded = furnished_encoder.transform([furnished])[0]
|
| 65 |
+
|
| 66 |
+
input_data = np.array([
|
| 67 |
+
area_sqft, bedrooms, bathrooms, location_encoded, furnished_encoded
|
| 68 |
+
]).reshape(1, -1)
|
| 69 |
+
|
| 70 |
+
prediction = model.predict(input_data)[0]
|
| 71 |
+
return f"Estimated Monthly Rent: ৳ {int(prediction)}"
|
| 72 |
+
|
| 73 |
+
# -----------------------------
|
| 74 |
+
# 5. GRADIO WEB APP
|
| 75 |
+
# -----------------------------
|
| 76 |
+
|
| 77 |
+
interface = gr.Interface(
|
| 78 |
+
fn=predict_rent,
|
| 79 |
+
inputs=[
|
| 80 |
+
gr.Number(label="Apartment Size (sqft)", value=1000),
|
| 81 |
+
gr.Number(label="Bedrooms", value=3),
|
| 82 |
+
gr.Number(label="Bathrooms", value=2),
|
| 83 |
+
gr.Dropdown(
|
| 84 |
+
choices=["Dhanmondi", "Mirpur", "Gulshan", "Banani", "Uttara", "Mohammadpur"],
|
| 85 |
+
label="Location"
|
| 86 |
+
),
|
| 87 |
+
gr.Dropdown(
|
| 88 |
+
choices=["Yes", "No"],
|
| 89 |
+
label="Furnished"
|
| 90 |
+
),
|
| 91 |
+
],
|
| 92 |
+
outputs="text",
|
| 93 |
+
title="🏙️ Dhaka Apartment Rent Predictor (AI)",
|
| 94 |
+
description="An AI-based system to predict apartment rents in Dhaka for newcomers."
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
interface.launch()
|