Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import joblib
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# This should be a DataFrame with the same structure as your training data
|
| 10 |
-
X = pd.DataFrame(columns=['host_id', 'neighbourhood_group', 'neighbourhood', 'room_type',
|
| 11 |
-
'latitude', 'longitude', 'number_of_reviews',
|
| 12 |
-
'calculated_host_listings_count']) # Add one-hot encoded columns as well
|
| 13 |
-
|
| 14 |
-
# Function to make predictions
|
| 15 |
-
def predict(host_id, neighbourhood_group, neighbourhood, room_type, latitude, longitude, number_of_reviews, calculated_host_listings_count):
|
| 16 |
-
# Prepare input data as DataFrame
|
| 17 |
-
input_data = pd.DataFrame({
|
| 18 |
'host_id': [host_id],
|
| 19 |
-
'
|
| 20 |
-
'
|
| 21 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
'latitude': [latitude],
|
| 23 |
-
'longitude': [longitude]
|
| 24 |
-
'number_of_reviews': [number_of_reviews],
|
| 25 |
-
'calculated_host_listings_count': [calculated_host_listings_count]
|
| 26 |
})
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# Ensure the input data has the same columns as the training data
|
| 32 |
-
input_data = input_data.reindex(columns=X.columns, fill_value=0)
|
| 33 |
-
|
| 34 |
-
# Make the prediction
|
| 35 |
-
predicted_price = loaded_model.predict(input_data)
|
| 36 |
return predicted_price[0]
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
fn=
|
| 41 |
inputs=[
|
| 42 |
gr.Number(label="Host ID"),
|
| 43 |
-
gr.Dropdown(
|
| 44 |
-
gr.Dropdown(
|
| 45 |
-
gr.
|
| 46 |
-
gr.Number(label="Latitude"),
|
| 47 |
-
gr.Number(label="Longitude"),
|
| 48 |
gr.Number(label="Number of Reviews"),
|
| 49 |
-
gr.Number(label="Calculated Host Listings Count")
|
|
|
|
|
|
|
| 50 |
],
|
| 51 |
-
outputs=
|
| 52 |
title="Airbnb Price Prediction",
|
| 53 |
-
description="Enter the details
|
| 54 |
)
|
| 55 |
|
| 56 |
# Launch the interface
|
| 57 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
|
| 4 |
+
# Function to predict price
|
| 5 |
+
def predict_price(host_id, neighbourhood_group, room_type, price, reviews, calculated_host_listings_count, latitude, longitude):
|
| 6 |
+
# Create a DataFrame for the input data
|
| 7 |
+
custom_data = pd.DataFrame({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
'host_id': [host_id],
|
| 9 |
+
'neighbourhood_group_Brooklyn': [1 if neighbourhood_group == 'Brooklyn' else 0],
|
| 10 |
+
'neighbourhood_group_Manhattan': [1 if neighbourhood_group == 'Manhattan' else 0],
|
| 11 |
+
'neighbourhood_group_Queens': [1 if neighbourhood_group == 'Queens' else 0],
|
| 12 |
+
'neighbourhood_group_Bronx': [1 if neighbourhood_group == 'Bronx' else 0],
|
| 13 |
+
'neighbourhood_group_Staten Island': [1 if neighbourhood_group == 'Staten Island' else 0],
|
| 14 |
+
'room_type_Shared room': [1 if room_type == 'Shared room' else 0],
|
| 15 |
+
'room_type_Private room': [1 if room_type == 'Private room' else 0],
|
| 16 |
+
'room_type_Entire home/apt': [1 if room_type == 'Entire home/apt' else 0],
|
| 17 |
+
'price': [price],
|
| 18 |
+
'number_of_reviews': [reviews],
|
| 19 |
+
'calculated_host_listings_count': [calculated_host_listings_count],
|
| 20 |
'latitude': [latitude],
|
| 21 |
+
'longitude': [longitude]
|
|
|
|
|
|
|
| 22 |
})
|
| 23 |
|
| 24 |
+
# Make prediction
|
| 25 |
+
predicted_price = model.predict(custom_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
return predicted_price[0]
|
| 27 |
|
| 28 |
+
# Define Gradio interface
|
| 29 |
+
interface = gr.Interface(
|
| 30 |
+
fn=predict_price,
|
| 31 |
inputs=[
|
| 32 |
gr.Number(label="Host ID"),
|
| 33 |
+
gr.Dropdown(["Brooklyn", "Manhattan", "Queens", "Bronx", "Staten Island"], label="Neighbourhood Group"),
|
| 34 |
+
gr.Dropdown(["Shared room", "Private room", "Entire home/apt"], label="Room Type"),
|
| 35 |
+
gr.Number(label="Price"),
|
|
|
|
|
|
|
| 36 |
gr.Number(label="Number of Reviews"),
|
| 37 |
+
gr.Number(label="Calculated Host Listings Count"),
|
| 38 |
+
gr.Number(label="Latitude"),
|
| 39 |
+
gr.Number(label="Longitude")
|
| 40 |
],
|
| 41 |
+
outputs="number",
|
| 42 |
title="Airbnb Price Prediction",
|
| 43 |
+
description="Enter the details to predict the price of an Airbnb listing."
|
| 44 |
)
|
| 45 |
|
| 46 |
# Launch the interface
|
| 47 |
+
interface.launch()
|