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()