vidhiparikh commited on
Commit
876ea87
1 Parent(s): 9d5853b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ #Orange is the Python library used by... well, Orange!
11
+ from Orange.data import *
12
+
13
+ #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.
14
+ #Within our function, there are inputs (bedrooms1, bathrooms1, etc.). These are passed from our website front end, which we will create further below.
15
+ def make_prediction(gender1,married1,dependents1,education1,self_employed1,applicantincome1,coapplicantincome1,loanamount1,loanamountterm1,credit_history1,property_area1):
16
+
17
+ #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.
18
+ #Even if you're not familiar with programming, you can probably decipher the below code.
19
+ gender=DiscreteVariable("Gender",values=["Male","Female"])
20
+ married=DiscreteVariable("Married",values=["Yes","No"])
21
+ dependents=DiscreteVariable("Dependents",values=["0","1","2","3+"])
22
+ education=DiscreteVariable("Education",values=["Graduate","Not Graduate"])
23
+ self_employed=DiscreteVariable("Self_Employed",values=["Yes","No"])
24
+ applicantincome=ContinuousVariable("ApplicantIncome")
25
+ coapplicantincome=ContinuousVariable("CoapplicantIncome")
26
+ loanamount=ContinuousVariable("LoanAmount")
27
+ loanamountterm=ContinuousVariable("Loan_Amount_Term")
28
+ credit_history=DiscreteVariable("Credit_History",values=["0","1"])
29
+ property_area=DiscreteVariable("Property_Area",values=["Rural","Semiurban","Urban"])
30
+
31
+ #This code is a bit of housekeeping.
32
+ #Since our model is expecting discrete inputs (just like in Orange), we need to convert our numeric values to strings
33
+ dependents1=str(dependents1)
34
+ credit_history1=str(credit_history1)
35
+ applicantincome1=float((applicantincome1-5403.46)/1.13)
36
+ print(applicantincome1)
37
+ coapplicantincome1=float((coapplicantincome1-1621.25)/1.80347)
38
+ print(coapplicantincome1)
39
+ loanamount1=float((loanamount1-146.41)/0.58)
40
+ print(loanamount1)
41
+ loanamountterm1=float((loanamountterm1-342)/0.19)
42
+ print(loanamountterm1)
43
+ #A domain is essentially an Orange file definition. Just like the one you set with the "file node" in the tool.
44
+ domain=Domain([gender,married,dependents,education,self_employed,applicantincome,coapplicantincome,loanamount,loanamountterm,credit_history,property_area])
45
+
46
+ #Our data is the data being passed by the website inputs. This gets mapped to our domain, which we defined above.
47
+ data=Table(domain,[[gender1,married1,dependents1,education1,self_employed1,applicantincome1,coapplicantincome1,loanamount1,loanamountterm1,credit_history1,property_area1]])
48
+
49
+ #Next, we can work on our predictions!
50
+
51
+ #This tiny piece of code loads our model (pickle load).
52
+ with open("model.pkcls", "rb") as f:
53
+ #Then feeds our data into the model, then sets the "preds" variable to the prediction output for our class variable, which is price.
54
+ clf = pickle.load(f)
55
+ ar=clf(data)
56
+ preds=clf.domain.class_var.str_val(ar)
57
+ #Finally, we send the prediction to the website.
58
+ return preds
59
+
60
+ #Now that we have defined our prediction function, we need to create our web interface.
61
+ #This code creates the input components for our website. Gradio has this well documented and it's pretty easy to modify.
62
+ TheGender=gr.Dropdown(["Male","Female"],label="Whats your gender?")
63
+ IsMarried=gr.Dropdown(["Yes","No"],label="Are you married?")
64
+ HasDependents=gr.Dropdown(["0","1","2","3+"], label="How many dependents do you have?")
65
+ #HasDependents=gr.Slider(minimum=0,maximum=3,step=1,label="How many dependents do you have?")
66
+ IsEducated=gr.Dropdown(["Graduate","Not Graduate"],label="Whats your education status?")
67
+ IsSelfEmployed=gr.Dropdown(["Yes","No"],label="Are you self-employed?")
68
+ ApplicantIncom=gr.Number(label="Whats the applicant income?")
69
+ CoApplicantIncome=gr.Number(label="Whats the co-applicant income? If any!")
70
+ LoanAmount=gr.Number(label="Whats the Loan Amount?")
71
+ LoanAmountTerm=gr.Number(label="Whats the Loan Amount Term?")
72
+ HasCreditHistory=gr.Dropdown(["0","1"],label="Do you have credit history?")
73
+ PropertyArea=gr.Dropdown(['Rural','Semiurban','Urban'],label='What is the area where your property is located?')
74
+
75
+ # 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).
76
+ output = gr.Textbox(label="Loan Approval Status: ")
77
+
78
+ #Then, we just feed all of this into Gradio and launch the web server.
79
+ #Our fn (function) is our make_prediction function above, which returns our prediction based on the inputs.
80
+ app = gr.Interface(fn = make_prediction, inputs=[TheGender, IsMarried, HasDependents,IsEducated,IsSelfEmployed,ApplicantIncom,CoApplicantIncome,LoanAmount,LoanAmountTerm,HasCreditHistory,PropertyArea], outputs=output)
81
+ app.launch()