Skip to content

Commit

Permalink
IRIS App
Browse files Browse the repository at this point in the history
  • Loading branch information
sampathweb committed Aug 12, 2016
0 parents commit f67d914
Show file tree
Hide file tree
Showing 19 changed files with 994 additions and 0 deletions.
99 changes: 99 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Virtual Env
env/venv

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# OSX Filesystem thing
.DS_Store

# Ignore temp files from other editors
*~
.#*
.*.sw?
.idea/
*.dpkg-*
*.exe
*.ZILESAVE

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# C extensions
*.so

# Installer logs
*.log
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Log Files
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# DotEnv configuration
.env

# Database
*.db
*.sqlite
*.rdb

# Pycharm
.idea

# Working Files - not ready to be shared
local/

# IPython NB Checkpoints
.ipynb_checkpoints/

# Exclude data from source control by default
/data/*

# Exclude Model Pickle Files
*.pickle
*.pkl
*.npy
10 changes: 10 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

The MIT License (MIT)
Copyright (c) 2016, Your name (or your organization/company/team)

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.

50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Sample IRIS API Application

Build IRIS Machine Learning Model using Scikit-Learn and deploy using Tornado Web Framework.

## Setup Environment on Local Machine

### Installation

```
cookiecutter https://github.com/sampathweb/cc-iris-api
cd <repo> # cd iris-api
# Install Packages
python env/create_env.py
source activate env/venv # Windows users: activate env/venv
python env/install_packages.py
# Build the Model
python ml_src/build_model.py
# Run the App
python run.py
````
### Test App
1. Open Browser: [http://localhost:9000](http://localhost:9000)
2. Command Line:
```
curl -i http://localhost:9000/api/iris/predict -X POST -d '{ "sepal_length": 2, "sepal_width": 5, "petal_length": 3, "petal_width": 4}'
```
3. Jupyter Notebook:
Open new terminal navigate to the new folder `iris-api`. Start `jupyter notebook`. Open ml_src -> `api_client.ipynb`. Test the API.
Api works!
## Credits:
Template from https://github.com/sampathweb/cc-iris-api
### The End.
Empty file added app/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions app/base_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Base Handler
"""

import json
import traceback
import logging
import tornado.web
import tornado.escape

from app.exceptions import ApplicationError, RouteNotFound, ServerError

logger = logging.getLogger("app")

class BaseApiHandler(tornado.web.RequestHandler):

@tornado.web.asynchronous
def post(self, action):
try:
# Fetch appropriate handler
if not hasattr(self, str(action)):
raise RouteNotFound(action)

# Pass along the data and get a result
handler = getattr(self, str(action))
data = tornado.escape.json_decode(self.request.body)
handler(data)
except ApplicationError as e:
logger.warning(e.message, e.code)
self.respond(e.message, e.code)
except Exception as e:
logger.error(traceback.format_exc())
error = ServerError()
self.respond(error.message, error.code)


def respond(self, data, code=200):
self.set_status(code)
self.write(json.JSONEncoder().encode({
"status": code,
"data": data
}))
self.finish()
41 changes: 41 additions & 0 deletions app/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Application Errors
"""

class ApplicationError(Exception):
def __init__(self, message, code):
self.message = message
self.code = code
super(Exception, self).__init__(message)


class InvalidJSON(ApplicationError):
def __init__(self):
ApplicationError.__init__(self,
"No JSON object could be decoded.",
400
)


class AuthError(ApplicationError):
def __init__(self):
ApplicationError.__init__(self,
"User not authenticated",
401
)


class RouteNotFound(ApplicationError):
def __init__(self, action):
ApplicationError.__init__(self,
"%s route could not be found" % action,
404
)


class ServerError(ApplicationError):
def __init__(self):
ApplicationError.__init__(self,
"we screwed up and have some debugging to do",
500
)
52 changes: 52 additions & 0 deletions app/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Request Handlers
"""

import tornado.web
from tornado import concurrent
from tornado import gen
from concurrent.futures import ThreadPoolExecutor

from app.base_handler import BaseApiHandler
from app.settings import MAX_MODEL_THREAD_POOL


class IndexHandler(tornado.web.RequestHandler):
"""APP is live"""

def get(self):
self.write("App is Live!")

def head(self):
self.finish()


class IrisPredictionHandler(BaseApiHandler):

_thread_pool = ThreadPoolExecutor(max_workers=MAX_MODEL_THREAD_POOL)

def initialize(self, model, *args, **kwargs):
self.model = model
super().initialize(*args, **kwargs)

@concurrent.run_on_executor(executor='_thread_pool')
def _blocking_predict(self, X):
target_values = self.model.predict(X)
target_names = ['setosa', 'versicolor', 'virginica']
results = [target_names[pred] for pred in target_values]
return results


@gen.coroutine
def predict(self, data):
if type(data) == dict:
data = [data]

X = []
for item in data:
record = (item.get("sepal_length"), item.get("sepal_width"), \
item.get("petal_length"), item.get("petal_width"))
X.append(record)

results = yield self._blocking_predict(X)
self.respond(results)
58 changes: 58 additions & 0 deletions app/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import logging
import logging.config

import tornado.ioloop
import tornado.web
from tornado.options import options

from sklearn.externals import joblib

from app.settings import MODEL_DIR
from app.handler import IndexHandler, IrisPredictionHandler


MODELS = {}


def load_model(pickle_filename):
return joblib.load(pickle_filename)


def main():

# Get the Port and Debug mode from command line options or default in settings.py
options.parse_command_line()


# create logger for app
logger = logging.getLogger('app')
logger.setLevel(logging.INFO)

FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(format=FORMAT)

# Load ML Models
logger.info("Loading IRIS Prediction Model...")
MODELS["iris"] = load_model(os.path.join(MODEL_DIR, "iris", "model.pkl"))

urls = [
(r"/$", IndexHandler),
(r"/api/iris/(?P<action>[a-zA-Z]+)?", IrisPredictionHandler,
dict(model=MODELS["iris"]))
]

# Create Tornado application
application = tornado.web.Application(
urls,
debug=options.debug,
autoreload=options.debug)

# Start Server
logger.info("Starting App on Port: {} with Debug Mode: {}".format(options.port, options.debug))
application.listen(options.port)
tornado.ioloop.IOLoop.current().start()


14 changes: 14 additions & 0 deletions app/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
Appliction configuration settings
"""
import os

from tornado.options import define

define("debug", default=True, help="Debug settings")
define("port", default=9000, help="Port to run the server on")

_CUR_DIR = os.path.dirname(os.path.realpath(__file__))
MODEL_DIR = os.path.join(_CUR_DIR, "..", "models")

MAX_MODEL_THREAD_POOL = 10
Loading

0 comments on commit f67d914

Please sign in to comment.