forked from olkuka/IUM-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
159 lines (125 loc) · 6.23 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
from flask import Flask
import flask
import pickle
import os
import pandas as pd
import numpy as np
from xgboost import XGBClassifier
from datetime import datetime
UPLOAD_FOLDER = os.path.join(os.getcwd(), "new_data")
# columns must be in the exact same order for the model to work
LOGISTIC_REGRESSION_COLUMNS = ['user_id','product_id','price','discount_price',
'category_path_Gry i konsole;Gry komputerowe', 'category_path_Gry i konsole;Gry na konsole;Gry PlayStation3',
'category_path_Gry i konsole;Gry na konsole;Gry Xbox 360', 'category_path_Komputery;Monitory;Monitory LCD',
'category_path_Sprzęt RTV;Video;Telewizory i akcesoria;Okulary 3D', 'category_path_Telefony i akcesoria;Telefony stacjonarne',
'city_Kutno','city_Police','city_Radom','city_Warszawa']
XGB_COLUMNS = ['user_id', 'product_id', 'offered_discount', 'price', 'discount_price',
'category_path_Gry i konsole;Gry komputerowe',
'category_path_Gry i konsole;Gry na konsole;Gry PlayStation3',
'category_path_Gry i konsole;Gry na konsole;Gry Xbox 360',
'category_path_Komputery;Drukarki i skanery;Biurowe urządzenia wielofunkcyjne',
'category_path_Komputery;Monitory;Monitory LCD',
'category_path_Komputery;Tablety i akcesoria;Tablety',
'category_path_Sprzęt RTV;Audio;Słuchawki',
'category_path_Sprzęt RTV;Przenośne audio i video;Odtwarzacze mp3 i mp4',
'category_path_Sprzęt RTV;Video;Odtwarzacze DVD',
'category_path_Sprzęt RTV;Video;Telewizory i akcesoria;Anteny RTV',
'category_path_Sprzęt RTV;Video;Telewizory i akcesoria;Okulary 3D',
'category_path_Telefony i akcesoria;Akcesoria telefoniczne;Zestawy głośnomówiące',
'category_path_Telefony i akcesoria;Akcesoria telefoniczne;Zestawy słuchawkowe',
'category_path_Telefony i akcesoria;Telefony komórkowe',
'category_path_Telefony i akcesoria;Telefony stacjonarne',
'city_Gdynia', 'city_Konin', 'city_Kutno', 'city_Mielec', 'city_Police',
'city_Radom', 'city_Szczecin', 'city_Warszawa']
app = Flask(__name__, template_folder='templates')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# preparing datasets
products = pd.read_json(os.path.join(UPLOAD_FOLDER, "products.jsonl"), lines=True)
sessions = pd.read_json(os.path.join(UPLOAD_FOLDER, "sessions.jsonl"), lines=True)
users = pd.read_json(os.path.join(UPLOAD_FOLDER, "users.jsonl"), lines=True)
# hash user_id
users['variant'] = pd.util.hash_pandas_object(users['user_id']) % 2
users['variant'].replace({0: "A", 1: "B"}, inplace=True)
@app.route('/', methods=['GET', 'POST'])
def main():
if flask.request.method == 'GET':
return flask.render_template('main.html', original_input={}, result="Please submit the values first", data=get_logs().tail(5))
if flask.request.method == 'POST':
user_id = int(flask.request.form['user_id'])
product_id = int(flask.request.form['product_id'])
offered_discount = int(flask.request.form['offered_discount'])
user, product = get_submit_data(user_id, product_id)
X = pd.concat([user, product], axis=1)
X["offered_discount"] = offered_discount
X["discount_price"] = X["offered_discount"] * X["price"]
variant = "-"
if user.shape[0] != 0:
if product.shape[0] != 0:
prediction, variant = predict(X, user_id)
if prediction == 0:
result = "VIEW"
else:
result = "BUY"
save(user_id, product_id, offered_discount, variant, result)
else:
result = "There is no data for this product"
else:
result = "There is no data for this user"
return flask.render_template('main.html',
original_input={'User id': user_id,
'Product id': product_id, 'Offered discount': offered_discount, 'Variant': variant},
result=result, data=get_logs().tail(5))
def predict(X, user_id):
current_user = users[users.user_id == user_id]
variant = current_user["variant"].to_numpy()[0]
# check the variant
if variant == 'A':
X = X[XGB_COLUMNS]
model = load_model('model/model_xgb.pkl')
else:
X = X[LOGISTIC_REGRESSION_COLUMNS]
model = load_model('model/model_lr.pkl')
return model.predict(X), variant
def load_model(path_from_cwd):
with open(os.path.join(os.getcwd(), path_from_cwd), 'rb') as f:
model = pickle.load(f)
return model
def get_submit_data(user_id, product_id):
"""Return user and product from our data"""
products_data = pd.read_json(os.path.join(UPLOAD_FOLDER, "products.jsonl"), lines=True)
users_data = pd.read_json(os.path.join(UPLOAD_FOLDER, "users.jsonl"), lines=True)
users_data = users_data[users_data.columns[~users_data.columns.isin(["name", "street"])]]
users_data = pd.get_dummies(users_data)
products_data = products_data[products_data.columns[~products_data.columns.isin(["product_name"])]]
products_data = pd.get_dummies(products_data)
user = users_data[users_data.user_id == user_id]
product = products_data[products_data.product_id == product_id]
user.reset_index(inplace=True)
user = user.drop("index", axis=1)
product.reset_index(inplace=True)
product = product.drop("index", axis=1)
return user, product
def save(user_id, product_id, offered_discount, variant, algo_decision):
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
data = {
'time': [dt_string],
'user_id': [user_id],
'product_id': [product_id],
'offered_discount': [offered_discount],
'variant': [variant],
'algorithm_decision': [algo_decision]
}
df = pd.DataFrame(data)
logs_path = os.path.join(os.getcwd(), 'logs/logs.csv')
if os.path.exists(logs_path):
df.to_csv(logs_path, mode='a', header=False, index=False)
else:
df.to_csv(logs_path, mode='w', header=True, index=False)
def get_logs():
logs_path = os.path.join(os.getcwd(), 'logs/logs.csv')
if os.path.exists(logs_path):
return pd.read_csv(logs_path)
return pd.DataFrame()
if __name__ == '__main__':
app.run()