Skip to content

Commit

Permalink
have working client example
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhanush123 committed Mar 5, 2020
1 parent 657c44d commit 7324d62
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Dockerfile
README.md
*.pyc
*.pyo
*.pyd
__pycache__
15 changes: 8 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ FROM python:3.8.1-slim
ENV APP_HOME /app
WORKDIR $APP_HOME

#workaround: https://github.com/moby/moby/issues/28617
ADD .env /app
RUN cat .env >> /etc/environment
# https://github.com/moby/moby/issues/28617
#*COPY_ENV_VARS_HERE*
ENV GCS_BUCKET_NAME shakti0
ENV GOOGLE_APPLICATION_CREDENTIALS gcs_creds.json
ENV PROJECT_ID shakti123

##install required files

#must run deploy command from folder containing server code
# must run deploy command from folder containing server code
COPY . ./

# Install production dependencies.
RUN pip install Flask gunicorn
RUN pip install Flask gunicorn joblib python-dotenv numpy scikit-learn

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 2 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
#to make app:app true, flask server variable name (left side of :) must be app
# to make app:app true, flask server variable name (left side of :) must be app
CMD exec gunicorn --bind :$PORT --workers 1 --threads 2 app:app
39 changes: 39 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os

from flask import Flask, jsonify, request
from joblib import load
from dotenv import load_dotenv
import sklearn
import numpy as np
import glob

app = Flask(__name__)
model = None


@app.before_request
def load_resources():
'''Flask template is currently only for scikit-learn models'''
load_dotenv()
# in future don't make user have to write flask server themselves
# that way don't need to use global b/c not thread-safe & using gunicorn
global model
if not model:
model = load(glob.glob("*joblib")[0])


def transform_data(input_data):
# will convert from 1D to required 2D
return np.array(input_data.tolist()[:784]).reshape(1, -1)


@app.route("/", methods=["GET", "POST"])
def predict():
img_nparray = np.fromstring(request.files["image"].read(), np.uint8)
transformed_data = transform_data(img_nparray)
prediction = model.predict(transformed_data).tolist()[0]
return jsonify({"prediction": prediction})


if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
Binary file added mnist_model.joblib
Binary file not shown.
34 changes: 0 additions & 34 deletions test1.py

This file was deleted.

0 comments on commit 7324d62

Please sign in to comment.