Spaces:
Sleeping
Sleeping
| import json | |
| import pickle | |
| import numpy as np | |
| import gradio as gr | |
| # global variables | |
| __locations = None | |
| __data_columns = None | |
| __model = None | |
| def get_estimated_price(sqft, bhk, bath, location): | |
| # Predicts the price of a property using the features given as arguments | |
| try: | |
| # Find the index of the given location in data_columns | |
| loc_index = __data_columns.index(location.lower()) | |
| except: | |
| # Location could not be found | |
| loc_index = -1 | |
| # Create an array of input features | |
| x = np.zeros(len(__data_columns)) | |
| x[0] = sqft | |
| x[1] = bath | |
| x[2] = bhk | |
| # If location is not "other" and is a valid location | |
| if loc_index >= 0: | |
| x[loc_index] = 1 | |
| # Make a prediction on the price of the property using the model | |
| y_pred = round(__model.predict([x])[0],2) # Round to 2 d.p | |
| if y_pred <0: | |
| y_pred = 0 | |
| y_pred = y_pred * 100000 | |
| y_pred = str(y_pred) + "₹" | |
| return y_pred | |
| def get_location_names(): | |
| # Returns a list of all the locations in the dataset | |
| return __locations | |
| def load_saved_artifacts(): | |
| print("Loading Saved Artifacts...Start") | |
| global __data_columns, __locations, __model | |
| # file_path = "Week 20 & 21\RealEstateProject\model\columns.json" | |
| file_path = "columns.json" | |
| with open(file_path,"r") as f: | |
| # Loads the data with data_columns as a key from the json file | |
| __data_columns = json.load(f)["data_columns"] | |
| # Extract locations from the data columns | |
| __locations = __data_columns[3:] | |
| # file_path = "Week 20 & 21\RealEstateProject\model\home_prices_model.pickle" | |
| file_path = "home_prices_model.pickle" | |
| with open(file_path, "rb") as f: | |
| # Load the pickle model | |
| __model = pickle.load(f) | |
| print("Loading Saved Artifacts...Done") | |
| if __name__ == "__main__": | |
| load_saved_artifacts() | |
| # Define a list of inputs | |
| inputs = [ | |
| gr.Number( | |
| minimum=0, maximum=10000, info="The area of the house in square feet", | |
| placeholder="e.g: 5000", label="Area" | |
| ), | |
| gr.Radio( | |
| choices=[1,2,3,4,5], | |
| label="BHK", | |
| interactive="True", | |
| info="The number of bedrooms, halls and kitchens combined in the house", | |
| value=1 | |
| ), | |
| gr.Radio( | |
| choices=[1,2,3,4,5], | |
| label="Bathrooms", | |
| interactive="True", | |
| info="The number of bathrooms in the house", | |
| value=1 | |
| ), | |
| gr.Dropdown( | |
| choices=get_location_names(), | |
| label="Location", | |
| interactive=True, | |
| info="The location of the house in Bengaluru, India" | |
| ) | |
| ] | |
| # Create a Gradio Interface | |
| demo = gr.Interface( | |
| fn=get_estimated_price, # Function to use | |
| inputs = inputs, | |
| outputs= ["text"] | |
| ) | |
| # Launch the interface | |
| demo.launch(share=True) | |