File size: 2,768 Bytes
f457408
 
 
 
 
 
e3c68f6
f457408
 
e3c68f6
f457408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ade5ce9
 
 
 
 
f457408
 
 
 
 
 
 
 
 
 
 
 
 
 
5ba6caa
f457408
 
784af7f
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
79
80
81
82
83
84
import gradio as gr
import pickle
import json
import numpy as np

# Load model and columns
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"]

# Define the location and property type mappings
location_mapping = {
    'gacuriro': 1,
    'kacyiru': 2,
    'kanombe': 3,
    'kibagabaga': 4,
    'kicukiro': 5,
    'kimironko': 6,
    'nyamirambo': 7,
    'nyarutarama': 8
}

property_type_mapping = {
    'apartment': 1,
    'bungalow': 2,
    'house': 3,
    'villa': 4
}

def transform_data(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type):
    # Prepare the input array
    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[4] = 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):
    # Transform input data
    input_data_transformed = transform_data(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type)
    
    # Predict using the model
    prediction = model.predict(input_data_transformed)
    return prediction[0]

# Define Gradio interface components
inputs = [
    gr.Number(label="Size (sqm)", value=0, placeholder="Enter size in sqm", minimum=0, interactive=True),
    gr.Number(label="Number of Bedrooms", value=0, placeholder="Enter number of bedrooms", minimum=0, interactive=True),
    gr.Number(label="Number of Bathrooms", value=0, placeholder="Enter number of bathrooms", minimum=0, interactive=True),
    gr.Number(label="Number of Floors", value=0, placeholder="Enter number of floors", minimum=0, interactive=True),
    gr.Number(label="Parking Space", value=0, placeholder="Enter parking space availability", minimum=0, interactive=True),
    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 content
footer = "Etienne NTAMBARA @AI_Engineer"

# Launch the interface
gr.Interface(
    fn=predict,
    inputs=inputs,
    outputs=outputs,
    title="Property Price Prediction in Kigali City @2024",
    description="Enter property details to get the price prediction.",
    article=footer
).launch(share=True)