Post

Streamlit and Model Serialization in Python

An introductory guide to building interactive web apps with Streamlit and deploying machine learning models through serialization using Pickle.

Streamlit and Model Serialization in Python

🌐 Streamlit and Model Serialization in Python

🚀 Introduction to Streamlit

Streamlit is an open-source app framework in Python used to create beautiful, interactive web applications for machine learning and data science projects.

It enables you to design web interfaces quickly using pure Python code, without needing knowledge of web technologies like HTML, CSS, or JavaScript.


🧩 Terminologies

TermDefinition
Pickle FileA file format used to serialize and save machine learning models for later use.
StreamlitA Python library used to create web applications for data science without needing HTML or JavaScript.
Linear RegressionA machine learning algorithm used to model the relationship between a scalar dependent and one or more explanatory variables.
Model DeploymentThe process of integrating a machine learning model into a production environment to make predictions.
FlaskA micro web framework for Python, used for building web applications.
Yahoo Finance APIA tool used to access stock market data programmatically.
WidgetsInteractive elements like buttons and sliders used in Streamlit applications.
Encoding Categorical FeaturesThe process of converting categorical variables into numerical values for model training.
Random ForestAn ensemble learning method for classification and regression that constructs multiple decision trees.
PrototypeAn initial model of a product used for testing and validating concepts.
Production EnvironmentA live environment where actual users interact with the deployed version of applications.
Feature EngineeringThe practice of using domain knowledge to select, modify, or create new features from raw data.

🧩 Basic Components of Streamlit

🎛️ Widgets

Interactive UI components such as sliders, checkboxes, buttons, and more.
They enable dynamic user inputs that can modify application behavior in real-time.

🧱 Layout Elements

Text structure and hierarchy are created using:

1
2
3
st.title("App Title")
st.header("Section Header")
st.subheader("Subsection Header")

These help organize your app layout visually and semantically.


⚙️ Getting Started with Streamlit

🪄 Installation

To install Streamlit, run:

1
pip install streamlit

✅ Verify Installation

Test Streamlit with its built-in demo:

1
streamlit hello

This launches a local server and opens a new browser tab showcasing interactive examples.


▶️ Running a Streamlit App

  1. Write your Python app (for example, app.py).
  2. Run your app with:

    1
    
    streamlit run app.py
    

Streamlit automatically detects changes and reruns the script from top to bottom each time an interaction occurs, providing real-time feedback during development.


🧠 Development and Data Flow

  • Streamlit apps execute reactively — every user action (like adjusting a slider) triggers a script rerun.
  • This ensures updated output instantly without needing manual page refreshes.

Streamlit integrates seamlessly with Python data libraries like:

  • pandas for data manipulation
  • matplotlib and plotly for visualization
  • scikit-learn for model predictions

Example:

1
2
3
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt

🧮 Building Interactive Applications

Streamlit allows you to add interactive widgets for dynamic input:

1
2
x = st.slider("Select a value for X:", 0, 100, 50)
st.write("You selected:", x)

You can combine widgets with visualizations or model predictions to make interactive ML dashboards.


🧠 Model Serialization with Pickle

💡 Concept

Model serialization (or pickling) means saving a trained machine learning model to a file so it can be reused later — without retraining.

This is commonly done with Python’s built-in pickle module.


🧱 Steps in Model Serialization

1. Creating a Model

Train your model (e.g., using a regression algorithm). Once trained, it contains learned parameters such as weights and coefficients.

2. Serializing or “Pickling” the Model

Save the trained model to a binary file:

1
2
3
4
import pickle

with open('model.pkl', 'wb') as file:
    pickle.dump(model, file)

3. Deserializing or “Unpickling” the Model

Load the model later to reuse it:

1
2
with open('model.pkl', 'rb') as file:
    loaded_model = pickle.load(file)

Now loaded_model can make predictions without retraining.


🧩 Model Deployment Workflow

Once your model is serialized, it can be integrated into a Streamlit application for deployment.

Example Integration:

1
2
3
4
5
6
7
8
9
10
11
12
13
import streamlit as st
import pickle

# Load serialized model
with open("model.pkl", "rb") as file:
    model = pickle.load(file)

st.title("House Price Prediction App")

sqft = st.number_input("Enter area (in sq. ft):", min_value=500, max_value=5000, step=100)
prediction = model.predict([[sqft]])

st.write("Predicted Price:", prediction[0])

This simple Streamlit app loads a saved model, accepts user input, and displays the prediction interactively.


🧰 Practical Applications

🖥️ Streamlit as a Front-End

In production (e.g., predicting car or house prices), Streamlit acts as a front-end interface that:

  • Accepts user inputs
  • Sends them to a deserialized model
  • Displays predictions instantly

⚡ Use Cases

  • Rapid prototyping of ML solutions
  • Sharing models with non-programmers via interactive web UIs
  • Demonstrating ML outputs in real time without complex deployment stacks

🧾 Summary

With Streamlit and Pickle, you can:

  • Quickly convert data science scripts into web apps.
  • Save and reuse trained models efficiently.
  • Build, deploy, and share interactive ML solutions — all using Python.

💡 Tip: Pair Streamlit with tools like scikit-learn, matplotlib, and pandas to create robust end-to-end applications.


📚 References

  1. Streamlit Official Documentation
  2. Streamlit GitHub Repository
  3. Pickle Module — Python Docs
  4. Scikit-learn Model Persistence
  5. Streamlit Cheat Sheet (GitHub)
  6. Streamlit App Cheat Sheet
This post is licensed under CC BY 4.0 by the author.