Deploying a Machine Learning Model Using Streamlit
Deploying a Machine Learning Model Using Streamlit – Step by Step Guide
Streamlit is a great tool for creating interactive web apps for machine learning models with minimal coding. Below is a detailed step-by-step guide to deploy your model using Streamlit.
Step 1: Prepare Your Environment
Before we begin, ensure you have the required libraries installed.
-
Streamlit → For building the web app
-
Pandas → For handling data
-
Scikit-learn → For machine learning model
-
Joblib → For saving and loading the trained model
Step 2: Train and Save the Model
First, train and save your machine learning model. Assume we are working with a Regression model predicting "Request Age".
📌 Train the Model (Example: Random Forest)
Step 3: Create a Streamlit App
Now, create a Streamlit app to load the trained model and make predictions.
📌 Create a Python Script for Streamlit
Create a new file app.py and write the following code:
Step 4: Run the Streamlit App
To start the app, run the following command in the terminal:
-
This will launch a web interface where users can input data and get predictions.
Step 5: Deploy the App Online
Once your app is working locally, you can deploy it on Streamlit Cloud or other platforms like Heroku.
📌 Deploy on Streamlit Cloud (Easy Way)
-
Push your code and model files to GitHub.
-
Go to Streamlit Cloud.
-
Click "New App", connect your GitHub repo.
-
Select the
app.pyfile and click "Deploy".
Done! 🎉 Your model is now live!
🚀 Summary of Steps
✅ Step 1: Install dependencies
✅ Step 2: Train and save the model
✅ Step 3: Create a Streamlit app (app.py)
✅ Step 4: Run the app locally using streamlit run app.py
✅ Step 5: Deploy the app on Streamlit Cloud
💡 Next Steps
-
Customize the UI with images, charts, and better styling.
-
Allow users to upload CSV files for batch predictions.
-
Deploy on Heroku or AWS for more flexibility.
To use OneHotEncoder and TargetEncoder instead of pd.get_dummies in your Streamlit app, follow these steps:
🔹 Steps to Implement:
-
Train the OneHotEncoder and TargetEncoder during model training
-
Save the encoders using joblib
-
Load the encoders in Streamlit app
-
Apply encoding on user inputs dynamically before prediction
✅ Updated Streamlit Code with OneHotEncoder & TargetEncoder
🔹 Saving the Encoders During Model Training
Before using this Streamlit app, make sure you save the trained OneHotEncoder and TargetEncoder during model training:
🔹 How to Run Streamlit App
After saving the encoders and model, run the Streamlit app using:
🔹 Summary
✔️ OneHotEncoder applied to LOB, Region, and Catalog Item
✔️ TargetEncoder applied to Bus Sector and Bus Segment
✔️ All encoders are pre-trained & loaded in Streamlit
✔️ Ensured same column structure as training for prediction
No comments