sivakornchong
commited on
Commit
•
2a7ee55
1
Parent(s):
ed2b5ab
first draft
Browse files
app.py
CHANGED
@@ -1,17 +1,23 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
6 |
|
7 |
iface = gr.Interface(
|
8 |
fn=main,
|
9 |
inputs= [
|
10 |
-
gr.inputs.Number(default=680705, label='Postal Code'
|
11 |
-
gr.inputs.Number(default=
|
|
|
|
|
|
|
12 |
],
|
13 |
outputs= [
|
14 |
-
gr.outputs.Textbox(type="text", label='House
|
15 |
]
|
16 |
)
|
17 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import main from main
|
3 |
|
4 |
+
#Input structure
|
5 |
+
##Postal_,age_,town_,storey_,room_ = 680705, 30, 'CHOA CHU KANG', 12, '5 ROOM'
|
6 |
+
|
7 |
+
town_list = ['ANG MO KIO', 'BEDOK', 'BISHAN', 'BUKIT BATOK', 'BUKIT MERAH', 'BUKIT PANJANG', 'BUKIT TIMAH', 'CENTRAL AREA', 'CHOA CHU KANG', 'CLEMENTI', 'GEYLANG', 'HOUGANG', 'JURONG EAST', 'JURONG WEST', 'KALLANG/WHAMPOA', 'MARINE PARADE', 'PASIR RIS', 'PUNGGOL', 'QUEENSTOWN', 'SEMBAWANG', 'SENGKANG', 'SERANGOON', 'TAMPINES', 'TOA PAYOH', 'WOODLANDS', 'YISHUN']
|
8 |
+
room_list = ['1 ROOM', '2 ROOM', '3 ROOM', '4 ROOM', '5 ROOM', 'EXECUTIVE', 'MULTI-GENERATION']
|
9 |
|
10 |
iface = gr.Interface(
|
11 |
fn=main,
|
12 |
inputs= [
|
13 |
+
gr.inputs.Number(default=680705, label='Postal Code'),
|
14 |
+
gr.inputs.Number(default=25, label='Age'),
|
15 |
+
gr.inputs.Dropdown(choices=town_list, type="value", default=None, label='Town'),
|
16 |
+
gr.inputs.Number(default=11, label='Floor'),
|
17 |
+
gr.inputs.Dropdown(choices=room_list, type="value", default=None, label='Room')
|
18 |
],
|
19 |
outputs= [
|
20 |
+
gr.outputs.Textbox(type="text", label='Predicted House Price ($)')
|
21 |
]
|
22 |
)
|
23 |
iface.launch()
|
main.py
CHANGED
@@ -4,88 +4,92 @@ from misc import nearest_mrt
|
|
4 |
import time
|
5 |
import pickle
|
6 |
import os
|
|
|
7 |
|
8 |
###This is to create MRT names and MRT locations
|
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 |
-
input[feature_names.index(
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
#
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
4 |
import time
|
5 |
import pickle
|
6 |
import os
|
7 |
+
import pandas as pd
|
8 |
|
9 |
###This is to create MRT names and MRT locations
|
10 |
+
|
11 |
+
|
12 |
+
def main(Postal_,age_,town_,storey_,room_):
|
13 |
+
##Input structure into model is##
|
14 |
+
filename = 'finalized_model.sav'
|
15 |
+
|
16 |
+
if os.path.exists("./finalized_model.sav"):
|
17 |
+
model = pickle.load(open(filename, 'rb'))
|
18 |
+
print('loaded model')
|
19 |
+
else:
|
20 |
+
print('failed loading model')
|
21 |
+
|
22 |
+
#extract feature names#
|
23 |
+
feature_names = model.feature_names
|
24 |
+
input = [0]*len(feature_names)
|
25 |
+
# print(feature_names)
|
26 |
+
|
27 |
+
#Set up mrt_list
|
28 |
+
mrt_name = []
|
29 |
+
mrt_loc = []
|
30 |
+
with open('data/mrt_list.json', 'r') as file:
|
31 |
+
for line in file:
|
32 |
+
item = json.loads(line)
|
33 |
+
mrt_name.append(item['MRT'])
|
34 |
+
loc = tuple([float(i) for i in item['location']])
|
35 |
+
mrt_loc.append(loc)
|
36 |
+
|
37 |
+
#Query for latitude and longitude
|
38 |
+
|
39 |
+
##POSTAL
|
40 |
+
Postal_input = Postal_
|
41 |
+
# Postal_input = 680705
|
42 |
+
input[feature_names.index('Postal')] = int(Postal_input)
|
43 |
+
|
44 |
+
##DISTANCE TO MRT
|
45 |
+
search_term = Postal_input #sample
|
46 |
+
query_string='https://developers.onemap.sg/commonapi/search?searchVal={}&returnGeom=Y&getAddrDetails=Y&pageNum=1'.format(search_term)
|
47 |
+
resp = requests.get(query_string)
|
48 |
+
data = json.loads(resp.content)
|
49 |
+
chosen_result = data['results'][0]
|
50 |
+
|
51 |
+
#Calculate the distance to nearest MRT
|
52 |
+
distance_km, nearest_mr = nearest_mrt(chosen_result['LATITUDE'], chosen_result['LONGITUDE'], mrt_name, mrt_loc)
|
53 |
+
input[feature_names.index('distance_mrt')] = distance_km
|
54 |
+
|
55 |
+
##STOREY
|
56 |
+
#Height is input, but then converted to the scale we used for iterating model
|
57 |
+
height_input = storey_
|
58 |
+
# height_input = 51
|
59 |
+
Height = (height_input+2)//3
|
60 |
+
input[feature_names.index('storey_height')] = Height
|
61 |
+
|
62 |
+
##TOWN
|
63 |
+
town_input = town_
|
64 |
+
# town_input = 'CHOA CHU KANG'
|
65 |
+
input[feature_names.index("town_"+town_input)] = 1
|
66 |
+
|
67 |
+
##ROOM
|
68 |
+
room_input = room_
|
69 |
+
# room_input = '4 ROOM'
|
70 |
+
input[feature_names.index("flat_num_"+room_input)] = 1
|
71 |
+
|
72 |
+
##AGE/ TRANSACTION YEAR [Current default to 2022]
|
73 |
+
age_input = age_
|
74 |
+
# age_input = 30
|
75 |
+
input[feature_names.index('age_transation')] = age_input
|
76 |
+
input[feature_names.index('transaction_yr')] = 2022 #Default to 2022 first
|
77 |
+
|
78 |
+
#Create final_dataframe as input to model
|
79 |
+
|
80 |
+
Actual = dict(zip(feature_names,input))
|
81 |
+
Actual_df = pd.DataFrame(Actual, index=[0])
|
82 |
+
|
83 |
+
resale_adj_price = model.predict(Actual_df)[0]
|
84 |
+
|
85 |
+
#New resale index is set arbitrarily as 170
|
86 |
+
resale_index = 170
|
87 |
+
price = resale_adj_price*resale_index/133.9
|
88 |
+
print(Actual_df)
|
89 |
+
|
90 |
+
return price
|
91 |
+
|
92 |
+
if __name__ == "__main__":
|
93 |
+
Postal_,age_,town_,storey_,room_ = 680705, 30, 'CHOA CHU KANG', 12, '5 ROOM'
|
94 |
+
price = main(Postal_,age_,town_,storey_,room_)
|
95 |
+
print(price)
|
sample.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
from misc import nearest_mrt
|
4 |
+
import time
|
5 |
+
import pickle
|
6 |
+
import os
|
7 |
+
import pandas as pd
|
8 |
+
|
9 |
+
|
10 |
+
filename = 'finalized_model.sav'
|
11 |
+
|
12 |
+
if os.path.exists("./finalized_model.sav"):
|
13 |
+
model = pickle.load(open(filename, 'rb'))
|
14 |
+
print('loaded model')
|
15 |
+
else:
|
16 |
+
print('failed loading model')
|
17 |
+
|
18 |
+
#extract feature names#
|
19 |
+
feature_names = model.feature_names
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
site_names = feature_names[5:31]
|
24 |
+
town = []
|
25 |
+
for town1 in site_names:
|
26 |
+
town1 = town1[5:]
|
27 |
+
town.append(town1)
|
28 |
+
|
29 |
+
print(town)
|