Skip to content

Commit

Permalink
Initial Import from BitBucket
Browse files Browse the repository at this point in the history
  • Loading branch information
hagzag committed Jul 10, 2018
0 parents commit b1c5d67
Show file tree
Hide file tree
Showing 39 changed files with 3,266 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
./idea/*
./idea
.idea
__pycache__
venv
images
data
.DS_Store
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM ubuntu:latest
MAINTAINER Rajdeep Dua "dua_rajdeep@yahoo.com"
RUN apt-get update -y
RUN apt-get install -y python3-pip build-essential
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3"]
CMD ["app.py"]
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn app:app --log-file=-
97 changes: 97 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import os
from flask import Flask, render_template, url_for, send_from_directory
from flask import jsonify
from flask import request
from flask_cors import CORS
from flask_uploads import UploadSet, configure_uploads, IMAGES
from boto import create_file_in_s3, create_file_in_local
import numpy as np

app = Flask(__name__)
app.config.from_pyfile('config.py', silent=True)
app.config['UPLOADED_PHOTOS_DEST'] = 'images'
photos = UploadSet('photos', IMAGES)

configure_uploads(app, photos)

CORS(app, automatic_options=True)


@app.route('/')
def space_invaders():
return render_template('%s.html' % "spaceinvaders")

@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')

@app.route('/thanks')
def thanks():
return "thanks"


@app.route('/upload', methods=['POST'], )
def save_photos():
if app.config['SAVE_IMAGES']:
for file in request.files.keys():
if app.config['SAVE_TARGET'] == 'local':
print("Saving images to local...")
create_file_in_local(file, "a", request.files[file])
elif app.config['SAVE_TARGET'] == 's3':
print("Saving images to s3...")
create_file_in_s3(file, "a", request.files[file])
else:
print("Target configuration invalid (%s). Skipping data saving." % app.config['SAVE_TARGET'])
else:
print("Skipping images saving.")

return jsonify({"path": url_for('thanks')})


@app.route('/upload_mobilenet_pred', methods=['POST'])
def save_mobilenet_predictions():
if request.json is None:
print("Non JSON predictions. Quitting")
return jsonify({"path": url_for('thanks')})

if not app.config['SAVE_TENSORS']:
print("Skipping tensor saving")
return jsonify({"path": url_for('thanks')})

body = request.json
xs = body['xs']
ys = body['ys']
xs_rows, xs_columns = get_2d_shape(xs['shape'])
ys_rows, ys_columns = get_2d_shape(ys['shape'])
if xs_rows != ys_rows:
print("X rows count (%d) are not equal to Y rows (%d)" % (xs_rows, ys_rows))
jsonify({"error": "X rows count (%d) are not equal to Y rows (%d)" % (xs_rows, ys_rows)})

print("Saving tensors of %d predictions" % xs_rows)

# Save X
xs_2d = np.reshape(xs['data'], (xs_rows, xs_columns))
xs_data_file = open('data/xs.csv','ab')
np.savetxt(xs_data_file, xs_2d, delimiter=",")
xs_data_file.close()

# Save Y
ys_2d = np.reshape(ys['data'], (ys_rows, ys_columns))
ys_data_file = open('data/ys.csv','ab')
np.savetxt(ys_data_file, ys_2d, delimiter=",")
ys_data_file.close()
return jsonify({"path": url_for('thanks')})


def get_2d_shape(shape):
rows = shape[0]
columns = 1
for i, num in enumerate(shape):
if i == 0:
continue
columns *= num
return rows, columns


if __name__ == '__main__':
app.run(host="0.0.0.0")
31 changes: 31 additions & 0 deletions boto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os


def create_file_in_s3(file_name, path, file):
import boto3
s3 = boto3.resource('s3')
print("fileName = %s, path = %s, file = %s" % (file_name, path, type(file)))
s3.Object('collect-data-for-machine-learning', file_name).put(Body=file)


def create_file_in_local(file_name, path, file):
save_path = "images"
if not os.path.exists(save_path):
os.mkdir(save_path)
print("fileName = %s, path = %s, file = %s" % (file_name, path, type(file)))
file_path = os.path.join(save_path, file_name)
file.save(str(file_path))


"""
bucket = s3.Bucket('collect-data-for-machine-learning')
exists = True
try:
s3.meta.client.head_bucket(Bucket='collect-data-for-machine-learning')
except botocore.exceptions.ClientError as e:
# If a client error is thrown, then check that it was a 404 error.
# If it was a 404 error, then the bucket does not exist.
error_code = int(e.response['Error']['Code'])
if error_code == 404:
exists = False
"""
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SAVE_IMAGES = False # Whether to save images of users. Only if True, the SAVE_TARGET is relevant
SAVE_TENSORS = True
SAVE_TARGET = "local" # For local: "local", for S3: "s3"
39 changes: 39 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: "3.2"

networks:
proxy:
driver: bridge

services:
traefik:
image: traefik:1.6
#command: --web --logLevel=DEBUG --web.metrics.prometheus --web.metrics.prometheus.buckets="0.1,0.3,1.2,5.0"
ports:
- "80:80"
- "8080:8080"
- "443:443"
networks:
- proxy
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./traefik.toml:/traefik.toml
#- ./usersFile:/usersFile
- ./acme.json:/acme.json
environment:
AWS_REGION: eu-central-1
AWS_HOSTED_ZONE_ID:
labels:
traefik.enable: "false"

spaceinvaders:
image: tikalk/si-model-trainer:latest
#ports:
# - "5000:5000"
networks:
- proxy
labels:
traefik.enable: "true"
traefik.port: 5000
traefik.frontend.passHostHeader: "true"
traefik.frontend.rule: "Host:si.hub.tikal.io"

45 changes: 45 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
absl-py==0.2.2
astor==0.6.2
bleach==1.5.0
boto3==1.7.35
botocore==1.10.35
click==6.7
cycler==0.10.0
docutils==0.14
Flask==1.0.2
Flask-Cors==3.0.4
Flask-Uploads==0.2.1
gast==0.2.0
grpcio==1.12.1
gunicorn==19.8.1
h5py==2.7.1
html5lib==0.9999999
itsdangerous==0.24
Jinja2==2.10
jmespath==0.9.3
Keras==2.1.4
Keras-Applications==1.0.2
Keras-Preprocessing==1.0.1
kiwisolver==1.0.1
Markdown==2.6.11
MarkupSafe==1.0
matplotlib==2.2.2
numpy==1.14.1
opencv-python==3.4.1.15
Pillow==5.1.0
protobuf==3.5.2.post1
pyparsing==2.2.0
python-dateutil==2.7.3
pytz==2018.4
PyYAML==3.12
s3transfer==0.1.13
scikit-learn==0.19.1
scipy==1.1.0
six==1.11.0
sklearn==0.0
tensorboard==1.8.0
tensorflow==1.8.0
tensorflow-hub==0.1.0
tensorflowjs==0.4.2
termcolor==1.1.0
Werkzeug==0.14.1
1 change: 1 addition & 0 deletions runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.5.2
Binary file added static/favicon.ico
Binary file not shown.
Loading

0 comments on commit b1c5d67

Please sign in to comment.