Spaces:
Sleeping
Sleeping
Download app.py from edcelbogs/Lung_Cancer_Class: direct link, hf CLI and curl.
- Browser
- Download file 4.98 kB
-
https://huggingface.co/spaces/edcelbogs/Lung_Cancer_Class/resolve/main/app.py
- Command line
-
hf download hf://spaces/edcelbogs/Lung_Cancer_Class/app.py
-
curl -L -o app.py https://huggingface.co/spaces/edcelbogs/Lung_Cancer_Class/resolve/main/app.py
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") | |