I’m trying to create a Streamlit page in Google Colab to deploy a simple spam-ham classifier, but I keep running into an error, and I can’t figure out what’s wrong. I’ve installed Streamlit and pyngrok, set up the necessary imports, and written the main app code for the Streamlit interface. My code loads a pre-trained model with joblib, takes user input, and predicts whether the message is spam or ham when I hit the “Predict” button. However, when I try to run the last block to start the Streamlit app and open it with ngrok, it throws an error. I’ve checked the code multiple times, but I can’t seem to pinpoint the issue.
Error Code:
code!pip install streamlit --quiet
!pip install pyngrok==4.1.1
from pyngrok import ngrok
%%writefile app.py
import streamlit as st
import joblib
st.title('Spam Ham Deployment')
test_model=joblib.load('spam_ham')
ip=st.text_input('Enter your message')
op=test_model.predict([ip])
if st.button('Predict')
st.title(op)
!nohup streamlit run app.py &
url=ngrok.connect(port='8501')
url
The code you’ve shared is intended to deploy a machine learning model for classifying messages as “spam” or “ham” using Streamlit and Pyngrok. The goal is to create a simple web app where users can input a message and receive a classification (spam or ham). However, the code contains several syntax and indentation errors that need correction to run properly.
Issues and Solutions:
- Indentation:
- The line
op=test_model.predict([ip])
is not aligned properly, which causes a syntax error. url
should be aligned correctly to ensure the code flows without indentation errors.
- The line
- Missing Colon:
- The
if
statement inif st.button('Predict')
lacks a colon, which is essential for Python syntax.
- The
- Streamlit Button Action:
- The prediction should be executed only when the “Predict” button is clicked.
- Running Streamlit with Ngrok:
- Ngrok is used to create a public URL for the Streamlit app, making it accessible over the internet.
Correct Code:
code# Install necessary packages
!pip install streamlit --quiet
!pip install pyngrok==4.1.1
from pyngrok import ngrok
# Write the Streamlit app to a Python file
%%writefile app.py
import streamlit as st
import joblib
# Set the title of the app
st.title('Spam Ham Deployment')
# Load the trained model
test_model = joblib.load('spam_ham')
# Get user input
ip = st.text_input('Enter your message')
# Predict and display result when button is clicked
if st.button('Predict'):
op = test_model.predict([ip]) # Predict spam or ham
st.title(op[0]) # Display the prediction
# Run the Streamlit app with Ngrok
!nohup streamlit run app.py &
url = ngrok.connect(port='8501')
url
Explanation:
- Package Installation:
streamlit
andpyngrok
are installed usingpip
.streamlit
is the web app framework, whilepyngrok
is used to make the app accessible from a public URL.
- Ngrok Setup:
- After importing
ngrok
, the code writes the Streamlit app code to a file namedapp.py
.
- After importing
- Streamlit App Setup:
- The Streamlit app begins by importing necessary libraries:
streamlit
for the app interface andjoblib
to load the saved model. - The app title is set to “Spam Ham Deployment”.
- The Streamlit app begins by importing necessary libraries:
- Model Loading:
- The
joblib.load('spam_ham')
command loads a pre-trained model (expected to be saved asspam_ham
), which will classify the input message.
- The
- User Input and Prediction:
st.text_input
creates a text box for the user to input a message.if st.button('Predict'):
waits for the user to click the “Predict” button. When clicked, it runs the model’spredict
function on the input and displays the prediction (spam
orham
).
- Running Streamlit and Exposing with Ngrok:
- The code uses
!nohup streamlit run app.py &
to run the Streamlit app in the background. ngrok.connect(port='8501')
opens a public URL for the app at port8501
, enabling external access to the Streamlit app.
- The code uses
Key Points:
- Indentation and colon syntax are essential for Python code structure.
- Streamlit and Ngrok combination allows for creating and sharing web applications without a full web server setup.
By using this corrected code, you should be able to run your Streamlit app and access it publicly through the Ngrok URL.