edcelbogs commited on
Commit
b916282
·
verified ·
1 Parent(s): d80c02f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +74 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,76 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ import joblib
5
+
6
+ # --- Load model and label encoder ---
7
+ model = tf.keras.models.load_model("lung_cancer_classifier.h5") # Load H5 model
8
+ le_target = joblib.load("label_encoder.pkl")
9
+
10
+ # --- Page configuration ---
11
+ st.set_page_config(page_title="Lung Cancer Risk Classifier", layout="centered")
12
+
13
+ # --- Title and description ---
14
+ st.title("Lung Cancer Risk Prediction")
15
+ st.write("""
16
+ This application predicts the **level of lung cancer risk** for a patient.
17
+ **Note:** The data used to train this model consists of patients already diagnosed with lung cancer.
18
+ The target of this classification is the **Lung Cancer Level**.
19
+ """)
20
+
21
+ # --- Input fields ---
22
+ Age = st.number_input("Age", min_value=1, max_value=100, value=37)
23
+
24
+ # Gender dropdown fixed to show Male/Female but return encoded value
25
+ Gender = st.selectbox(
26
+ "Gender",
27
+ options=[("Male", 1), ("Female", 2)],
28
+ format_func=lambda x: x[0] # show only label
29
+ )[1] # get encoded value
30
+
31
+ Air_Pollution = st.slider("Air Pollution Exposure", 1, 8, 4)
32
+ Alcohol_use = st.slider("Alcohol Use", 1, 8, 5)
33
+ Dust_Allergy = st.slider("Dust Allergy", 1, 8, 5)
34
+ Occupational_Hazards = st.slider("Occupational Hazards", 1, 8, 5)
35
+ Genetic_Risk = st.slider("Genetic Risk", 1, 7, 5)
36
+ Chronic_Lung_Disease = st.slider("Chronic Lung Disease", 1, 7, 4)
37
+ Balanced_Diet = st.slider("Balanced Diet", 1, 7, 4)
38
+ Obesity = st.slider("Obesity", 1, 7, 4)
39
+ Smoking = st.slider("Smoking", 1, 8, 4)
40
+ Passive_Smoker = st.slider("Passive Smoker", 1, 8, 4)
41
+ Chest_Pain = st.slider("Chest Pain", 1, 9, 4)
42
+ Coughing_of_Blood = st.slider("Coughing of Blood", 1, 9, 4)
43
+ Fatigue = st.slider("Fatigue", 1, 9, 4)
44
+ Weight_Loss = st.slider("Weight Loss", 1, 8, 4)
45
+ Shortness_of_Breath = st.slider("Shortness of Breath", 1, 9, 4)
46
+ Wheezing = st.slider("Wheezing", 1, 8, 4)
47
+ Swallowing_Difficulty = st.slider("Swallowing Difficulty", 1, 8, 4)
48
+ Clubbing_of_Finger_Nails = st.slider("Clubbing of Finger Nails", 1, 9, 4)
49
+ Frequent_Cold = st.slider("Frequent Cold", 1, 7, 4)
50
+ Dry_Cough = st.slider("Dry Cough", 1, 7, 4)
51
+ Snoring = st.slider("Snoring", 1, 7, 3)
52
+
53
+ # --- Combine inputs ---
54
+ input_data = np.array([[
55
+ Age, Gender, Air_Pollution, Alcohol_use, Dust_Allergy,
56
+ Occupational_Hazards, Genetic_Risk, Chronic_Lung_Disease,
57
+ Balanced_Diet, Obesity, Smoking, Passive_Smoker,
58
+ Chest_Pain, Coughing_of_Blood, Fatigue, Weight_Loss,
59
+ Shortness_of_Breath, Wheezing, Swallowing_Difficulty,
60
+ Clubbing_of_Finger_Nails, Frequent_Cold, Dry_Cough, Snoring
61
+ ]])
62
+
63
+ # --- Prediction ---
64
+ if st.button("Predict"):
65
+ prediction = model.predict(input_data)
66
+ predicted_class = prediction.argmax(axis=1)[0]
67
+ confidence = prediction.max(axis=1)[0]
68
+
69
+ label = le_target.inverse_transform([predicted_class])[0]
70
+
71
+ st.success(f"Predicted Lung Cancer Level: {label}")
72
+ st.info(f"Prediction Confidence: {confidence:.2%}")
73
 
74
+ # --- Footer ---
75
+ st.markdown("---")
76
+ st.markdown("**Train By:** Edcel Bogay")