Spaces:
Runtime error
Runtime error
File size: 3,140 Bytes
bc4a2dd 0e4ddf0 027cd16 0e4ddf0 03aa718 62fe446 f3ecd41 03d1361 03aa718 37711b8 03aa718 36b4c9b b56326b 21b298b 03d1361 bc4a2dd 36b4c9b d15cbdd f3ecd41 03d1361 21c4657 f3ecd41 bc4a2dd f3ecd41 bc4a2dd 03d1361 0e4ddf0 bc4a2dd f3ecd41 d15cbdd 03d1361 bc4a2dd 03aa718 bc4a2dd bbfd2d3 bc4a2dd ef67739 bc4a2dd 37711b8 03aa718 37711b8 03aa718 37711b8 bc4a2dd 55372f4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
from datetime import datetime
from joblib import load
from PIL import Image
import pandas as pd
import random
import streamlit as st
model = load("model_title_update.joblib")
session = st.session_state
# if 'df' not in session:
# session.df = pd.read_csv("model_log.csv").drop_duplicates()
if 'user_id' not in session:
session.user_id = random.randint(1, 1_000_000)
st.markdown('# Titanic Workshop - Would you be able to survive the Titanic disaster?')
image = Image.open('titanic.jpeg')
st.image(image)
st.markdown("## ")
st.markdown("### Input your passenger data: ")
age = st.number_input('Age', min_value=1, max_value=80)
sex = st.radio("Sex", ("Male", "Female"))
name = st.text_input("Name", "")
if sex == "Male":
title = st.radio("Title", ("Doctor", "Master", "Reverend"))
else:
title = st.radio("Title", ("Doctor", "Master", "Miss", "Mrs", "Reverend"))
embark_port = st.radio(
"Port of Embarkation",
('Cherbourg', 'Queenstown', 'Southampton'))
p_class = st.radio(
"Passenger Class",
('Class 1', 'Class 2', 'Class 3'))
siblings = st.number_input('Number of Spouses Aboard', min_value=0, max_value=5)
parents = st.number_input("Number of Children Aboard", min_value=0, max_value=6)
passenger_data = {
'Age': age,
'is_male': 1 if sex == "Male" else 0,
'Pclass_class 1': 1 if p_class == "Class 1" else 0,
'Pclass_class 2': 1 if p_class == "Class 2" else 0,
'Pclass_class 3': 1 if p_class == "Class 3" else 0,
'Embarked_C': 1 if embark_port[0] == "C" else 0,
'Embarked_Q': 1 if embark_port[0] == "Q" else 0,
'Embarked_S': 1 if embark_port[0] == "S" else 0,
'SibSp': siblings,
'Parch': parents,
'Mr': 1 if title == "Mr" else 0,
'Miss': 1 if title == "Miss" else 0,
'Mrs': 1 if title == "Mrs" else 0,
"Master": 1 if title == "Master" else 0,
'Dr': 1 if title == "Doctor" else 0,
"Rev": 1 if title == "Reverend" else 0,
'Other': 0,
"name_len": len(name),
}
if st.button("Calculate my chances of survival"):
passenger_data_df = pd.DataFrame.from_dict([passenger_data])
prediction = model.predict(passenger_data_df)[0]
prediction_proba = model.predict_proba(passenger_data_df)[:,1][0]
if prediction == 0:
st.markdown("# YOU'RE DEAD :skull:")
elif sex == "Male":
st.markdown("# YOU MADE IT, :man-swimming:")
else:
st.markdown("# YOU MADE IT, :woman-swimming:")
st.write(f"Probabilty of survival: {round(prediction_proba, 2)* 100}%.")
# passenger_data['user_id'] = session.user_id
# passenger_data['date'] = str(datetime.now())
# passenger_data["survived"] = prediction
# passenger_data["survival_prob"] = prediction_proba
# session.df = pd.concat(
# [
# session.df,
# pd.DataFrame([passenger_data])
# ],
# ignore_index=True
# )
# session.df.drop_duplicates().to_csv('model_log.csv', mode="a", header=False, index=False)
# session.df = pd.read_csv("model_log.csv")
# session.df = session.df.drop_duplicates()
# session.df.to_csv("model_log.csv", index=False) |