Created
December 28, 2020 13:37
-
-
Save deepak-karkala/7c62510825ed01574ff0a2c7dec8e2ab to your computer and use it in GitHub Desktop.
FLASK Webapp for serving price Predictions for Airbnb listings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FLASK Webapp for serving price Predictions for Airbnb listings | |
import os, sys | |
sys.path.append(".") | |
import webapp_predict_price | |
import pickle | |
from flask import Flask | |
import flask | |
import sklearn | |
import joblib | |
import pandas as pd | |
# Load pre-trained machine learning model. | |
BASE_PATH = "webapp_predict_price/" | |
model = joblib.load(BASE_PATH + "best_model.pkl") | |
def create_app(test_config=None): | |
# create and configure the app | |
app = Flask(__name__, instance_relative_config=True) | |
app.config.from_mapping( | |
SECRET_KEY='dev', | |
#DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), | |
) | |
if test_config is None: | |
# load the instance config, if it exists, when not testing | |
app.config.from_pyfile('config.py', silent=True) | |
else: | |
# load the test config if passed in | |
app.config.from_mapping(test_config) | |
# ensure the instance folder exists | |
try: | |
os.makedirs(app.instance_path) | |
except OSError: | |
pass | |
# Landing page | |
@app.route('/', methods=['GET', 'POST']) | |
def hello(): | |
# return 'Hello, World!' | |
# Return landing page | |
if flask.request.method == 'GET': | |
return(flask.render_template('base.html')) | |
# Return prediction output | |
if flask.request.method == 'POST': | |
# Create input to Model from form data | |
df_input = pd.DataFrame([[country, city, neighbourhood, propertytype, roomtype, bedtype, | |
cancellationpolicy, hostresponsetime, accommodates, num_bedrooms, num_beds, | |
min_nights, availability_30, availability_60, availability_90, availability_365, | |
num_reviews, reviews_per_month, review_scores_rating, review_scores_accuracy, | |
review_scores_cleanliness, review_scores_checkin, review_scores_communication, | |
review_scores_location, review_scores_value, host_response_rate, | |
]], dtype=float) | |
# Inference: Get prediction from Model | |
prediction_price = model.predict(df_input)[0] | |
prediction_price = round(prediction_price) | |
return(flask.render_template('base.html', result=prediction_price)) | |
return app | |
# if this is the main thread of execution first load the model and | |
# then start the server | |
if __name__ == "__main__": | |
print(("* Loading Scikit-learn model and Flask starting server..." | |
"please wait until server has fully started")) | |
app = create_app() | |
app.run(host='0.0.0.0', port=5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment