-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
301 lines (239 loc) · 11.3 KB
/
app.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from flask import Flask,request,render_template,jsonify
import numpy as np
import joblib
import pandas as pd
import folium
from branca.element import Figure
app=Flask(__name__)
model=None
rmodel=None
@app.route('/')
def home():
return render_template('index.html')
@app.route("/main",methods=['POST'])
def main():
global model
global subtitle
global df
global rdf
global rmodel
button_value = request.form['button']
if button_value == 'hyderabad':
subtitle="Hyderabad"
model=joblib.load('models/hyderabad-pred.joblib')
df=pd.read_csv('models/FINAL_hyd.csv')
rmodel=joblib.load('models/FINAL_hyderabad_rent.joblib')
rdf=pd.read_csv('models/final_Hyderabad_rent.csv')
elif button_value == 'chennai':
subtitle="Chennai"
model=joblib.load('models/chennai-pred.joblib')
df=pd.read_csv('models/FINAL_che.csv')
rmodel=joblib.load('models/FINAL_chennai_rent.joblib')
rdf=pd.read_csv('models/final_Chennai_rent.csv')
elif button_value == 'kolkata':
subtitle="Kolkata"
model=joblib.load('models/kolkata-pred.joblib')
df=pd.read_csv('models/FINAL_kol.csv')
rmodel=joblib.load('models/FINAL_kolkata_rent.joblib')
rdf=pd.read_csv('models/final_Kolkata_rent.csv')
elif button_value == 'bangalore':
subtitle="Bangalore"
model=joblib.load('models/bangalore-pred.joblib')
df=pd.read_csv('models/FINAL_bang.csv')
rmodel=joblib.load('models/FINAL_bangalore_rent.joblib')
rdf=pd.read_csv('models/final_Bangalore_rent.csv')
elif button_value == 'mumbai':
subtitle="Mumbai"
model=joblib.load('models/mumbai-pred.joblib')
df=pd.read_csv('models/FINAL_mum.csv')
rmodel=joblib.load('models/FINAL_mumbai_rent.joblib')
rdf=pd.read_csv('models/final_Mumbai_rent.csv')
elif button_value == 'delhi':
subtitle="Delhi"
model=joblib.load('models/delhi-pred.joblib')
df=pd.read_csv('models/FINAL_del.csv')
rmodel=joblib.load('models/FINAL_delhi_rent.joblib')
rdf=pd.read_csv('models/final_Delhi_rent.csv')
rdf=rdf[rdf['Latitude']!='Not found'].reset_index(drop=True)
rdf['price'] = rdf['price'].str.replace(',', '')
rdf['price']=rdf['price'].astype(float)
jsonify(df.to_dict(orient='records'))
jsonify(rdf.to_dict(orient='records'))
print(rdf.head())
return render_template('main.html',sub_title=subtitle,)
@app.route("/predict",methods=['POST'])
def predict():
global subtitle
if request.form.get('predict') == 'predict':
print('c')
# extract the input values from the form
area = int(request.form['area'])
bed = int(request.form['bed'])
resale = int(request.form.get('resale'))
vastu = int(request.form.get('vastu'))
nearbylist=[]
nearby = request.form.getlist('nearby')
a=['gym', 'swim', 'land', 'jog', 'indo', 'mall', 'sport', '1atm', 'club', 'school', 'cafe', 'hosp', 'child']
for i in range(0,13):
if(a[i] in nearby):
nearbylist.append('1')
else:
nearbylist.append("0")
print(nearbylist)
nearbystr = ''.join(nearbylist)
nearbyint = int(nearbystr, 2)
esslist=[]
ess=request.form.getlist('ess')
b=['pow','car','lift','sec','gas']
for i in range(0,5):
if(b[i] in ess):
esslist.append('1')
else:
esslist.append("0")
print(esslist)
essstr = ''.join(esslist)
essint = int(essstr, 2)
location_str=(request.form['location'])
from geopy.geocoders import Nominatim
# Create a Nominatim geocoder object
geolocator = Nominatim(user_agent="http")
# Use geocoder to convert location string to latitude and longitude
location = geolocator.geocode(location_str)
if location is not None:
latitude = location.latitude
longitude = location.longitude
latitude=float(latitude)
longitude=float(longitude)
ip=[area,latitude,longitude,bed,resale,vastu,nearbyint,essint]
print(ip)
global model
pred=model.predict([ip])
out=round(pred[0],4)
pred_text="the PREDICTED price maybe:{} lakh".format(out)
else:
pred_text="Location not found"
return render_template('main.html',prediction_text=pred_text,sub_title=subtitle)
elif request.form.get('show') == 'show':
print('a')
global df
global rdf
f = float(request.form['from'])
t = float(request.form['to'])
d= request.form.get('rentorown')
print(d)
if(d=="1"):
houses_in_range = rdf[(rdf['price'] >= f) & (rdf['price'] <= t)]
locations = houses_in_range['locality'].tolist()
locations=list(set(locations))
# Check if there are any houses in the price range
if houses_in_range.empty:
print('d')
return render_template('main.html', map_message='No houses in that range',sub_title=subtitle)
else:
# Create a map centered on the first house in the range
map_center = [houses_in_range.iloc[0]['Latitude'], houses_in_range.iloc[0]['Longitude']]
m = folium.Map(location=map_center, zoom_start=12)
# loop through the filtered dataset and add markers to the map for each property
for index, row in houses_in_range.iterrows():
# create a popup with the property information
popup = folium.Popup(
f"<b>Place:</b> {row['locality']}<br>"
f"<b>Price:</b> {row['price']} rupees/month<br>"
f"<b>No. of Bedrooms:</b> {row['bedroom']} <br>"
f"<b>No. of Bathrooms:</b> {row['bathroom']} <br>"
f"<b>Location:</b> {row['Latitude']}, {row['Longitude']}<br>"
f"<b>Seller Type:</b> {row['seller_type']}<br>"
f"<b>Layout Type:</b> {row['layout_type']}<br>",
max_width=300)
tooltip=folium.Tooltip(
f"<b>Place:</b> {row['locality']}<br>"
f"<b>Area:</b> {row['area']} sqft<br>",
)
# add a marker for the property
folium.Marker(
location=[row['Latitude'], row['Longitude']],
popup=popup,tooltip=tooltip,
icon=folium.Icon(color='blue', icon='home')).add_to(m)
# Show the map
fig2=Figure(width=540,height=350) #for he size of the map
fig2.add_child(m)
folium.TileLayer('Stamen Terrain').add_to(m)
folium.TileLayer('Stamen Toner').add_to(m)
folium.TileLayer('Stamen Water Color').add_to(m)
folium.TileLayer('cartodbpositron').add_to(m)
folium.TileLayer('cartodbdark_matter').add_to(m)
folium.LayerControl().add_to(m)
else:
houses_in_range = df[(df['price'] >= f) & (df['price'] <= t)]
locations = houses_in_range['Location'].tolist()
locations=list(set(locations))
# Check if there are any houses in the price range
if houses_in_range.empty:
print('d')
return render_template('main.html', map_message='No houses in that range',sub_title=subtitle)
else:
# Create a map centered on the first house in the range
map_center = [houses_in_range.iloc[0]['latitude'], houses_in_range.iloc[0]['longitude']]
m = folium.Map(location=map_center, zoom_start=12)
# loop through the filtered dataset and add markers to the map for each property
for index, row in houses_in_range.iterrows():
# create a popup with the property information
popup = folium.Popup(
f"<b>Place:</b> {row['Location']}<br>"
f"<b>Area:</b> {row['Area']} sqft<br>"
f"<b>Price:</b> {row['price']} lakhs<br>"
f"<b>Location:</b> {row['latitude']}, {row['longitude']}<br>"
f"<b>No. of Bedrooms:</b> {row['No. of Bedrooms']}<br>"
f"<b>Resale:</b> {row['Resale']}<br>",
max_width=300)
tooltip=folium.Tooltip(
f"<b>Place:</b> {row['Location']}<br>"
f"<b>Area:</b> {row['Area']} sqft<br>",
)
# add a marker for the property
folium.Marker(
location=[row['latitude'], row['longitude']],
popup=popup,tooltip=tooltip,
icon=folium.Icon(color='blue', icon='home')).add_to(m)
# Show the map
fig2=Figure(width=540,height=350) #for he size of the map
fig2.add_child(m)
folium.TileLayer('Stamen Terrain').add_to(m)
folium.TileLayer('Stamen Toner').add_to(m)
folium.TileLayer('Stamen Water Color').add_to(m)
folium.TileLayer('cartodbpositron').add_to(m)
folium.TileLayer('cartodbdark_matter').add_to(m)
folium.LayerControl().add_to(m)
print('b')
return render_template('main.html',my_map=m._repr_html_(),locations=locations,fromm=int(f),to=int(t),sub_title=subtitle)
elif request.form.get('rpredict') == 'rpredict':
# extract the input values from the form
area = int(request.form['rarea'])
bed = int(request.form['rbed'])
bath = int(request.form['rbath'])
sellertype = int(request.form.get('sellertype'))
layouttype = int(request.form.get('layouttype'))
propertytype = int(request.form.get('propertytype'))
furnishtype = int(request.form.get('furnishtype'))
location_str=(request.form['location'])
from geopy.geocoders import Nominatim
# Create a Nominatim geocoder object
geolocator = Nominatim(user_agent="my_app")
# Use geocoder to convert location string to latitude and longitude
location = geolocator.geocode(location_str)
if location is not None:
latitude = location.latitude
longitude = location.longitude
latitude=float(latitude)
longitude=float(longitude)
ip=[sellertype, bed, layouttype, propertytype, area, furnishtype, bath, latitude, longitude]
print(ip)
global rmodel
pred=rmodel.predict([ip])
out=round(pred[0],4)
rpred_text="the PREDICTED price maybe:{} rupees ".format(out)
else:
rpred_text="Location not found"
return render_template('main.html',rprediction_text=rpred_text,sub_title=subtitle)
if __name__=="__main__":
app.run(debug=True)