Spaces:
Runtime error
Runtime error
Upload app_predict_loan.py
Browse files- app_predict_loan.py +62 -0
app_predict_loan.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import urllib.request
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
st.title('Loan Prediction App')
|
| 6 |
+
|
| 7 |
+
# Create a form for user input
|
| 8 |
+
st.subheader('Enter Customer Information:')
|
| 9 |
+
gender = st.selectbox('Gender', ['Male', 'Female'])
|
| 10 |
+
married = st.selectbox('Married', ['Yes', 'No'])
|
| 11 |
+
dependents = st.selectbox('Dependents', ['0', '1', '2', '3+'])
|
| 12 |
+
education = st.selectbox('Education', ['Graduate', 'Not Graduate'])
|
| 13 |
+
self_employed = st.selectbox('Self Employed', ['Yes', 'No'])
|
| 14 |
+
applicant_income = st.number_input('Applicant Income')
|
| 15 |
+
coapplicant_income = st.number_input('Coapplicant Income')
|
| 16 |
+
loan_amount = st.number_input('Loan Amount')
|
| 17 |
+
term = st.number_input('Term (in months)')
|
| 18 |
+
credit_history = st.selectbox('Credit History', ['0.0', '1.0'])
|
| 19 |
+
area = st.selectbox('Area', ['Urban', 'Rural', 'Semiurban'])
|
| 20 |
+
|
| 21 |
+
# When the user clicks the "Predict" button
|
| 22 |
+
if st.button('Predict'):
|
| 23 |
+
# Create the data payload
|
| 24 |
+
data = {
|
| 25 |
+
"Inputs": {
|
| 26 |
+
"data": [
|
| 27 |
+
{
|
| 28 |
+
"Gender": gender,
|
| 29 |
+
"Married": married,
|
| 30 |
+
"Dependents": dependents,
|
| 31 |
+
"Education": education,
|
| 32 |
+
"Self_Employed": self_employed,
|
| 33 |
+
"Applicant_Income": applicant_income,
|
| 34 |
+
"Coapplicant_Income": coapplicant_income,
|
| 35 |
+
"Loan_Amount": loan_amount,
|
| 36 |
+
"Term": term,
|
| 37 |
+
"Credit_History": float(credit_history),
|
| 38 |
+
"Area": area
|
| 39 |
+
}
|
| 40 |
+
]
|
| 41 |
+
},
|
| 42 |
+
"GlobalParameters": {
|
| 43 |
+
"method": "predict"
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Convert data to JSON
|
| 48 |
+
json_data = json.dumps(data).encode('utf-8')
|
| 49 |
+
|
| 50 |
+
# Send a POST request to the specified URL
|
| 51 |
+
url = 'http://28132852-a923-4783-b934-0c91b8f04bfa.southeastasia.azurecontainer.io/score'
|
| 52 |
+
headers = {'Content-Type': 'application/json'}
|
| 53 |
+
try:
|
| 54 |
+
response = urllib.request.urlopen(urllib.request.Request(url, json_data, headers))
|
| 55 |
+
|
| 56 |
+
# Read and display the result
|
| 57 |
+
result = response.read()
|
| 58 |
+
st.subheader('Prediction Result:')
|
| 59 |
+
st.json(json.loads(result))
|
| 60 |
+
except urllib.error.HTTPError as error:
|
| 61 |
+
st.error(f'The request failed with status code: {error.code}')
|
| 62 |
+
st.error(error.read().decode("utf8", 'ignore'))
|