Keywords: Web App, Flask, AJAX, API, AWS, Virtual Environment
Previously, I built the Flask web app that runs on my local machine for predicting border crossing wait time. This time I’ll show how it gets deployed on AWS and becomes a public available web app.
Here is the link to web app http://35.164.32.109:5000/
There is a small change to my workflow. Instead of using Facebook Prophet, I changed to build an XGBoost model due to Prophet requirement of minimum 4GB memory. AWS free tier EC2 service only has 1GB memory.
Model is rebuilt daily using the new wait time records available from prior day, and makes forecasts for the next 7 days. The last 7 days records are held out for model validation and RMSE is used for model evaluation.
Python code can be found on my GitHub.
AWS Setup
There are plenty of resources on how to set up an AWS EC2 instance, e.g. AWS user guide, fast.ai reference, etc. I used Linux 2 AMI t2.micro which has 1 CPU and 1GB memory because it’s cheap. Just remember to configure HTTP, SSH and port 5000 (used for our web app access) in the security group.
Environment Setup and Package Install
After set up AWS EC2 instance, clone my GitHub project
git clone https://github.com/wangpengcn/border-crossing-delay-forecast-web-app-flask.git border_forecast
Then, run script to install python, create virtual environment, install necessary packages, and schedule daily model rebuild
./install.sh
Let’s take a look what this script does.
Define some environment variables.
FOLDER_PATH="/home/ec2-user/border_forecast/" FOLDER_NAME="border_forecast" VENV_NAME="venv_border_forecast"
Perform yum install update and install Python 3.6.
sudo yum update sudo yum install python36-pip
Note, if not sure what Python package available to install, run sudo yum list | grep python3
to check.
Next, install and create virtual environment, which has its own site directories and is isolated from system site directories. It makes project deployment much easier and independent of other ongoing Python projects.
pip3 install --user virtualenv python3 -m venv $VENV_NAME
Activate our virtual environment.
source $VENV_NAME/bin/activate
Next, install required Python packages
$VENV_NAME/bin/pip3 install -r requirements.txt
That’s it! Your Python environment is good to go.
The last line in this script is to schedule a daily job to rebuild prediction model and generate forecast for next 7 days.
(crontab -l 2>/dev/null; echo "0 5 * * * cd $FOLDER_PATH && $VENV_NAME/bin/python3 border_wait_time_forecast.py > /tmp/border.log") | crontab -
It’s coffee time! Let the script run for several minutes, and we’re ready to launch our web app.
Run Web App
Run ./run_app.sh
to launch the web app. It’ll run with a warning message “This is a development server“. This is because we’re running on a Flask development server. It should be replaced by a WSGI server in production instead.
Web app now is running on http://0.0.0.0:5000 When access, replace 0.0.0.0 with your AWS IPv4 public IP e.g. http://35.164.32.109:5000/
Done! Your web app is now live!
Future Works
- To expand forecast for other border crossing ports
- Allow user reporting actual border crossing time and incorporate into model
Again, Python code can be found on my GitHub.
Happy Machine Learning!
Wow, this looks really good. Do you have tracking of forecast vs. actual as a side process?
Thank you Nestor. Prediction evaluation via RMSE for the past 7 days compares predicted wait times and actual wait times. I know what you mean. It’s a great idea and it can be added.