saaara's picture
Create app.py
6f5130b verified
raw
history blame
3.06 kB
import gradio as gr
import numpy as np
import pandas as pd
import tensorflow as tf
# Load the trained model
model = tf.keras.models.load_model('real_estate_price_prediction_model.h5')
# Load the original dataset to get unique categories for 'secteur' and 'city'
original_df = pd.read_excel('/content/Moroccan Real Estate Price Clean Dataset .xlsx') # Replace with your dataset path
# Get unique categories for 'secteur' and 'city'
unique_secteurs = original_df['secteur'].unique()
unique_cities = original_df['city'].unique()
# Define the column names
columns = ['surface', 'pieces', 'chambres', 'sdb', 'age', 'etage', 'etat_Bon état', 'etat_Nouveau', 'etat_À rénover', 'secteur', 'city']
# Function to preprocess user input
def preprocess_input(user_input, columns, unique_secteurs, unique_cities):
# Define the total number of features expected by the model
total_features = 1015
# Initialize all features to 0
input_array = np.zeros((1, total_features), dtype=np.float64)
# Update numerical features
numerical_features = ['surface', 'pieces', 'chambres', 'sdb', 'age', 'etage', 'etat_Bon état', 'etat_Nouveau', 'etat_À rénover']
for feature in numerical_features:
input_array[0, columns.index(feature)] = user_input[feature]
# Update categorical features
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
# Function to predict price based on user input
def predict_price(user_input):
# Preprocess the user input
input_array = preprocess_input(user_input, columns, unique_secteurs, unique_cities)
# Make prediction using the model
predicted_price = model.predict(input_array)
return predicted_price[0][0]
# Gradio interface setup
interface = gr.Interface(
fn=predict_price, # The function to be called with user input
inputs=[
gr.Slider(label=f"Enter value for 'surface'", 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:", interactive=False)
)
# Launch the Gradio interface
interface.launch(share=False, debug=False)