Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,75 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
from sklearn.pipeline import Pipeline
|
| 5 |
-
from sklearn.compose import ColumnTransformer
|
| 6 |
-
from sklearn.impute import SimpleImputer
|
| 7 |
-
from sklearn.preprocessing import StandardScaler, OrdinalEncoder, OneHotEncoder
|
| 8 |
-
from xgboost import XGBClassifier
|
| 9 |
-
|
| 10 |
-
# Define columns
|
| 11 |
-
numeric = ['Age', 'Tumor_Size']
|
| 12 |
-
ordinal = ['Tumor_Grade', 'Symptoms_Severity', 'Alcohol_Consumption', 'Exercise_Frequency']
|
| 13 |
-
nominal = ['Gender', 'Family_History', 'Smoking_History']
|
| 14 |
-
|
| 15 |
-
# Define preprocessing pipelines
|
| 16 |
-
numeric_preprocess = Pipeline([
|
| 17 |
-
('Mean Imputation', SimpleImputer(strategy='mean')),
|
| 18 |
-
('Scaling', StandardScaler())
|
| 19 |
-
])
|
| 20 |
-
|
| 21 |
-
ordinal_preprocess = Pipeline([
|
| 22 |
-
('Mode Imputation', SimpleImputer(strategy='most_frequent')),
|
| 23 |
-
('Encoding', OrdinalEncoder())
|
| 24 |
-
])
|
| 25 |
-
|
| 26 |
-
nominal_preprocess = Pipeline([
|
| 27 |
-
('Mode Imputation', SimpleImputer(strategy='most_frequent')),
|
| 28 |
-
('Encoding', OneHotEncoder(sparse_output=False))
|
| 29 |
-
])
|
| 30 |
-
|
| 31 |
-
preprocess = ColumnTransformer([
|
| 32 |
-
('Numerical Transformer', numeric_preprocess, numeric),
|
| 33 |
-
('Ordinal Transformer', ordinal_preprocess, ordinal),
|
| 34 |
-
('Nominal Transformer', nominal_preprocess, nominal)
|
| 35 |
-
], remainder='passthrough')
|
| 36 |
-
|
| 37 |
-
# Load dataset and train model
|
| 38 |
-
df = pd.read_csv(
|
| 39 |
-
x = df.drop('Cancer_Present', axis=1)
|
| 40 |
-
y = df['Cancer_Present']
|
| 41 |
-
xgb = Pipeline([
|
| 42 |
-
('Data Preprocessing', preprocess),
|
| 43 |
-
('Algorithm', XGBClassifier())
|
| 44 |
-
])
|
| 45 |
-
xgb.fit(x, y)
|
| 46 |
-
|
| 47 |
-
# Streamlit UI
|
| 48 |
-
st.set_page_config(page_title="Cancer Prediction App", page_icon="π©Ί", layout="centered")
|
| 49 |
-
st.title("π¬ Cancer Prediction App")
|
| 50 |
-
st.write("This application predicts the likelihood of having cancer based on various health parameters.")
|
| 51 |
-
|
| 52 |
-
# User input fields
|
| 53 |
-
age = st.number_input("π
Age", min_value=1, max_value=120, value=30)
|
| 54 |
-
gender = st.selectbox("β§οΈ Gender", ["Male", "Female"])
|
| 55 |
-
tumor_size = st.number_input("π Tumor Size (0-10)", min_value=0.0, max_value=10.0, value=5.0)
|
| 56 |
-
tumor_grade = st.selectbox("π’ Tumor Grade", ["Low", "Medium", "High"])
|
| 57 |
-
symptoms_severity = st.selectbox("π€ Symptoms Severity", ["Mild", "Moderate", "Severe"])
|
| 58 |
-
family_history = st.selectbox("π¨βπ©βπ§ Family History", ["Yes", "No"])
|
| 59 |
-
smoking_history = st.selectbox("π¬ Smoking History", ["Non-Smoker", "Former Smoker", "Current Smoker"])
|
| 60 |
-
alcohol_consumption = st.selectbox("π· Alcohol Consumption", ["Low", "Moderate", "High"])
|
| 61 |
-
exercise_frequency = st.selectbox("ποΈ Exercise Frequency", ["Never", "Occasionally", "Rarely", "Regularly"])
|
| 62 |
-
|
| 63 |
-
# Predict button
|
| 64 |
-
if st.button("π Predict Cancer"):
|
| 65 |
-
input_data = pd.DataFrame([[age, gender, tumor_size, tumor_grade, symptoms_severity,
|
| 66 |
-
family_history, smoking_history, alcohol_consumption, exercise_frequency]],
|
| 67 |
-
columns=x.columns)
|
| 68 |
-
prediction = xgb.predict(input_data)[0]
|
| 69 |
-
|
| 70 |
-
if prediction == 0:
|
| 71 |
-
st.success("β
Not a Cancer")
|
| 72 |
-
else:
|
| 73 |
-
st.error("β οΈ Cancer Detected! Please consult a doctor.")
|
| 74 |
-
|
| 75 |
-
st.write("π This model is trained using XGBoost and provides predictions based on health data.")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.pipeline import Pipeline
|
| 5 |
+
from sklearn.compose import ColumnTransformer
|
| 6 |
+
from sklearn.impute import SimpleImputer
|
| 7 |
+
from sklearn.preprocessing import StandardScaler, OrdinalEncoder, OneHotEncoder
|
| 8 |
+
from xgboost import XGBClassifier
|
| 9 |
+
|
| 10 |
+
# Define columns
|
| 11 |
+
numeric = ['Age', 'Tumor_Size']
|
| 12 |
+
ordinal = ['Tumor_Grade', 'Symptoms_Severity', 'Alcohol_Consumption', 'Exercise_Frequency']
|
| 13 |
+
nominal = ['Gender', 'Family_History', 'Smoking_History']
|
| 14 |
+
|
| 15 |
+
# Define preprocessing pipelines
|
| 16 |
+
numeric_preprocess = Pipeline([
|
| 17 |
+
('Mean Imputation', SimpleImputer(strategy='mean')),
|
| 18 |
+
('Scaling', StandardScaler())
|
| 19 |
+
])
|
| 20 |
+
|
| 21 |
+
ordinal_preprocess = Pipeline([
|
| 22 |
+
('Mode Imputation', SimpleImputer(strategy='most_frequent')),
|
| 23 |
+
('Encoding', OrdinalEncoder())
|
| 24 |
+
])
|
| 25 |
+
|
| 26 |
+
nominal_preprocess = Pipeline([
|
| 27 |
+
('Mode Imputation', SimpleImputer(strategy='most_frequent')),
|
| 28 |
+
('Encoding', OneHotEncoder(sparse_output=False))
|
| 29 |
+
])
|
| 30 |
+
|
| 31 |
+
preprocess = ColumnTransformer([
|
| 32 |
+
('Numerical Transformer', numeric_preprocess, numeric),
|
| 33 |
+
('Ordinal Transformer', ordinal_preprocess, ordinal),
|
| 34 |
+
('Nominal Transformer', nominal_preprocess, nominal)
|
| 35 |
+
], remainder='passthrough')
|
| 36 |
+
|
| 37 |
+
# Load dataset and train model
|
| 38 |
+
df = pd.read_csv("cancer_prediction_data (2).csv")
|
| 39 |
+
x = df.drop('Cancer_Present', axis=1)
|
| 40 |
+
y = df['Cancer_Present']
|
| 41 |
+
xgb = Pipeline([
|
| 42 |
+
('Data Preprocessing', preprocess),
|
| 43 |
+
('Algorithm', XGBClassifier())
|
| 44 |
+
])
|
| 45 |
+
xgb.fit(x, y)
|
| 46 |
+
|
| 47 |
+
# Streamlit UI
|
| 48 |
+
st.set_page_config(page_title="Cancer Prediction App", page_icon="π©Ί", layout="centered")
|
| 49 |
+
st.title("π¬ Cancer Prediction App")
|
| 50 |
+
st.write("This application predicts the likelihood of having cancer based on various health parameters.")
|
| 51 |
+
|
| 52 |
+
# User input fields
|
| 53 |
+
age = st.number_input("π
Age", min_value=1, max_value=120, value=30)
|
| 54 |
+
gender = st.selectbox("β§οΈ Gender", ["Male", "Female"])
|
| 55 |
+
tumor_size = st.number_input("π Tumor Size (0-10)", min_value=0.0, max_value=10.0, value=5.0)
|
| 56 |
+
tumor_grade = st.selectbox("π’ Tumor Grade", ["Low", "Medium", "High"])
|
| 57 |
+
symptoms_severity = st.selectbox("π€ Symptoms Severity", ["Mild", "Moderate", "Severe"])
|
| 58 |
+
family_history = st.selectbox("π¨βπ©βπ§ Family History", ["Yes", "No"])
|
| 59 |
+
smoking_history = st.selectbox("π¬ Smoking History", ["Non-Smoker", "Former Smoker", "Current Smoker"])
|
| 60 |
+
alcohol_consumption = st.selectbox("π· Alcohol Consumption", ["Low", "Moderate", "High"])
|
| 61 |
+
exercise_frequency = st.selectbox("ποΈ Exercise Frequency", ["Never", "Occasionally", "Rarely", "Regularly"])
|
| 62 |
+
|
| 63 |
+
# Predict button
|
| 64 |
+
if st.button("π Predict Cancer"):
|
| 65 |
+
input_data = pd.DataFrame([[age, gender, tumor_size, tumor_grade, symptoms_severity,
|
| 66 |
+
family_history, smoking_history, alcohol_consumption, exercise_frequency]],
|
| 67 |
+
columns=x.columns)
|
| 68 |
+
prediction = xgb.predict(input_data)[0]
|
| 69 |
+
|
| 70 |
+
if prediction == 0:
|
| 71 |
+
st.success("β
Not a Cancer")
|
| 72 |
+
else:
|
| 73 |
+
st.error("β οΈ Cancer Detected! Please consult a doctor.")
|
| 74 |
+
|
| 75 |
+
st.write("π This model is trained using XGBoost and provides predictions based on health data.")
|