File size: 1,878 Bytes
4e43f51 2a2e057 7fd2308 2a2e057 7ba6cbc 4e43f51 654e814 0168a9b 654e814 4e43f51 21e0e83 4e43f51 654e814 4e43f51 654e814 |
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 |
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()
|