streamlit app to classify ecg images and predict cardiovascular disease, i am running it locally on google colab, it give me this link; but when i click it its blank saying the site cannot be reached


import streamlit as st
import joblib
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras.applications.inception_v3 import decode_predictions

def main():
# Load the best saved models
loaded_densenet121_model = load_model(β€˜/content/best_DenseNet121_model.h5’)
svm_classifier = joblib.load(β€˜/content/svm_classifier.pkl’)

# Set page title and favicon
st.set_page_config(page_title="Cardiac Disease Prediction & ECG Image Classification", page_icon="βœ…")

# Create a sidebar for options
option ="Select an Option", ["Predict Cardiac Disease", "Classify ECG Image"])

# Create the main content area
st.title("Cardiac Disease Prediction and ECG Image Classification")

if option == "Predict Cardiac Disease":
    st.header("Cardiac Disease Prediction")
    st.write("Enter Patient Parameters:")

    # Create input fields for patient data
    age = st.slider("Age", min_value=0, max_value=100, step=1)
    gender ="Gender", ["Male", "Female"])
    chest_pain = st.selectbox("Chest Pain Type", ["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"])
    resting_bp = st.number_input("Resting Blood Pressure", min_value=0)
    serum_cholesterol = st.number_input("Serum Cholesterol (mg/dl)", min_value=0)
    resting_ecg = st.selectbox("Resting Electrocardiographic Results", ["Normal", "ST-T Wave Abnormality", "Left Ventricular Hypertrophy"])
    max_heart_rate = st.number_input("Max Heart Rate", min_value=0)
    old_peak = st.number_input("Oldpeak", min_value=0.0)
    slope = st.selectbox("Slope of Peak Exercise ST Segment", ["Upsloping", "Flat", "Downsloping"])
    num_major_vessels = st.number_input("Number of Major Vessels", min_value=0)

    if st.button("Predict Cardiac Disease"):
        # Preprocess the input data
        gender = 1 if gender == "Male" else 0
        chest_pain = ["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"].index(chest_pain)
        resting_ecg = ["Normal", "ST-T Wave Abnormality", "Left Ventricular Hypertrophy"].index(resting_ecg)

        input_data = [age, gender, chest_pain, resting_bp, serum_cholesterol, resting_ecg, max_heart_rate, old_peak, slope, num_major_vessels]
        input_data = np.array(input_data).reshape(1, -1)

        # Make predictions
        result_svm = svm_classifier.predict(input_data)

        # Display the results
        st.subheader("Cardiac Disease Prediction Results:")
        if result_svm[0] == 0:
            st.error("High likelihood of heart disease.")
            st.success("Low likelihood of heart disease")

if option == "Classify ECG Image":
    st.header("ECG Image Classification")
    uploaded_file = st.file_uploader("Choose an ECG image...", type=["jpg", "png"])

    if uploaded_file:
        st.subheader("Selected ECG Image:")
        st.image(uploaded_file, use_column_width=True)

        if st.button("Classify ECG Image"):
            # Perform ECG image classification using InceptionV3
            img = load_img(uploaded_file, target_size=(299, 299))
            img_array = img_to_array(img)
            img_array = preprocess_input(img_array)
            img_array = np.expand_dims(img_array, axis=0)

            predictions = loaded_densenet121_model.predict(img_array)
            decoded_predictions = decode_predictions(predictions, top=1)[0]

            st.subheader("ECG Image Classification Results:")
            st.write(f"Predicted class: {decoded_predictions[0][1]}")

Run the Streamlit app

if name == β€˜main’:

Welcome to the community, @peace1980! We’re thrilled to have you join us! :hugs:

If you’re running a Streamlit app on Google Colab and having trouble accessing it externally, using ngrok can help expose your local server to the internet.

Here’s how you can set it up:

  1. Install ngrok in Colab:
    You can install ngrok directly in your Colab notebook with the following command:

    !pip install pyngrok
  2. Establish a Tunnel:
    Set up ngrok to tunnel the Streamlit port (8501 by default) to a public URL:

    from pyngrok import ngrok
    # Terminate open tunnels if exist
    # Launch ngrok tunnel to the streamlit port 8501
    public_url = ngrok.connect(port=8501)

    Running this will display a link that you can use to access your Streamlit app from anywhere.

  3. Run Your Streamlit App:
    Make sure to run your Streamlit application in the background in Colab so it doesn’t block the execution of further cells:

    !streamlit run &
  4. Access the App:
    Use the link provided by ngrok to access your Streamlit application from your browser.

Let me know if that helps you! :slight_smile:

Best wishes,

Thank you for the quick response, I will try it out and get back to you.
I really appreciate.
Best regards