Turn Python Models into Interactive Web Apps with STREAMLIT

lakshya ruhela
5 min readJun 12, 2024

--

You’ve built a solid foundation in data science! Now, let’s turn your machine learning models into real-world applications. With your understanding of deep learning and its implementation, it’s time to create interactive web apps. These web apps will act as user-friendly interfaces for your models, allowing people to easily interact with them. Imagine a web page where users can input data, and your model instantly generates a prediction or recommendation. This makes your powerful models accessible to anyone, not just data scientists. By building these web apps, you unlock the true potential of your creations and empower people to benefit from your machine learning expertise.

USING STREAMLIT TO CREATE INTERACTIVE WEBPAGES

Streamlit is a fantastic tool for creating interactive web pages that showcase your machine learning models. We can leverage it in the following ways :

  • Simple Deployment: Streamlit excels in simplifying deployment. Unlike traditional web development, you write Python code with Streamlit functions to define your web app’s layout and functionality. This allows you to focus on your model and user interaction without getting bogged down in complex web frameworks.
  • Built-in Widgets: Streamlit offers a rich set of widgets like sliders, dropdowns, text inputs, and buttons. These widgets become interactive elements within your web page, allowing users to provide input to your machine learning model. Imagine a slider for adjusting parameters and seeing the model’s output update in real-time.
  • Data Visualization: Streamlit integrates seamlessly with popular data science libraries like pandas and matplotlib. This lets you easily visualize your model’s predictions or insights gained from the data. You can create charts, graphs, and other visual aids to enhance user understanding and engagement.
  • Rapid Prototyping: Streamlit’s hot reloading feature is a game-changer. As you make changes to your Python code, Streamlit automatically refreshes the web app in your browser. This allows for rapid iteration and testing, making it ideal for prototyping and refining your interactive web page before deployment.

HOW I CREATED A WEBPAGE TO DEPLOY MY MACHINE LEARNING MODEL ?

The following is a flowchart which provides an overview how i went on to create my own webapp using streamlit

Start

1. Data Acquisition — Choose a flower classification dataset (e.g., Iris flower dataset) — Load the data into your Python environment

2. Data Preprocessing — Clean and prepare the data (handle missing values, outliers, etc.) — Feature engineering (if necessary) — Split data into training and testing sets

3. Model Selection and Training — Choose a machine learning model (e.g., Random Forest Classifier) — Train the model on the training data — Evaluate model performance on the testing data

4. Model Refinement (Optional) — If performance is not satisfactory, try: — Hyperparameter tuning — Trying different machine learning models

NOTE: If model performance meets your needs -> No: Go back to Step 3

5. Model Serialization (Optional) — Save the trained model for future use (e.g., pickle format)

6. Streamlit App Development — Create a new Python script for your Streamlit app — Design the user interface with Streamlit functions (widgets for flower features) — Load the trained model (if serialized) — Write functions to take user input, make predictions, and display results

7. Deployment — Choose a deployment method for your Streamlit app (e.g., local server, cloud platform) — Run your Streamlit app to make it accessible.

The models generated in this process are illustrated in the figure below

The subsequent illustration details the Streamlit deployment process.

To run the app , we write the command streamlit run (name of the file).

This image showcases the user interface of the deployed web application built with Streamlit.

Advantages of using streamit

  • Simplicity: Streamlit uses Python code, eliminating the need to learn complex web development frameworks like HTML, CSS, or Javascript. This allows data scientists and machine learning engineers to focus on their core expertise — building models — and quickly create functional web interfaces.
  • Rapid Prototyping: Streamlit’s hot reloading feature is a game-changer. As you make changes to your Python code, Streamlit automatically refreshes the web app in your browser. This allows for rapid iteration and testing, making it ideal for prototyping and refining your interactive web page before deployment.
  • Built-in Widgets: Streamlit provides a rich set of widgets like sliders, dropdowns, text inputs, and buttons. These widgets become interactive elements within your web page, allowing users to easily provide input to your machine learning model. Imagine a slider for adjusting parameters and seeing the model’s output update in real-time.
  • Data Visualization Integration: Streamlit integrates seamlessly with popular data science libraries like pandas and matplotlib. This lets you easily visualize your model’s predictions or insights gained from the data. You can create charts, graphs, and other visual aids to enhance user understanding and engagement.
  • Deployment Flexibility: Streamlit apps can be deployed in various ways. You can run them locally on your machine for development or deploy them to a cloud platform for wider accessibility. This flexibility allows you to choose the deployment option that best suits your project’s needs.
  • Sharing and Collaboration: Streamlit apps are shareable with minimal effort. You can simply share the Python code with others, allowing them to run the app locally or deploy it themselves. This fosters collaboration and makes it easier to share your machine learning models with colleagues or stakeholders.

Thanks for reading! If you found this helpful, consider following for more data science tips and tricks. Feel free to share it with anyone who might be interested!

--

--

No responses yet