-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
137 lines (109 loc) · 4.73 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
from flask import Flask, url_for, render_template, jsonify, Config
from flask_migrate import Migrate
from flask_security import Security
from flask_security.utils import encrypt_password
from flask_admin import helpers as admin_helpers
from adminlte.admin import AdminLte, admins_store, admin_db
from adminlte.models import Role, User
from adminlte.views import FaLink, AdminsView
from flask_admin import menu
from models import db
from models.message import Message
from models.data_source import DataSource
from models.data_table import DataTable
from models.data_feature import DataFeature
from models.portrait_user import PortraitUser
from models.model_admin import ModelAdmin
from models.model_train import ModelTrain
from models.model_pub import ModelPub
from models.model_output import ModelOutput
from models.model_strategy import ModelStrategy
from models.model_monitor import ModelMonitor
from views.index import IndexView
from views.message import MessageView
from views.data_source import DataSourceView
from views.data_table import DataTableView
from views.data_feature import DataFeatureView
from views.portrait_user import PortraitUserView
from views.model_admin import ModelAdminView
from views.model_train import ModelTrainView
from views.model_pub import ModelPubView
from views.model_output import ModelOutputView
from views.model_strategy import ModelStrategyView
from views.model_monitor import ModelMonitorView
from tools.utils import *
from ml.model import utils as ai
app = Flask(__name__)
# app.config.from_pyfile('config.ini')
# 需要后台调用,所以使用 configparser 读取
# 注意 working directory
conf_ini = read_flask_config()
app.config.from_mapping(conf_ini)
@app.route('/')
def index():
sys_conf = {'title': 'KnifeREC', 'author': 'zergskj'}
return render_template("index.html", sys=sys_conf)
@app.route('/predict')
def predict(type='sort'):
# todo 返回预测结果
result = {'user_id': '1', 'prod_id': ''}
try:
result = ai.model_predict()
except Exception as e:
print(e)
return jsonify(result)
db.init_app(app)
db.app = app
migrate = Migrate(app, db)
admin_migrate = Migrate(app, admin_db)
security = Security(app, admins_store)
admin = AdminLte(app, skin='green', name='KnifeREC', index_view=IndexView(endpoint=None), short_name="<b>K</b>R",
long_name=u"<b>KnifeREC</b>推荐系统")
def create_menu():
admin.add_view(DataSourceView(DataSource, db.session, name=u'数据源', menu_icon_value='fa-database'))
admin.add_view(DataTableView(DataTable, db.session, name=u"数据表", menu_icon_value='fa-table'))
admin.add_view(DataFeatureView(DataFeature, db.session, name=u"特征工程", menu_icon_value='fa-filter'))
admin.add_view(MessageView(Message, db.session, name=u"商家画像", menu_icon_value='fa-user-circle'))
admin.add_view(PortraitUserView(PortraitUser, db.session, name=u"用户画像", menu_icon_value='fa-user'))
admin.add_view(ModelAdminView(ModelAdmin, db.session, name=u"模型管理", menu_icon_value='fa-cube'))
admin.add_view(ModelTrainView(ModelTrain, db.session, name=u"模型训练", menu_icon_value='fa-retweet'))
admin.add_view(ModelOutputView(ModelOutput, db.session, name=u"模型输出", menu_icon_value='fa-rocket'))
admin.add_view(ModelStrategyView(ModelStrategy, db.session, name=u"策略设置", menu_icon_value='fa-gears'))
admin.add_view(ModelPubView(ModelPub, db.session, name=u"模型部署", menu_icon_value='fa-tasks'))
admin.add_view(ModelMonitorView(ModelMonitor, db.session, name=u"模型监控", menu_icon_value='fa-laptop'))
admin.add_view(AdminsView(User, admin_db.session, name=u"管理员", menu_icon_value='fa-user-secret'))
admin.set_category_icon(name='Author', icon_value='fa-address-card')
create_menu()
@security.context_processor
def security_context_processor():
return dict(
admin_base_template=admin.base_template,
admin_view=admin.index_view,
h=admin_helpers,
get_url=url_for
)
@app.cli.command()
def build_sample_db():
"""
Populate a small db with some example entries.
"""
admin_db.drop_all()
admin_db.create_all()
with app.app_context():
super_admin_role = Role(name='superadmin')
admin_role = Role(name='admin')
admin_db.session.add(super_admin_role)
admin_db.session.add(admin_role)
admin_db.session.commit()
test_user = admins_store.create_user(
first_name='kejia',
last_name='shao',
email='[email protected]',
password=encrypt_password('admin'),
roles=[super_admin_role, admin_role]
)
admin_db.session.add(test_user)
admin_db.session.commit()
return
if __name__ == '__main__':
app.run()