Skip to content

Commit 3ebef46

Browse files
committed
initial commit
0 parents  commit 3ebef46

24 files changed

+1081
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Whitespace-only changes.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Ramesh Sampath
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Cookiecutter Starter Template to build and deploy Machine Learning model as Flask App
2+
3+
_A logical, reasonably standardized, but flexible project structure for doing and sharing data science and deploy the model as flask app_
4+
5+
6+
### Requirements to use the cookiecutter template:
7+
-----------
8+
- Python 2.7 or 3.5
9+
- [cookiecutter Python package](http://cookiecutter.readthedocs.org/en/latest/installation.html) >= 1.4.0: `pip install cookiecutter`
10+
11+
12+
### To start a new project, run:
13+
------------
14+
15+
cookiecutter https://github.com/sampathweb/cc-api-app
16+
17+
### Credits:
18+
------
19+
20+
* Based on the excellent CookieCutter Project (http://cookiecutter.readthedocs.org)
21+
22+
* Template adopted from [Data Driven CookieCutter Project](http://drivendata.github.io/cookiecutter-data-science/)
23+
24+
* Tornado App Structure inspired from Indico Server API (https://github.com/sihrc/tornado-boilerplate)

cookiecutter.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"project_name": "project_name",
3+
"repo_name": "{{ cookiecutter.project_name|replace(' ', '_') }}",
4+
"author_name": "Your name (or your organization/company/team)",
5+
"description": "A short description of the project.",
6+
"year": "2016",
7+
"open_source_license": ["MIT", "BSD", "Not open source"]
8+
}

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cookiecutter

{{cookiecutter.repo_name}}/.gitignore

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Virtual Env
2+
env/venv
3+
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
9+
# OSX Filesystem thing
10+
.DS_Store
11+
12+
# Ignore temp files from other editors
13+
*~
14+
.#*
15+
.*.sw?
16+
.idea/
17+
*.dpkg-*
18+
*.exe
19+
*.ZILESAVE
20+
21+
# Distribution / packaging
22+
.Python
23+
build/
24+
develop-eggs/
25+
dist/
26+
downloads/
27+
eggs/
28+
.eggs/
29+
lib/
30+
lib64/
31+
parts/
32+
sdist/
33+
var/
34+
*.egg-info/
35+
.installed.cfg
36+
*.egg
37+
38+
# PyInstaller
39+
# Usually these files are written by a python script from a template
40+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
41+
*.manifest
42+
*.spec
43+
44+
# C extensions
45+
*.so
46+
47+
# Installer logs
48+
*.log
49+
pip-log.txt
50+
pip-delete-this-directory.txt
51+
52+
# Unit test / coverage reports
53+
htmlcov/
54+
.tox/
55+
.coverage
56+
.coverage.*
57+
.cache
58+
nosetests.xml
59+
coverage.xml
60+
*,cover
61+
.hypothesis/
62+
63+
# Translations
64+
*.mo
65+
*.pot
66+
67+
# Log Files
68+
*.log
69+
70+
# Sphinx documentation
71+
docs/_build/
72+
73+
# PyBuilder
74+
target/
75+
76+
# DotEnv configuration
77+
.env
78+
79+
# Database
80+
*.db
81+
*.sqlite
82+
*.rdb
83+
84+
# Pycharm
85+
.idea
86+
87+
# Working Files - not ready to be shared
88+
local/
89+
90+
# IPython NB Checkpoints
91+
.ipynb_checkpoints/
92+
93+
# Exclude data from source control by default
94+
/data/*
95+
96+
# Exclude Model Pickle Files
97+
*.pickle
98+
*.pkl
99+
*.npy

{{cookiecutter.repo_name}}/LICENSE

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{% if cookiecutter.open_source_license == 'MIT' %}
2+
The MIT License (MIT)
3+
Copyright (c) {{ cookiecutter.year }}, {{ cookiecutter.author_name }}
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
{% elif cookiecutter.open_source_license == 'BSD' %}
11+
Copyright (c) {{ cookiecutter.year }}, {{ cookiecutter.author_name }}
12+
All rights reserved.
13+
14+
Redistribution and use in source and binary forms, with or without modification,
15+
are permitted provided that the following conditions are met:
16+
17+
* Redistributions of source code must retain the above copyright notice, this
18+
list of conditions and the following disclaimer.
19+
20+
* Redistributions in binary form must reproduce the above copyright notice, this
21+
list of conditions and the following disclaimer in the documentation and/or
22+
other materials provided with the distribution.
23+
24+
* Neither the name of {{ cookiecutter.project_name }} nor the names of its
25+
contributors may be used to endorse or promote products derived from this
26+
software without specific prior written permission.
27+
28+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
29+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
32+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
35+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
36+
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
37+
OF THE POSSIBILITY OF SUCH DAMAGE.
38+
{% endif %}

{{cookiecutter.repo_name}}/README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Sample IRIS API Application
2+
3+
Build IRIS Machine Learning Model using Scikit-Learn and deploy using Tornado Web Framework on AWS.
4+
5+
## Setup Environment on Local Machine
6+
7+
### Installation
8+
9+
```
10+
cookiecutter https://github.com/sampathweb/cc-iris-api
11+
12+
cd <repo> # cd iris-api
13+
14+
# Install Packages
15+
python env/create_env.py
16+
source activate env/venv # Windows users: activate env/venv
17+
python env/install_packages.py
18+
19+
# Build the Model
20+
python ml_src/build_model.py
21+
22+
# Run the App
23+
python run.py
24+
````
25+
26+
### Test App
27+
28+
29+
1. Open Browser: (http://localhost:9000)
30+
31+
2. Command Line:
32+
33+
```
34+
curl -i http://localhost:9000/api/iris/predict -X POST -d '{ "sepal_length": 2, "sepal_width": 5, "petal_length": 3, "petal_width": 4}'
35+
```
36+
37+
3. Jupyter Notebook:
38+
39+
Open new terminal navigate to the new folder `iris-api`. Start `jupyter notebook`. Open ml_src -> `api_client.ipynb`. Test the API.
40+
41+
Api works!
42+
43+
### Push code to your own git repo. (Optional - We will not do in tutorial)
44+
45+
46+
```
47+
git init
48+
git add --all
49+
git commit -m "first commit"
50+
git remote add origin https://github.com/<your github username>/<remote repo name>.git
51+
git push --set-upstream origin master
52+
53+
```
54+
55+
56+
## Deploy Steps for AWS Ubuntu 14.04 LTS EC2 Instance
57+
58+
### Login to AWS Instance:
59+
60+
`ssh -i <your AWS Pem key file> ubuntu@<aws ip>`
61+
62+
63+
### Install Python / Git
64+
65+
```
66+
sudo apt-get update
67+
sudo apt-get upgrade
68+
69+
# Install GIT
70+
sudo apt-get install git
71+
72+
# Install Anaconda (Miniconda)
73+
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
74+
75+
bash Miniconda3-latest-Linux-x86_64.sh
76+
77+
# To update the path (Make sure you said Yes when it asked to update path in the Miniconda install steps)
78+
source ~/.bashrc
79+
```
80+
81+
### Download App Source Code:
82+
83+
```
84+
git clone https://github.com/sampathweb/iris-api.git
85+
86+
87+
cd iris-api-app
88+
python env/create_env.py
89+
source activate env/venv
90+
python env/install_packages.py
91+
92+
python ml_src/build_model.py
93+
python run.py (Confirm that App is running)
94+
95+
96+
sudo apt-get install supervisor
97+
sudo vi /etc/supervisor/conf.d/iris-api.conf
98+
<press i insert mode>
99+
100+
[program:iris-api]
101+
autorestart = true
102+
command = /home/ubuntu/iris-api/env/venv/bin/python /home/ubuntu/iris-api/run.py --debug=False --port=80
103+
numprocs = 2
104+
startsecs = 10
105+
stderr_logfile = /var/log/supervisor/iris-api.log
106+
stdout_logfile = /var/log/supervisor/iris-api.log
107+
# stderr_logfile = syslog
108+
# stdout_logfile = syslog
109+
environment = PYTHONPATH="/home/ubuntu/iris-api/env/bin/"
110+
111+
<escape :wq>
112+
113+
sudo supervisorctl reload
114+
115+
<Your APP is live now>
116+
```
117+
118+
### Test the App
119+
120+
1. Open Browser: http://<AWS IP> (App is Live!)
121+
122+
2. Test API:
123+
124+
curl -i http://<aws ip address>/api/iris/predict -X POST -d '{ "sepal_length": 2, "sepal_width": 5, "petal_length": 3, "petal_width": 4}'
125+
126+
Congratulations you have deployed your App
127+
128+
## Credits:
129+
130+
Template from https://github.com/sampathweb/cc-iris-api
131+
132+
133+
### The End.

{{cookiecutter.repo_name}}/app/__init__.py

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Base Handler
3+
"""
4+
5+
import json
6+
import traceback
7+
import logging
8+
import tornado.web
9+
import tornado.escape
10+
11+
from app.exceptions import ApplicationError, RouteNotFound, ServerError
12+
13+
logger = logging.getLogger("app")
14+
15+
class BaseApiHandler(tornado.web.RequestHandler):
16+
17+
@tornado.web.asynchronous
18+
def post(self, action):
19+
try:
20+
# Fetch appropriate handler
21+
if not hasattr(self, str(action)):
22+
raise RouteNotFound(action)
23+
24+
# Pass along the data and get a result
25+
handler = getattr(self, str(action))
26+
data = tornado.escape.json_decode(self.request.body)
27+
handler(data)
28+
except ApplicationError as e:
29+
logger.warning(e.message, e.code)
30+
self.respond(e.message, e.code)
31+
except Exception as e:
32+
logger.error(traceback.format_exc())
33+
error = ServerError()
34+
self.respond(error.message, error.code)
35+
36+
37+
def respond(self, data, code=200):
38+
self.set_status(code)
39+
self.write(json.JSONEncoder().encode({
40+
"status": code,
41+
"data": data
42+
}))
43+
self.finish()

0 commit comments

Comments
 (0)