|
import gradio as gr |
|
import pickle |
|
import json |
|
import numpy as np |
|
|
|
|
|
with open("kigali_model.pickle", "rb") as f: |
|
model = pickle.load(f) |
|
|
|
with open("columns.json", "r") as f: |
|
data_columns = json.load(f)["data_columns"] |
|
|
|
|
|
location_mapping = { |
|
'kacyiru': 1, |
|
'kanombe': 2, |
|
'kibagabaga': 3, |
|
'kicukiro': 4, |
|
'kimironko': 5, |
|
'nyamirambo': 6, |
|
'nyarutarama': 7 |
|
} |
|
|
|
property_type_mapping = { |
|
'bungalow': 1, |
|
'house': 2, |
|
'villa': 3 |
|
} |
|
|
|
def transform_data(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type): |
|
|
|
x = np.zeros(len(data_columns)) |
|
|
|
|
|
x[0] = size_sqm |
|
x[1] = number_of_bedrooms |
|
x[2] = number_of_bathrooms |
|
x[3] = number_of_floors |
|
x[5] = parking_space |
|
|
|
|
|
if location in location_mapping: |
|
loc_index = data_columns.index(location) |
|
x[loc_index] = 1 |
|
|
|
|
|
if property_type in property_type_mapping: |
|
prop_index = data_columns.index(property_type) |
|
x[prop_index] = 1 |
|
|
|
return np.array([x]) |
|
|
|
|
|
def predict(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type): |
|
|
|
input_data_transformed = transform_data(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type) |
|
|
|
|
|
prediction = model.predict(input_data_transformed) |
|
return round(prediction[0], 2) |
|
|
|
|
|
inputs = [ |
|
gr.Number(label="Size (sqm)", value=0), |
|
gr.Number(label="Number of Bedrooms", value=0), |
|
gr.Number(label="Number of Bathrooms", value=0), |
|
gr.Number(label="Number of Floors", value=0), |
|
gr.Number(label="Parking Space", value=0), |
|
gr.Dropdown(choices=list(location_mapping.keys()), label="Location"), |
|
gr.Dropdown(choices=list(property_type_mapping.keys()), label="Property Type"), |
|
|
|
] |
|
|
|
|
|
outputs = gr.Textbox(label="Prediction (FRW)") |
|
|
|
|
|
footer = "Etienne NTAMBARA @AI_Engineer" |
|
|
|
|
|
gr.Interface( |
|
fn=predict, |
|
inputs=inputs, |
|
outputs=outputs, |
|
title="KIGALI Property Price Prediction (Real Time AI APPLICATION", |
|
description="Enter property details to get the price prediction.", |
|
article=footer |
|
).launch(debug=True) |
|
|
|
|