model_prediction_lightgbm.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import pandas as pd
  2. from pymongo import MongoClient
  3. import pickle
  4. from flask import Flask,request
  5. import time
  6. import logging
  7. import traceback
  8. from common.database_dml import get_data_from_mongo,insert_data_into_mongo
  9. app = Flask('model_prediction_lightgbm——service')
  10. def model_prediction(df,args):
  11. mongodb_connection,mongodb_database,mongodb_model_table,model_name,model_key = "mongodb://root:sdhjfREWFWEF23e@192.168.1.43:30000/",args['mongodb_database'],args['mongodb_model_table']
  12. client = MongoClient(mongodb_connection)
  13. db = client[mongodb_database]
  14. collection = db[mongodb_model_table]
  15. model_data = collection.find_one({"model_name": model_name})
  16. if model_data is not None:
  17. model_binary = model_data['model'] # 确保这个字段是存储模型的二进制数据
  18. # 反序列化模型
  19. model = pickle.loads(model_binary)
  20. df['predict'] = model.predict(df[model.feature_name()])
  21. df['model'] = model_name
  22. print("model predict result successfully!")
  23. return df
  24. @app.route('/model_prediction_lightgbm', methods=['POST'])
  25. def model_prediction_lightgbm():
  26. # 获取程序开始时间
  27. start_time = time.time()
  28. result = {}
  29. success = 0
  30. print("Program starts execution!")
  31. try:
  32. args = request.values.to_dict()
  33. print('args',args)
  34. logger.info(args)
  35. power_df = get_data_from_mongo(args)
  36. model = model_prediction(power_df,args)
  37. insert_data_into_mongo(model,args)
  38. success = 1
  39. except Exception as e:
  40. my_exception = traceback.format_exc()
  41. my_exception.replace("\n","\t")
  42. result['msg'] = my_exception
  43. end_time = time.time()
  44. result['success'] = success
  45. result['args'] = args
  46. result['start_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time))
  47. result['end_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time))
  48. print("Program execution ends!")
  49. return result
  50. if __name__=="__main__":
  51. print("Program starts execution!")
  52. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  53. logger = logging.getLogger("model_prediction_lightgbm log")
  54. from waitress import serve
  55. serve(app, host="0.0.0.0", port=10090)
  56. print("server start!")