|
|
import json
|
|
|
from flask import Flask
|
|
|
from flask_cors import CORS
|
|
|
from flask import request
|
|
|
# from shixun_recommend_online import shixun_recommend_online
|
|
|
from shixun_recommend_offline import shixun_recommend_offline
|
|
|
from shixun_partner_recommend import shixun_partner_recommend
|
|
|
from relevant_shixun_recommend import relevant_shixun_recommend
|
|
|
# from subject_recommend_online import subject_recommend_online
|
|
|
from subject_recommend_offline import subject_recommend_offline
|
|
|
from subject_partner_recommend import subject_partner_recommend
|
|
|
from relevant_subject_recommend import relevant_subject_recommend
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
CORS(app, resources = r'/*')
|
|
|
|
|
|
@app.route('/shixun_recommend', methods=["POST"])
|
|
|
def get_shixun():
|
|
|
'''
|
|
|
以RESTful的方式获取实训推荐模型结果
|
|
|
传入参数为: user_id, topk, rank_method, disciplines
|
|
|
:param user_id: 用户ID
|
|
|
:param topk: 需要返回的实训数量
|
|
|
:param rank_method: 使用的深度学习排序模型
|
|
|
0: xDeepFM 1: BST 2: DIFM
|
|
|
:param disciplines: 兴趣标签ID,多个以英文逗号分隔
|
|
|
|
|
|
:return 以json格式返回推荐的topk个实训
|
|
|
'''
|
|
|
result = {}
|
|
|
|
|
|
user_id = request.form.get('user_id', type=str, default='')
|
|
|
topk = request.form.get('topk', type=int, default=20)
|
|
|
rank_method = request.form.get('rank_method', type=str, default='2')
|
|
|
disciplines = request.form.get('disciplines', type=str, default='')
|
|
|
|
|
|
if rank_method.strip() == '':
|
|
|
rank_method = '2'
|
|
|
|
|
|
if user_id.strip() == '':
|
|
|
result = {
|
|
|
"status_code": str('False'),
|
|
|
"error_msg": str('参数错误: 缺少user_id')
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
if rank_method.strip() not in ['0', '1', '2']:
|
|
|
result = {
|
|
|
"status_code": str('False'),
|
|
|
"error_msg": str('参数错误: rank_method参数不合法')
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
# 以逗号分隔兴趣标签为列表
|
|
|
disciplines = str(disciplines).strip().split(',')
|
|
|
|
|
|
# 没传值时的处理
|
|
|
if len(disciplines) == 1 and disciplines[0] == '':
|
|
|
disciplines.clear()
|
|
|
|
|
|
if (len(disciplines) > 0):
|
|
|
disciplines = [int(i) for i in disciplines]
|
|
|
|
|
|
if (topk == '') or (topk < 10):
|
|
|
topk = 10
|
|
|
|
|
|
# 最多返回320个,也就是20页
|
|
|
if (topk > 320):
|
|
|
topk = 320
|
|
|
|
|
|
recommend_results = shixun_recommend_offline(int(user_id),
|
|
|
list(disciplines),
|
|
|
int(topk),
|
|
|
str(rank_method))
|
|
|
result = {
|
|
|
"status_code": str('True'),
|
|
|
"user_id": str(user_id),
|
|
|
"recommend_count": str(len(dict(recommend_results))),
|
|
|
"recommend_results": dict(recommend_results),
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
@app.route('/shixun_partner_recommend', methods=["POST"])
|
|
|
def get_shixun_partner():
|
|
|
'''
|
|
|
以RESTful的方式获取实训学习伙伴推荐模型结果
|
|
|
:param user_id: 用户ID
|
|
|
:param topk: 需要返回的用户数量
|
|
|
:param method: 返回数据的方式 0:返回用户及选择的实训列表 1:只返回用户列
|
|
|
|
|
|
:return 以json格式返回topk个实训学习伙伴
|
|
|
'''
|
|
|
result = {}
|
|
|
|
|
|
user_id = request.form.get('user_id', type=str, default='')
|
|
|
topk = request.form.get('topk', type=int, default=20)
|
|
|
method = request.form.get('method', type=str, default='1')
|
|
|
|
|
|
if user_id.strip() == '':
|
|
|
result = {
|
|
|
"status_code": str('False'),
|
|
|
"error_msg": str('参数错误: 缺少user_id')
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
if method.strip() == '':
|
|
|
method = '1'
|
|
|
|
|
|
if (topk == '') or (topk < 10):
|
|
|
topk = 10
|
|
|
|
|
|
if (topk > 100):
|
|
|
topk = 100
|
|
|
|
|
|
recommend_results = shixun_partner_recommend(int(user_id), int(topk), str(method))
|
|
|
result = {
|
|
|
"status_code": str('True'),
|
|
|
"user_id": str(user_id),
|
|
|
"recommend_count": str(len(recommend_results)),
|
|
|
"recommend_results": recommend_results,
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
@app.route('/relevant_shixun_recommend', methods=["POST"])
|
|
|
def get_relevant_shixun():
|
|
|
'''
|
|
|
以RESTful的方式获取实训相关实训推荐模型结果
|
|
|
:param shixun_id: 实训ID
|
|
|
:param shixun_name: 实训名称
|
|
|
:param topk: 需要返回的实训数量
|
|
|
|
|
|
:return 以json格式返回topk个相关实训
|
|
|
'''
|
|
|
result = {}
|
|
|
|
|
|
shixun_id = request.form.get('shixun_id', type=str, default='')
|
|
|
shixun_name = request.form.get('shixun_name', type=str, default='')
|
|
|
topk = request.form.get('topk', type=int, default=20)
|
|
|
|
|
|
if (shixun_id.strip() == '') or (shixun_name.strip() == ''):
|
|
|
result = {
|
|
|
"status_code": str('False'),
|
|
|
"error_msg": str('参数错误: 缺少shixun_id')
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
if shixun_name.strip() == '':
|
|
|
result = {
|
|
|
"status_code": str('False'),
|
|
|
"error_msg": str('参数错误: 缺少shixun_name')
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
if (topk == '') or (topk < 10):
|
|
|
topk = 10
|
|
|
|
|
|
if (topk > 100):
|
|
|
topk = 100
|
|
|
|
|
|
recommend_results = relevant_shixun_recommend(int(shixun_id),
|
|
|
str(shixun_name),
|
|
|
int(topk))
|
|
|
result = {
|
|
|
"status_code": str('True'),
|
|
|
"shixun_id": str(shixun_id),
|
|
|
"shixun_name": str(shixun_name),
|
|
|
"recommend_count": str(len(dict(recommend_results))),
|
|
|
"recommend_results": dict(recommend_results),
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
@app.route('/subject_recommend', methods=["POST"])
|
|
|
def get_subject():
|
|
|
'''
|
|
|
以RESTful的方式获取课程推荐模型结果
|
|
|
传入参数为: user_id, topk, rank_method, disciplines
|
|
|
:param user_id: 用户ID
|
|
|
:param topk: 需要返回的课程数量
|
|
|
:param rank_method: 使用的深度学习排序模型
|
|
|
0: xDeepFM 1: BST 2: DIFM
|
|
|
:param disciplines: 兴趣标签ID,多个以英文逗号分隔
|
|
|
|
|
|
:return 以json格式返回推荐的topk个课程
|
|
|
'''
|
|
|
result = {}
|
|
|
|
|
|
user_id = request.form.get('user_id', type=str, default='')
|
|
|
topk = request.form.get('topk', type=int, default=20)
|
|
|
rank_method = request.form.get('rank_method', type=str, default='2')
|
|
|
disciplines = request.form.get('disciplines', type=str, default='')
|
|
|
|
|
|
if rank_method.strip() == '':
|
|
|
rank_method = '2'
|
|
|
|
|
|
if user_id.strip() == '':
|
|
|
result = {
|
|
|
"status_code": str('False'),
|
|
|
"error_msg": str('参数错误: 缺少user_id')
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
if rank_method.strip() not in ['0', '1', '2', '3']:
|
|
|
result = {
|
|
|
"status_code": str('False'),
|
|
|
"error_msg": str('参数错误: rank_method参数不合法')
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
# 以逗号分隔兴趣标签为列表
|
|
|
disciplines = str(disciplines).strip().split(',')
|
|
|
|
|
|
# 没传值时的处理
|
|
|
if len(disciplines) == 1 and disciplines[0] == '':
|
|
|
disciplines.clear()
|
|
|
|
|
|
if (len(disciplines) > 0):
|
|
|
disciplines = [int(i) for i in disciplines]
|
|
|
|
|
|
if (topk == '') or (topk < 10):
|
|
|
topk = 10
|
|
|
|
|
|
# 最多返回320个,也就是20页
|
|
|
if (topk > 320):
|
|
|
topk = 320
|
|
|
|
|
|
recommend_results = subject_recommend_offline(int(user_id),
|
|
|
list(disciplines),
|
|
|
int(topk),
|
|
|
str(rank_method))
|
|
|
result = {
|
|
|
"status_code": str('True'),
|
|
|
"user_id": str(user_id),
|
|
|
"recommend_count": str(len(dict(recommend_results))),
|
|
|
"recommend_results": dict(recommend_results),
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
@app.route('/subject_partner_recommend', methods=["POST"])
|
|
|
def get_subject_partner():
|
|
|
'''
|
|
|
以RESTful的方式获取课程学习伙伴推荐模型结果
|
|
|
:param user_id: 用户ID
|
|
|
:param topk: 需要返回的用户数量
|
|
|
:param method: 返回数据的方式 0:返回用户及选择的课程列表 1:只返回用户列表
|
|
|
|
|
|
:return 以json格式返回topk个课程学习伙伴
|
|
|
'''
|
|
|
result = {}
|
|
|
|
|
|
user_id = request.form.get('user_id', type=str, default='')
|
|
|
topk = request.form.get('topk', type=int, default=20)
|
|
|
method = request.form.get('method', type=str, default='1')
|
|
|
|
|
|
if user_id.strip() == '':
|
|
|
result = {
|
|
|
"status_code": str('False'),
|
|
|
"error_msg": str('参数错误: 缺少user_id')
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
if method.strip() == '':
|
|
|
method = '1'
|
|
|
|
|
|
if (topk == '') or (topk < 10):
|
|
|
topk = 10
|
|
|
|
|
|
if (topk > 100):
|
|
|
topk = 100
|
|
|
|
|
|
recommend_results = subject_partner_recommend(int(user_id), int(topk), str(method))
|
|
|
result = {
|
|
|
"status_code": str('True'),
|
|
|
"user_id": str(user_id),
|
|
|
"recommend_count": str(len(recommend_results)),
|
|
|
"recommend_results": recommend_results,
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
@app.route('/relevant_subject_recommend', methods=["POST"])
|
|
|
def get_relevant_subject():
|
|
|
'''
|
|
|
以RESTful的方式获取实训相关课程推荐模型结果
|
|
|
:param subject_id: 课程ID
|
|
|
:param subject_name: 课程名称
|
|
|
:param topk: 需要返回的课程数量
|
|
|
|
|
|
:return 以json格式返回topk个相关课程
|
|
|
'''
|
|
|
result = {}
|
|
|
|
|
|
subject_id = request.form.get('subject_id', type=str, default='')
|
|
|
subject_name = request.form.get('subject_name', type=str, default='')
|
|
|
topk = request.form.get('topk', type=int, default=20)
|
|
|
|
|
|
if (subject_id.strip() == '') or (subject_name.strip() == ''):
|
|
|
result = {
|
|
|
"status_code": str('False'),
|
|
|
"error_msg": str('参数错误: 缺少subject_id')
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
if subject_name.strip() == '':
|
|
|
result = {
|
|
|
"status_code": str('False'),
|
|
|
"error_msg": str('参数错误: 缺少subject_name')
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
if (topk == '') or (topk < 10):
|
|
|
topk = 10
|
|
|
|
|
|
if (topk > 100):
|
|
|
topk = 100
|
|
|
|
|
|
recommend_results = relevant_subject_recommend(int(subject_id),
|
|
|
str(subject_name),
|
|
|
int(topk))
|
|
|
result = {
|
|
|
"status_code": str('True'),
|
|
|
"subject_id": str(subject_id),
|
|
|
"subject_name": str(subject_name),
|
|
|
"recommend_count": str(len(dict(recommend_results))),
|
|
|
"recommend_results": dict(recommend_results),
|
|
|
}
|
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
# python -m flask run
|
|
|
if __name__ == '__main__':
|
|
|
app.run(host='0.0.0.0', port=8087, debug=True, use_reloader=False) |