Spaces:
Sleeping
Sleeping
| 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") # Load H5 model | |
| 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. | |
| **Note:** The data used to train this model consists of patients already diagnosed with lung cancer. | |
| The target of this classification is the **Lung Cancer Level**. | |
| """) | |
| # --- Input fields --- | |
| Age = st.number_input("Age", min_value=1, max_value=100, value=37) | |
| # Gender dropdown fixed to show Male/Female but return encoded value | |
| Gender = st.selectbox( | |
| "Gender", | |
| options=[("Male", 1), ("Female", 2)], | |
| format_func=lambda x: x[0] # show only label | |
| )[1] # get encoded value | |
| Air_Pollution = st.slider("Air Pollution Exposure", 1, 8, 4) | |
| Alcohol_use = st.slider("Alcohol Use", 1, 8, 5) | |
| Dust_Allergy = st.slider("Dust Allergy", 1, 8, 5) | |
| Occupational_Hazards = st.slider("Occupational Hazards", 1, 8, 5) | |
| Genetic_Risk = st.slider("Genetic Risk", 1, 7, 5) | |
| Chronic_Lung_Disease = st.slider("Chronic Lung Disease", 1, 7, 4) | |
| Balanced_Diet = st.slider("Balanced Diet", 1, 7, 4) | |
| Obesity = st.slider("Obesity", 1, 7, 4) | |
| Smoking = st.slider("Smoking", 1, 8, 4) | |
| Passive_Smoker = st.slider("Passive Smoker", 1, 8, 4) | |
| Chest_Pain = st.slider("Chest Pain", 1, 9, 4) | |
| Coughing_of_Blood = st.slider("Coughing of Blood", 1, 9, 4) | |
| Fatigue = st.slider("Fatigue", 1, 9, 4) | |
| Weight_Loss = st.slider("Weight Loss", 1, 8, 4) | |
| Shortness_of_Breath = st.slider("Shortness of Breath", 1, 9, 4) | |
| Wheezing = st.slider("Wheezing", 1, 8, 4) | |
| Swallowing_Difficulty = st.slider("Swallowing Difficulty", 1, 8, 4) | |
| Clubbing_of_Finger_Nails = st.slider("Clubbing of Finger Nails", 1, 9, 4) | |
| Frequent_Cold = st.slider("Frequent Cold", 1, 7, 4) | |
| Dry_Cough = st.slider("Dry Cough", 1, 7, 4) | |
| Snoring = st.slider("Snoring", 1, 7, 3) | |
| # --- 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("**Train By:** Edcel Bogay") | |