File size: 2,359 Bytes
07a6b14
 
 
 
 
 
 
 
 
 
 
 
 
0744d1c
07a6b14
b59cf8f
 
0744d1c
 
 
b59cf8f
07a6b14
b59cf8f
 
07a6b14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0744d1c
 
07a6b14
 
0744d1c
07a6b14
 
 
b59cf8f
07a6b14
 
 
 
 
b59cf8f
07a6b14
 
 
 
 
0744d1c
07a6b14
 
 
 
 
 
 
0744d1c
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
# app.py
import gradio as gr
import pandas as pd
import pickle
from sklearn.linear_model import LinearRegression

# ------------------------
# Step 1: Create or load a model
# ------------------------
try:
    with open("house_price_model.pkl", "rb") as f:
        model, locations = pickle.load(f)
except:
    # Sample dataset with area, rooms, and locations
    data = pd.DataFrame({
        "area": [800, 1000, 1200, 1500, 1800, 2000, 1300, 1600],
        "rooms": [2, 3, 3, 4, 4, 5, 3, 4],
        "location": ["Dhaka, Gulshan", "Chattogram, Agrabad", "Khulna, Sonadanga", 
                     "Rajshahi, Motihar", "Sylhet, Zindabazar", "Barishal, Kolabagan",
                     "Rangpur, Sadar", "Bogura, Sadar"],
        "price": [80, 100, 90, 110, 95, 85, 105, 100]
    })

    locations = data["location"].unique()
    
    # One-hot encode locations
    data_encoded = pd.get_dummies(data, columns=["location"])
    
    X = data_encoded.drop("price", axis=1)
    y = data_encoded["price"]
    
    model = LinearRegression()
    model.fit(X, y)
    
    # Save the model
    with open("house_price_model.pkl", "wb") as f:
        pickle.dump((model, locations), f)

# ------------------------
# Step 2: Prediction function
# ------------------------
def predict_price(area, rooms, location_input):
    # Prepare input for one-hot encoding
    input_dict = {"area": area, "rooms": rooms}
    for loc in locations:
        input_dict[f"location_{loc}"] = 1 if loc.lower() == location_input.lower() else 0
    
    X_input = pd.DataFrame([input_dict])
    price = model.predict(X_input)[0]
    return f"💰 Predicted House Price: {price:.2f}k BDT"

# ------------------------
# Step 3: Gradio interface
# ------------------------
with gr.Blocks() as app:
    gr.Markdown("# 🏠 Bangladesh House Price Prediction")
    
    with gr.Row():
        with gr.Column():
            area_input = gr.Number(label="Area (sq ft)", value=1000)
            rooms_input = gr.Number(label="Number of Rooms", value=3)
            location_input = gr.Textbox(label="Location (e.g., Dhaka, Savar)", value="Dhaka, Gulshan")
            btn = gr.Button("Predict Price")
        with gr.Column():
            output = gr.Textbox(label="Prediction")
    
    btn.click(fn=predict_price, inputs=[area_input, rooms_input, location_input], outputs=output)

app.launch()