Load the pre-trained model
Deploying your machine learning models into production as web apps is a crucial final step that makes them accessible and useful to end-users. Python offers several excellent frameworks for serving models, with Flask and Streamlit being two of the most popular. In this article, we‘ll compare Flask and Streamlit in depth and walk through how to deploy a model using Streamlit step-by-step.
What is Machine Learning Model Deployment?
Machine learning model deployment refers to putting models into production so they can be used to make predictions on new data in real-world applications. After a model has been trained and validated, it needs to be deployed in order to provide real business value and be utilized by others. Deployment makes the model available via a web app or API that can take in requests and serve results.
Some key benefits of deploying machine learning models include:
- Automating decision-making processes
- Enabling applications powered by intelligent models
- Allowing other developers to integrate the model into their apps
- Making model results conveniently available to non-technical end users
Model deployment bridges the gap between data science development and practical usage. However, it requires machine learning engineers to have web development skills in addition to their modeling chops. Luckily, Python web frameworks like Flask and Streamlit aim to simplify the process.
Overview of Python Web Frameworks for Model Deployment
A web framework provides a structured way to build web applications and APIs quickly, without needing to code everything from scratch. Using an established framework saves a ton of time and enables best practices by default. In Python, Flask and Streamlit are two major frameworks used for deploying models:
Flask
Flask is a lightweight, minimalist web framework that‘s highly customizable. Released in 2010, it‘s a veteran framework that‘s been battle-tested. Flask doesn‘t make many decisions for you, which makes it very flexible but also means more setup code and boilerplate. Flask uses a templating engine, Jinja2, to generate dynamic HTML pages. It integrates well with other libraries for tasks like database management and authentication.
Streamlit
Streamlit is a newer framework, launched in 2019, that‘s specifically designed for data science and machine learning applications. Its goal is to enable data scientists to create web apps with pure Python scripts, with minimal web dev knowledge required. Streamlit has many built-in widgets and components for visualizing data and collecting user input. You don‘t need to write HTML or JS. While not as fully featured or customizable as Flask, Streamlit excels at letting you build model-powered apps quickly.
Comparing Flask and Streamlit for Model Deployment
Let‘s compare Flask and Streamlit on several key dimensions important for model deployment:
Ease of Setup
Both Flask and Streamlit are easy to install with pip. However, Streamlit requires significantly less configuration and boilerplate code to get an app up and running. With Flask, you need to create HTML templates, define routes, and instantiate the Flask app object. Streamlit lets you start coding your app functionality immediately in a single Python script.
Simplicity of Code
Streamlit has a large advantage over Flask in terms of concise, readable code. Since Streamlit is designed for data scientists, it has a simple, intuitive API for creating UI elements like sliders, text boxes, file uploaders, and charts. Building the same interface in Flask would require much more code split across HTML templates and Flask view functions handling requests.
As an example, displaying a dataframe in Streamlit is as easy as:
import streamlit as st
st.dataframe(my_dataframe)
Achieving this in Flask would require sending the dataframe to an HTML template and rendering it there with a library like Pandas.
Customizability
While Streamlit is simpler to use, Flask offers far more customization options. Flask is a blank canvas where you can structure your application however you like. You have full control over your URLs and routing logic, and can use any database, caching, or other libraries you prefer.
Streamlit is more opinionated and limited in customizability. It automatically creates URLs and doesn‘t easily support multi-page apps. You‘re constrained to the UI elements provided by Streamlit components. For many straightforward model-serving applications this is sufficient, but more complex apps may require Flask‘s flexibility.
Performance and Scalability
Flask is a lightweight framework, but still has some overhead in templating and request/response cycle. Streamlit takes a different approach by re-running your entire script on every user interaction and redrawing the UI with any changes. This means each user session is stateful and isolated.
For serving machine learning models, performance is mainly a function of the model inference speed rather than the web framework. Both Flask and Streamlit can handle moderate traffic loads if you use a WSGI server like Gunicorn and set up multiple worker processes. Streamlit‘s stateful architecture makes it less scalable than Flask for high-traffic applications.
Resources and Community
As a mature, popular web framework, Flask has a large ecosystem of extensions and a wealth of tutorials and resources available online. Its official documentation is comprehensive. There are also many books and courses that can help you get up to speed with Flask development.
While newer, Streamlit also has quite good documentation with many examples. It has an active community of data scientists contributing components and examples. However, the depth of resources and size of community is not yet on par with Flask. Since it‘s a younger framework, there are also occasional bugs and a smaller set of third-party extensions.
Deploying a Machine Learning Model with Streamlit
Now that we‘ve compared Flask and Streamlit, let‘s walk through a real example of deploying a machine learning model using Streamlit. We‘ll serve a pre-trained dog breed classification model.
1. Install Streamlit and Create a New Python File
First, make sure you have Python 3.6+ and install Streamlit using pip:
pip install streamlit
Then create a new Python file for your app, e.g. app.py
2. Load the Model and Prepare Prediction Function
At the top of app.py, import the required libraries and load your dog breed classification model and label encoders:
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
import pickle
model = load_model(‘dog_breed_model.h5‘)
with open(‘dog_breed_classes.pkl‘, ‘rb‘) as handle:
class_names = pickle.load(handle)
def preprocess(img):
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = img/255
return img
def predict(img):
img = preprocess(img)
pred = model.predict(img)
pred_class = class_names[np.argmax(pred)]
pred_conf = np.max(pred) * 100
return pred_class, pred_conf
We define functions to preprocess the image and make a prediction using the loaded model.
3. Create the Streamlit App UI and Workflow
Next, let‘s use Streamlit elements to create our app‘s UI:
st.set_page_config(page_title="Dog Breed Classifier")
st.title("Dog Breed Classification Model")
st.sidebar.subheader("Input Image")
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg","png"])
if uploaded_file is not None:
st.sidebar.image(uploaded_file, caption=‘Uploaded Image‘, use_column_width=True)
# Convert to PIL image for model input
img = image.load_img(uploaded_file, target_size=(224,224))
# Make prediction when button is clicked
if st.sidebar.button(‘Predict Dog Breed‘):
pred_class, pred_conf = predict(img)
# Display prediction results
st.subheader("Prediction Results:")
st.write(f"Predicted Dog Breed: {pred_class}")
st.write(f"Confidence: {pred_conf:.2f}%")
We add a file uploader that accepts JPG/PNG images. When an image is uploaded, we display it and a Predict button. Upon clicking Predict, we pass the image through our prediction function and display the most probable dog breed and its confidence percentage.
4. Run the Streamlit App
To launch the app, run this command in your terminal:
streamlit run app.py
The Streamlit app will open in a new browser tab. You can now upload images and get real-time predictions from your deployed model!
Best Practices for Machine Learning Model Deployment
Here are some tips and best practices to keep in mind when deploying models with Streamlit or Flask:
- Ensure your app is secure by validating user inputs. Make sure to prevent malicious files or overly large inputs from being uploaded.
- Avoid model files and assets in your Git repo, especially if it‘s public. Use .gitignore to exclude model files and load them separately in your app.
- If you need to handle high traffic, look into a scalable, managed deployment solution like AWS SageMaker or Google Cloud AI Platform.
- Caching model results can significantly speed up inference. Store results by input hash for quick lookup if the same input is sent again. Flask-Caching and Streamlit‘s @st.cache make this easy.
- Optimize your models as much as possible for inference speed, as this is often the performance bottleneck. Quantization, pruning, and using lighter architectures help.
- Gather predictions and input data (with permission) to continually monitor model performance and collect data for future re-training.
- Document your app well, including how to install dependencies and how it should be used. Create a nice demo page with sample inputs and expected outputs.
Conclusion
We‘ve covered the Machine Learning model deployment process and compared two top Python frameworks for the job – Flask and Streamlit.
To summarize:
- Flask is a flexible, customizable web framework that requires more up-front development work
- Streamlit is designed for quick, easy model deployments using simple Python scripts
- Flask is better for deploying complex applications that may involve databases and other components
- Streamlit is ideal for standard model serving applications that can be a single page
For most straightforward ML model deployments, Streamlit is a great choice that will have you up and running in no time. Its simple API and awesome built-in components allow data scientists to create interactive ML web apps without being web development experts.
However, Flask is the way to go for more complex, larger-scale projects that need customization and extensibility. Its lightweight architecture, robust feature set, and integration with other libraries provide a solid foundation.
Whichever framework you choose, I hope this guide helps you get your models out of your notebooks and into production. Happy deploying!