|
import gradio as gr |
|
import pandas as pd |
|
import joblib |
|
|
|
|
|
model = joblib.load('random_forest_model.pkl') |
|
|
|
|
|
def predict_price(host_id, neighbourhood_group, latitude, longitude, number_of_reviews, calculated_host_listings_count): |
|
|
|
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'}) |
|
|
|
|
|
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, f'neighbourhood_group_{neighbourhood_group}'] = 1 |
|
|
|
|
|
predicted_price = model.predict(custom_data) |
|
|
|
|
|
input_data_display = custom_data.iloc[0].to_dict() |
|
input_data_display['Predicted Price'] = predicted_price[0] |
|
|
|
return input_data_display |
|
|
|
|
|
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() |
|
|