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