-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
114 lines (82 loc) · 3.12 KB
/
main.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from flask import Flask, render_template, request
import pandas as pd
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split
from flask import Flask, render_template, jsonify
from dotenv import load_dotenv
import os
def config():
load_dotenv()
config()
app = Flask(__name__)
df = pd.read_csv(r"E:\EarthMark\House_Price_dataset.csv")
df['area'] = pd.to_numeric(
df['area'].str.replace(' Marla', ''), errors='coerce')
X = df[['area', 'bedrooms', 'baths', 'location']]
y = df['price']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
numerical_features = ['area', 'bedrooms', 'baths']
categorical_features = ['location']
numerical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='mean')),
('scaler', StandardScaler())
])
categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')),
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
preprocessor = ColumnTransformer(
transformers=[
('num', numerical_transformer, numerical_features),
('cat', categorical_transformer, categorical_features)
])
model = GradientBoostingRegressor(
n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)
pipeline = Pipeline(steps=[('preprocessor', preprocessor),
('model', model)])
pipeline.fit(X_train, y_train)
@app.route('/')
def landing_page():
return render_template('landing_page.html')
@app.route('/price_prediction', methods=['GET', 'POST'])
def price_prediction():
errors = {}
input_data = {}
predicted_price = None
if request.method == 'POST':
try:
area = float(request.form['land-area'])
bedrooms = float(request.form['num-bedrooms'])
baths = float(request.form['num-bathrooms'])
location = request.form['location']
input_data = {
'area': area,
'bedrooms': bedrooms,
'bathrooms': baths,
'location': location
}
user_input = pd.DataFrame({'area': [area],
'bedrooms': [bedrooms],
'baths': [baths],
'location': [location]})
predicted_price = pipeline.predict(user_input)[0]
except ValueError:
errors['input'] = 'Invalid input. Please enter numeric values.'
return render_template('price_prediction.html', predicted_price=predicted_price, input_data=input_data, errors=errors)
@app.route('/about')
def about():
return render_template('about_page.html')
@app.route('/news')
def news():
return render_template('news_page.html')
@app.route('/api/key', methods=['GET'])
def get_api_key():
api_key = os.getenv("API_KEY")
return jsonify(api_key=api_key)
if __name__ == "__main__":
app.run(debug=True)