Maaz Uddin
commited on
Commit
·
e7769d4
1
Parent(s):
81522e3
'Addnewfiles'
Browse files- app.py +91 -0
- requirements.txt +24 -0
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, render_template, redirect, url_for
|
2 |
+
import pickle
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
# Load the model and feature names
|
9 |
+
def load_model_and_features():
|
10 |
+
try:
|
11 |
+
model_path = os.path.join(os.getcwd(), "models", "lr_regg.pkl")
|
12 |
+
feature_path = os.path.join(os.getcwd(), "models", "feature_names.pkl")
|
13 |
+
|
14 |
+
with open(model_path, "rb") as file:
|
15 |
+
model = pickle.load(file)
|
16 |
+
|
17 |
+
with open(feature_path, "rb") as file:
|
18 |
+
feature_names = pickle.load(file)
|
19 |
+
|
20 |
+
return model, feature_names
|
21 |
+
except FileNotFoundError as e:
|
22 |
+
raise FileNotFoundError(f"Required file not found: {str(e)}")
|
23 |
+
|
24 |
+
def predict_price(location, sqft, bath, bhk, model, feature_names):
|
25 |
+
x = np.zeros(len(feature_names))
|
26 |
+
|
27 |
+
if 'total_sqft' in feature_names:
|
28 |
+
x[feature_names.index('total_sqft')] = sqft
|
29 |
+
if 'bath' in feature_names:
|
30 |
+
x[feature_names.index('bath')] = bath
|
31 |
+
if 'bhk' in feature_names:
|
32 |
+
x[feature_names.index('bhk')] = bhk
|
33 |
+
if location in feature_names:
|
34 |
+
loc_index = feature_names.index(location)
|
35 |
+
x[loc_index] = 1
|
36 |
+
else:
|
37 |
+
raise ValueError(f"Location '{location}' is not recognized.")
|
38 |
+
|
39 |
+
return model.predict([x])[0]
|
40 |
+
|
41 |
+
# Load model and features when app starts
|
42 |
+
model, feature_names = load_model_and_features()
|
43 |
+
|
44 |
+
# Get list of locations (excluding non-location features)
|
45 |
+
locations = [feat for feat in feature_names if feat not in ['total_sqft', 'bath', 'bhk']]
|
46 |
+
locations.sort()
|
47 |
+
|
48 |
+
@app.route('/', methods=['GET', 'POST'])
|
49 |
+
def home():
|
50 |
+
if request.method == 'POST':
|
51 |
+
try:
|
52 |
+
location = request.form.get('location')
|
53 |
+
sqft = float(request.form.get('sqft'))
|
54 |
+
bath = int(request.form.get('bath'))
|
55 |
+
bhk = int(request.form.get('bhk'))
|
56 |
+
|
57 |
+
price = predict_price(location, sqft, bath, bhk, model, feature_names)
|
58 |
+
prediction = round(price / 10, 2) # Convert to lakhs and round to 2 decimal places
|
59 |
+
|
60 |
+
# Redirect to results page with the prediction details
|
61 |
+
return redirect(url_for('results',
|
62 |
+
predicted_price=prediction,
|
63 |
+
location=location,
|
64 |
+
sqft=sqft,
|
65 |
+
bath=bath,
|
66 |
+
bhk=bhk))
|
67 |
+
except Exception as e:
|
68 |
+
error = f"Error: {str(e)}"
|
69 |
+
return render_template('index.html', locations=locations, error=error)
|
70 |
+
|
71 |
+
return render_template('index.html', locations=locations)
|
72 |
+
|
73 |
+
@app.route('/results')
|
74 |
+
def results():
|
75 |
+
# Fetch query parameters from the URL
|
76 |
+
predicted_price = request.args.get('predicted_price')
|
77 |
+
location = request.args.get('location')
|
78 |
+
sqft = request.args.get('sqft')
|
79 |
+
bath = request.args.get('bath')
|
80 |
+
bhk = request.args.get('bhk')
|
81 |
+
|
82 |
+
return render_template('results.html',
|
83 |
+
predicted_price=predicted_price,
|
84 |
+
location=location,
|
85 |
+
sqft=sqft,
|
86 |
+
bath=bath,
|
87 |
+
bhk=bhk)
|
88 |
+
|
89 |
+
if __name__ == '__main__':
|
90 |
+
debug_mode = os.getenv('FLASK_DEBUG', 'False').lower() in ['true', '1', 'yes']
|
91 |
+
app.run(debug=debug_mode)
|
requirements.txt
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Core packages
|
2 |
+
pandas==2.1.1
|
3 |
+
numpy==1.23.5
|
4 |
+
scikit-learn==1.3.0
|
5 |
+
flask==3.0.0
|
6 |
+
# Model and Data Processing
|
7 |
+
scipy==1.11.3
|
8 |
+
joblib==1.3.2
|
9 |
+
# Visualization
|
10 |
+
plotly==5.15.0
|
11 |
+
seaborn==0.12.2
|
12 |
+
matplotlib==3.8.0
|
13 |
+
# Dev and Utilities
|
14 |
+
ipython==8.12.0
|
15 |
+
jupyterlab==4.0.6
|
16 |
+
pytest==7.4.2
|
17 |
+
black==23.9.1
|
18 |
+
flake8==6.1.0
|
19 |
+
# Serialization
|
20 |
+
pickle-mixin==1.0.2
|
21 |
+
# Web App Utilities
|
22 |
+
gunicorn==21.2.0
|
23 |
+
# Environment Management
|
24 |
+
python-dotenv==1.0.0
|