Last active
September 16, 2018 19:03
-
-
Save armsp/ecf07c12d14c2a5dc9329d12b3ebec1d to your computer and use it in GitHub Desktop.
Experiments with docker-compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
client: | |
#image: ubuntu:18.10 | |
# build : This directive can be used instead of image. Specifies the location of the Dockerfile that will be used to build this container | |
build: ./ | |
container_name: Chat_client | |
volumes: | |
- ./:/code | |
ports: | |
- 5001:5000 | |
environment: | |
DEBUG: "true" | |
server: | |
build: ./ | |
#image: ubuntu:18.10 | |
container_name: Chat_server | |
volumes: | |
- ./:/code | |
ports: | |
- 5002:5000 | |
environment: | |
DEBUG: "true" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.datasets import load_breast_cancer | |
from sklearn.model_selection import train_test_split | |
from sklearn.naive_bayes import GaussianNB | |
from sklearn.metrics import accuracy_score | |
from sklearn.externals import joblib | |
from mongoengine import * | |
from datetime import datetime | |
from flask import Flask, request | |
from flask_restful import Resource, Api | |
APP = Flask(__name__) | |
API = Api(APP) | |
connect('mlaas', host='localhost', port=27017) | |
try: | |
pickled_model = joblib.load('model.pkl') | |
except: | |
pickled_model = None | |
class TrainCollection(Document): | |
time = DateTimeField(default=datetime.now(), assign=True) | |
params = DictField(required=True) | |
class PredictCollection(Document): | |
time = DateTimeField(default=datetime.now(), assign=True) | |
features = DictField(required=True) | |
class Train(Resource): | |
def post(self): | |
model_kwargs = request.get_json(force=True) | |
if len(model_kwargs) > 1: | |
return "Invalid Parameters" | |
collection_obj = TrainCollection() | |
collection_obj.params = model_kwargs | |
collection_obj.save() | |
if model_kwargs.get('priors') == 'None': | |
model_kwargs['priors'] = None | |
data = load_breast_cancer() | |
labels = data['target'] | |
features = data['data'] | |
train, test, train_labels, test_labels = train_test_split(features, labels, test_size=0.33, random_state=42) | |
print(test.shape) | |
gnb = GaussianNB(**model_kwargs) | |
model = gnb.fit(train, train_labels) | |
joblib.dump(model, 'model.pkl') | |
preds = gnb.predict(test) | |
#print(preds) | |
accuracy = accuracy_score(test_labels, preds) | |
return {"Accuracy Score ":accuracy} | |
class Predict(Resource): | |
def get(self): | |
for predictions in PredictCollection.objects: | |
print(predictions.time) | |
def post(self): | |
global pickled_model | |
features = request.get_json(force=True) | |
collection_obj = PredictCollection() | |
collection_obj.features = features | |
collection_obj.save() | |
if pickled_model == None: | |
try: | |
pickled_model = joblib.load('model.pkl') | |
except: | |
return "Please Train your model" | |
else: | |
model_ = pickled_model | |
prediction_result = model_.predict(features['features']) | |
print(list(prediction_result)) | |
return {"Prediction Results": str(prediction_result)} | |
API.add_resource(Train, '/train') | |
API.add_resource(Predict, '/predict') | |
APP.run(host='0.0.0.0', port=5000, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment