File size: 3,335 Bytes
e7769d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e027b20
 
 
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
from flask import Flask, request, render_template, redirect, url_for
import pickle
import numpy as np
import os

app = Flask(__name__)

# Load the model and feature names
def load_model_and_features():
    try:
        model_path = os.path.join(os.getcwd(), "models", "lr_regg.pkl")
        feature_path = os.path.join(os.getcwd(), "models", "feature_names.pkl")

        with open(model_path, "rb") as file:
            model = pickle.load(file)

        with open(feature_path, "rb") as file:
            feature_names = pickle.load(file)

        return model, feature_names
    except FileNotFoundError as e:
        raise FileNotFoundError(f"Required file not found: {str(e)}")

def predict_price(location, sqft, bath, bhk, model, feature_names):
    x = np.zeros(len(feature_names))

    if 'total_sqft' in feature_names:
        x[feature_names.index('total_sqft')] = sqft
    if 'bath' in feature_names:
        x[feature_names.index('bath')] = bath
    if 'bhk' in feature_names:
        x[feature_names.index('bhk')] = bhk
    if location in feature_names:
        loc_index = feature_names.index(location)
        x[loc_index] = 1
    else:
        raise ValueError(f"Location '{location}' is not recognized.")

    return model.predict([x])[0]

# Load model and features when app starts
model, feature_names = load_model_and_features()

# Get list of locations (excluding non-location features)
locations = [feat for feat in feature_names if feat not in ['total_sqft', 'bath', 'bhk']]
locations.sort()

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        try:
            location = request.form.get('location')
            sqft = float(request.form.get('sqft'))
            bath = int(request.form.get('bath'))
            bhk = int(request.form.get('bhk'))

            price = predict_price(location, sqft, bath, bhk, model, feature_names)
            prediction = round(price / 10, 2)  # Convert to lakhs and round to 2 decimal places

            # Redirect to results page with the prediction details
            return redirect(url_for('results',
                                    predicted_price=prediction,
                                    location=location,
                                    sqft=sqft,
                                    bath=bath,
                                    bhk=bhk))
        except Exception as e:
            error = f"Error: {str(e)}"
            return render_template('index.html', locations=locations, error=error)

    return render_template('index.html', locations=locations)

@app.route('/results')
def results():
    # Fetch query parameters from the URL
    predicted_price = request.args.get('predicted_price')
    location = request.args.get('location')
    sqft = request.args.get('sqft')
    bath = request.args.get('bath')
    bhk = request.args.get('bhk')

    return render_template('results.html',
                           predicted_price=predicted_price,
                           location=location,
                           sqft=sqft,
                           bath=bath,
                           bhk=bhk)

if __name__ == '__main__':
    debug_mode = os.getenv('FLASK_DEBUG', 'False').lower() in ['true', '1', 'yes']
    # app.run(debug=debug_mode)
    app.run(host='0.0.0.0', port=7860, debug=debug_mode)