Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import tensorflow as tf
|
5 |
+
|
6 |
+
# Load the trained model
|
7 |
+
model = tf.keras.models.load_model('real_estate_price_prediction_model.h5')
|
8 |
+
|
9 |
+
# Load the original dataset to get unique categories for 'secteur' and 'city'
|
10 |
+
original_df = pd.read_excel('/content/Moroccan Real Estate Price Clean Dataset .xlsx') # Replace with your dataset path
|
11 |
+
|
12 |
+
# Get unique categories for 'secteur' and 'city'
|
13 |
+
unique_secteurs = original_df['secteur'].unique()
|
14 |
+
unique_cities = original_df['city'].unique()
|
15 |
+
|
16 |
+
# Define the column names
|
17 |
+
columns = ['surface', 'pieces', 'chambres', 'sdb', 'age', 'etage', 'etat_Bon état', 'etat_Nouveau', 'etat_À rénover', 'secteur', 'city']
|
18 |
+
|
19 |
+
# Function to preprocess user input
|
20 |
+
def preprocess_input(user_input, columns, unique_secteurs, unique_cities):
|
21 |
+
# Define the total number of features expected by the model
|
22 |
+
total_features = 1015
|
23 |
+
|
24 |
+
# Initialize all features to 0
|
25 |
+
input_array = np.zeros((1, total_features), dtype=np.float64)
|
26 |
+
|
27 |
+
# Update numerical features
|
28 |
+
numerical_features = ['surface', 'pieces', 'chambres', 'sdb', 'age', 'etage', 'etat_Bon état', 'etat_Nouveau', 'etat_À rénover']
|
29 |
+
for feature in numerical_features:
|
30 |
+
input_array[0, columns.index(feature)] = user_input[feature]
|
31 |
+
|
32 |
+
# Update categorical features
|
33 |
+
for feature in ['secteur', 'city']:
|
34 |
+
if user_input[feature] in unique_secteurs or user_input[feature] in unique_cities:
|
35 |
+
input_array[0, columns.index(user_input[feature])] = 1
|
36 |
+
|
37 |
+
return input_array
|
38 |
+
|
39 |
+
# Function to predict price based on user input
|
40 |
+
def predict_price(user_input):
|
41 |
+
# Preprocess the user input
|
42 |
+
input_array = preprocess_input(user_input, columns, unique_secteurs, unique_cities)
|
43 |
+
|
44 |
+
# Make prediction using the model
|
45 |
+
predicted_price = model.predict(input_array)
|
46 |
+
|
47 |
+
return predicted_price[0][0]
|
48 |
+
|
49 |
+
# Gradio interface setup
|
50 |
+
interface = gr.Interface(
|
51 |
+
fn=predict_price, # The function to be called with user input
|
52 |
+
inputs=[
|
53 |
+
gr.Slider(label=f"Enter value for 'surface'", minimum=0, maximum=500, step=1),
|
54 |
+
gr.Slider(label=f"Enter value for 'pieces'", minimum=0, maximum=15, step=1),
|
55 |
+
gr.Slider(label=f"Enter value for 'chambres'", minimum=0, maximum=10, step=1),
|
56 |
+
gr.Slider(label=f"Enter value for 'sdb'", minimum=0, maximum=5, step=1),
|
57 |
+
gr.Slider(label=f"Enter value for 'age'", minimum=0, maximum=115, step=1),
|
58 |
+
gr.Slider(label=f"Enter value for 'etage'", minimum=0, maximum=20, step=1),
|
59 |
+
gr.Slider(label=f"Enter value for 'etat_Bon état'", minimum=0, maximum=1, step=1),
|
60 |
+
gr.Slider(label=f"Enter value for 'etat_Nouveau'", minimum=0, maximum=1, step=1),
|
61 |
+
gr.Slider(label=f"Enter value for 'etat_À rénover'", minimum=0, maximum=1, step=1),
|
62 |
+
gr.Textbox(label=f"Enter value for 'secteur'", type="text"),
|
63 |
+
gr.Textbox(label=f"Enter value for 'city'", type="text")
|
64 |
+
],
|
65 |
+
outputs=gr.Textbox(label="Predicted Price:", interactive=False)
|
66 |
+
)
|
67 |
+
|
68 |
+
# Launch the Gradio interface
|
69 |
+
interface.launch(share=False, debug=False)
|