Spaces:
Build error
Build error
vidhiparikh
commited on
Commit
•
9233f03
1
Parent(s):
e5be3ab
This file contains the code that has been used to develop the loan approval checker tool using gradio and orange2
Browse files![img1.png](https://s3.amazonaws.com/moonup/production/uploads/644afdf8958b7796980a3266/n3mxxW_iWg7kPAp4Ekl-k.png)
![img2.png](https://s3.amazonaws.com/moonup/production/uploads/644afdf8958b7796980a3266/saxeL43kqF0MtGfBP9mrD.png)
- webapp_modified.py +84 -0
webapp_modified.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#First we have to import libraries
|
2 |
+
#Think of libraries as "pre-written programs" that help us accelerate what we do in Python
|
3 |
+
|
4 |
+
#Gradio is a web interface library for deploying machine learning models
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
#Pickle is a library that lets us work with machine learning models, which in Python are typically in a "pickle" file format
|
8 |
+
import pickle
|
9 |
+
import statistics
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
#Orange is the Python library used by... well, Orange!
|
14 |
+
from Orange.data import *
|
15 |
+
|
16 |
+
#This is called a function. This function can be "called" by our website (when we click submit). Every time it's called, the function runs.
|
17 |
+
#Within our function, there are inputs (bedrooms1, bathrooms1, etc.). These are passed from our website front end, which we will create further below.
|
18 |
+
def make_prediction(gender1,married1,dependents1,education1,self_employed1,applicantincome1,coapplicantincome1,loanamount1,loanamountterm1,credit_history1,property_area1):
|
19 |
+
|
20 |
+
#Because we already trained a model on these variables, any inputs we feed to our model has to match the inputs it was trained on.
|
21 |
+
#Even if you're not familiar with programming, you can probably decipher the below code.
|
22 |
+
gender=DiscreteVariable("Gender",values=["Male","Female"])
|
23 |
+
married=DiscreteVariable("Married",values=["Yes","No"])
|
24 |
+
dependents=DiscreteVariable("Dependents",values=["0","1","2","3+"])
|
25 |
+
education=DiscreteVariable("Education",values=["Graduate","Not Graduate"])
|
26 |
+
self_employed=DiscreteVariable("Self_Employed",values=["Yes","No"])
|
27 |
+
applicantincome=ContinuousVariable("ApplicantIncome")
|
28 |
+
coapplicantincome=ContinuousVariable("CoapplicantIncome")
|
29 |
+
loanamount=ContinuousVariable("LoanAmount")
|
30 |
+
loanamountterm=ContinuousVariable("Loan_Amount_Term")
|
31 |
+
credit_history=DiscreteVariable("Credit_History",values=["0","1"])
|
32 |
+
property_area=DiscreteVariable("Property_Area",values=["Rural","Semiurban","Urban"])
|
33 |
+
|
34 |
+
#This code is a bit of housekeeping.
|
35 |
+
#Since our model is expecting discrete inputs (just like in Orange), we need to convert our numeric values to strings
|
36 |
+
dependents1=str(dependents1)
|
37 |
+
credit_history1=str(credit_history1)
|
38 |
+
applicantincome1=float((applicantincome1-5403.46)/1.13)
|
39 |
+
print(applicantincome1)
|
40 |
+
coapplicantincome1=float((coapplicantincome1-1621.25)/1.80347)
|
41 |
+
print(coapplicantincome1)
|
42 |
+
loanamount1=float((loanamount1-146.41)/0.58)
|
43 |
+
print(loanamount1)
|
44 |
+
loanamountterm1=float((loanamountterm1-342)/0.19)
|
45 |
+
print(loanamountterm1)
|
46 |
+
#A domain is essentially an Orange file definition. Just like the one you set with the "file node" in the tool.
|
47 |
+
domain=Domain([gender,married,dependents,education,self_employed,applicantincome,coapplicantincome,loanamount,loanamountterm,credit_history,property_area])
|
48 |
+
|
49 |
+
#Our data is the data being passed by the website inputs. This gets mapped to our domain, which we defined above.
|
50 |
+
data=Table(domain,[[gender1,married1,dependents1,education1,self_employed1,applicantincome1,coapplicantincome1,loanamount1,loanamountterm1,credit_history1,property_area1]])
|
51 |
+
|
52 |
+
#Next, we can work on our predictions!
|
53 |
+
|
54 |
+
#This tiny piece of code loads our model (pickle load).
|
55 |
+
with open("model.pkcls", "rb") as f:
|
56 |
+
#Then feeds our data into the model, then sets the "preds" variable to the prediction output for our class variable, which is price.
|
57 |
+
clf = pickle.load(f)
|
58 |
+
ar=clf(data)
|
59 |
+
preds=clf.domain.class_var.str_val(ar)
|
60 |
+
#Finally, we send the prediction to the website.
|
61 |
+
return preds
|
62 |
+
|
63 |
+
#Now that we have defined our prediction function, we need to create our web interface.
|
64 |
+
#This code creates the input components for our website. Gradio has this well documented and it's pretty easy to modify.
|
65 |
+
TheGender=gr.Dropdown(["Male","Female"],label="Whats your gender?")
|
66 |
+
IsMarried=gr.Dropdown(["Yes","No"],label="Are you married?")
|
67 |
+
HasDependents=gr.Dropdown(["0","1","2","3+"], label="How many dependents do you have?")
|
68 |
+
#HasDependents=gr.Slider(minimum=0,maximum=3,step=1,label="How many dependents do you have?")
|
69 |
+
IsEducated=gr.Dropdown(["Graduate","Not Graduate"],label="Whats your education status?")
|
70 |
+
IsSelfEmployed=gr.Dropdown(["Yes","No"],label="Are you self-employed?")
|
71 |
+
ApplicantIncom=gr.Number(label="Whats the applicant income?")
|
72 |
+
CoApplicantIncome=gr.Number(label="Whats the co-applicant income? If any!")
|
73 |
+
LoanAmount=gr.Number(label="Whats the Loan Amount?")
|
74 |
+
LoanAmountTerm=gr.Number(label="Whats the Loan Amount Term?")
|
75 |
+
HasCreditHistory=gr.Dropdown(["0","1"],label="Do you have credit history?")
|
76 |
+
PropertyArea=gr.Dropdown(['Rural','Semiurban','Urban'],label='What is the area where your property is located?')
|
77 |
+
|
78 |
+
# Next, we have to tell Gradio what our model is going to output. In this case, it's going to be a text result (house prices).
|
79 |
+
output = gr.Textbox(label="Loan Approval Status: ")
|
80 |
+
|
81 |
+
#Then, we just feed all of this into Gradio and launch the web server.
|
82 |
+
#Our fn (function) is our make_prediction function above, which returns our prediction based on the inputs.
|
83 |
+
app = gr.Interface(fn = make_prediction, inputs=[TheGender, IsMarried, HasDependents,IsEducated,IsSelfEmployed,ApplicantIncom,CoApplicantIncome,LoanAmount,LoanAmountTerm,HasCreditHistory,PropertyArea], outputs=output)
|
84 |
+
app.launch()
|