Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,109 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
from sklearn.pipeline import Pipeline
|
| 5 |
-
from sklearn.preprocessing import StandardScaler, OrdinalEncoder, OneHotEncoder
|
| 6 |
-
from sklearn.impute import SimpleImputer
|
| 7 |
-
from sklearn.compose import ColumnTransformer
|
| 8 |
-
from sklearn.model_selection import train_test_split
|
| 9 |
-
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
-
from sklearn.linear_model import LogisticRegression
|
| 11 |
-
from sklearn.neighbors import KNeighborsClassifier
|
| 12 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 13 |
-
from xgboost import XGBClassifier
|
| 14 |
-
|
| 15 |
-
# Load dataset
|
| 16 |
-
def load_data():
|
| 17 |
-
return pd.read_csv('cancer_prediction_data (2).csv')
|
| 18 |
-
|
| 19 |
-
# Data Preprocessing
|
| 20 |
-
def preprocess_data(df):
|
| 21 |
-
numeric = ['Age', 'Tumor_Size']
|
| 22 |
-
ordinal = ['Tumor_Grade', 'Symptoms_Severity', 'Alcohol_Consumption', 'Exercise_Frequency']
|
| 23 |
-
nominal = ['Gender', 'Family_History', 'Smoking_History']
|
| 24 |
-
|
| 25 |
-
preprocess = ColumnTransformer([
|
| 26 |
-
('num', Pipeline([
|
| 27 |
-
('imputer', SimpleImputer(strategy='mean')),
|
| 28 |
-
('scaler', StandardScaler())
|
| 29 |
-
]), numeric),
|
| 30 |
-
('ord', Pipeline([
|
| 31 |
-
('imputer', SimpleImputer(strategy='most_frequent')),
|
| 32 |
-
('encoder', OrdinalEncoder(handle_unknown="use_encoded_value", unknown_value=-1))
|
| 33 |
-
]), ordinal),
|
| 34 |
-
('nom', Pipeline([
|
| 35 |
-
('imputer', SimpleImputer(strategy='most_frequent')),
|
| 36 |
-
('encoder', OneHotEncoder(sparse_output=False, handle_unknown='ignore'))
|
| 37 |
-
]), nominal)
|
| 38 |
-
], remainder='passthrough')
|
| 39 |
-
|
| 40 |
-
X = df.drop('Cancer_Present', axis=1)
|
| 41 |
-
y = df['Cancer_Present']
|
| 42 |
-
return train_test_split(X, y, test_size=0.2, random_state=23), preprocess
|
| 43 |
-
|
| 44 |
-
# Train Model
|
| 45 |
-
def train_model(X_train, y_train, preprocess, model_name):
|
| 46 |
-
models = {
|
| 47 |
-
'Decision Tree': DecisionTreeClassifier(),
|
| 48 |
-
'Logistic Regression': LogisticRegression(),
|
| 49 |
-
'KNN': KNeighborsClassifier(),
|
| 50 |
-
'Random Forest': RandomForestClassifier(),
|
| 51 |
-
'XGBoost': XGBClassifier()
|
| 52 |
-
}
|
| 53 |
-
pipeline = Pipeline([
|
| 54 |
-
('preprocessor', preprocess),
|
| 55 |
-
('classifier', models[model_name])
|
| 56 |
-
])
|
| 57 |
-
pipeline.fit(X_train, y_train)
|
| 58 |
-
return pipeline
|
| 59 |
-
|
| 60 |
-
# Streamlit UI
|
| 61 |
-
st.set_page_config(page_title='Cancer Prediction App', layout='wide')
|
| 62 |
-
|
| 63 |
-
with st.sidebar:
|
| 64 |
-
st.image('https://via.placeholder.com/300x150.png?text=Cancer+Prediction')
|
| 65 |
-
st.markdown("### Select Machine Learning Model")
|
| 66 |
-
model_name = st.radio("Choose a Model", ['Decision Tree', 'Logistic Regression', 'KNN', 'Random Forest', 'XGBoost'])
|
| 67 |
-
if st.button("Train Model"):
|
| 68 |
-
df = load_data()
|
| 69 |
-
(X_train, X_test, y_train, y_test), preprocess = preprocess_data(df)
|
| 70 |
-
model = train_model(X_train, y_train, preprocess, model_name)
|
| 71 |
-
accuracy = model.score(X_test, y_test)
|
| 72 |
-
st.session_state['trained_model'] = model
|
| 73 |
-
st.session_state['X_train'] = X_train
|
| 74 |
-
st.success(f"Model Trained Successfully! Accuracy: {accuracy:.2f}")
|
| 75 |
-
|
| 76 |
-
st.title("🎗️ Cancer Prediction Using Machine Learning 🎗️")
|
| 77 |
-
|
| 78 |
-
st.markdown("""<style>.big-font {font-size:20px !important;}</style>
|
| 79 |
-
<p class="big-font">Provide patient details below to predict cancer presence:</p>""", unsafe_allow_html=True)
|
| 80 |
-
|
| 81 |
-
# Updated controls with radio buttons or buttons
|
| 82 |
-
col1, col2 = st.columns(2)
|
| 83 |
-
|
| 84 |
-
with col1:
|
| 85 |
-
age = st.radio("Age", [18, 20, 30, 40, 50, 60, 70, 80, 90, 100])
|
| 86 |
-
tumor_size = st.radio("Tumor Size", [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
|
| 87 |
-
tumor_grade = st.radio("Tumor Grade", ['High', 'Low', 'Medium'])
|
| 88 |
-
symptoms_severity = st.radio("Symptoms Severity", ['Mild', 'Moderate', 'Severe'])
|
| 89 |
-
|
| 90 |
-
with col2:
|
| 91 |
-
smoking_history = st.radio("Smoking History", ['Never Smoker', 'Former Smoker', 'Current Smoker'])
|
| 92 |
-
alcohol_consumption = st.radio("Alcohol Consumption", ['Low', 'Moderate', 'High'])
|
| 93 |
-
exercise_frequency = st.radio("Exercise Frequency", ['Rarely', 'Occasionally', 'Regularly', 'Never'])
|
| 94 |
-
gender = st.radio("Gender", ['Male', 'Female'])
|
| 95 |
-
family_history = st.radio("Family History", ["No", "Yes"])
|
| 96 |
-
|
| 97 |
-
input_data = [[age, tumor_size, tumor_grade, symptoms_severity, smoking_history,
|
| 98 |
-
alcohol_consumption, exercise_frequency, gender, family_history]]
|
| 99 |
-
|
| 100 |
-
if st.button("Predict Cancer Presence"):
|
| 101 |
-
if 'trained_model' in st.session_state:
|
| 102 |
-
model = st.session_state['trained_model']
|
| 103 |
-
X_train = st.session_state['X_train']
|
| 104 |
-
input_df = pd.DataFrame(input_data, columns=X_train.columns)
|
| 105 |
-
input_transformed = model.named_steps['preprocessor'].transform(input_df)
|
| 106 |
-
prediction = model.named_steps['classifier'].predict(input_transformed)
|
| 107 |
-
st.markdown(f"**Prediction Result: {'🟥 Positive' if prediction[0] == 1 else '🟩 Negative'}**")
|
| 108 |
-
else:
|
| 109 |
-
st.error("Please train a model first!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|