model / app.py
fahad1995's picture
Create app.py
4e43f51 verified
raw
history blame
1.94 kB
import gradio as gr
import pandas as pd
import joblib
# Load your Random Forest model
loaded_model = joblib.load('random_forest_model.pkl')
# Function to make predictions
def predict(host_id, neighbourhood_group, neighbourhood, room_type, latitude, longitude, number_of_reviews, calculated_host_listings_count):
# Prepare input data as DataFrame
input_data = pd.DataFrame({
'host_id': [host_id],
'neighbourhood_group': [neighbourhood_group],
'neighbourhood': [neighbourhood],
'room_type': [room_type],
'latitude': [latitude],
'longitude': [longitude],
'number_of_reviews': [number_of_reviews],
'calculated_host_listings_count': [calculated_host_listings_count]
})
# One-hot encode the categorical features
input_data = pd.get_dummies(input_data, columns=['room_type', 'neighbourhood_group', 'neighbourhood'], drop_first=True)
# Ensure the input data has the same columns as the training data
input_data = input_data.reindex(columns=X.columns, fill_value=0)
# Make the prediction
predicted_price = loaded_model.predict(input_data)
return predicted_price[0]
# Create a Gradio interface
iface = gr.Interface(
fn=predict,
inputs=[
gr.Number(label="Host ID"),
gr.Dropdown(["Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island"], label="Neighbourhood Group"),
gr.Dropdown(["Upper East Side", "Chelsea", "Williamsburg"], label="Neighbourhood"),
gr.Dropdown(["Entire home/apt", "Private room", "Shared room"], label="Room Type"),
gr.Number(label="Latitude"),
gr.Number(label="Longitude"),
gr.Number(label="Number of Reviews"),
gr.Number(label="Calculated Host Listings Count")
],
outputs="number",
title="NYC Rental Price Prediction",
description="Predict the rental price of an Airbnb listing in NYC."
)
# Launch the interface
iface.launch()