ntam0001 commited on
Commit
024b7f6
1 Parent(s): a519dd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -20
app.py CHANGED
@@ -2,7 +2,6 @@ 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("kigali_model.pickle", "rb") as f:
@@ -13,49 +12,52 @@ with open("columns.json", "r") as f:
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 = [
@@ -65,9 +67,11 @@ inputs = [
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
@@ -78,7 +82,7 @@ gr.Interface(
78
  fn=predict,
79
  inputs=inputs,
80
  outputs=outputs,
81
- title="Property Price Prediction in Kigali City @2024",
82
  description="Enter property details to get the price prediction.",
83
  article=footer
84
- ).launch(share=True)
 
2
  import pickle
3
  import json
4
  import numpy as np
 
5
 
6
  # Load model and columns
7
  with open("kigali_model.pickle", "rb") as f:
 
12
 
13
  # Define the location and property type mappings
14
  location_mapping = {
15
+ 'kacyiru': 1,
16
+ 'kanombe': 2,
17
+ 'kibagabaga': 3,
18
+ 'kicukiro': 4,
19
+ 'kimironko': 5,
20
+ 'nyamirambo': 6,
21
+ 'nyarutarama': 7
 
22
  }
23
 
24
  property_type_mapping = {
25
+ 'bungalow': 1,
26
+ 'house': 2,
27
+ 'villa': 3
 
28
  }
29
 
30
  def transform_data(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type):
31
+ # Prepare the input array with zeros
32
  x = np.zeros(len(data_columns))
33
+
34
+ # Assign input values to the corresponding 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[5] = parking_space # Ensure that parking_space aligns with the correct index in your model
40
 
41
+ # Apply location mapping
42
  if location in location_mapping:
43
  loc_index = data_columns.index(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(property_type)
49
  x[prop_index] = 1
50
 
51
  return np.array([x])
52
 
53
+
54
  def predict(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type):
55
  # Transform input data
56
  input_data_transformed = transform_data(size_sqm, number_of_bedrooms, number_of_bathrooms, number_of_floors, parking_space, location, property_type)
57
 
58
  # Predict using the model
59
  prediction = model.predict(input_data_transformed)
60
+ return round(prediction[0], 2) # round prediction for better readability
61
 
62
  # Define Gradio interface components
63
  inputs = [
 
67
  gr.Number(label="Number of Floors", value=0),
68
  gr.Number(label="Parking Space", value=0),
69
  gr.Dropdown(choices=list(location_mapping.keys()), label="Location"),
70
+ gr.Dropdown(choices=list(property_type_mapping.keys()), label="Property Type"),
71
+ # Add new inputs for other columns, like furnished, proximity, etc.
72
  ]
73
 
74
+
75
  outputs = gr.Textbox(label="Prediction (FRW)")
76
 
77
  # Footer content
 
82
  fn=predict,
83
  inputs=inputs,
84
  outputs=outputs,
85
+ title="KIGALI Property Price Prediction (Real Time AI APPLICATION",
86
  description="Enter property details to get the price prediction.",
87
  article=footer
88
+ ).launch(debug=True)