edcelbogs's picture
Update app.py
59bd23e verified
Raw History Blame Contribute Delete
4.98 kB
import streamlit as st
import numpy as np
import tensorflow as tf
import joblib
# --- Load model and label encoder ---
model = tf.keras.models.load_model("lung_cancer_classifier.h5")
le_target = joblib.load("label_encoder.pkl")
# --- Page configuration ---
st.set_page_config(
page_title="Lung Cancer Risk Classifier",
layout="centered"
)
# --- Title and description ---
st.title("Lung Cancer Risk Prediction")
st.write("""
This application predicts the **level of lung cancer risk** for a patient.
**Important:**
The model was trained using data from patients **already diagnosed** with lung cancer.
The output represents a **risk/severity level**, not a medical diagnosis.
""")
# --- Input fields ---
Age = st.number_input("Age", min_value=1, max_value=100, value=37)
Gender = st.selectbox(
"Gender",
options=[("Male", 1), ("Female", 2)],
format_func=lambda x: x[0]
)[1]
Air_Pollution = st.slider(
"Air Pollution Exposure",
1, 8, 4,
help="""
1 – Rural area
2 – Small town
3 – Urban residential
4 – Heavy traffic area
5 – Near industrial zone
6 – Factory worker
7 – High industrial exposure
8 – Extremely polluted environment
"""
)
Alcohol_use = st.slider(
"Alcohol Use",
1, 8, 5,
help="""
1 – Never drinks
2 – Rare
3 – Occasional
4 – Social drinking
5 – Weekly
6 – Several times a week
7 – Daily
8 – Heavy drinker
"""
)
Dust_Allergy = st.slider(
"Dust Allergy",
1, 8, 5,
help="1=None | 3=Mild | 5=Moderate | 7=Severe | 8=Chronic"
)
Occupational_Hazards = st.slider(
"Occupational Hazards",
1, 8, 5,
help="""
1 – Office / home work
3 – Outdoor non-industrial
5 – Factory work
6 – Chemical exposure
8 – Extreme industrial hazard
"""
)
Genetic_Risk = st.slider(
"Genetic Risk",
1, 7, 5,
help="1=None | 3=Distant family history | 5=Immediate family | 7=Strong genetic risk"
)
Chronic_Lung_Disease = st.slider(
"Chronic Lung Disease",
1, 7, 4,
help="1=None | 3=Past condition | 5=Ongoing | 7=Severe"
)
Balanced_Diet = st.slider(
"Balanced Diet",
1, 7, 4,
help="1=Very poor | 3=Poor | 5=Mostly balanced | 7=Highly balanced"
)
Obesity = st.slider(
"Obesity",
1, 7, 4,
help="1=Normal | 3=Overweight | 5=Obese | 7=Severely obese"
)
Smoking = st.slider(
"Smoking",
1, 8, 4,
help="""
1 – Never smoked
3 – Former smoker
4 – Occasional smoker
5 – Light daily smoker
6 – Moderate smoker
7 – Heavy smoker
8 – Chain smoker
"""
)
Passive_Smoker = st.slider(
"Passive Smoker",
1, 8, 4,
help="1=None | 3=Occasional | 5=Daily exposure | 8=Constant exposure"
)
Chest_Pain = st.slider(
"Chest Pain",
1, 9, 4,
help="1=None | 3=Mild | 5=Moderate | 7=Severe | 9=Extreme"
)
Coughing_of_Blood = st.slider(
"Coughing of Blood",
1, 9, 4,
help="1=None | 5=Occasional | 9=Frequent / severe"
)
Fatigue = st.slider(
"Fatigue",
1, 9, 4,
help="1=None | 3=Mild | 5=Moderate | 7=Severe | 9=Extreme"
)
Weight_Loss = st.slider(
"Weight Loss",
1, 8, 4,
help="1=None | 4=Noticeable | 6=Significant | 8=Severe"
)
Shortness_of_Breath = st.slider(
"Shortness of Breath",
1, 9, 4,
help="1=None | 3=Mild | 5=Moderate | 7=Severe | 9=Extreme"
)
Wheezing = st.slider(
"Wheezing",
1, 8, 4,
help="1=None | 4=Occasional | 6=Frequent | 8=Constant"
)
Swallowing_Difficulty = st.slider(
"Swallowing Difficulty",
1, 8, 4,
help="1=None | 4=Occasional | 6=Frequent | 8=Severe"
)
Clubbing_of_Finger_Nails = st.slider(
"Clubbing of Finger Nails",
1, 9, 4,
help="1=None | 5=Visible | 9=Severe"
)
Frequent_Cold = st.slider(
"Frequent Cold",
1, 7, 4,
help="1=Rare | 3=Occasional | 5=Frequent | 7=Very frequent"
)
Dry_Cough = st.slider(
"Dry Cough",
1, 7, 4,
help="1=None | 3=Mild | 5=Persistent | 7=Severe"
)
Snoring = st.slider(
"Snoring",
1, 7, 3,
help="1=None | 3=Occasional | 5=Frequent | 7=Severe"
)
# --- Combine inputs ---
input_data = np.array([[
Age, Gender, Air_Pollution, Alcohol_use, Dust_Allergy,
Occupational_Hazards, Genetic_Risk, Chronic_Lung_Disease,
Balanced_Diet, Obesity, Smoking, Passive_Smoker,
Chest_Pain, Coughing_of_Blood, Fatigue, Weight_Loss,
Shortness_of_Breath, Wheezing, Swallowing_Difficulty,
Clubbing_of_Finger_Nails, Frequent_Cold, Dry_Cough, Snoring
]])
# --- Prediction ---
if st.button("Predict"):
prediction = model.predict(input_data)
predicted_class = prediction.argmax(axis=1)[0]
confidence = prediction.max(axis=1)[0]
label = le_target.inverse_transform([predicted_class])[0]
st.success(f"Predicted Lung Cancer Level: {label}")
st.info(f"Prediction Confidence: {confidence:.2%}")
# --- Footer ---
st.markdown("---")
st.markdown("**Trained By:** Edcel Bogay")