Skip to content

Instantly share code, notes, and snippets.

View leehanchung's full-sized avatar
👾

Han leehanchung

👾
View GitHub Profile
@leehanchung
leehanchung / push_docker_ecr.sh
Last active January 26, 2020 02:24
Shell script for pushing Docker images to ECR
#!/usr/bin/env bash
IMAGE_NAME={name your local Docker image here}
REGION=$(aws configure get region)
ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
# If the ECS repository doesn't exist, creates it.
aws ecr create-repository --repository-name ${IMAGE_NAME} > /dev/null 2>&1
@leehanchung
leehanchung / nginx.conf
Created December 20, 2019 23:56
NGINX configuration file for Tensorflow Serving Docker image on AWS Sagemaker
events {
# determines how many requests can simultaneously be served
worker_connections 2048;
}
http {
server {
# Increase JSON POST payload to bypass 413 Client Error: Request Entity Too Large for URL
client_max_body_size 100M;
@leehanchung
leehanchung / dockerfile
Created December 20, 2019 23:57
Dockerfile for using Tensorflow Serving Docker images on AWS Sagemaker
# Using the official tensorflow serving image from docker hub as base image
FROM tensorflow/serving
# Installing NGINX, used to rever proxy the predictions from SageMaker to TF Serving
RUN apt-get update && apt-get install -y --no-install-recommends nginx git
# Copy our model folder to the container
COPY <<local model directory>> /<<model directory inside Docker image>>
# Copy NGINX configuration to the container
@leehanchung
leehanchung / create_sagemaker_model.sh
Created December 21, 2019 00:02
Bash script for creating Sagemaker Model from AWS ECR Docker Image repository
#!/usr/bin/env bash
MODEL_NAME=<<name your model>>
ROLE_NAME=AmazonSageMaker-ExecutionRole-XXXXXXXXXXXXXXXX
# the name of the image created with
ECS_IMAGE_NAME=<<your image name>>
# the role arn of the role
EXECUTION_ROLE_ARN=$(aws iam get-role --role-name ${ROLE_NAME} | jq -r .Role.Arn)
@leehanchung
leehanchung / create_sagemaker_endpoint.sh
Created December 21, 2019 00:04
Bash script to create Sagemaker Inference Endpoint from Sagemaker Model
#!/usr/bin/env bash
MODEL_NAME=<<name your model>>
ENDPOINT_CONFIG_NAME=<<name your endpointn config>>
ENDPOINT_NAME=<<name your endpoint>>
# Using one ml.c4.large instance for the endpoint
PRODUCTION_VARIANTS="VariantName=Default,ModelName=${MODEL_NAME},"\
@leehanchung
leehanchung / test_sagemaker_cli.sh
Created December 21, 2019 00:06
Bash script for testing Sagemaker Endpoint with a bogus payload
#!/usr/bin/env bash
ENDPOINT_NAME=<<name your endpoint>>
aws sagemaker-runtime invoke-endpoint \
--endpoint-name ${ENDPOINT_NAME} \
--body '{"instances": [1.0,2.0,3.0]}' response.json
@leehanchung
leehanchung / tf_text_classification.py
Created June 12, 2020 06:47
Tensorflow Text Classification Tutorial
import tensorflow_datasets as tfds
import tensorflow as tf
import matplotlib.pyplot as plt
def plot_graphs(history, metric):
plt.plot(history.history[metric])
plt.plot(history.history['val_'+metric], '')
plt.xlabel("Epochs")
plt.ylabel(metric)
def get_benfords() -> List[float]:
"""get list of expected discrete benford distribution values to be used
for chi square test
Returns:
List[float]: discrete benford's law distribution
"""
benfords = [log10(1 + 1/d) for d in range(1, 10)]
return benfords
def preprocess(data: Dict[str, List[str]]) -> Tuple[Dict[str, List[int]], Dict[str, List[float]]]:
"""preprocess a dictionary of candidate name and votes and generate counts
of the leading digits and the expected values using Benford's Law
Args:
data (Dict[str, List[str]]): candidate: vote counts for all prescints
Returns:
Tuple[Dict[str, List[int]], Dict[str, List[float]]]: observed and
expected leading digit count for all candidates
def get_soup(url:str):
"""get html data from input url and load it into beautifulsoup parser
Args:
url (str): source url
Returns:
beautifulsoup parser with loaded data
"""
headers = {