model / app.py
fahad1995's picture
Update app.py
58ab919 verified
import gradio as gr
import pandas as pd
import joblib
# Load the trained model
model = joblib.load('random_forest_model.pkl') # replace with your model path
# Define the function to make predictions
def predict_price(host_id, latitude, longitude, number_of_reviews, calculated_host_listings_count,
room_type_Private_room, room_type_Shared_room,
neighbourhood_group_Brooklyn, neighbourhood_group_Manhattan,
neighbourhood_group_Queens, neighbourhood_group_Staten_Island,
neighbourhood_Arden_Heights, neighbourhood_Arrochar, neighbourhood_Arverne):
# Prepare input data
custom_data = pd.DataFrame(0, index=[0], columns=model.feature_names_in_)
custom_data.at[0, 'host_id'] = host_id
custom_data.at[0, 'latitude'] = latitude
custom_data.at[0, 'longitude'] = longitude
custom_data.at[0, 'number_of_reviews'] = number_of_reviews
custom_data.at[0, 'calculated_host_listings_count'] = calculated_host_listings_count
custom_data.at[0, 'room_type_Private room'] = room_type_Private_room
custom_data.at[0, 'room_type_Shared room'] = room_type_Shared_room
custom_data.at[0, 'neighbourhood_group_Brooklyn'] = neighbourhood_group_Brooklyn
custom_data.at[0, 'neighbourhood_group_Manhattan'] = neighbourhood_group_Manhattan
custom_data.at[0, 'neighbourhood_group_Queens'] = neighbourhood_group_Queens
custom_data.at[0, 'neighbourhood_group_Staten Island'] = neighbourhood_group_Staten_Island
custom_data.at[0, 'neighbourhood_Arden Heights'] = neighbourhood_Arden_Heights
custom_data.at[0, 'neighbourhood_Arrochar'] = neighbourhood_Arrochar
custom_data.at[0, 'neighbourhood_Arverne'] = neighbourhood_Arverne
# Make prediction
predicted_price = model.predict(custom_data)
return f"The predicted house price is: ${predicted_price[0]:.2f}"
# Set up the Gradio interface
title = "House Price Predictor"
description = """
This application predicts the price of a house based on several features.
Please fill in the following details to get a prediction:
- **Latitude**: Geographic coordinate.
- **Longitude**: Geographic coordinate.
- **Number of Reviews**: Total reviews received by the listing.
- **Calculated Host Listings Count**: Total number of listings by the host.
- **Room Type**: Select whether the room is a private or shared room.
- **Neighbourhood Groups**: Select the corresponding neighbourhood group.
After entering the information, click on the **'Submit'** button to see the predicted price.
"""
inputs = [
gr.Number(label="Latitude"),
gr.Number(label="Longitude"),
gr.Number(label="Number of Reviews"),
gr.Number(label="Calculated Host Listings Count"),
gr.Radio(label="Room Type - Private Room", choices=[0, 1]),
gr.Radio(label="Room Type - Shared Room", choices=[0, 1]),
gr.Radio(label="Neighbourhood Group - Brooklyn", choices=[0, 1]),
gr.Radio(label="Neighbourhood Group - Manhattan", choices=[0, 1]),
gr.Radio(label="Neighbourhood Group - Queens", choices=[0, 1]),
gr.Radio(label="Neighbourhood Group - Staten Island", choices=[0, 1]),
gr.Radio(label="Neighbourhood - Arden Heights", choices=[0, 1]),
gr.Radio(label="Neighbourhood - Arrochar", choices=[0, 1]),
gr.Radio(label="Neighbourhood - Arverne", choices=[0, 1]),
]
output = gr.Textbox(label="Predicted Price", placeholder="The predicted price will appear here.", lines=2)
gr.Interface(fn=predict_price, inputs=inputs, outputs=output, title=title, description=description).launch()