|
import gradio as gr |
|
import numpy as np |
|
import pandas as pd |
|
import tensorflow as tf |
|
from IPython.display import HTML |
|
|
|
|
|
model = tf.keras.models.load_model('real_estate_price_prediction_model.h5') |
|
|
|
|
|
original_df = pd.read_excel('Moroccan Real Estate Price Clean Dataset .xlsx') |
|
|
|
|
|
unique_secteurs = original_df['secteur'].unique() |
|
unique_cities = original_df['city'].unique() |
|
|
|
|
|
columns = ['surface', 'pieces', 'chambres', 'sdb', 'age', 'etage', 'etat_Bon état', 'etat_Nouveau', 'etat_À rénover', 'secteur', 'city'] |
|
|
|
|
|
def preprocess_input(user_input, columns, unique_secteurs, unique_cities): |
|
|
|
total_features = 1015 |
|
|
|
|
|
input_array = np.zeros((1, total_features), dtype=np.float64) |
|
|
|
|
|
numerical_features = ['surface', 'pieces', 'chambres', 'sdb', 'age', 'etage', 'etat_Bon état', 'etat_À rénover'] |
|
for feature in numerical_features: |
|
input_array[0, columns.index(feature)] = user_input[feature] |
|
|
|
|
|
for feature in ['secteur', 'city']: |
|
if user_input[feature] in unique_secteurs or user_input[feature] in unique_cities: |
|
input_array[0, columns.index(user_input[feature])] = 1 |
|
|
|
return input_array |
|
|
|
|
|
def predict_price(surface, pieces, chambres, sdb, age, etage, etat_Bon_état, etat_Nouveau, etat_À_rénover, secteur, city): |
|
|
|
user_input = { |
|
'surface': surface, |
|
'pieces': pieces, |
|
'chambres': chambres, |
|
'sdb': sdb, |
|
'age': age, |
|
'etage': etage, |
|
'etat_Bon état': etat_Bon_état, |
|
'etat_Nouveau': etat_Nouveau, |
|
'etat_À rénover': etat_À_rénover, |
|
'secteur': secteur, |
|
'city': city |
|
} |
|
input_array = preprocess_input(user_input, columns, unique_secteurs, unique_cities) |
|
|
|
|
|
predicted_price = model.predict(input_array) |
|
|
|
return f"Predicted price: {predicted_price[0][0]}" |
|
|
|
|
|
image_html = "<img src='/content/Capture d’écran 2024-01-28 155359.jpg' style='max-width:100%;'>" |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict_price, |
|
inputs=[ |
|
gr.Slider(label=f"Enter value for 'surface(m²)'", minimum=0, maximum=500, step=1), |
|
gr.Slider(label=f"Enter value for 'pieces'", minimum=0, maximum=15, step=1), |
|
gr.Slider(label=f"Enter value for 'chambres'", minimum=0, maximum=10, step=1), |
|
gr.Slider(label=f"Enter value for 'sdb'", minimum=0, maximum=5, step=1), |
|
gr.Slider(label=f"Enter value for 'age'", minimum=0, maximum=115, step=1), |
|
gr.Slider(label=f"Enter value for 'etage'", minimum=0, maximum=20, step=1), |
|
gr.Slider(label=f"Enter value for 'etat_Bon état'", minimum=0, maximum=1, step=1), |
|
gr.Slider(label=f"Enter value for 'etat_Nouveau'", minimum=0, maximum=1, step=1), |
|
gr.Slider(label=f"Enter value for 'etat_À rénover'", minimum=0, maximum=1, step=1), |
|
gr.Textbox(label=f"Enter value for 'secteur'", type="text"), |
|
gr.Textbox(label=f"Enter value for 'city'", type="text") |
|
], |
|
outputs=gr.Textbox(label="Predicted Price(Dh):", interactive=False), |
|
title="Real Estate Price Prediction", |
|
description="Enter property details to predict its price.", |
|
examples=[ |
|
[250, 5, 3, 2, 10, 3, 1, 0, 0, "'Secteur_A'", "'City_X'"], |
|
[150, 4, 2, 1, 5, 2, 1, 0, 0, "'Secteur_B'", "'City_Y'"] |
|
], |
|
theme="compact", |
|
) |
|
|
|
|
|
interface.launch(share=False, debug=False) |