Deploying a Computer Vision Model as a Streamlit App on HuggingFace Spaces
You’ve put in countless hours cleaning and augmenting your data, spent days tracking training and validation losses, and now your model finally performs well enough to make you proud. You might think you’re just a few clicks away from sharing your achievement with clients, colleagues, or the entire internet. Unfortunately, there’s still a bit more work ahead. But don’t worry — deploying your model as an app doesn’t have to be hard. In this guide, I’ll show you how to quickly, easily, and for free deploy your ML-powered app so it’s accessible to everyone.
For this project, I used the FastAI vision learner to fine-tune a ResNet50 model, a popular deep learning architecture for image classification tasks. FastAI makes training deep learning models incredibly easy, requiring only a few lines of code. The fine-tuned model is then wrapped into a Streamlit app, an open-source framework that makes it easy to create and share beautiful, custom web apps for machine learning and data science.
I’ll walk you through deploying this app on Huggingface Spaces, a platform that allows you to host and share your machine learning models and apps for free.
By the end of this article, we will have built an ML-powered app that lets users upload a picture of a dog and identifies its breed from 120 breeds the model was trained on. The app will be accessible via a meaningful link on the public internet.
Preparing the Model
After we’ve fine-tuned our model using the FastAI vision learner, we’re ready to move on to the next step: exporting the model. We do this by calling the export() method on the learner object:
learn.export("model.pkl")
This method saves the entire learner, including architecture, weights, etc., to a file named model.pkl. Once your model is exported, you no longer need a GPU. If you’re using a cloud GPU like I am, you can download the file to your local machine and shut down the expensive server with GPUs to save costs. If you only want to build the app and skip the model fine-tuning part, feel free to use my export result.
Next, we need to publish the model on Huggingface. Assuming you already have a Huggingface account, the first step is to issue an authorization token. Be careful with this token — don’t share it with anyone and avoid publishing it anywhere. I prefer to use Python for publishing the model:
from fastai.vision.all import load_learner
from huggingface_hub import login, push_to_hub_fastai
# For simplicity's sake, I provide the token as a string in this example.
# However, I strongly recommend storing and loading it as an environment
# variable instead, like this: token=os.getenv("HUGGINGFACE_TOKEN").
login(token="<your-secret-token>")
learn = load_learner("model.pkl")
# "<username>/<model>" example: "TheDima/resnet50-dog-breed-identification"
push_to_hub_fastai(learner=learn, repo_id="<username>/<model>")
With the model now uploaded to Huggingface, we can shift our focus to developing the app. The next step involves creating a Streamlit app that will utilize our fine-tuned model to make predictions.
Building the Streamlit App
When building the Streamlit app, you can design the interface as elaborately as you like. However, for simplicity’s sake, I’ll provide a minimalist example. This example will use the Huggingface library to load the model, with the actual model file being served by the Huggingface platform. Before deploying the app to the cloud, it’s important to ensure everything works correctly by running the Streamlit app locally first. I recommend starting by creating a new Huggingface Space using the Streamlit app template and then cloning it to your local machine.
Clone the space locally with git clone and add the app code to app.py:
from copy import deepcopy
from huggingface_hub import from_pretrained_fastai
from PIL import Image
import streamlit as st
import pandas as pd
_LABELS = (
# omitted for simplicity
)
def get_breed(path):
# This is a workaround I had to use with my model since I provided a custom
# function during training. You likely won't need it if you trained your own
# model and were able to load it successfully without any similar hacks.
pass
# This decorated function prevents the app from becoming overwhelmed by
# reloading the model.
@st.cache_resource
def get_predictor():
return from_pretrained_fastai("TheDima/resnet50-dog-breed-identification")
def predict(image):
predictor = get_predictor()
pred, pred_idx, probs = predictor.predict(image)
return pred, probs[pred_idx].item()
st.title("Dog Breed Recognition")
uploaded_file = st.file_uploader("Upload a doggy...", type=["jpg", "jpeg"])
if uploaded_file is not None:
# original image will go to inference
image = Image.open(uploaded_file)
# its copy will be showen for the reference
image_copy = deepcopy(image)
# Make a prediction
with st.spinner("Checking..."):
pred, prob = predict(image)
centered_html = f"""
<div>
It is {pred.replace("_", " ").title()}!
(I am {100*prob:.1f}% sure)</h3> <br>
</div>
"""
st.markdown(centered_html, unsafe_allow_html=True)
# Display uploaded image
st.image(image_copy, caption="Uploaded doggy", use_column_width=True)
Deploying on Huggingface Spaces
Before pushing your changes to Huggingface with git push, make sure to test the app locally by running streamlit run app.py to ensure it’s working as expected. If you need any additional dependencies, you can specify them in the requirements.txt file. Huggingface Spaces builds and deploys a container, which means that all dependencies listed in the requirements.txt file will be installed during the container build process. This process is triggered automatically whenever you push changes. Once you’ve entered git push, head over to the Huggingface Spaces UI to see your app in the deployment process.
With everything in place, you’re all set! Your Streamlit app is now up and running, hosted on a free instance in Huggingface Spaces. You can share the link with everyone, making your machine learning model accessible to clients, colleagues, or anyone else on the internet.
You can find this app here: https://thedima-dog-breed-recognition.hf.space
Final Notes
While the approach described in this guide is a quick and easy way to deploy an ML app, it does have its limitations. It’s not a replacement for professional front-end engineering and may not offer the robustness or scalability required for production-grade applications. However, for rapid prototyping, personal projects, or sharing results with a small audience, this method works well.
It’s also important to note that this isn’t the only way to share your work with the public. There are numerous alternatives and platforms available, each with its own strengths and features. This approach is just one fast and easy method that I’ve found useful in my daily practice. Whether you’re testing a new idea, showcasing a project, or getting feedback, deploying a model with Streamlit and Huggingface Spaces can be a valuable tool in your toolkit.