Flask with Embedded Machine Learning IV : Deploy
Continued from Flask with Embedded Machine Learning III : Embedding Classifier.
Now that our app is ready, we're going to deploy our Flask web application.
Before we deploy our app to a remote, as the last step, we may want to test it on local Apache or Nginx server. In this case, we'll use Proxy HTTP traffic through apache2 to Flask.
First, we may set the port as 5000 from the default 5000 in app.py:
if __name__ == '__main__': app.run(debug=False, host='0.0.0.0', port=5077)
Apache proxy setup (/etc/apache2/sites-available/) should look like this:
<VirtualHost *:80> ServerName www.ahaman.example.com ServerAlias ahaman.example.com ProxyPreserveHost On ProxyPass / http://127.0.0.1:5077/ ProxyPassReverse / http://127.0.0.1:5077/ ErrorLog /var/www/ahaman.com/logs/error.log CustomLog /var/www/ahaman.com/logs/access.log combined LogLevel warn ServerSignature Off </VirtualHost>
We run app.py using PM2 like this:
$ pm2 start app.py
We can see the status of PM2 via pm2 ls:
To stop our app:
We may have to install wtforms, numpy if not already been installed on a remote server (CentOS7):
$ sudo pip install flask $ sudo pip install wtforms $ sudo yum install numpy scipy $ sudo pip install scikit-learn
Apache configuration looks like this:
<VirtualHost *:80> ServerName www.ahaman.com ServerAlias ahaman.com ProxyPreserveHost On ProxyPass / http://127.0.0.1:5077/ ProxyPassReverse / http://127.0.0.1:5077/ ErrorLog /var/www/ahaman.com/logs/error.log CustomLog /var/www/ahaman.com/logs/access.log combined LogLevel warn ServerSignature Off </VirtualHost>
Now we need two more things to make our app working: restart apache server and app run via pm2:
$ sudo service httpd restart $ pm2 start app.py
Now we can see our app is up and running.
Here is the initial page:
When we click "Submit review" button on the initial page, we get the classification with a probability from the embedded machine learning's sentiment analysis:
If the user select "Correct", we get the following thank you message:
Source is available from ahaman-Flask-with-Machine-Learning-Sentiment-Analysis
The final look of the demo site (ahaman.com has been changed. So, please checkout "production" branch.
Python Machine Learning, Sebastian Raschka
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization