🧠 AI with Python – 🖥️ Streamlit ML App for Predictions


Description:

Streamlit has become one of the simplest and fastest ways to turn machine learning models into interactive web apps.

Instead of building HTML, CSS, or frontend frameworks, Streamlit lets you create sliders, text inputs, buttons, and real-time prediction displays with just Python.

In this project, we build a clean ML prediction interface that loads a saved model and allows users to input values directly through the browser.


Understanding the Problem

Most machine learning workflows live in notebooks or scripts.

But real users—developers, teammates, or clients—need a simple interface to interact with the model.

Streamlit solves this by offering:

  • a pure Python-based UI
  • real-time widget updates
  • a simple launch command
  • auto-refreshing interface
  • easy HTML/Markdown rendering

This makes it perfect for building quick internal tools, testing ML models, or developing full-fledged dashboards.


1. Load the Saved ML Model

We load the RandomForest model saved earlier using joblib.

import joblib
import numpy as np

model = joblib.load("iris_model.joblib")

This allows us to use the trained model without retraining it every time.


2. Build the Streamlit Interface

We create widgets for the Iris flower features.

import streamlit as st

st.title("🌸 Iris Flower Prediction App")
st.write("Enter the features below to predict the Iris species.")

sepal_length = st.number_input("Sepal Length", 0.0, 10.0, 5.1)
sepal_width  = st.number_input("Sepal Width",  0.0, 10.0, 3.5)
petal_length = st.number_input("Petal Length", 0.0, 10.0, 1.4)
petal_width  = st.number_input("Petal Width",  0.0, 10.0, 0.2)

Each widget captures a numerical input from the user.


3. Run Predictions When the User Clicks a Button

We convert the inputs into a NumPy array and predict the Iris species.

if st.button("Predict"):
    features = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
    pred = model.predict(features)[0]

    species_map = {0: "Setosa", 1: "Versicolour", 2: "Virginica"}
    st.success(f"Predicted Species: **{species_map[pred]}**")

The result updates instantly on the page.


4. Run the Streamlit App

Use the following command to launch the UI:

streamlit run app.py

(Replace app.py with your filename.)

Streamlit automatically opens a local web UI where users can interact with the model.


Key Takeaways

  1. Streamlit makes ML app development extremely fast with no frontend code required.
  2. Widgets like number inputs, sliders, and buttons allow interactive user input.
  3. Saved .joblib models load instantly into Streamlit apps.
  4. Perfect for demos, internal tools, workshops, and rapid prototyping.
  5. Forms the foundation for larger dashboards or multi-page ML apps.

Conclusion

Streamlit bridges the gap between ML experimentation and practical usability.

By wrapping your model inside a simple web interface, you make predictions accessible to anyone—without writing HTML or deploying heavy frameworks.

This approach becomes even more powerful when combined with APIs, model monitoring, and cloud deployment, forming a complete ML application pipeline.


Code Snippet:

import joblib
import numpy as np
import streamlit as st

model = joblib.load("../models/iris_model.joblib")

st.title("🌸 Iris Flower Prediction App")
st.write("Enter the features below to predict the Iris species.")

sepal_length = st.number_input("Sepal Length", 0.0, 10.0, 5.1)
sepal_width  = st.number_input("Sepal Width",  0.0, 10.0, 3.5)
petal_length = st.number_input("Petal Length", 0.0, 10.0, 1.4)
petal_width  = st.number_input("Petal Width",  0.0, 10.0, 0.2)

if st.button("Predict"):
    features = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
    pred = model.predict(features)[0]

    species_map = {0: "Setosa", 1: "Versicolour", 2: "Virginica"}
    st.success(f"Predicted Species: **{species_map[pred]}**")

Link copied!

Comments

Add Your Comment

Comment Added!