搭建模型线上打分服务

在几台不同机器分别部署相同服务,指定IP和端口,让运维做负载均衡。

更新时,需几台机器依序关闭、修改、重启。

1. 启动服务

start_service.sh

nohup /data/zz/anaconda3/bin/python algorithms_predict.py >> ../logs/algorithms_predict.log 2>&1 &

2.关闭服务

stop_service.sh

#/bin/bash
PID=$(ps -ef|grep algorithms_predict | grep -v grep | awk '{print $2}')
if [ -z $PID ]; then
	  echo "process provider not exist" >> ../logs/algorithms_predict.log
	  exit
else
	  echo "process id: $PID" >> ../logs/algorithms_predict.log
	  kill -9 ${PID}
	  echo "process algorithms_predict killed" >> ../logs/algorithms_predict.log
fi

3. 打分脚本

algorithms_predict.py

from flask import Flask, request, jsonify, render_template
import lightgbm as lgb
import numpy as np


app = Flask(__name__)
lx_strangers_model = lgb.Booster(model_file='../../models/strangers_model.txt')
lx_1d_is_agree_model = lgb.Booster(model_file='../../models/lx_1d_is_agree_v9.txt')

@app.route('/lx_1d_is_agree/predict',methods=['POST', 'GET'])
def lx_1d_is_agree_predict():
    data = request.get_json(force=True)
    features = np.array(data['features']).reshape(1, -1)
    threshold = data['threshold']
    score = lx_1d_is_agree_model.predict(features)[0]

    output = {}
    output['uid'] = data['uid']
    output['uid_f'] = data['uid_f']
    output['score'] = score
    output['is_agree'] = 0
    if score >= threshold:
        output['is_agree'] = 1
    return jsonify(output)


@app.route('/lx_strangers/predict',methods=['POST', 'GET'])
def lx_strangers_predict():
    data = request.get_json(force=True)
    features = np.array(data['features']).reshape(1, -1)
    threshold = data['threshold']
    features_ = features.copy()
    features_[0, -1] = 0
    score0 = lx_strangers_model.predict(features_)[0]
    features_[0, -1] = 1
    score1 = lx_strangers_model.predict(features_)[0]

    output = {}
    output['uid'] = data['uid']
    output['score0'] = score0
    output['score1'] = score1
    output['is_cancel'] = 0
    if features[0, -1] == 1:
        output['is_cancel'] = 1
    elif score1 > score0 and score0 < threshold and score1 >= threshold:
        output['is_cancel'] = 1
    return jsonify(output)


def start_server():
    app.run(host='0.0.0.0', port=5005, threaded=True)

if __name__ == '__main__':
    start_server()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值