Spaces:
Sleeping
Sleeping
File size: 3,317 Bytes
1c262e7 057590e 6f5130b c32b028 6f5130b 11f7fa4 6f5130b c32b028 6f5130b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
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("saaara/real_estate_price_prediction/mon_modele.bin")
import os
file_path = "saaara/real_estate_price_prediction/mon_modele.bin"
if os.path.exists(file_path):
model = tf.keras.models.load_model(file_path)
else:
print(f"Error: File '{file_path}' not found.")
# Load the original dataset to get unique categories for 'secteur' and 'city'
original_df = pd.read_excel("saaara/real_estate_price_prediction/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)
|