DEV Community

Cover image for ๐Ÿง  From Notebook to Web App: Building a Handwritten Digit Recognizer with TensorFlow & Streamlit
PIYUSH KUMAR GHOSH
PIYUSH KUMAR GHOSH

Posted on

๐Ÿง  From Notebook to Web App: Building a Handwritten Digit Recognizer with TensorFlow & Streamlit

Ever wondered how machine learning models recognize handwritten digits? I recently took on a fun project where I trained a model using TensorFlow and deployed it as a live web app using Streamlit โ€” so anyone can draw a number and get a prediction in real-time.

In this post, Iโ€™ll walk you through the entire process, from building the model in a Jupyter notebook to turning it into an interactive app available online. No backend. No frontend. Just Python ๐Ÿ


๐Ÿงฐ Tools & Technologies

  • Python
  • TensorFlow/Keras โ€“ for training the CNN
  • Streamlit โ€“ to build the web app
  • streamlit-drawable-canvas โ€“ for drawing digits
  • Streamlit Cloud โ€“ for free hosting
  • GitHub โ€“ version control & deployment

๐Ÿ“Š Model Training (in a Notebook)

I used the classic MNIST dataset, which contains 70,000 grayscale images of handwritten digits (0โ€“9), each 28x28 pixels.

โœ… Model Summary


from tensorflow.keras import layers, models

model = models.Sequential([
    layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, (3,3), activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

๐Ÿ Results

  • Training Accuracy: ~99%
  • Test Accuracy: ~98%
  • Loss: Stable and low

After training, I saved the model with:


model.save("mnist_digit_model.keras")

๐ŸŽจ Building the Web App with Streamlit

The app lets users draw a digit on a canvas, processes the image, and displays the predicted number instantly.

Key steps:

  • Capture the canvas input
  • Resize it to 28x28
  • Preprocess: grayscale, invert, normalize
  • Predict using the trained model

I used the streamlit-drawable-canvas library to easily integrate a drawing board into the app.


๐Ÿš€ Deploying the App

I pushed all the code + model to GitHub, and used Streamlit Cloud to deploy.

โœ… Live App

๐Ÿ‘‰ https://digit-predictor-hqfeqrpxxs5vvkgefv7ltp.streamlit.app/

๐Ÿ“ GitHub Repo

๐Ÿ‘‰ https://github.com/Piyush-Kumar-Ghosh/digit-predictor


๐Ÿคฏ What I Learned

  • Saving/loading models: use .keras instead of .h5 to avoid compatibility issues
  • Handling canvas data and preprocessing for prediction
  • How to use Streamlit Cloud for quick ML deployment
  • Creating an intuitive UI for ML models matters!

๐Ÿงฉ Next Steps (Stretch Goals)

  • Add confidence scores or probability charts
  • Enable uploading images instead of drawing
  • Try other datasets like Fashion-MNIST
  • Convert it to a mobile app using tools like BeeWare or Kivy
  • Write a follow-up tutorial or make a YouTube demo

๐Ÿ’ฌ Final Thoughts

This project was a great intro to end-to-end ML workflows โ€” from training to deployment. If you're learning machine learning, I highly recommend turning your models into something people can interact with. Itโ€™s the most satisfying part!

Try it out and let me know what you think. And feel free to fork the repo to build your own version!


๐Ÿ“ข Letโ€™s Connect

If you enjoyed this, drop a like or comment!

Iโ€™d love to see what others are building with Streamlit + ML. ๐Ÿ˜Š

Top comments (0)