import gradio as gr import numpy as np import pandas as pd import tensorflow as tf from IPython.display import HTML from tensorflow.keras.metrics import MeanSquaredError # 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('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_À 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(surface, pieces, chambres, sdb, age, etage, etat_Bon_état, etat_Nouveau, etat_À_rénover, secteur, city): # Preprocess the user input 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) # Make prediction using the model predicted_price = model.predict(input_array) return f"Predicted price: {predicted_price[0][0]}" # Create HTML code to display an image image_html = "" # 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(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", # Compact theme for a cleaner look ) # Launch the Gradio interface interface.launch(share=False, debug=False)