-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
297 lines (275 loc) · 5.31 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from flask import Flask, render_template, redirect, current_app as app
from mapping_values import vehicle_condition_map, vehicle_size_map
from Load_SVR import submit_model
error_text = "PLEASE FILL OUT ALL THE DATA"
color = [
"Color",
"blue",
"white",
"grey",
"black",
"brown",
"red",
"silver",
"green",
"yellow",
"purple",
"custom",
"orange",
]
condition = [
"Condition",
"new",
"like new",
"excellent",
"good",
"fair",
"salvage",
]
cyl = ["Drive", "Front Wheel Drive", "Rear Wheel Drive", "Four Wheel Drive"]
fuel_type = [
"Fuel_Type",
"electric",
"hybrid",
"gas",
"diesel",
"other",
]
manufacturer = [
"Make",
"bmw",
"toyota",
"honda",
"chevrolet",
"mazda",
"ford",
"volvo",
"cadillac",
"saturn",
"subaru",
"dodge",
"gmc",
"ram",
"chrysler",
"mercedes-benz",
"infiniti",
"jeep",
"buick",
"nissan",
"volkswagen",
"mercury",
"hyundai",
"lexus",
"porsche",
"rover",
"audi",
"fiat",
"mini",
"mitsubishi",
"lincoln",
"jaguar",
"kia",
"pontiac",
"acura",
"tesla",
"alfa-romeo",
"datsun",
"harley-davidson",
"land rover",
"aston-martin",
"ferrari",
]
odometer = "Odometer"
size = [
"Vehicle_Size",
"compact",
"sub-compact",
"mid-size",
"full-size",
]
state = [
"State",
"al",
"ak",
"az",
"ar",
"ca",
"co",
"ct",
"dc",
"de",
"fl",
"ga",
"hi",
"id",
"il",
"in",
"ia",
"ks",
"ky",
"la",
"me",
"md",
"ma",
"mi",
"mn",
"ms",
"mo",
"mt",
"nc",
"ne",
"nv",
"nj",
"nm",
"ny",
"nh",
"nd",
"oh",
"ok",
"or",
"pa",
"ri",
"sc",
"sd",
"tn",
"tx",
"ut",
"vt",
"va",
"wa",
"wv",
"wi",
"wy",
]
title = [
"Title_Status",
"clean",
"rebuilt",
"lien",
"salvage",
"parts only",
"missing",
]
transmission = [
"Transmission",
"automatic",
"other",
"manual",
]
car_type = [
"Type",
"SUV",
"mini-van",
"convertible",
"coupe",
"truck",
"wagon",
"sedan",
"pickup",
"hatchback",
"van",
"other",
"bus",
"offroad",
]
year = "Year"
# Set up flask
app = Flask(__name__)
def send_machine_learning_data(vehicle):
if vehicle == None:
print("No data!", flush=True)
return
print(vehicle, flush=True)
machine_learning_data = {
# "id",
# "region",
# "price",
"year": int(vehicle["year"]),
# "manufacturer",
# "model",
"condition": int(vehicle_condition_map[vehicle["condition"]]),
# "cylinders",
# "fuel",
"odometer": int(vehicle["odometer"]),
# "title_status",
"transmission": vehicle["transmission"],
"size": int(vehicle_size_map[vehicle["size"]]),
"type": vehicle["car_type"],
# "paint_color",
"state": vehicle["state"].lower(),
# "lat",
# "long",
# "posting_date",
"fwd": 1 * (cyl.index(vehicle["cyl"]) == 1 or cyl.index(vehicle["cyl"]) == 3),
"rwd": 1 * (cyl.index(vehicle["cyl"]) == 2 or cyl.index(vehicle["cyl"]) == 3),
}
print(machine_learning_data, flush=True)
result = submit_model(
machine_learning_data,
[
"year",
"condition",
"odometer",
"transmission",
"size",
"type",
"state",
"fwd",
"rwd",
],
"Used_Car_Price_Pipeline_SVR.pkl",
)
return redirect("/" + str(result), code=302)
def convertFormToVehicle(formData):
formData = formData.replace("%20", " ")
rawDataFields = formData.split("&")
vehicle = {}
if "year=" + year in rawDataFields:
return None
if "odometer=" + odometer in rawDataFields:
return None
for rawData in rawDataFields:
var_name, var_value = rawData.split("=")
if globals()[var_name][0] == var_value:
print(f"Missing value for {var_name}: got {var_value}", flush=True)
return None
vehicle[var_name] = var_value
return vehicle
@app.route("/postmethod", methods=["POST"])
def post_javascript_data():
return {
"color": color,
"condition": condition,
"cyl": cyl,
"fuel_type": fuel_type,
"manufacturer": manufacturer,
"odometer": odometer,
"size": size,
"state": state,
"title": title,
"transmission": transmission,
"car_type": car_type,
"year": year,
}
@app.route("/")
@app.route("/<money>")
def index(money=None):
display_color = "green"
try:
cash = float(money)
if (cash < 0):
display_color = "red"
money = "${:,}".format(cash)
except:
money = ""
return render_template("index.html", money=money, color=display_color)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/submit/<formData>")
def submit(formData):
print(formData, flush=True)
vehicle = convertFormToVehicle(formData)
return send_machine_learning_data(vehicle)
if __name__ == "__main__":
app.run(debug=True)