Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import pickle | |
import json | |
# Load all files | |
with open('random_forest_model.pkl', 'rb') as file_2: | |
loaded_model = pickle.load(file_2) | |
with open('model_scaler.pkl', 'rb') as file_2: | |
model_scaler = pickle.load(file_2) | |
# Load the list of numerical columns | |
with open('list_num_cols.txt', 'r') as file_3: | |
list_num_cols = json.load(file_3) | |
# Load the list of categorical columns | |
with open('list_cat_cols.txt', 'r') as file_4: | |
list_cat_cols = json.load(file_4) | |
def run(): | |
with st.form(key='from_heart_failure'): | |
Age = st.number_input('age', min_value=16, max_value=50, value=25, step=1, help='Usia Prediksi') | |
Anaemia = st.selectbox('anaemia', (1, 0), index=1, help='0 for NO 1 for YES') | |
Creatinine = st.slider('creatinine_phosphokinase', min_value=0, max_value=1000, value=78) | |
Diabetes = st.selectbox('diabetes', (1, 0), index=1, help='0 for NO 1 for YES') | |
Ejection = st.slider('ejection_fraction', min_value=0, max_value=100, value=80) | |
BloodPleasure = st.selectbox('high_blood_pressure', (1, 0), index=1, help='0 for NO 1 for YES') | |
st.markdown('---') | |
Platelets = st.number_input('platelets', min_value=100000, max_value=500000, value=250000) | |
SerumCreatinine = st.number_input('serum_creatinine', min_value=0.1, max_value=5.0, value=1.7) | |
Serum_Sodium = st.number_input('serum_sodium', min_value=100, max_value=160, value=137) | |
st.markdown('---') | |
Gender = st.selectbox('sex', (1, 0), index=1, help='0 for Male 1 for Famele') | |
Smoking = st.selectbox('smoking', (1, 0), index=1, help='0 for No 1 for Yes') | |
Time = st.number_input('time', min_value=10, max_value=300, value=197) | |
submitted = st.form_submit_button('Prediction') | |
data_inf = { | |
'age': Age, | |
'anaemia': Anaemia, | |
'creatinine_phosphokinase': Creatinine, | |
'diabetes': Diabetes, | |
'ejection_fraction': Ejection, | |
'high_blood_pressure': BloodPleasure, | |
'platelets': Platelets, | |
'serum_creatinine': SerumCreatinine, | |
'serum_sodium': Serum_Sodium, | |
'sex': Gender, | |
'smoking': Smoking, | |
'time': Time | |
} | |
data_inf = pd.DataFrame([data_inf]) | |
st.dataframe(data_inf) | |
if submitted: | |
# Split between Numerical Columns and Categorical Columns | |
data_inf_num = data_inf[list_num_cols] | |
data_inf_cat = data_inf[list_cat_cols] | |
# Feature Scaling and Feature Encoding | |
data_inf_num_scaled = model_scaler.transform(data_inf_num) | |
data_inf_final = np.concatenate([data_inf_num_scaled], axis=1) | |
# Prediksi menggunakan random forest | |
y_pred_inf = loaded_model.predict(data_inf_final) | |
# Mengubah hasil prediksi menjadi label yang sesuai | |
if y_pred_inf == 0: | |
hasil_prediksi = "Ada Harapan" | |
else: | |
hasil_prediksi = "Death" | |
st.write('# Kemungkinan : ', hasil_prediksi) | |
if __name__== '__main__': | |
run() |