vidhiparikh commited on
Commit
50c7779
·
1 Parent(s): 75cf84a

The code file for the estimator

Browse files

This file let's the user input a bunch of parameters and then spins out the estimated house price based upon that value!
![img1.png](https://s3.amazonaws.com/moonup/production/uploads/644afdf8958b7796980a3266/kjSokGGhWprz5l2maTPog.png)

Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(bedrooms1, bathrooms1, stories1, mainroad1,guestroom1,basement1,hotwaterheating1,airconditioning1,parking1,prefarea1,furnishingstatus1):
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
+ bedrooms=DiscreteVariable("bedrooms",values=["1","2","3","4","5","6"])
20
+ bathrooms=DiscreteVariable("bathrooms",values=["1","2","3"])
21
+ stories=DiscreteVariable("stories",values=["1","2","3","4"])
22
+ mainroad=DiscreteVariable("mainroad",values=["yes","no"])
23
+ guestroom=DiscreteVariable("guestroom",values=["yes","no"])
24
+ basement=DiscreteVariable("basement",values=["yes","no"])
25
+ hotwaterheating=DiscreteVariable("hotwaterheating",values=["yes","no"])
26
+ airconditioning=DiscreteVariable("airconditioning",values=["yes","no"])
27
+ parking=DiscreteVariable("parking",values=["0","1","2","3"])
28
+ prefarea=DiscreteVariable("prefarea",values=["yes","no"])
29
+ furnishingstatus=DiscreteVariable("furnishingstatus",values=['furnished','semi-furnished','unfurnished'])
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
+ bedrooms1=str(bedrooms1)
34
+ bathrooms1=str(bathrooms1)
35
+ stories1=str(stories1)
36
+ parking1=str(parking1)
37
+ prefarea1=str(prefarea1)
38
+
39
+ #A domain is essentially an Orange file definition. Just like the one you set with the "file node" in the tool.
40
+ domain=Domain([bedrooms,bathrooms,stories,mainroad,guestroom,basement,hotwaterheating,airconditioning,parking,prefarea,furnishingstatus])
41
+
42
+ #Our data is the data being passed by the website inputs. This gets mapped to our domain, which we defined above.
43
+ data=Table(domain,[[bedrooms1, bathrooms1, stories1, mainroad1,guestroom1,basement1,hotwaterheating1,airconditioning1,parking1,prefarea1,furnishingstatus1]])
44
+
45
+ #Next, we can work on our predictions!
46
+
47
+ #This tiny piece of code loads our model (pickle load).
48
+ with open("model_custom.pkcls", "rb") as f:
49
+ #Then feeds our data into the model, then sets the "preds" variable to the prediction output for our class variable, which is price.
50
+ clf = pickle.load(f)
51
+ ar=clf(data)
52
+ preds=clf.domain.class_var.str_val(ar)
53
+ preds="$"+preds
54
+ #Finally, we send the prediction to the website.
55
+ return preds
56
+
57
+ #Now that we have defined our prediction function, we need to create our web interface.
58
+ #This code creates the input components for our website. Gradio has this well documented and it's pretty easy to modify.
59
+ NumberOfBedrooms=gr.Slider(minimum=1,maximum=6,step=1,label="How many bedrooms?")
60
+ NumberOfBathrooms=gr.Slider(minimum=1,maximum=3,step=1,label="How many bathrooms?")
61
+ NumberOfStories=gr.Slider(minimum=1,maximum=4,step=1,label="How many stories?")
62
+ OnMainRoad=gr.Dropdown(["yes","no"],label="Is the house on a main road?")
63
+ HasGuestRoom=gr.Dropdown(["yes","no"],label="Does the house have a guest room?")
64
+ HasBasement=gr.Dropdown(["yes","no"],label="Does the house have a basement?")
65
+ HasHotWaterHeating=gr.Dropdown(["yes","no"],label="Does the house have hot water heating?")
66
+ HasAC=gr.Dropdown(["yes","no"],label="Does the house have air conditioning?")
67
+ parkingstatus=gr.Slider(minimum=0,maximum=3,step=1,label="How many parking spots?")
68
+ HasPrefArea=gr.Dropdown(["yes","no"],label="Is the house in a preferred area?")
69
+ Furnished=gr.Dropdown(['furnished','semi-furnished','unfurnished'],label='Is the house furnished?')
70
+
71
+ # 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).
72
+ output = gr.Textbox(label="House price estimate:")
73
+
74
+ #Then, we just feed all of this into Gradio and launch the web server.
75
+ #Our fn (function) is our make_prediction function above, which returns our prediction based on the inputs.
76
+ app = gr.Interface(fn = make_prediction, inputs=[NumberOfBedrooms, NumberOfBathrooms, NumberOfStories,OnMainRoad,HasGuestRoom,HasBasement,HasHotWaterHeating,HasAC,parkingstatus,HasPrefArea,Furnished], outputs=output)
77
+ app.launch()