text
stringlengths 0
1.36k
|
---|
## Demo of the "MNIST mind-mapper" app in action
|
You can watch this short demo to see how the app works:
|
## Overview
|
I've been an application and infrastructure developer for the majority of my career, so I wanted to get hands-on with training and deploying a neural network.
|
I also wanted to wrap that trained neural network in a REST API, so that I could build a frontend that would allow folks to play with it, because interacting with something is more engaging than reading a text description of it.
|
I knew it would be important to go beyond the working neural net, because issues often arise at the seams, when you're fitting system components together.
|
This article details the steps I took and the many issues I encountered along the way to building and successfully deploying my original vision.
|
## Open source code
|
I open source most of my work, and this project is no exception:
|
## App flow diagram
|
Let's step through how the app works, end to end:
|
The frontend exposes a small drawable canvas, which the user scribbles on.
|
On a regular interval, the frontend captures what the user drew, using the `toDataURL` method:
|
This image is sent to the backend API, which wraps the trained neural network. The backend runs inference on the image, and returns the predicted digit, which the frontend displays.
|
## Step 1: Model training
|
The following code is all that's needed with pytorch to:
|
1. Define a simple neural network architecture 2. Load the MNIST dataset 3. Train the neural network 4. Save the model to disk so that it can be loaded and reused later
|
With the training complete, I wanted to quickly sanity check the trained network's performance on the intended task, so I created a simple FAST API server that exposes a `/test_images` route:
|
You can view the entire `main.py` file in [the repository](https://github.com/zackproser/pytorch-mnist/blob/main/main.py), but we'll examine the route itself here, which runs a self-test and shares the output as a PNG image like so:
|
Now I had a trained and working neural network and I was ready to deploy it. Here's where all the fun began.
|
## First challenge: Vercel's Python support is immature
|
Vercel's beta support for the Python runtime for use in backend functions is very exciting. I believe the ability to deploy a mixed app with a Next.js frontend and a Python backend has huge potential.
|
The Python ecosystem is rich with popular machine learning libraries, utilities and datasets. Meanwhile, JavaScript provides an excellent way to provide charts, graphs, data visualizations, games and other interactive representations of complex data models.
|
I'm personally very excited to build a ton of example applications with Python backends and TypeScript frontends on Vercel. But we're not quite there yet.
|
Unfortunately, the docs Vercel has for using their Python runtime are very sparse, the examples are light and most of the new concepts are not sufficiently explained. You have to read through the Python Vercel templates to understand how everything fits together.
|
Errors are also opaque. The showstopper for getting my Python backend deployed successfully on Vercel was an unintuitive error message: `data too long`.
|
I was pretty sure that pytorch and torchvision were likely blowing out the 4.5MB size limit on serverless functions, but there wasn't a great way to confirm this. My model/weights file was just under 400KB.
|
Things that you'd expect to be extensively documented, such as how to get around common bundling issues, or how to deploy a Python backend with PyTorch and a custom model, are also sorely needed.
|
You find extremely important details like this hiding out in 4 year old GitHub issue comment threads:
|
## Converting to ONNX and trying again
|
ONNX, (which stands for Open Neural Network Exchange) is a fascinating project that defines a common language for machine learning models, to allow Cloud providers and developers to more easily write and deploy models.
|
You can convert a PyTorch model to ONNX using the `torch.onnx.export` function. ONNX CAN sometimes reduce the size of the exported model, so I decided to give it a shot.
|
I also was happy to take any excuse to play around with ONNX after reading about it. After converting the model to the ONNX format, I tested everything again locally to ensure the app still functioned as intended. The potential size savings from ONNX did not end up making a difference in my case, and I got the same error.
|
## Finding a Python backend host in Modal.com
|
In researching platforms that host your Python code as cloud functions, I found modal.com which was lightweight and fast to set up.
|
I ended up converting the local version of my Fast API Python backend to a file I named `modal_function.py` which I then deployed like so:
|
`modal deploy modal_function.py`
|
Of course, my prefence would be for the backend to be hosted on Vercel, so that I could colocate the frontend and backend code and have a simpler deployment model, but modal.com ended up being exactly what I wanted in the absence of that.
|
Modal.com lets you sign up quickly, install their CLI and deploy your Python code as a serverless function or a deployed REST API, or an ephemeral testing endpoint. So far, it's been great.
|
There is the issue of cold starts, because Modal will spin down your container when your service is not receiving traffic, but overall I've been impressed with the developer experience.
|
[View and clone the project here](https://github.com/zackproser/teatutor)
|
In this post, I’ll do a deep-dive on my recently published open-source app, Tea Tutor. This application allows you to quickly define and serve colorful and interactive quizzes on any topic over SSH, and it was written using Charm’s Bubbletea and Wish libraries.
|
## What is Bubbletea and why should I care?
|
[Charm](https://charm.sh) has built a truly impressive suite of terminal user interface (TUI) libraries and tools. For Golang developers seeking to build rich applications that approach the interactivity of HTML in the terminal, solutions like Bubbletea, complimented by Glamour and Lipgloss, are some of the best options today.
|
## Introducing the Bubbletea blog post series
|
This article is the first in a series about building interactive terminal applications with Bubbletea. The next posts in the series are:
|
[Introducing the Tea Tutor program and my Bubbletea blog post series](https://medium.com/@zackproser/introducing
|
tea
|
tutor
|
a
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.