DasariHarshitha commited on
Commit
28d1dfd
·
verified ·
1 Parent(s): 9a3c77a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -23
app.py CHANGED
@@ -11,6 +11,16 @@ from sklearn.svm import SVC
11
  from sklearn.linear_model import LogisticRegression
12
  from sklearn.neighbors import KNeighborsClassifier
13
 
 
 
 
 
 
 
 
 
 
 
14
  # Load dataset
15
  def load_data():
16
  df = pd.read_csv('cancer_prediction_data (2).csv')
@@ -22,7 +32,6 @@ def preprocess_data(df):
22
  ordinal = ['Tumor_Grade', 'Symptoms_Severity', 'Alcohol_Consumption', 'Exercise_Frequency']
23
  nominal = ['Gender', 'Family_History', 'Smoking_History']
24
 
25
- # Pipelines
26
  numeric_preprocess = Pipeline([
27
  ('imputer', SimpleImputer(strategy='mean')),
28
  ('scaler', StandardScaler())
@@ -36,7 +45,6 @@ def preprocess_data(df):
36
  ('encoder', OneHotEncoder(sparse_output=False, handle_unknown='ignore'))
37
  ])
38
 
39
- # Column Transformer
40
  preprocess = ColumnTransformer([
41
  ('num', numeric_preprocess, numeric),
42
  ('ord', ordinal_preprocess, ordinal),
@@ -66,45 +74,45 @@ def train_model(X_train, y_train, preprocess, model_name):
66
  return pipeline
67
 
68
  # Streamlit UI
69
- st.markdown("<h1 style='text-align: center; color: #1976D2;'>🎗️ Cancer Prediction Using ML 🎗️</h1>", unsafe_allow_html=True)
 
70
 
71
  df = load_data()
72
  X_train, X_test, y_train, y_test, preprocess = preprocess_data(df)
73
 
74
- model_name = st.radio("Select Model", ['Decision Tree', 'SVM', 'Logistic Regression', 'KNN'])
 
75
 
76
- if st.button("Train Model"):
77
  model = train_model(X_train, y_train, preprocess, model_name)
78
  accuracy = model.score(X_test, y_test)
79
- st.write(f"Model Accuracy: {accuracy:.2f}")
80
  st.session_state['trained_model'] = model
81
- st.success("Model trained successfully!")
82
 
83
  # Prediction Section
84
- st.markdown("<h2 style='color: #D32F2F;'>🔍 Make a Prediction</h2>", unsafe_allow_html=True)
 
 
 
 
85
 
86
- age = st.number_input("Age", min_value=18, max_value=100, value=30)
87
- tumor_size = st.number_input("Tumor Size", min_value=1.0, max_value=10.0, value=5.0)
88
- tumor_grade = st.radio("Tumor Grade", [1, 2, 3])
89
- symptoms_severity = st.radio("Symptoms Severity", [1, 2, 3])
90
- smoking_history = st.radio("Smoking History", [0, 1, 2])
91
- alcohol_consumption = st.radio("Alcohol Consumption", [0, 1, 2, 3])
92
- exercise_frequency = st.radio("Exercise Frequency", [0, 1, 2, 3])
93
- gender = st.radio("Gender", [0, 1])
94
- family_history = st.radio("Family History", [0, 1])
95
 
96
  input_data = [[age, tumor_size, tumor_grade, symptoms_severity, smoking_history,
97
  alcohol_consumption, exercise_frequency, gender, family_history]]
98
 
99
- if st.button("Predict Cancer Presence"):
100
  if 'trained_model' in st.session_state:
101
  model = st.session_state['trained_model']
102
  input_df = pd.DataFrame(input_data, columns=X_train.columns)
103
-
104
- # Transform input data using the same preprocessor
105
  input_transformed = model.named_steps['preprocessor'].transform(input_df)
106
  prediction = model.named_steps['classifier'].predict(input_transformed)
107
-
108
- st.write("**Cancer Prediction:**", "**🟥Positive**" if prediction[0] == 1 else "**🟩Negative**")
109
  else:
110
- st.error("Please train a model first!")
 
 
11
  from sklearn.linear_model import LogisticRegression
12
  from sklearn.neighbors import KNeighborsClassifier
13
 
14
+ # Set dark theme and page config
15
+ st.set_page_config(page_title="🎗️Cancer Prediction🎗️", page_icon="🩺", layout="centered")
16
+ st.markdown("""
17
+ <style>
18
+ body { background-color: #121212; color: white; }
19
+ .stButton>button { background-color: #ff4b4b; color: white; }
20
+ .stSelectbox, .stRadio, .stNumberInput, .stSlider { color: white; }
21
+ </style>
22
+ """, unsafe_allow_html=True)
23
+
24
  # Load dataset
25
  def load_data():
26
  df = pd.read_csv('cancer_prediction_data (2).csv')
 
32
  ordinal = ['Tumor_Grade', 'Symptoms_Severity', 'Alcohol_Consumption', 'Exercise_Frequency']
33
  nominal = ['Gender', 'Family_History', 'Smoking_History']
34
 
 
35
  numeric_preprocess = Pipeline([
36
  ('imputer', SimpleImputer(strategy='mean')),
37
  ('scaler', StandardScaler())
 
45
  ('encoder', OneHotEncoder(sparse_output=False, handle_unknown='ignore'))
46
  ])
47
 
 
48
  preprocess = ColumnTransformer([
49
  ('num', numeric_preprocess, numeric),
50
  ('ord', ordinal_preprocess, ordinal),
 
74
  return pipeline
75
 
76
  # Streamlit UI
77
+ st.markdown("<h1 style='text-align: center; color: white;'>🩺 Cancer Prediction 🩺</h1>", unsafe_allow_html=True)
78
+ st.write("An intelligent system for early cancer detection using machine learning.")
79
 
80
  df = load_data()
81
  X_train, X_test, y_train, y_test, preprocess = preprocess_data(df)
82
 
83
+ st.subheader("🔬 Choose a Machine Learning Model")
84
+ model_name = st.selectbox("", ['Decision Tree', 'SVM', 'Logistic Regression', 'KNN'])
85
 
86
+ if st.button("🚀 Train & Evaluate Model"):
87
  model = train_model(X_train, y_train, preprocess, model_name)
88
  accuracy = model.score(X_test, y_test)
89
+ st.success(f"Model trained successfully! Accuracy: {accuracy:.2f}")
90
  st.session_state['trained_model'] = model
 
91
 
92
  # Prediction Section
93
+ st.markdown("<h2 style='color: red;'>🔍 Make a Prediction</h2>", unsafe_allow_html=True)
94
+ age = st.number_input("📅 Age", min_value=18, max_value=100, value=30)
95
+ tumor_size = st.number_input("🧬 Tumor Size (cm)", min_value=1.0, max_value=10.0, value=5.0)
96
+ smoking_history = st.radio("🚬 Smoking History", ['Non-Smoker', 'Former Smoker', 'Current Smoker'])
97
+ alcohol_consumption = st.selectbox("🍷 Alcohol Consumption", ['None', 'Low', 'Moderate', 'High'])
98
 
99
+ tumor_grade = st.selectbox("Tumor Grade", [1, 2, 3])
100
+ symptoms_severity = st.selectbox("Symptoms Severity", [1, 2, 3])
101
+ exercise_frequency = st.selectbox("Exercise Frequency", ['Never', 'Rarely', 'Occasionally', 'Regularly'])
102
+ gender = st.radio("Gender", ['Male', 'Female'])
103
+ family_history = st.radio("Family History", ['No', 'Yes'])
 
 
 
 
104
 
105
  input_data = [[age, tumor_size, tumor_grade, symptoms_severity, smoking_history,
106
  alcohol_consumption, exercise_frequency, gender, family_history]]
107
 
108
+ if st.button("🔮Predict Cancer Presence"):
109
  if 'trained_model' in st.session_state:
110
  model = st.session_state['trained_model']
111
  input_df = pd.DataFrame(input_data, columns=X_train.columns)
 
 
112
  input_transformed = model.named_steps['preprocessor'].transform(input_df)
113
  prediction = model.named_steps['classifier'].predict(input_transformed)
114
+
115
+ st.write("Cancer Prediction:", "Positive" if prediction[0] == 1 else "Negative")
116
  else:
117
+ st.error("Please train a model first!")
118
+