#First we have to import libraries #Think of libraries as "pre-written programs" that help us accelerate what we do in Python #Gradio is a web interface library for deploying machine learning models import gradio as gr #Pickle is a library that lets us work with machine learning models, which in Python are typically in a "pickle" file format import pickle #Orange is the Python library used by... well, Orange! from Orange.data import * #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. #Within our function, there are inputs (bedrooms1, bathrooms1, etc.). These are passed from our website front end, which we will create further below. def make_prediction(gender1,married1,dependents1,education1,self_employed1,applicantincome1,coapplicantincome1,loanamount1,loanamountterm1,credit_history1,property_area1): #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. #Even if you're not familiar with programming, you can probably decipher the below code. gender=DiscreteVariable("Gender",values=["Male","Female"]) married=DiscreteVariable("Married",values=["Yes","No"]) dependents=DiscreteVariable("Dependents",values=["0","1","2","3+"]) education=DiscreteVariable("Education",values=["Graduate","Not Graduate"]) self_employed=DiscreteVariable("Self_Employed",values=["Yes","No"]) applicantincome=ContinuousVariable("ApplicantIncome") coapplicantincome=ContinuousVariable("CoapplicantIncome") loanamount=ContinuousVariable("LoanAmount") loanamountterm=ContinuousVariable("Loan_Amount_Term") credit_history=DiscreteVariable("Credit_History",values=["0","1"]) property_area=DiscreteVariable("Property_Area",values=["Rural","Semiurban","Urban"]) #This code is a bit of housekeeping. #Since our model is expecting discrete inputs (just like in Orange), we need to convert our numeric values to strings dependents1=str(dependents1) credit_history1=str(credit_history1) applicantincome1=float((applicantincome1-5403.46)/1.13) print(applicantincome1) coapplicantincome1=float((coapplicantincome1-1621.25)/1.80347) print(coapplicantincome1) loanamount1=float((loanamount1-146.41)/0.58) print(loanamount1) loanamountterm1=float((loanamountterm1-342)/0.19) print(loanamountterm1) #A domain is essentially an Orange file definition. Just like the one you set with the "file node" in the tool. domain=Domain([gender,married,dependents,education,self_employed,applicantincome,coapplicantincome,loanamount,loanamountterm,credit_history,property_area]) #Our data is the data being passed by the website inputs. This gets mapped to our domain, which we defined above. data=Table(domain,[[gender1,married1,dependents1,education1,self_employed1,applicantincome1,coapplicantincome1,loanamount1,loanamountterm1,credit_history1,property_area1]]) #Next, we can work on our predictions! #This tiny piece of code loads our model (pickle load). with open("model.pkcls", "rb") as f: #Then feeds our data into the model, then sets the "preds" variable to the prediction output for our class variable, which is price. clf = pickle.load(f) ar=clf(data) preds=clf.domain.class_var.str_val(ar) #Finally, we send the prediction to the website. return preds #Now that we have defined our prediction function, we need to create our web interface. #This code creates the input components for our website. Gradio has this well documented and it's pretty easy to modify. TheGender=gr.Dropdown(["Male","Female"],label="Whats your gender?") IsMarried=gr.Dropdown(["Yes","No"],label="Are you married?") HasDependents=gr.Dropdown(["0","1","2","3+"], label="How many dependents do you have?") #HasDependents=gr.Slider(minimum=0,maximum=3,step=1,label="How many dependents do you have?") IsEducated=gr.Dropdown(["Graduate","Not Graduate"],label="Whats your education status?") IsSelfEmployed=gr.Dropdown(["Yes","No"],label="Are you self-employed?") ApplicantIncom=gr.Number(label="Whats the applicant income?") CoApplicantIncome=gr.Number(label="Whats the co-applicant income? If any!") LoanAmount=gr.Number(label="Whats the Loan Amount?") LoanAmountTerm=gr.Number(label="Whats the Loan Amount Term?") HasCreditHistory=gr.Dropdown(["0","1"],label="Do you have credit history?") PropertyArea=gr.Dropdown(['Rural','Semiurban','Urban'],label='What is the area where your property is located?') # 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). output = gr.Textbox(label="Loan Approval Status: ") #Then, we just feed all of this into Gradio and launch the web server. #Our fn (function) is our make_prediction function above, which returns our prediction based on the inputs. app = gr.Interface(fn = make_prediction, inputs=[TheGender, IsMarried, HasDependents,IsEducated,IsSelfEmployed,ApplicantIncom,CoApplicantIncome,LoanAmount,LoanAmountTerm,HasCreditHistory,PropertyArea], outputs=output) app.launch()