I’m Blake. I write about probability, stochastic processes, Python tooling, and the practical side of building things with code. More about me →

  • TIL How to test a Flask server locally with CURL

    This might be a bit of a basic one, but I was testing a codebase that implemented an ML model behind an endpoint with Flask at /api/redact, which accepted a json input and returned a json output.

    I hosted the Flask app locally on port 5001 and used this command to invoke a response

    curl -X POST http://localhost:5001/api/redact -H "Content-Type: application/json" -d '{"text": "John Doe lives at 123 Baker Street. You can contact him at john.doe@example.com or 555-123-4567."}'
    # {
    #   "redacted_text": "[NAME] lives at [ADDRESS]. You can contact him at [EMAIL] or [ADDRESS]."
    # }
  • Metacognition and AI usage

    I was recently pointed in the direction of this paper:

    Beware of Metacognitive Laziness: Effects of Generative Artificial Intelligence on Learning Motivation, Processes, and Performance

    Having looked through the paper, they seem to really only make a claim about essay writing with regards to a relatively small sample, it’d be cool to see if larger sample sizes exhibit the same differences

    I think there’s more to evaluate than just essay writing as well. What convinced me about ChatGPT was when I stated chatting with it, bouncing ideas off in order to discover a new field I’m inexperienced in – it gave me a starting point, essentially taking the place of hours of googling

    Speaking of which, the laziness they refer to warrants comparison with other technological leaps such as Google, or even GPS! I’d wager the average persons map reading skills has declined measurably in the last 25 years, but for most people, using a GPS is worth that trade-off. When it’s used well, people can use GPS to navigate totally foreign places with ease – which is worth the trade-off in that case

    Personally, i don’t think ChatGPT is that different. It lowers the activation energy required to learn a new concept, but it may also decrease the depth of knowledge attained. For 80% of life this is a worthwhile trade-off to get breadth over depth, but maintaining good learning skills is essential for that last 20%

    What I’m more concerned about is how these models are served by massive companies and how they hold all the cards. Improvements in local models will address that partially, but this goes into my concerns with cloud computing in general.

    We can only improve in the direction in which we stress ourselves. Take away the stress, improvements will decelerate. It up to the individual to make the best of the time they have

  • How to un-commit files in git

    There’s a few situations that can arise when you’d want to reverse committing a file to your repo:

    • Accidentally committing secrets
    • Accidentally committing data or artifacts and running into a Git LFS warning when pushing
    • Accidentally committing a Jupyter notebook with absurdly large outputs which result in slow diffs

    However you’ve landed in this position, it’s luckily not too hard to get out of! But it requires going a bit beyond the basics of Git and understanding a bit more about how Git works.

    Git’s tracking architecture optimizes for tracking changes between snapshots. When I learned about Git initially, I incorrectly assumed it was just like Dropbox or other versioned file storage systems, and it took a while to un-learn this.

    Each commit is a snapshot of the repository’s state (referenced by a random SHA such as d85e8fe...), and Git internally optimizes storage by storing differences (deltas) between these states (represented as the added/removed lines across each file in the codebase). These diffs can be thought of as patches which can be applied to move from one commit to another, enabling rapid traversal of codebase versions and branches by following the tree of commits!

    I made changes to my codebase, and I just want to go back to the HEAD commit

    Save your uncommitted changes with git stash, so that you can apply them later with git stash pop. This reverts the codebase to the last commit, and most people usually learn this early on their Git journey.

    But if I’ve already git add-ed some changes (staged my changes), this command will unstage all changes without deleting anything in my codebase:

    git reset HEAD

    But if you’d prefer to delete changes made beyond the last commit:

    git reset --hard HEAD

    I committed something I shouldn’t have a few commits ago, help!

    Lets assume we’re working with a codebase with an output of git log --oneline -n 5:

    d85e8fe (HEAD -> main) add even more code
    14bfedd edit file I shouldn't have added
    f18083a add code
    b425f55 add file I shouldn't have added
    974d26b (origin/main, origin/HEAD) add some code

    I’ve made 5 commits before pushing them to Github, two of them (b425f55 and 14bfedd) touch a file I shouldn’t have added. I can list the commits that touch the file I want to remove using

    git log --oneline --follow -- <file_path>

    Which gives me the output

    14bfedd edit file I shouldn't have added
    b425f55 add file I shouldn't have added

    Lets edit the commits starting from the earliest commit in that result:

    git rebase -i b425f55^

    This starts an interactive rebase (starting from the parent of the first bad commit), which opens up a text editor to the following:

    pick d85e8fe add even more code
    pick 14bfedd edit file I shouldn't have added
    pick f18083a add code
    pick b425f55 add file I shouldn't have added
    # Rebase 974d26b..d85e8fe onto 974d26b (4 commands)
    #
    # Commands:
    # ...

    We’ll change pick to edit for the commits we’ve selected, then save and quit.

    Our first stop is the first bad commit b425f55. At this stop we’ll

    1. Remove the bad file (and make the edits we need to this snapshot)
    2. Update the commit message with git commit --amend
    3. Continue the rebase with git rebase --continue

    Our next stop will be the next bad commit 14bfedd. We’ll repeat the steps above

    There may be some conflicts that need to be resolved. This will take a bit of time, but it’ll work out if you look through them slowly and methodically.

    After the rebase is done, all the commits are back in order, and we can safely git push out code now! (If we’ve already pushed commits, we’ll need to do a git push --force to edit the history on Github, which is a destructive action).

    Prevention is the best cure

    The awkwardness of Git is a great incentive to do things the right way. Operations with Git that go outside the developers knowledge can be hazardous in some cases. I think it’s worth learning a little bit about what’s happening under the hood, but avoiding these situations is also a smart thing to do. There are a few pre-commit hooks that might be worth checking out:

    • Strip notebook output before committing: nbstripout
    • Detect secrets and prevent commits: detect-secrets
    • Commit large files with Git LFS
    • Above all else, make sure you prevent git from tracking specific files and dirs with a good .gitignore file ;)
  • TIL How to build and host a container on GCP VM instance

    I had a project that required updating a legacy web app (built with dash) hosted on GCP Cloud Run. We needed to update replace the authentication layer by placing the app behind a VPN, which requires a static external IP address.

    Unfortunately, static IP addresses aren’t simply assigned to Cloud Run deployments – a more involved setup using a load balancer is required for this. Since this was a relatively small app that isn’t expected to have much traffic, I elected to try take a shorter route to satisfy these requirements: host the image on a small Compute VM instance with a static IP.

    There are a few variables I define that I’ll reference throughout the post:

    PROJECT_ID=my-gcp-project
    REGION=us-central1
    ZONE=us-central1-a
    REPOSITORY_NAME=my-legacy-web-app
    CONTAINER_NAME=my-container
    
    INSTANCE_NAME=my-web-app-instance
    MACHINE_TYPE=e2-small
    ADDRESS_NAME=my-address
    
    REPO=${REGION}-docker.pkg.dev
    COMMIT_SHA=$(git rev-parse --short HEAD)
    
    CONTAINER_REPO=${REPO}/${PROJECT_ID}/${REPOSITORY_NAME}
    CONTAINER_NAME_REMOTE=${CONTAINER_REPO}/${CONTAINER_NAME}

    Firstly we need to build the docker image. To avoid platform issues with building things locally, I used gcloud builds submit to build and store an image in the Artifacts storage:

    gcloud builds submit \
        --tag $(CONTAINER_NAME_REMOTE):$(COMMIT_SHA)
    gcloud container images add-tag \
        $(CONTAINER_NAME_REMOTE):$(COMMIT_SHA) \
        $(CONTAINER_NAME_REMOTE):latest \
        --quiet

    This kicks off a job that builds and pushes the Docker image from my local code repository to a private repo on in my GCP project. It will also assign the latest tag to the built image.

    Among the public machine images you can use on VMs, there’s “container optimised systems” (COS), which come with minimal dependencies and come equipped with docker – seems like a perfect tool for the job!

    Creating the static IP and the instance:

    gcloud compute addresses create ${ADDRESS_NAME} --region=${REGION}
    gcloud compute instances create ${INSTANCE_NAME} \
        --project=${PROJECT_ID} \
        --zone=${ZONE} \
        --address=${ADDRESS_NAME} \
        --machine-type=${MACHINE_TYPE} \
        --image-project=cos-cloud \
        --image-family=cos-117-lts

    So it should be as simple as ssh-ing to the instance, pulling and running the image right?

    Unfortunately not. Attempting to do this will result in a “permission denied” error, despite the compute service account having all the correct permissions for pulling from artifact storage… So what gives?

    In a nutshell, the docker installation on these images come equipped with credential helpers. If you have a look in the ~/.docker/config.json on the instance:

    {
      "auths": {},
      "credHelpers": {
        "asia.gcr.io": "gcr",
        "eu.gcr.io": "gcr",
        "gcr.io": "gcr",
        "marketplace.gcr.io": "gcr",
        "us.gcr.io": "gcr"
      }
    }

    All we need is to add our private artifact repo to this list of cred helpers:

    cat > ~/.docker/config.json << 'EOF'
    {
            "auths": {},
            "credHelpers": {
                    "asia.gcr.io": "gcr",
                    "eu.gcr.io": "gcr",
                    "gcr.io": "gcr",
                    "marketplace.gcr.io": "gcr",
                    "us.gcr.io": "gcr",
                    "$REPO": "gcr"
            }
    }
    EOF

    I’d like to put this into a startup script, so that I can deploy the latest docker image, or a particular commit. To do this, we can add environment variables to the instance creation, and read them in a provided startup script with

    TAG=abcd1234
    gcloud compute instances create ${INSTANCE_NAME} \
        --project=${PROJECT_ID} \
        --zone=${ZONE} \
        --address=${ADDRESS_NAME} \
        --machine-type=${MACHINE_TYPE} \
        --image-project=cos-cloud \
        --image-family=cos-117-lts \
        --metadata TAG=${TAG:-latest},REPO=${REPO} \
        --metadata-from-file startup-script=./startup.sh

    Using the startup script:

    #!/bin/bash
    
    TAG=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/TAG" -H "Metadata-Flavor: Google")
    echo "TAG=${TAG}"
    export TAG
    
    REPO=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/TAG" -H "Metadata-Flavor: Google")
    echo "REPO=${REPO}"
    export REPO
    
    # Define the writable location for the Docker configuration
    DOCKER_CONFIG_DIR="/home/$(whoami)/.docker"
    
    # Create the Docker configuration directory
    mkdir -p "$DOCKER_CONFIG_DIR"
    
    # Write the config.json file
    cat > "$DOCKER_CONFIG_DIR/config.json" << 'EOF'
    {
            "auths": {},
            "credHelpers": {
                    "asia.gcr.io": "gcr",
                    "eu.gcr.io": "gcr",
                    "gcr.io": "gcr",
                    "marketplace.gcr.io": "gcr",
                    "us.gcr.io": "gcr",
                    "$REPO": "gcr"
            }
    }
    EOF
    
    # Ensure proper permissions for the Docker configuration
    chmod 600 "$DOCKER_CONFIG_DIR/config.json"
    
    # Set the environment variable to let Docker know about the custom configuration
    export DOCKER_CONFIG="$DOCKER_CONFIG_DIR"
    echo "export DOCKER_CONFIG=\"${DOCKER_CONFIG_DIR}\"" >> /etc/profile
    
    # Start Docker service (if not already running)
    systemctl start docker
    
    # Start image
    docker run -d -p 8080:8080 "${REPO}/my-gcp-project/my-legacy-web-app/my-container:${TAG}"

    Putting this altogether in a Makefile:

    SHELL := /usr/bin/env bash
    .SHELLFLAGS := -eu -o pipefail -c
    
    CONTAINER_NAME = my-container
    
    PROJECT_ID := my-gcp-project
    REGION := us-central1
    ZONE := us-central1-a
    REPOSITORY_NAME := my-legacy-web-app
    
    REPO := $(REGION)-docker.pkg.dev
    COMMIT_SHA := $(shell git rev-parse --short HEAD)
    INSTANCE_NAME = "my-instance"
    MACHINE_TYPE := "e2-medium"
    STARTUP_SCRIPT := ./startup.sh
    ADDRESS_NAME := my-address
    
    CONTAINER_REPO := $(REPO)/$(PROJECT_ID)/$(REPOSITORY_NAME)
    CONTAINER_NAME_REMOTE := $(CONTAINER_REPO)/$(CONTAINER_NAME)
    
    .PHONY: create-address list-tags build-remote-container deploy clean-instance
    
    create-address:
        @if ! gcloud compute addresses describe $(ADDRESS_NAME) --region=$(REGION) > /dev/null 2>&1; then \
            echo "Creating static IP address: $(ADDRESS_NAME)"; \
            gcloud compute addresses create $(ADDRESS_NAME) --region=$(REGION); \
        else \
            echo "Static IP address $(ADDRESS_NAME) already exists."; \
        fi
    
    build-remote-container: _check-clean
        @gcloud builds submit \
            --tag $(CONTAINER_NAME_REMOTE):$(COMMIT_SHA)
        @gcloud container images add-tag \
            $(CONTAINER_NAME_REMOTE):$(COMMIT_SHA) \
            $(CONTAINER_NAME_REMOTE):latest \
            --quiet
    
    clean-instance:
        @if gcloud compute instances list --filter="name=$(INSTANCE_NAME)" --format="value(name)" | grep -q $(INSTANCE_NAME); then \
            echo "Instance '$(INSTANCE_NAME)' exists. Deleting..."; \
            gcloud compute instances delete $(INSTANCE_NAME) --zone=$(ZONE) --quiet; \
        else \
            echo "Instance '$(INSTANCE_NAME)' does not exist."; \
        fi
    
    list-tags:
        @gcloud container images list-tags $(CONTAINER_NAME_REMOTE) \
            --filter 'tags:*'\
            --format="table(timestamp, digest, tags)"
    
    tag ?= latest
    # Deploy custom tag with `make deploy tag=<TAG>`
    deploy: _check-image-exists clean-instance
        @echo "Creating instance '$(INSTANCE_NAME)' from tag '$(tag)'"; \
        gcloud compute instances create $(INSTANCE_NAME) \
            --project=$(PROJECT_ID) \
            --zone=$(ZONE) \
            --address=$(ADDRESS_NAME) \
            --machine-type=$(MACHINE_TYPE) \
            --image-project=cos-cloud \
            --image-family=cos-117-lts \
            --metadata TAG=$(tag),REPO=$(REPO) \
            --metadata-from-file startup-script=$(STARTUP_SCRIPT)
    
    
    .PHONY: _check-image-exists _check-clean
    
    _check-image-exists:
        @if gcloud container images list-tags $(CONTAINER_NAME_REMOTE) --filter="tags=$(tag)" --format="value(tags)" | grep -wq "$(tag)$$"; then \
            echo "Image with tag '$(tag)' exists in the repository."; \
        else \
            echo "Image with tag '$(tag)' does NOT exist."; \
            exit 1; \
        fi
    
    
    _check-clean:
        @git diff --quiet || { echo "Error: Uncommitted changes found. Commit or stash changes before proceeding."; exit 1; }