ntam0001 commited on
Commit
0577c3e
·
verified ·
1 Parent(s): 1c50b8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -21
app.py CHANGED
@@ -4,43 +4,82 @@ import json
4
  import numpy as np
5
 
6
  # Load model and columns
7
- with open("banglore_home_prices_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
- locations = data_columns[3:] # Extract location columns (FROM FORTH COLUMN TO END FOUND IN LOCATION)
 
 
 
 
 
 
 
 
 
 
14
 
15
- def predict_price(total_sqft, bath, bhk, location):
 
 
 
 
 
 
 
16
  # Prepare the input array
17
  x = np.zeros(len(data_columns))
18
- x[0] = total_sqft
19
- x[1] = bath
20
- x[2] = bhk
21
- if location in locations:
22
- loc_index = data_columns.index(location)
 
 
 
 
23
  x[loc_index] = 1
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Make prediction
26
- return model.predict([x])[0]
 
27
 
28
- # Create the Gradio interface
29
  inputs = [
30
- gr.Number(minimum=1,label="Total Square Feet"),
31
- gr.Number(minimum=1,label="Bath"),
32
- gr.Number(minimum=1,label="BHK [Bedroom, Hall, and Kitchen]"),
33
- gr.Dropdown(choices=locations, label="Location")
 
 
 
34
  ]
35
 
36
- outputs = gr.Textbox(label="Predicted Price (Lakh)")
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(fn=predict_price, inputs=inputs, outputs=outputs, title="Real Estate Price Prediction", article=footer).launch()
 
 
 
 
 
 
 
 
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)