import pandas as pd from pymongo import MongoClient import pickle from flask import Flask,request import time import logging import traceback from common.database_dml import get_data_from_mongo,insert_data_into_mongo app = Flask('model_prediction_lightgbm——service') def str_to_list(arg): if arg == '': return [] else: return arg.split(',') def model_prediction(df,args): mongodb_connection,mongodb_database,mongodb_model_table,model_name,col_reserve = "mongodb://root:sdhjfREWFWEF23e@192.168.1.43:30000/",args['mongodb_database'],args['mongodb_model_table'],args['model_name'],str_to_list(args['col_reserve']) client = MongoClient(mongodb_connection) db = client[mongodb_database] collection = db[mongodb_model_table] model_data = collection.find_one({"model_name": model_name}) if model_data is not None: model_binary = model_data['model'] # 确保这个字段是存储模型的二进制数据 # 反序列化模型 model = pickle.loads(model_binary) df['predict'] = model.predict(df[model.feature_name()]) df.loc[df['predict']<0,'predict']=0 df['model'] = model_name print("model predict result successfully!") features_reserve = col_reserve + ['model','predict'] return df[set(features_reserve)] @app.route('/model_prediction_lightgbm', methods=['POST']) def model_prediction_lightgbm(): # 获取程序开始时间 start_time = time.time() result = {} success = 0 print("Program starts execution!") try: args = request.values.to_dict() print('args',args) logger.info(args) power_df = get_data_from_mongo(args) model = model_prediction(power_df,args) insert_data_into_mongo(model,args) success = 1 except Exception as e: my_exception = traceback.format_exc() my_exception.replace("\n","\t") result['msg'] = my_exception end_time = time.time() result['success'] = success result['args'] = args result['start_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time)) result['end_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time)) print("Program execution ends!") return result if __name__=="__main__": print("Program starts execution!") logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') logger = logging.getLogger("model_prediction_lightgbm log") from waitress import serve serve(app, host="0.0.0.0", port=10090) print("server start!")