model / app.py
fahad1995's picture
Update app.py
7ba6cbc verified
raw
history blame
1.88 kB
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 prediction function
def predict_price(host_id, neighbourhood_group, latitude, longitude, number_of_reviews, calculated_host_listings_count):
# Initialize custom input data
training_columns = model.feature_names_in_
custom_data = pd.DataFrame(0, index=[0], columns=training_columns)
custom_data = custom_data.astype({'latitude': 'float64', 'longitude': 'float64'}) # Ensure float types
# Specify values for the relevant columns in `custom_data`
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
# Set neighbourhood group feature
custom_data.at[0, f'neighbourhood_group_{neighbourhood_group}'] = 1
# Make prediction
predicted_price = model.predict(custom_data)
# Display input data and predicted price
input_data_display = custom_data.iloc[0].to_dict()
input_data_display['Predicted Price'] = predicted_price[0]
return input_data_display
# Define Gradio interface
inputs = [
gr.Number(label="Host ID"),
gr.Dropdown(choices=["Brooklyn", "Manhattan", "Queens", "Bronx", "Staten Island"], label="Neighbourhood Group"),
gr.Number(label="Latitude"),
gr.Number(label="Longitude"),
gr.Number(label="Number of Reviews"),
gr.Number(label="Calculated Host Listings Count")
]
output = gr.JSON(label="Input Data and Predicted Price")
gr.Interface(fn=predict_price, inputs=inputs, outputs=output, title="Airbnb Price Prediction", description="Input data to predict Airbnb listing prices").launch()