Spaces:
Runtime error
Runtime error
Updated
Browse files- app.py +176 -0
- ml_components +0 -0
- requirements.txt +4 -0
- titanic_background.avif +0 -0
- titanic_ship.jpeg +0 -0
app.py
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importing required Libraries
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import os, pickle
|
6 |
+
|
7 |
+
# Setting up page configuration and directory path
|
8 |
+
st.set_page_config(page_title="Titanic Survival App", page_icon="🛳️", layout="centered")
|
9 |
+
DIRPATH = os.path.dirname(os.path.realpath(__file__))
|
10 |
+
|
11 |
+
# Setting background image
|
12 |
+
import base64
|
13 |
+
def add_bg_from_local(image_file):
|
14 |
+
with open(image_file, "rb") as image_file:
|
15 |
+
encoded_string = base64.b64encode(image_file.read())
|
16 |
+
st.markdown(
|
17 |
+
f"""
|
18 |
+
<style>
|
19 |
+
.stApp {{
|
20 |
+
background-image: url(data:image/{"jpg"};base64,{encoded_string.decode()});
|
21 |
+
background-size: cover
|
22 |
+
}}
|
23 |
+
</style>
|
24 |
+
""",
|
25 |
+
unsafe_allow_html=True
|
26 |
+
)
|
27 |
+
add_bg_from_local('images/titanic_background.avif')
|
28 |
+
|
29 |
+
# Setting up logo
|
30 |
+
left1, left2, mid,right1, right2 = st.columns(5)
|
31 |
+
with mid:
|
32 |
+
st.image("images/titanic_ship.jpeg", use_column_width=True)
|
33 |
+
|
34 |
+
# Setting up Sidebar
|
35 |
+
social_acc = ['Data Field Description', 'EDA', 'About App']
|
36 |
+
social_acc_nav = st.sidebar.radio('**INFORMATION SECTION**', social_acc)
|
37 |
+
|
38 |
+
if social_acc_nav == 'Data Field Description':
|
39 |
+
st.sidebar.markdown("<h2 style='text-align: center;'> Data Field Description </h2> ", unsafe_allow_html=True)
|
40 |
+
st.sidebar.markdown("""
|
41 |
+
The table below gives a description on the variables required to make predictions.
|
42 |
+
| Variable | Definition: | Key |
|
43 |
+
| :------------ |:--------------- |:-----|
|
44 |
+
| pclass | Ticket Class |1st / 2nd / 3rd|
|
45 |
+
| sex | sex of passenger |male / female|
|
46 |
+
| Age | Age of passenger |Enter age of passenger|
|
47 |
+
| Fare | Passenger fare |Enter Fare of passenger|
|
48 |
+
| Embarked | Port of Embarkation|C=Cherbourg/ Q=Queenstown/ S=Southampton|
|
49 |
+
| IsAlone | Whether passenger has relative(s) onboard or not|No = Passenger has relatives on board/ Yes = Passenger is Alone|
|
50 |
+
""")
|
51 |
+
|
52 |
+
elif social_acc_nav == 'EDA':
|
53 |
+
st.sidebar.markdown("<h2 style='text-align: center;'> Exploratory Data Analysis </h2> ", unsafe_allow_html=True)
|
54 |
+
st.sidebar.markdown('''---''')
|
55 |
+
st.sidebar.markdown("""
|
56 |
+
| About EDA|
|
57 |
+
| :------------ |
|
58 |
+
The exploratory data analysis of this project can be find in a Jupyter notebook from the linl below""" )
|
59 |
+
st.sidebar.markdown("[Open Notebook](https://github.com/Kyei-frank/Titanic-Project---Machine-Learning-from-Disaster/blob/main/workflow.ipynb)")
|
60 |
+
|
61 |
+
elif social_acc_nav == 'About App':
|
62 |
+
st.sidebar.markdown("<h2 style='text-align: center;'> Titanic Survival Prediction App </h2> ", unsafe_allow_html=True)
|
63 |
+
st.sidebar.markdown('''---''')
|
64 |
+
st.sidebar.markdown("""
|
65 |
+
| Brief Introduction|
|
66 |
+
| :------------ |
|
67 |
+
On April 15, 1912, during her maiden voyage, the widely considered “unsinkable” RMS Titanic sank after colliding with an iceberg. Unfortunately, there weren’t enough lifeboats for everyone onboard, resulting in the death of 1502 out of 2224 passengers and crew. While there was some element of luck involved in surviving, it seems some groups of people were more likely to survive than others. This App uses classification model to predict whether a passenger survives or not based on the data of the passenger.""")
|
68 |
+
st.sidebar.markdown("")
|
69 |
+
st.sidebar.markdown("[ Visit Github Repository for more information](https://github.com/Kyei-frank/Titanic-Project---Machine-Learning-from-Disaster)")
|
70 |
+
|
71 |
+
# Loading Machine Learning Objects
|
72 |
+
@st.cache()
|
73 |
+
def load_saved_objects(file_path = 'ml_components'):
|
74 |
+
# Function to load saved objects
|
75 |
+
with open('ml_components', 'rb') as file:
|
76 |
+
loaded_object = pickle.load(file)
|
77 |
+
|
78 |
+
return loaded_object
|
79 |
+
|
80 |
+
# Instantiating ML_items
|
81 |
+
Loaded_object = load_saved_objects(file_path = 'ml_components')
|
82 |
+
pipeline_of_my_app = Loaded_object["pipeline"]
|
83 |
+
|
84 |
+
|
85 |
+
# Setting up variables for input data
|
86 |
+
@st.cache()
|
87 |
+
def setup(tmp_df_file):
|
88 |
+
"Setup the required elements like files, models, global variables, etc"
|
89 |
+
pd.DataFrame(
|
90 |
+
dict(
|
91 |
+
Pclass=[],
|
92 |
+
Sex=[],
|
93 |
+
Age=[],
|
94 |
+
Fare=[],
|
95 |
+
Embarked=[],
|
96 |
+
IsAlone=[],
|
97 |
+
)
|
98 |
+
).to_csv(tmp_df_file, index=False)
|
99 |
+
|
100 |
+
# Setting up a file to save our input data
|
101 |
+
tmp_df_file = os.path.join(DIRPATH, "tmp", "data.csv")
|
102 |
+
setup(tmp_df_file)
|
103 |
+
|
104 |
+
# setting Title for forms
|
105 |
+
st.markdown("<h2 style='text-align: center;'> Titanic Survival Prediction </h2> ", unsafe_allow_html=True)
|
106 |
+
st.markdown("<h7 style='text-align: center;'> Fill in the details below and click on SUBMIT button to make a prediction on the Survival of a passenger. </h7> ", unsafe_allow_html=True)
|
107 |
+
|
108 |
+
# Creating columns for for input data(forms)
|
109 |
+
left_col, right_col = st.columns(2)
|
110 |
+
|
111 |
+
# Developing forms to collect input data
|
112 |
+
with st.form(key="information", clear_on_submit=True):
|
113 |
+
|
114 |
+
# Setting up input data for 1st column
|
115 |
+
left_col.markdown("**CATEGORICAL DATA**")
|
116 |
+
Pclass = left_col.selectbox("Passenger Class:", options = ["1st", "2nd", "3rd"])
|
117 |
+
Sex = left_col.selectbox("Gender of Passenger:", options= ["male", "female"])
|
118 |
+
IsAlone = left_col.selectbox("Does passenger has relative onboard?", options= ["Yes", "No"])
|
119 |
+
Embarked = left_col.radio("Port of Embarkation:", options= ["C", "Q", "S"])
|
120 |
+
|
121 |
+
# Setting up input data for 2nd column
|
122 |
+
right_col.markdown("**NUMERICAL DATA**")
|
123 |
+
Age = right_col.number_input("Enter Age of Passenger")
|
124 |
+
Fare = right_col.number_input("Enter Fare of passenger")
|
125 |
+
|
126 |
+
submitted = st.form_submit_button(label="Submit")
|
127 |
+
|
128 |
+
# Setting up background operations after submitting forms
|
129 |
+
if submitted:
|
130 |
+
# Saving input data as csv after submission
|
131 |
+
pd.read_csv(tmp_df_file).append(
|
132 |
+
dict(
|
133 |
+
Pclass= Pclass,
|
134 |
+
Sex= Sex,
|
135 |
+
Age= Age,
|
136 |
+
Fare= Fare,
|
137 |
+
Embarked= Embarked,
|
138 |
+
IsAlone= IsAlone,
|
139 |
+
),
|
140 |
+
ignore_index=True,
|
141 |
+
).to_csv(tmp_df_file, index=False)
|
142 |
+
st.balloons()
|
143 |
+
|
144 |
+
# Converting input data to a dataframe for prediction
|
145 |
+
df = pd.read_csv(tmp_df_file)
|
146 |
+
df= df.copy()
|
147 |
+
|
148 |
+
# Making Predictions
|
149 |
+
# Passing data to pipeline to make prediction
|
150 |
+
pred_output = pipeline_of_my_app.predict(df)
|
151 |
+
prob_output = np.max(pipeline_of_my_app.predict_proba(df))
|
152 |
+
|
153 |
+
# Interpleting prediction output for display
|
154 |
+
X= pred_output[-1]
|
155 |
+
if X == 1:
|
156 |
+
explanation = 'Passenger Survived'
|
157 |
+
else:
|
158 |
+
explanation = 'Passenger did not Survive'
|
159 |
+
output = explanation
|
160 |
+
|
161 |
+
# Displaying prediction results
|
162 |
+
st.markdown('''---''')
|
163 |
+
st.markdown("<h4 style='text-align: center;'> Prediction Results </h4> ", unsafe_allow_html=True)
|
164 |
+
st.success(f"Prediction: {output}")
|
165 |
+
st.success(f"Confidence Probability: {prob_output}")
|
166 |
+
st.markdown('''---''')
|
167 |
+
|
168 |
+
# Making expander to view all records
|
169 |
+
expander = st.expander("See all records")
|
170 |
+
with expander:
|
171 |
+
df = pd.read_csv(tmp_df_file)
|
172 |
+
df['Survival']= pred_output
|
173 |
+
st.dataframe(df)
|
174 |
+
|
175 |
+
|
176 |
+
|
ml_components
ADDED
Binary file (131 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas==1.4.2
|
3 |
+
numpy==1.21.5
|
4 |
+
seaborn==0.11.2
|
titanic_background.avif
ADDED
titanic_ship.jpeg
ADDED