sivakornchong commited on
Commit
2a7ee55
1 Parent(s): ed2b5ab

first draft

Browse files
Files changed (3) hide show
  1. app.py +12 -6
  2. main.py +87 -83
  3. sample.py +29 -0
app.py CHANGED
@@ -1,17 +1,23 @@
1
  import gradio as gr
 
2
 
3
- def main(postal: int, floor: int):
4
- test = postal/floor
5
- return test
 
 
6
 
7
  iface = gr.Interface(
8
  fn=main,
9
  inputs= [
10
- gr.inputs.Number(default=680705, label='Postal Code', optional=False),
11
- gr.inputs.Number(default=11, label='Floor', optional=False)
 
 
 
12
  ],
13
  outputs= [
14
- gr.outputs.Textbox(type="text", label='House price ($)')
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
- # def main(Postal_,age_,town_,storey_,room_):
10
-
11
-
12
-
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
-
40
- ##POSTAL
41
- # Postal_input = Postal_
42
- Postal_input = 680705
43
- input[feature_names.index('Postal')] = int(Postal_input)
44
-
45
- ##DISTANCE TO MRT
46
- search_term = Postal_input #sample
47
- query_string='https://developers.onemap.sg/commonapi/search?searchVal={}&returnGeom=Y&getAddrDetails=Y&pageNum=1'.format(search_term)
48
- resp = requests.get(query_string)
49
- data = json.loads(resp.content)
50
- chosen_result = data['results'][0]
51
-
52
- #Calculate the distance to nearest MRT
53
- distance_km, nearest_mr = nearest_mrt(chosen_result['LATITUDE'], chosen_result['LONGITUDE'], mrt_name, mrt_loc)
54
- input[feature_names.index('distance_mrt')] = distance_km
55
- print(input)
56
-
57
- ##STOREY
58
- #Height is input, but then converted to the scale we used for iterating model
59
- # height_input = storey_
60
- height_input = 51
61
- Height = (height_input+2)//3
62
- input[feature_names.index('storey_height')] = Height
63
- print(input)
64
-
65
- ##TOWN
66
- # town_input = town_
67
- town_input = 'CHOA CHU KANG'
68
- input[feature_names.index("town_"+town_input)] = 1
69
- print(input)
70
-
71
- ##ROOM
72
- # room_input = room_
73
- room_input = '4 ROOM'
74
- input[feature_names.index("flat_num_"+room_input)] = 1
75
- print(input)
76
-
77
- ##AGE/ TRANSACTION YEAR [Current default to 2022]
78
- # age_input = age_
79
- age_input = 30
80
- input[feature_names.index('age_transation')] = age_input
81
- input[feature_names.index('transaction_yr')] = 2022 #Default to 2022 first
82
- print(input)
83
-
84
- #town is input
85
- #distance to MRT is calculated
86
- #age_transaction is age at transaction, which is input
87
- #Postal is input
88
-
89
-
90
-
91
- #reverse price adjustment is done with RPI
 
 
 
 
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)