File size: 2,866 Bytes
280110b
 
 
 
 
 
ccce4ac
280110b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccce4ac
280110b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import utils
import pickle
import pandas as pd
import gradio as gr

def main(address :str, floor_area_sqft :int, remaining_lease_years :int, flat_type, storey_range):
    
    model_file = 'model.sav'

    flat_type_1, flat_type_2, flat_type_3, flat_type_4, flat_type_5, flat_type_6, flat_type_7 = 0, 0, 0 , 0, 0, 0, 0
    storey_low, storey_mid, storey_high = 0, 0, 0

    if flat_type == '1 ROOM':
        flat_type_1 = 1,

    elif flat_type == '2 ROOM':
        flat_type_2 = 1,

    elif flat_type == '3 ROOM':
        flat_type_3 = 1,

    elif flat_type == '4 ROOM':
        flat_type_4 = 1,

    elif flat_type == '5 ROOM':
        flat_type_5 = 1,

    elif flat_type == 'EXECUTIVE':
        flat_type_6 = 1,

    elif flat_type == 'MULTI-GENERATION':
        flat_type_7 = 1,

    if storey_range == 'LOW FLOOR':
        storey_low = 1,

    elif storey_range == 'MID FLOOR':
        storey_mid = 1,

    elif storey_range == 'HIGH FLOOR':
        storey_high = 1,
    
    if os.path.exists("./model.sav"):
        model = pickle.load(open(model_file, 'rb'))
        input_dict = pd.DataFrame({
            'floor_area_sqft': floor_area_sqft,
            'remaining_lease_years': remaining_lease_years,
            'distance_to_nearest_MRT_station': utils.distance_to_nearest_MRT_station(address),
            'distance_to_city': utils.distance_to_city(address),
            'flat_type_1 ROOM': flat_type_1,
            'flat_type_2 ROOM': flat_type_2,
            'flat_type_3 ROOM': flat_type_3,
            'flat_type_4 ROOM': flat_type_4,
            'flat_type_5 ROOM': flat_type_5,
            'flat_type_EXECUTIVE': flat_type_6,
            'flat_type_MULTI-GENERATION': flat_type_7,
            'storey_range_High Floor': storey_high,
            'storey_range_Low Floor': storey_low,
            'storey_range_Mid Floor': storey_mid}, 
            index = [0])
            
        return ({'Predicted PSF': round(model.predict(input_dict).item(),2), 'Predicted Price': round(model.predict(input_dict).item() * floor_area_sqft, 2)})

    else:
        return ('ERROR: No saved model')

iface = gr.Interface(
    fn = main,
    inputs = [
        gr.inputs.Textbox(lines=2, placeholder= "Example: 88 dawson road or Singapore 142088", default=None, label="Address"),
        gr.inputs.Number(default=893, label='Floor Area (sqft)', optional=False),
        gr.inputs.Number(default=None, label='Remaining Lease (years)', optional=False),
        gr.inputs.Dropdown(choices=['1 ROOM', '2 ROOM', '3 ROOM', '4 ROOM', '5 ROOM', 'EXECUTIVE', 'MULTI-GENERATION'], type="value", default=None, label="Flat Type"),
        gr.inputs.Dropdown(choices=['LOW FLOOR', 'MID FLOOR', 'HIGH FLOOR'], type="value", default=None, label="Storey Range"),
    ],
    outputs = [gr.outputs.Textbox(type="auto", label='Predicted Price per SQFT')])
iface.launch()