Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import json
|
4 |
+
import numpy as np
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
# Load model and columns
|
8 |
+
with open("model/kigali_model.pickle", "rb") as f:
|
9 |
+
model = pickle.load(f)
|
10 |
+
|
11 |
+
with open("json/columns.json", "r") as f:
|
12 |
+
data_columns = json.load(f)["data_columns"]
|
13 |
+
|
14 |
+
# Define the location and property type mappings
|
15 |
+
location_mapping = {
|
16 |
+
'gacuriro': 1,
|
17 |
+
'kacyiru': 2,
|
18 |
+
'kanombe': 3,
|
19 |
+
'kibagabaga': 4,
|
20 |
+
'kicukiro': 5,
|
21 |
+
'kimironko': 6,
|
22 |
+
'nyamirambo': 7,
|
23 |
+
'nyarutarama': 8
|
24 |
+
}
|
25 |
+
|
26 |
+
property_type_mapping = {
|
27 |
+
'apartment': 1,
|
28 |
+
'bungalow': 2,
|
29 |
+
'house': 3,
|
30 |
+
'villa': 4
|
31 |
+
}
|
32 |
+
|
33 |
+
def transform_data(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type):
|
34 |
+
# Prepare the input array
|
35 |
+
x = np.zeros(len(data_columns))
|
36 |
+
x[0] = size_sqm
|
37 |
+
x[1] = number_of_bedrooms
|
38 |
+
x[2] = number_of_bathrooms
|
39 |
+
x[3] = number_of_floors
|
40 |
+
x[4] = parking_space
|
41 |
+
|
42 |
+
if location in location_mapping:
|
43 |
+
loc_index = data_columns.index(location)
|
44 |
+
x[loc_index] = 1
|
45 |
+
|
46 |
+
if property_type in property_type_mapping:
|
47 |
+
prop_index = data_columns.index(property_type)
|
48 |
+
x[prop_index] = 1
|
49 |
+
|
50 |
+
return np.array([x])
|
51 |
+
|
52 |
+
def predict(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type):
|
53 |
+
# Transform input data
|
54 |
+
input_data_transformed = transform_data(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type)
|
55 |
+
|
56 |
+
# Predict using the model
|
57 |
+
prediction = model.predict(input_data_transformed)
|
58 |
+
return prediction[0]
|
59 |
+
|
60 |
+
# Define Gradio interface components
|
61 |
+
inputs = [
|
62 |
+
gr.Number(label="Size (sqm)", value=0),
|
63 |
+
gr.Number(label="Number of Bedrooms", value=0),
|
64 |
+
gr.Number(label="Number of Bathrooms", value=0),
|
65 |
+
gr.Number(label="Number of Floors", value=0),
|
66 |
+
gr.Number(label="Parking Space", value=0),
|
67 |
+
gr.Dropdown(choices=list(location_mapping.keys()), label="Location"),
|
68 |
+
gr.Dropdown(choices=list(property_type_mapping.keys()), label="Property Type")
|
69 |
+
]
|
70 |
+
|
71 |
+
outputs = gr.Textbox(label="Prediction (FRW)")
|
72 |
+
|
73 |
+
# Footer content
|
74 |
+
footer = "Etienne NTAMBARA @AI_Engineer"
|
75 |
+
|
76 |
+
# Launch the interface
|
77 |
+
gr.Interface(
|
78 |
+
fn=predict,
|
79 |
+
inputs=inputs,
|
80 |
+
outputs=outputs,
|
81 |
+
title="Property Price Prediction",
|
82 |
+
description="Enter property details to get the price prediction.",
|
83 |
+
article=footer
|
84 |
+
).launch()
|