Skip to content

Instantly share code, notes, and snippets.

@PulkitS01
Last active Feb 21, 2021
Embed
What would you like to do?
ML_model_deployment_streamlit.py
%%writefile app.py
import pickle
import streamlit as st
# loading the trained model
pickle_in = open('classifier.pkl', 'rb')
classifier = pickle.load(pickle_in)
@st.cache()
# defining the function which will make the prediction using the data which the user inputs
def prediction(Gender, Married, ApplicantIncome, LoanAmount, Credit_History):
# Pre-processing user input
if Gender == "Male":
Gender = 0
else:
Gender = 1
if Married == "Unmarried":
Married = 0
else:
Married = 1
if Credit_History == "Unclear Debts":
Credit_History = 0
else:
Credit_History = 1
LoanAmount = LoanAmount / 1000
# Making predictions
prediction = classifier.predict(
[[Gender, Married, ApplicantIncome, LoanAmount, Credit_History]])
if prediction == 0:
pred = 'Rejected'
else:
pred = 'Approved'
return pred
# this is the main function in which we define our webpage
def main():
# front end elements of the web page
html_temp = """
<div style ="background-color:yellow;padding:13px">
<h1 style ="color:black;text-align:center;">Streamlit Loan Prediction ML App</h1>
</div>
"""
# display the front end aspect
st.markdown(html_temp, unsafe_allow_html = True)
# following lines create boxes in which user can enter data required to make prediction
Gender = st.selectbox('Gender',("Male","Female"))
Married = st.selectbox('Marital Status',("Unmarried","Married"))
ApplicantIncome = st.number_input("Applicants monthly income")
LoanAmount = st.number_input("Total loan amount")
Credit_History = st.selectbox('Credit_History',("Unclear Debts","No Unclear Debts"))
result =""
# when 'Predict' is clicked, make the prediction and store it
if st.button("Predict"):
result = prediction(Gender, Married, ApplicantIncome, LoanAmount, Credit_History)
st.success('Your loan is {}'.format(result))
print(LoanAmount)
if __name__=='__main__':
main()
X = train[['Gender', 'Married', 'ApplicantIncome', 'LoanAmount', 'Credit_History']]
y = train.Loan_Status
X.shape, y.shape
!pip install -q pyngrok
!pip install -q streamlit
!pip install -q streamlit_ace
import pandas as pd
train = pd.read_csv('train_ctrUa4K.csv')
train.head()
train.isnull().sum()
train = train.dropna()
train.isnull().sum()
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(max_depth=4, random_state = 10)
model.fit(x_train, y_train)
# saving the model
import pickle
pickle_out = open("classifier.pkl", mode = "wb")
pickle.dump(model, pickle_out)
pickle_out.close()
train['Gender']= train['Gender'].map({'Male':0, 'Female':1})
train['Married']= train['Married'].map({'No':0, 'Yes':1})
train['Loan_Status']= train['Loan_Status'].map({'N':0, 'Y':1})
from pyngrok import ngrok
public_url = ngrok.connect('8501')
public_url
!streamlit run app.py &>/dev/null&
pred_train = model.predict(x_train)
accuracy_score(y_train,pred_train)
from sklearn.model_selection import train_test_split
x_train, x_cv, y_train, y_cv = train_test_split(X,y, test_size = 0.2, random_state = 10)
from sklearn.metrics import accuracy_score
pred_cv = model.predict(x_cv)
accuracy_score(y_cv,pred_cv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment